-
Notifications
You must be signed in to change notification settings - Fork 0
/
SRIToken.fst
52 lines (40 loc) · 1.63 KB
/
SRIToken.fst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
.¸.(* NAME_START:SRITOKEN:NAME_END *)
open Zen.Types
open Zen.Base
open Zen.Cost
open Zen.Asset
open Zen.Data
module RT = Zen.ResultT
module OT = Zen.OptionT
module Tx = Zen.TxSkeleton
module CR = Zen.ContractResult.NoMessage
let buy txSkeleton contractHash returnAddress =
let! tokens = Tx.getAvailableTokens zenAsset txSkeleton in
let! contractAsset = getDefault contractHash in
let! txSkeleton =
Tx.lockToContract zenAsset tokens contractHash txSkeleton
>>= Tx.mint tokens contractAsset
>>= Tx.lockToAddress contractAsset tokens returnAddress in
CR.ret txSkeleton
let redeem txSkeleton contractHash returnAddress wallet =
let! contractAsset = getDefault contractHash in
let! tokens = Tx.getAvailableTokens contractAsset txSkeleton in
let! txSkeleton =
Tx.destroy tokens contractAsset txSkeleton
>>= Tx.lockToAddress zenAsset tokens returnAddress
>>= Tx.fromWallet zenAsset tokens contractHash wallet in
CR.ofOption "contract doesn't have enough zens to pay you" txSkeleton
let main txSkeleton contractHash command sender data wallet =
let! returnAddress = data >!> tryDict >?> tryFindLock "returnAddress" in
match returnAddress with
| Some returnAddress ->
if command = "redeem" then
redeem txSkeleton contractHash returnAddress wallet
else if command = "" || command = "buy" then
buy txSkeleton contractHash returnAddress
|> autoInc
else
RT.autoFailw "unsupported command"
| None ->
RT.autoFailw "returnAddress is required"
let cf _ _ _ _ wallet = ret ((2 + 66 + (64 + (64 + (64 + 64 + (Zen.Wallet.size wallet * 128 + 192) + 0)) + 25) + 29) <: nat)