func Claim
crossing ActionClaim loads a registered swap and tries to claim it.
Package atomicswap implements a hash time-locked contract (HTLC) for atomic swaps between native coins (ugnot) or GRC...
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:
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).
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:
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:
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:
1atomicswap.Refund(id)
Claim loads a registered swap and tries to claim it.
NewCoinSwap creates a new atomic swap contract for native coins. It uses a default timelock duration.
1func NewCustomCoinSwap(cur realm, recipient address, hashlock string, timelock time.Time) (int, *Swap)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.
1func NewCustomGRC20Swap(cur realm, recipient address, hashlock string, timelock time.Time, token *grc20.Token) (int, *Swap)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`.
1func NewGRC20Swap(cur realm, recipient address, hashlock string, tokenRegistryKey string) (int, *Swap)NewGRC20Swap creates a new atomic swap contract for grc20 tokens. It uses gno.land/r/demo/defi/grc20reg to lookup for a registered token.
Refund loads a registered swap and tries to refund it.
Render returns a list of swaps (simplified) for the homepage, and swap details when specifying a swap ID.
Swap represents an atomic swap contract.
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.
Refund allows the sender to refund the funds after the timelock has expired.
String returns the current state of the swap.