Search Apps Documentation Source Content File Folder Download Copy Actions Download

atomicswap package

Overview

Package atomicswap implements a hash time-locked contract (HTLC) for atomic swaps between native coins (ugnot) or GRC20 tokens. An atomic swap allows two parties to exchange assets in a trustless way, where either both transfers happen or neither does. The process works as follows: 1. Alice wants to swap with Bob. She generates a secret and creates a swap with Bob's address and the hash of the secret (hashlock). 2. Bob can claim the assets by providing the correct secret before the timelock expires. The secret proves Bob knows the preimage of the hashlock. 3\. If Bob doesn't claim in time, Alice can refund the assets back to herself. Example usage for native coins: // Alice creates a swap with 1000ugnot for Bob secret := "mysecret" hashlock := hex.EncodeToString(sha256.Sum256([]byte(secret))) id, _ := atomicswap.NewCoinSwap(bobAddr, hashlock) // -send 1000ugnot // Bob claims the swap by providing the secret atomicswap.Claim(id, "mysecret") Example usage for GRC20 tokens: // Alice approves the swap contract to spend her tokens token.Approve(swapAddr, 1000) // Alice creates a swap with 1000 tokens for Bob id, _ := atomicswap.NewGRC20Swap(bobAddr, hashlock, "gno.land/r/demo/token") // Bob claims the swap by providing the secret atomicswap.Claim(id, "mysecret") If Bob doesn't claim in time (default 1 week), Alice can refund: atomicswap.Refund(id)

Functions

Claim

func Claim(cur realm, id int, secret string)
Execute

Claim loads a registered swap and tries to claim it.

Params

Command

# WARNING: This command is running in an INSECURE mode.
# It is strongly recommended to use a hardware device for signing
# and avoid trusting any computer connected to the internet,
# as your private keys could be exposed.

gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "Claim" -args "" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" -broadcast -chainid "test9" -remote "https://rpc.test9.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test9.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "Claim" -args "" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test9" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test9.testnets.gno.land" call.tx

NewCoinSwap

func NewCoinSwap(cur realm, recipient address, hashlock string) (int, *Swap)
Execute

NewCoinSwap creates a new atomic swap contract for native coins. It uses a default timelock duration.

Params

Command

# WARNING: This command is running in an INSECURE mode.
# It is strongly recommended to use a hardware device for signing
# and avoid trusting any computer connected to the internet,
# as your private keys could be exposed.

gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "NewCoinSwap" -args "" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" -broadcast -chainid "test9" -remote "https://rpc.test9.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test9.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "NewCoinSwap" -args "" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test9" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test9.testnets.gno.land" call.tx

NewCustomCoinSwap

func NewCustomCoinSwap(cur realm, recipient address, hashlock string, timelock time.Time) (int, *Swap)
Execute

NewCoinSwapWithTimelock creates a new atomic swap contract for native coin. It allows specifying a custom timelock duration. It is not callable with \`gnokey maketx call\`, but can be imported by another contract or \`gnokey maketx run\`.

Params

Command

# WARNING: This command is running in an INSECURE mode.
# It is strongly recommended to use a hardware device for signing
# and avoid trusting any computer connected to the internet,
# as your private keys could be exposed.

gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "NewCustomCoinSwap" -args "" -args "" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" -broadcast -chainid "test9" -remote "https://rpc.test9.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test9.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "NewCustomCoinSwap" -args "" -args "" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test9" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test9.testnets.gno.land" call.tx

NewCustomGRC20Swap

func NewCustomGRC20Swap(cur realm, recipient address, hashlock string, timelock time.Time, token *grc20.Token) (int, *Swap)
Execute

NewCustomGRC20Swap creates a new atomic swap contract for grc20 tokens. It is not callable with \`gnokey maketx call\`, but can be imported by another contract or \`gnokey maketx run\`.

Params

Command

# WARNING: This command is running in an INSECURE mode.
# It is strongly recommended to use a hardware device for signing
# and avoid trusting any computer connected to the internet,
# as your private keys could be exposed.

gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "NewCustomGRC20Swap" -args "" -args "" -args "" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" -broadcast -chainid "test9" -remote "https://rpc.test9.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test9.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "NewCustomGRC20Swap" -args "" -args "" -args "" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test9" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test9.testnets.gno.land" call.tx

NewGRC20Swap

func NewGRC20Swap(cur realm, recipient address, hashlock string, tokenRegistryKey string) (int, *Swap)
Execute

NewGRC20Swap creates a new atomic swap contract for grc20 tokens. It uses gno.land/r/demo/defi/grc20reg to lookup for a registered token.

Params

Command

# WARNING: This command is running in an INSECURE mode.
# It is strongly recommended to use a hardware device for signing
# and avoid trusting any computer connected to the internet,
# as your private keys could be exposed.

gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "NewGRC20Swap" -args "" -args "" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" -broadcast -chainid "test9" -remote "https://rpc.test9.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test9.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "NewGRC20Swap" -args "" -args "" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test9" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test9.testnets.gno.land" call.tx

Refund

func Refund(cur realm, id int)
Execute

Refund loads a registered swap and tries to refund it.

Param

Command

# WARNING: This command is running in an INSECURE mode.
# It is strongly recommended to use a hardware device for signing
# and avoid trusting any computer connected to the internet,
# as your private keys could be exposed.

gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "Refund" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" -broadcast -chainid "test9" -remote "https://rpc.test9.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test9.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "Refund" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test9" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test9.testnets.gno.land" call.tx

Render

func Render(path string) string
Execute

Render returns a list of swaps (simplified) for the homepage, and swap details when specifying a swap ID.

Param

Command

# WARNING: This command is running in an INSECURE mode.
# It is strongly recommended to use a hardware device for signing
# and avoid trusting any computer connected to the internet,
# as your private keys could be exposed.

gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "Render" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" -broadcast -chainid "test9" -remote "https://rpc.test9.testnets.gno.land" ADDRESSgnokey query -remote "https://rpc.test9.testnets.gno.land" auth/accounts/ADDRESS
gnokey maketx call -pkgpath "gno.land/r/demo/defi/atomicswap" -func "Render" -args "" -gas-fee 1000000ugnot -gas-wanted 5000000 -send "" ADDRESS > call.tx
gnokey sign -tx-path call.tx -chainid "test9" -account-number ACCOUNTNUMBER -account-sequence SEQUENCENUMBER ADDRESS
gnokey broadcast -remote "https://rpc.test9.testnets.gno.land" call.tx