Skip to content

Instantly share code, notes, and snippets.

@et4te
Last active August 5, 2018 09:36
Show Gist options
  • Save et4te/eefc237f3086f382d27ae9dbaab3a5c0 to your computer and use it in GitHub Desktop.
Save et4te/eefc237f3086f382d27ae9dbaab3a5c0 to your computer and use it in GitHub Desktop.
Tezos Atomic Swap
[%%version 0.3]
type storage =
{ version : string;
secret_hash : bytes;
issuer : address;
issuer_hash : key_hash;
recipient_hash : key_hash;
timeout : timestamp;
}
type action =
Claim of bytes
| Refund
let%entry main (parameter : action) (storage : storage) =
match parameter with
Claim secret ->
if Crypto.sha256 secret = storage.secret_hash && Bytes.size secret = 32p then
begin
let send_to_recipient = Account.default storage.recipient_hash in
let next_op = Contract.call send_to_recipient (Current.balance ()) () in
([next_op], storage)
end
else
Current.failwith "Invalid secret"
| Refund ->
if Current.sender () = storage.issuer then
if Current.time () >= storage.timeout then
begin
let refund_to_issuer = Account.default storage.issuer_hash in
let next_op = Contract.call refund_to_issuer (Current.balance ()) () in
([next_op], storage)
end
else
Current.failwith "Timeout has not been reached"
else
Current.failwith "Sender is not the issuer and thus may not be refunded"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment