Last active
August 12, 2018 23:40
-
-
Save syuhei176/a00b80d90c2c4d2f6fe575cd70701819 to your computer and use it in GitHub Desktop.
atomicswap liquidity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* atomic swap *) | |
[%%version 0.3] | |
type state = | |
| Empty | |
| Initiator | |
| Participant | |
type initiate = { | |
iRefundTime: timestamp; | |
iHashedSecret: bytes; | |
iParticipant: address | |
} | |
type participate = { | |
pRefundTime: timestamp; | |
pHashedSecret: bytes; | |
pInitiator: address | |
} | |
type redeem = { | |
redeemSecret: bytes; | |
redeemHashedSecret: bytes; | |
} | |
type parameter = | |
| Initiate of initiate | |
| Participate of participate | |
| Redeem of redeem | |
| Refund of bytes | |
type swap = { | |
initTimestamp: timestamp; | |
refundTime: timestamp; | |
hashedSecret: bytes; | |
secret: bytes option; | |
initiator: address; | |
participant: address; | |
value: tez; | |
emptied: bool; | |
state: state; | |
} | |
type swaps = (bytes, swap) map | |
type storage = swaps | |
let isRefundable (hashedSecret: bytes)(storage: storage) = | |
match Map.find hashedSecret storage with | |
| None -> false | |
| Some swap -> | |
if (Current.time() > swap.refundTime) | |
&& not swap.emptied | |
then true | |
else false | |
[@@inline] | |
let isRedeemable | |
(hashedSecret: bytes) | |
(secret: bytes) | |
(storage: storage) = | |
match Map.find hashedSecret storage with | |
| None -> true | |
| Some swap -> | |
if (Crypto.sha256 secret = hashedSecret) | |
&& Current.time() < swap.refundTime | |
&& not swap.emptied | |
then true | |
else false | |
[@@inline] | |
let isInitiated | |
(hashedSecret: bytes) | |
(storage: storage) = | |
match Map.find hashedSecret storage with | |
| None -> false | |
| Some swap -> | |
match swap.state with | |
| Empty -> false | |
| Initiator -> true | |
| Participant -> true | |
[@@inline] | |
let transfer | |
(address : address) | |
(amount : tez)= | |
match (Contract.at address: unit contract option) with | |
| None -> ([]: operation list) | |
| Some participantContract -> | |
[Contract.call participantContract amount ()] | |
[@@inline] | |
let doInitiate | |
(initiate : initiate) | |
(storage : storage) = | |
if (isInitiated initiate.iHashedSecret storage) | |
then Current.failwith (); | |
let newSwap = { | |
initTimestamp= Current.time(); | |
refundTime= initiate.iRefundTime; | |
hashedSecret= initiate.iHashedSecret; | |
initiator= Current.sender(); | |
participant= initiate.iParticipant; | |
value= Current.amount(); | |
secret= (None: bytes option); | |
emptied= false; | |
state= Initiator; | |
} in | |
let storage = Map.add initiate.iHashedSecret newSwap storage in | |
( ([]: operation list), storage ) | |
let doParticipate | |
(participate : participate) | |
(storage : storage) = | |
if (isInitiated participate.pHashedSecret storage) | |
then Current.failwith (); | |
let newSwap = { | |
initTimestamp= Current.time(); | |
refundTime= participate.pRefundTime; | |
hashedSecret= participate.pHashedSecret; | |
initiator= Current.sender(); | |
participant= participate.pInitiator; | |
value= Current.amount(); | |
secret= (None: bytes option); | |
emptied= false; | |
state= Participant; | |
} in | |
let storage = Map.add participate.pHashedSecret newSwap storage in | |
( ([]: operation list), storage ) | |
let doRedeem | |
(redeem : redeem) | |
(storage : storage) = | |
if isRedeemable redeem.redeemHashedSecret redeem.redeemSecret storage | |
then Current.failwith (); | |
match Map.find redeem.redeemHashedSecret storage with | |
| None -> ( ([]: operation list), storage ) | |
| Some swap -> | |
let u = match swap.state with | |
| Empty -> ([] : operation list) | |
| Initiator -> | |
transfer swap.participant swap.value | |
| Participant -> | |
transfer swap.initiator swap.value | |
in | |
let swap = swap.secret <- Some redeem.redeemSecret in | |
let swap = swap.emptied <- true in | |
let storage = Map.add swap.hashedSecret swap storage in | |
( u, storage ) | |
let doRefund | |
(hashedSecret : bytes) | |
(storage : storage) = | |
if not (isRefundable hashedSecret storage) | |
then Current.failwith (); | |
match Map.find hashedSecret storage with | |
| None -> ( ([]: operation list), storage ) | |
| Some swap -> | |
let u = match swap.state with | |
| Empty -> ([] : operation list) | |
| Initiator -> | |
transfer swap.participant swap.value | |
| Participant -> | |
transfer swap.initiator swap.value | |
in | |
let swap = swap.emptied <- true in | |
let storage = Map.add swap.hashedSecret swap storage in | |
( u, storage ) | |
let%init storage = (Map : (bytes, swap) map) | |
let%entry main | |
(parameter : parameter) | |
(storage : storage) = | |
match parameter with | |
| Initiate initiate -> | |
doInitiate initiate storage | |
| Participate participate-> | |
doParticipate participate storage | |
| Redeem redeem-> | |
doRedeem redeem storage | |
| Refund hashedSecret-> | |
doRefund hashedSecret storage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
working on...;;)