Atomic Token Swap via Escrow Contracts
This document describes a protocol by which two parties, Alice and Bob, can atomically swap tokens across two distinct smart contract platforms using Hash Time-Locked Contracts (HTLCs). The protocol ensures that either both token transfers happen or neither does, eliminating counterparty risk.
In the scenario considered, Alice wishes to trade 10 units of token A for 20 units of Bob's token B.
Assumptions
Tokens & Contracts
- Token A
- Defined by contract
A
, deployed to podPA
- Contract address:
A.addr
- Defined by contract
- Token B
- Defined by contract
B
, deployed to podPB
- Contract address:
B.addr
- Defined by contract
- HTLC_A
- Escrow contract for token
A
, deployed to podPA
- Address:
HTLC_A.addr
- Escrow contract for token
- HTLC_B
- Escrow contract for token
B
, deployed to podPB
- Address:
HTLC_B.addr
- Escrow contract for token
Parties
- Alice
- Holds at least 10
A
tokens - Address:
Alice.addr
- Holds at least 10
- Bob
- Holds at least 20
B
tokens - Address:
Bob.addr
- Holds at least 20
Happy path Steps
Step-by-step Description
- Agreement: Alice and Bob agree to swap 10
A
tokens for 20B
tokens.- This agreement may be reached in different forms, e.g., in person or using a matching application.
- Approval: Alice allows
HTLC_A
to spend 10A
tokens on her behalf. - Contract Creation: Alice creates a contract via
HTLC_A.new_contract
, specifying Bob's address, the hash of a secretS
, a deadlinet+D
, the token addressA.addr
, and the amount10
. HTLC_A
stores the escrow details and returnsescrow_id_A
.HTLC_A
transfers 10A
tokens from Alice to itself.- Alice sends
hash(S)
andescrow_id_A
to Bob. - Bob allows
HTLC_B
to spend 20B
tokens. - Bob creates a contract via
HTLC_B.new_contract
, specifying Alice's address,hash(S)
, a deadlinet
, tokenB.addr
, and the amount20
. HTLC_B
stores the escrow details and returnsescrow_id_B
.HTLC_B
transfers 20B
tokens from Bob to itself.- Bob informs Alice of
escrow_id_B
. - Alice calls
HTLC_B.withdraw(escrow_id_B, S)
to claim the 20B
tokens. HTLC_B
transfers 20B
tokens to Alice.- Bob, having learned
S
from Alice's transaction, callsHTLC_A.withdraw(escrow_id_A, S)
. HTLC_A
transfers 10A
tokens to Bob.
Fallback Scenario: Bob Disappears
If Bob disappears after step 6 (e.g., to stall the trade or attempt sabotage), Alice waits for the t+D
deadline and recovers her tokens.
- Alice waits until time
t+D
. - Alice calls
HTLC_A.refund(escrow_id_A)
to recover her 10A
tokens. HTLC_A
refunds the tokens to Alice.
Choosing the Delay Parameter D
- If
D
is too small, Alice might wait until just before timet
to withdraw theB
tokens, leaving Bob insufficient time to claim theA
tokens beforet+D
. In that case, Alice could also reclaim herA
tokens, unfairly acquiring both tokens. - If
D
is too large, and Bob disappears, Alice must wait unnecessarily long to reclaim herA
tokens.
Thus, D
must be chosen to provide Bob with a fair time window, while not overly delaying refunds for Alice.
Choosing the Timeout t
- If Bob colludes with a validator, they may see Alice's withdrawal and intercept the secret
S
without broadcasting her transaction. This would let Bob useS
to claim theA
tokens and later refund theB
tokens, effectively stealing from Alice. - To prevent this,
t
must be long enough to allow Alice to retry her transaction via multiple validators.
A sufficiently large value of t
reduces the likelihood that a censoring validator could cause the swap to fail unfairly.