Michelson (docs): fixed scrutable reservoir example.

This commit is contained in:
Benjamin Canou 2017-01-16 23:44:39 +01:00 committed by Benjamin Canou
parent 5e06b9c719
commit 52ec257e1d

View File

@ -1432,12 +1432,12 @@ The complete source `reservoir.tz` is:
We basically want the same contract as the previous one, but instead We basically want the same contract as the previous one, but instead
of destroying it, we want to keep it alive, storing a flag `S` so that of destroying it, we want to keep it alive, storing a flag `S` so that
we can afterwards if the tokens have been transfered to `A` or `B`. We also we can tell afterwards if the tokens have been transfered to `A` or `B`.
want the broker `A` to get some fee `P` in any case. We also want a broker `X` to get some fee `P` in any case.
We thus add variables `P` and `S` to the global data of the contract, We thus add variables `P` and `S` and `X` to the global data of the
which becomes `(Pair (S, Pair (T, Pair (Pair P N) (Pair A B))))`. `P` contract, now `(Pair (S, Pair (T, Pair (Pair P N) (Pair X (Pair A B)))))`.
is the fee for broker `A`, `S` is the state, as a string `"open"`, `P` is the fee for broker `A`, `S` is the state, as a string `"open"`,
`"timeout"` or `"success"`. `"timeout"` or `"success"`.
At the beginning of the transaction: At the beginning of the transaction:
@ -1446,61 +1446,78 @@ At the beginning of the transaction:
T via a CDDAR T via a CDDAR
P via a CDDDAAR P via a CDDDAAR
N via a CDDDADR N via a CDDDADR
A via a CDDDDAR X via a CDDDDAR
B via a CDDDDDR A via a CDDDDDAR
B via a CDDDDDDR
For the contract to stay alive, we test that all least `(Tez "1.00")` is For the contract to stay alive, we test that all least `(Tez "1.00")` is
still available after each transaction. This value is given as an still available after each transaction. This value is given as an
example and must be updated according to the actual Tezos minmal example and must be updated according to the actual Tezos minmal
value for contract balance. value for contract balance.
DUP ; CDAR # S The complete source `scrutable_reservoir.tz` is:
PUSH string "open" ;
COMPARE ; NEQ ; parameter timestamp ;
IF { FAIL ; CDR } # on "success", "timeout" or a bad init value storage
{ DUP ; CDDAR ; # T pair
NOW ; string # S
COMPARE ; LT ; pair
IF { # Before timeout timestamp # T
# We compute ((1 + P) + N) tez for keeping the contract alive pair
PUSH tez "1.00" ; (pair tez tez) ; # P N
DIP { DUP ; CDDDAAR } ; ADD ; # P pair
DIP { DUP ; CDDDADR } ; ADD ; # N (contract unit unit) # X
# We compare to the cumulated amount pair (contract unit unit) (contract unit unit) ; # A B
BALANCE ; return unit ;
COMPARE; LT ; code
IF { # Not enough cash, we accept the transaction { DUP ; CDAR # S
# and leave the global PUSH string "open" ;
CDR } COMPARE ; NEQ ;
{ # We transfer the fee to the broker IF { FAIL } # on "success", "timeout" or a bad init value
DUP ; CDDDAAR ; # P { DUP ; CDDAR ; # T
DIP { DUP ; CDDDDAR } # A NOW ;
UNIT ; TRANSFER_TOKENS ; DROP ; COMPARE ; LT ;
# We transfer the rest to the destination IF { # Before timeout
DUP ; CDDDADR ; # N # We compute ((1 + P) + N) tez for keeping the contract alive
DIP { DUP ; CDDDDDR } # B PUSH tez "1.00" ;
UNIT ; TRANSFER_TOKENS ; DROP ; DIP { DUP ; CDDDAAR } ; ADD ; # P
# We update the global DIP { DUP ; CDDDADR } ; ADD ; # N
CDR ; CDR ; PUSH string "success" ; PAIR } } # We compare to the cumulated amount
{ # After timeout BALANCE ;
# We try to transfer P tez to A COMPARE; LT ;
PUSH tez "1.00" ; BALANCE ; SUB ; # available IF { # Not enough cash, we just accept the transaction
DIP { DUP ; CDDDAAR } ;# P # and leave the global untouched
COMPARE ; LT ; # available < P CDR }
IF { PUSH tez "1.00" ; BALANCE ; SUB ; # available { # Enough cash, successful ending
DIP { DUP ; CDDDDAR } # A # We update the global
UNIT ; TRANSFER_TOKENS ; DROP } CDDR ; PUSH string "success" ; PAIR ;
{ DUP ; CDDDAAR ; # P # We transfer the fee to the broker
DIP { DUP ; CDDDDAR } # A DUP ; CDDAAR ; # P
UNIT ; TRANSFER_TOKENS ; DROP } DIP { DUP ; CDDDAR } # X
# We transfer the rest to B UNIT ; TRANSFER_TOKENS ; DROP ;
PUSH tez "1.00" ; BALANCE ; SUB ; # available # We transfer the rest to A
DIP { DUP ; CDDDDDR } # B DUP ; CDDADR ; # N
UNIT ; TRANSFER_TOKENS ; DROP ; DIP { DUP ; CDDDDAR } # A
# We update the global UNIT ; TRANSFER_TOKENS ; DROP } }
CDR ; CDR ; PUSH string "timeout" ; PAIR } } { # After timeout, we refund
# return Unit # We update the global
UNIT ; PAIR CDDR ; PUSH string "timeout" ; PAIR ;
# We try to transfer the fee to the broker
PUSH tez "1.00" ; BALANCE ; SUB ; # available
DIP { DUP ; CDDAAR } ; # P
COMPARE ; LT ; # available < P
IF { PUSH tez "1.00" ; BALANCE ; SUB ; # available
DIP { DUP ; CDDDAR } # X
UNIT ; TRANSFER_TOKENS ; DROP }
{ DUP ; CDDAAR ; # P
DIP { DUP ; CDDDAR } # X
UNIT ; TRANSFER_TOKENS ; DROP }
# We transfer the rest to B
PUSH tez "1.00" ; BALANCE ; SUB ; # available
DIP { DUP ; CDDDDDR } # B
UNIT ; TRANSFER_TOKENS ; DROP } }
# return Unit
UNIT ; PAIR }
### Forward contract ### Forward contract