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
|
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
|
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
|
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
|
peas, and the accounts of the buyer `B`, the seller `S` and the warehouse `W`.
|
||||||
`W`.
|
|
||||||
|
|
||||||
These parameters as grouped in the global storage as follows:
|
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
|
The contract returns a unit value, and we assume that it is created
|
||||||
with the minimum amount, set to `(Tez "1.00")`.
|
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
|
PUSH uint64 86400 ; SWAP ; ADD ; # one day in second
|
||||||
NOW ; COMPARE ; LT ;
|
NOW ; COMPARE ; LT ;
|
||||||
IF { # Before Z + 24
|
IF { # Before Z + 24
|
||||||
@ -1641,8 +1653,8 @@ The code of the contract is thus as follows.
|
|||||||
DIP { CDDR } ; PAIR ; # parameters
|
DIP { CDDR } ; PAIR ; # parameters
|
||||||
# and return Unit
|
# and return Unit
|
||||||
UNIT ; PAIR }
|
UNIT ; PAIR }
|
||||||
{ FAIL ; CDR ; UNIT ; PAIR }}} # (Left _)
|
{ FAIL } } } # (Left _)
|
||||||
{ FAIL ; DROP ; CDR ; UNIT ; PAIR }} # (Right _)
|
{ FAIL } } # (Right _)
|
||||||
{ # After Z + 24
|
{ # After Z + 24
|
||||||
# test if the required amount is reached
|
# test if the required amount is reached
|
||||||
DUP ; CDDAAR ; # Q
|
DUP ; CDDAAR ; # Q
|
||||||
@ -1651,22 +1663,22 @@ The code of the contract is thus as follows.
|
|||||||
PUSH tez "1.00" ; ADD ;
|
PUSH tez "1.00" ; ADD ;
|
||||||
BALANCE ; COMPARE ; LT ; # balance < 2 * (Q * C) + 1
|
BALANCE ; COMPARE ; LT ; # balance < 2 * (Q * C) + 1
|
||||||
IF { # refund the parties
|
IF { # refund the parties
|
||||||
DUP ; CDADAR ; # amount versed by the buyer
|
CDR ; DUP ; CADAR ; # amount versed by the buyer
|
||||||
DIP { DUP ; CDDDDAAR } # B
|
DIP { DUP ; CDDDAAR } # B
|
||||||
UNIT ; TRANSFER_TOKENS ; DROP
|
UNIT ; TRANSFER_TOKENS ; DROP
|
||||||
DUP ; CDADDR ; # amount versed by the seller
|
DUP ; CADDR ; # amount versed by the seller
|
||||||
DIP { DUP ; CDDDDADR } # S
|
DIP { DUP ; CDDDADR } # S
|
||||||
UNIT ; TRANSFER_TOKENS ; DROP
|
UNIT ; TRANSFER_TOKENS ; DROP
|
||||||
BALANCE ; # bonus to the warehouse to destroy the account
|
BALANCE ; # bonus to the warehouse to destroy the account
|
||||||
DIP { DUP ; CDDDDDR } # W
|
DIP { DUP ; CDDDDR } # W
|
||||||
UNIT ; TRANSFER_TOKENS ; DROP
|
UNIT ; TRANSFER_TOKENS ; DROP
|
||||||
# return unit, don't change the global
|
# return unit, don't change the global
|
||||||
# since the contract will be destroyed
|
# since the contract will be destroyed
|
||||||
CDR ; UNIT ; PAIR }
|
UNIT ; PAIR }
|
||||||
{ # otherwise continue
|
{ # otherwise continue
|
||||||
DUP ; CDDADAR # T
|
DUP ; CDDADAR # T
|
||||||
NOW ; COMPARE ; LT
|
NOW ; COMPARE ; LT
|
||||||
IF { FAIL ; CDR ; UNIT ; PAIR } # Between Z + 24 and T
|
IF { FAIL } # Between Z + 24 and T
|
||||||
{ # after T
|
{ # after T
|
||||||
DUP ; CDDADAR # T
|
DUP ; CDDADAR # T
|
||||||
PUSH uint64 86400 ; ADD # one day in second
|
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
|
DIP { CDDR } ; PAIR ; # parameters
|
||||||
# and return Unit
|
# and return Unit
|
||||||
UNIT ; PAIR }
|
UNIT ; PAIR }
|
||||||
{ FAIL ; CDR ; UNIT ; PAIR }} # (Left _)
|
{ FAIL } } # (Left _)
|
||||||
{ FAIL ; DROP ; CDR ; UNIT ; PAIR }} # (Right _)
|
{ FAIL } } # (Right _)
|
||||||
{ # After T + 24
|
{ # After T + 24
|
||||||
# test if the required payment is reached
|
# test if the required payment is reached
|
||||||
DUP ; CDDAAR ; # Q
|
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
|
IF { # not reached, pay the seller and destroy the contract
|
||||||
BALANCE ;
|
BALANCE ;
|
||||||
DIP { DUP ; CDDDDADR } # S
|
DIP { DUP ; CDDDDADR } # S
|
||||||
|
DIIP { CDR } ;
|
||||||
UNIT ; TRANSFER_TOKENS ; DROP ;
|
UNIT ; TRANSFER_TOKENS ; DROP ;
|
||||||
# and return Unit
|
# and return Unit
|
||||||
CDR ; UNIT ; PAIR }
|
UNIT ; PAIR }
|
||||||
{ # otherwise continue
|
{ # otherwise continue
|
||||||
DUP ; CDDADAR # T
|
DUP ; CDDADAR # T
|
||||||
PUSH uint64 86400 ; ADD ;
|
PUSH uint64 86400 ; ADD ;
|
||||||
@ -1717,28 +1730,31 @@ The code of the contract is thus as follows.
|
|||||||
IF { FAIL } {} # fail if not the warehouse
|
IF { FAIL } {} # fail if not the warehouse
|
||||||
DUP ; CADR ; # we must receive (Right amount)
|
DUP ; CADR ; # we must receive (Right amount)
|
||||||
IF_LEFT
|
IF_LEFT
|
||||||
{ FAIL ; DROP ; CDR ; UNIT ; PAIR } # (Left _)
|
{ FAIL } # (Left _)
|
||||||
{ # We increment the counter
|
{ # We increment the counter
|
||||||
DIP { DUP ; CDAAR } ; ADD ;
|
DIP { DUP ; CDAAR } ; ADD ;
|
||||||
# And rebuild the globals in advance
|
# And rebuild the globals in advance
|
||||||
DIP { DUP ; CDADR } ; PAIR ;
|
DIP { DUP ; CDADR } ; PAIR ;
|
||||||
DIP CDDR ; PAIR ;
|
DIP { CDDR } ; PAIR ;
|
||||||
UNIT ; PAIR ;
|
UNIT ; PAIR ;
|
||||||
# We test if enough have been delivered
|
# We test if enough have been delivered
|
||||||
DUP ; CDAAR ;
|
DUP ; CDAAR ;
|
||||||
DIP { DUP ; CDDAAR } ;
|
DIP { DUP ; CDDAAR } ;
|
||||||
COMPARE ; LT ; # counter < Q
|
COMPARE ; LT ; # counter < Q
|
||||||
IF { } # wait for more
|
IF { CDR } # wait for more
|
||||||
{ # Transfer all the money to the seller
|
{ # Transfer all the money to the seller
|
||||||
BALANCE ; # and destroy the contract
|
BALANCE ; # and destroy the contract
|
||||||
DIP { DUP ; CDDDDADR } # S
|
DIP { DUP ; CDDDDADR } # S
|
||||||
UNIT ; TRANSFER_TOKENS ; DROP }}}
|
DIIP { CDR } ;
|
||||||
|
UNIT ; TRANSFER_TOKENS ; DROP } } ;
|
||||||
|
UNIT ; PAIR }
|
||||||
{ # after T + 48, transfer everything to the buyer
|
{ # after T + 48, transfer everything to the buyer
|
||||||
BALANCE ; # and destroy the contract
|
BALANCE ; # and destroy the contract
|
||||||
DIP { DUP ; CDDDDAAR } # B
|
DIP { DUP ; CDDDDAAR } # B
|
||||||
|
DIIP { CDR } ;
|
||||||
UNIT ; TRANSFER_TOKENS ; DROP ;
|
UNIT ; TRANSFER_TOKENS ; DROP ;
|
||||||
# and return unit
|
# and return unit
|
||||||
CDR ; UNIT ; PAIR }}}}}}
|
UNIT ; PAIR } } } } } } }
|
||||||
|
|
||||||
X - Full grammar
|
X - Full grammar
|
||||||
----------------
|
----------------
|
||||||
|
Loading…
Reference in New Issue
Block a user