Michelson (docs): fixed forward example.
This commit is contained in:
parent
52ec257e1d
commit
d72746c990
@ -1524,8 +1524,7 @@ The complete source `scrutable_reservoir.tz` is:
|
||||
We want to write a forward contract on dried peas. The contract takes
|
||||
as global data the tons of peas `Q`, the expected delivery date `T`, the
|
||||
contract agreement date `Z`, a strike `K`, a collateral `C` per ton of dried
|
||||
peas, and the accounts of the buyer `B`, the seller `S` and the warehouse
|
||||
`W`.
|
||||
peas, and the accounts of the buyer `B`, the seller `S` and the warehouse `W`.
|
||||
|
||||
These parameters as grouped in the global storage as follows:
|
||||
|
||||
@ -1614,9 +1613,22 @@ At the beginning of the transaction:
|
||||
The contract returns a unit value, and we assume that it is created
|
||||
with the minimum amount, set to `(Tez "1.00")`.
|
||||
|
||||
The code of the contract is thus as follows.
|
||||
The complete source `forward.tz` is:
|
||||
|
||||
DUP ; CDDADDR ; # Z
|
||||
parameter (or string uint32) ;
|
||||
return unit ;
|
||||
storage
|
||||
pair
|
||||
pair uint32 (pair tez tez) # counter from_buyer from_seller
|
||||
pair
|
||||
pair uint32 (pair timestamp timestamp) # Q T Z
|
||||
pair
|
||||
pair tez tez # K C
|
||||
pair
|
||||
pair (contract unit unit) (contract unit unit) # B S
|
||||
(contract unit unit); # W
|
||||
code
|
||||
{ DUP ; CDDADDR ; # Z
|
||||
PUSH uint64 86400 ; SWAP ; ADD ; # one day in second
|
||||
NOW ; COMPARE ; LT ;
|
||||
IF { # Before Z + 24
|
||||
@ -1641,8 +1653,8 @@ The code of the contract is thus as follows.
|
||||
DIP { CDDR } ; PAIR ; # parameters
|
||||
# and return Unit
|
||||
UNIT ; PAIR }
|
||||
{ FAIL ; CDR ; UNIT ; PAIR }}} # (Left _)
|
||||
{ FAIL ; DROP ; CDR ; UNIT ; PAIR }} # (Right _)
|
||||
{ FAIL } } } # (Left _)
|
||||
{ FAIL } } # (Right _)
|
||||
{ # After Z + 24
|
||||
# test if the required amount is reached
|
||||
DUP ; CDDAAR ; # Q
|
||||
@ -1651,22 +1663,22 @@ The code of the contract is thus as follows.
|
||||
PUSH tez "1.00" ; ADD ;
|
||||
BALANCE ; COMPARE ; LT ; # balance < 2 * (Q * C) + 1
|
||||
IF { # refund the parties
|
||||
DUP ; CDADAR ; # amount versed by the buyer
|
||||
DIP { DUP ; CDDDDAAR } # B
|
||||
CDR ; DUP ; CADAR ; # amount versed by the buyer
|
||||
DIP { DUP ; CDDDAAR } # B
|
||||
UNIT ; TRANSFER_TOKENS ; DROP
|
||||
DUP ; CDADDR ; # amount versed by the seller
|
||||
DIP { DUP ; CDDDDADR } # S
|
||||
DUP ; CADDR ; # amount versed by the seller
|
||||
DIP { DUP ; CDDDADR } # S
|
||||
UNIT ; TRANSFER_TOKENS ; DROP
|
||||
BALANCE ; # bonus to the warehouse to destroy the account
|
||||
DIP { DUP ; CDDDDDR } # W
|
||||
DIP { DUP ; CDDDDR } # W
|
||||
UNIT ; TRANSFER_TOKENS ; DROP
|
||||
# return unit, don't change the global
|
||||
# since the contract will be destroyed
|
||||
CDR ; UNIT ; PAIR }
|
||||
UNIT ; PAIR }
|
||||
{ # otherwise continue
|
||||
DUP ; CDDADAR # T
|
||||
NOW ; COMPARE ; LT
|
||||
IF { FAIL ; CDR ; UNIT ; PAIR } # Between Z + 24 and T
|
||||
IF { FAIL } # Between Z + 24 and T
|
||||
{ # after T
|
||||
DUP ; CDDADAR # T
|
||||
PUSH uint64 86400 ; ADD # one day in second
|
||||
@ -1690,8 +1702,8 @@ The code of the contract is thus as follows.
|
||||
DIP { CDDR } ; PAIR ; # parameters
|
||||
# and return Unit
|
||||
UNIT ; PAIR }
|
||||
{ FAIL ; CDR ; UNIT ; PAIR }} # (Left _)
|
||||
{ FAIL ; DROP ; CDR ; UNIT ; PAIR }} # (Right _)
|
||||
{ FAIL } } # (Left _)
|
||||
{ FAIL } } # (Right _)
|
||||
{ # After T + 24
|
||||
# test if the required payment is reached
|
||||
DUP ; CDDAAR ; # Q
|
||||
@ -1701,9 +1713,10 @@ The code of the contract is thus as follows.
|
||||
IF { # not reached, pay the seller and destroy the contract
|
||||
BALANCE ;
|
||||
DIP { DUP ; CDDDDADR } # S
|
||||
DIIP { CDR } ;
|
||||
UNIT ; TRANSFER_TOKENS ; DROP ;
|
||||
# and return Unit
|
||||
CDR ; UNIT ; PAIR }
|
||||
UNIT ; PAIR }
|
||||
{ # otherwise continue
|
||||
DUP ; CDDADAR # T
|
||||
PUSH uint64 86400 ; ADD ;
|
||||
@ -1717,28 +1730,31 @@ The code of the contract is thus as follows.
|
||||
IF { FAIL } {} # fail if not the warehouse
|
||||
DUP ; CADR ; # we must receive (Right amount)
|
||||
IF_LEFT
|
||||
{ FAIL ; DROP ; CDR ; UNIT ; PAIR } # (Left _)
|
||||
{ FAIL } # (Left _)
|
||||
{ # We increment the counter
|
||||
DIP { DUP ; CDAAR } ; ADD ;
|
||||
# And rebuild the globals in advance
|
||||
DIP { DUP ; CDADR } ; PAIR ;
|
||||
DIP CDDR ; PAIR ;
|
||||
DIP { CDDR } ; PAIR ;
|
||||
UNIT ; PAIR ;
|
||||
# We test if enough have been delivered
|
||||
DUP ; CDAAR ;
|
||||
DIP { DUP ; CDDAAR } ;
|
||||
COMPARE ; LT ; # counter < Q
|
||||
IF { } # wait for more
|
||||
IF { CDR } # wait for more
|
||||
{ # Transfer all the money to the seller
|
||||
BALANCE ; # and destroy the contract
|
||||
DIP { DUP ; CDDDDADR } # S
|
||||
UNIT ; TRANSFER_TOKENS ; DROP }}}
|
||||
DIIP { CDR } ;
|
||||
UNIT ; TRANSFER_TOKENS ; DROP } } ;
|
||||
UNIT ; PAIR }
|
||||
{ # after T + 48, transfer everything to the buyer
|
||||
BALANCE ; # and destroy the contract
|
||||
DIP { DUP ; CDDDDAAR } # B
|
||||
DIIP { CDR } ;
|
||||
UNIT ; TRANSFER_TOKENS ; DROP ;
|
||||
# and return unit
|
||||
CDR ; UNIT ; PAIR }}}}}}
|
||||
UNIT ; PAIR } } } } } } }
|
||||
|
||||
X - Full grammar
|
||||
----------------
|
||||
|
Loading…
Reference in New Issue
Block a user