Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

atomicswap source realm

Package atomicswap implements a hash time-locked contract (HTLC) for atomic swaps between native coins (ugnot) or GRC...

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:

Example
1// Alice creates a swap with 1000ugnot for Bob
2secret := "mysecret"
3hashlock := hex.EncodeToString(sha256.Sum256([]byte(secret)))
4id, _ := atomicswap.NewCoinSwap(bobAddr, hashlock) // -send 1000ugnot
5
6// Bob claims the swap by providing the secret
7atomicswap.Claim(id, "mysecret")

Example usage for GRC20 tokens:

Example
1// Alice approves the swap contract to spend her tokens
2token.Approve(swapAddr, 1000)
3
4// Alice creates a swap with 1000 tokens for Bob
5id, _ := atomicswap.NewGRC20Swap(bobAddr, hashlock, "gno.land/r/demo/token.TKN")
6
7// Bob claims the swap by providing the secret
8atomicswap.Claim(id, "mysecret")

If Bob doesn't claim in time (default 1 week), Alice can refund:

Example
1atomicswap.Refund(id)

Functions 7

func Claim

crossing Action
1func Claim(cur realm, id int, secret string)
source

Claim loads a registered swap and tries to claim it.

func NewCoinSwap

crossing Action
1func NewCoinSwap(cur realm, recipient address, hashlock string) (int, *Swap)
source

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

func NewCustomCoinSwap

crossing Action
1func NewCustomCoinSwap(cur realm, recipient address, hashlock string, timelock time.Time) (int, *Swap)
source

NewCoinSwapWithTimelock creates a new atomic swap contract for native coin. It allows specifying a custom timelock duration.

Only direct user-call (maketx call) is accepted: unsafe.OriginSend() describes a real receipt at this realm only when the caller is a pure EOA. Intermediate code realms or `maketx run` ephemeral realms can attach -send to the tx but spend the envelope elsewhere, leaving OriginSend() describing a phantom payment that would let the swap drain the realm's pre-existing balance on Claim.

func NewCustomGRC20Swap

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

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`.

func NewGRC20Swap

crossing Action
1func NewGRC20Swap(cur realm, recipient address, hashlock string, tokenRegistryKey string) (int, *Swap)
source

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

func Refund

crossing Action
1func Refund(cur realm, id int)
source

Refund loads a registered swap and tries to refund it.

func Render

1func Render(path string) string
source

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

Types 1

type Swap

struct
 1type Swap struct {
 2	sender    address
 3	recipient address
 4	hashlock  string
 5	timelock  time.Time
 6	claimed   bool
 7	refunded  bool
 8	amountStr string
 9	sendFn    func(cur realm, to address)
10}
source

Swap represents an atomic swap contract.

Methods on Swap

func Claim

method on Swap
1func (s *Swap) Claim(_ int, rlm realm, preimage string)
source

Claim allows the recipient to claim the funds if they provide the correct preimage. rlm is the cur of the surrounding crossing wrapper; rlm.Previous() is the immediate caller of that wrapper, against which we authorize.

func Refund

method on Swap
1func (s *Swap) Refund(_ int, rlm realm)
source

Refund allows the sender to refund the funds after the timelock has expired.

func Status

method on Swap
1func (s Swap) Status() string
source

func String

method on Swap
1func (s Swap) String() string
source

String returns the current state of the swap.

Imports 10

Source Files 3