ERC-2266 · Last Call · ERC · 2019-08-17
ERC-2266: Atomic Swap-based American Call Option Contract Standard
A proposed contract standard that turns a hash time-locked atomic swap into a fair American call option by adding a premium the initiator pays for the right to walk away.
What problem does it solve?
Atomic swaps let two parties trade tokens without a trusted middleman. The usual construction is a Hashed Time-Locked Contract (HTLC): the initiator picks a secret, both sides lock funds behind its hash, and revealing the secret to claim one side lets the other side claim too. If nothing happens before the timelocks expire, both get refunds.
The catch is that the initiator gets a free option. After the counterparty has locked funds, the initiator can wait and only complete the swap if the exchange rate moved in their favour. Research cited in the spec shows this makes an HTLC swap equivalent to an American call option, but one where the counterparty is never paid for granting it.
ERC-2266 standardizes a protocol that prices that optionality: the initiator deposits a premium that the participant collects if they join the swap.
How it works
Each swap is keyed by secretHash. The steps are:
setup: record both parties, the two tokens, the asset amounts and the premium amount.initiate: the initiator locks their asset with a refund time.fillPremium: the initiator locks the premium with its own refund time.participate: the participant locks their asset. They should only do this after the premium is in place.- Then either:
- the initiator calls
redeemAsset(secret, ...)to claim the participant's asset, revealing the secret so the participant can redeem the initiator's asset; or - timelocks expire and each side calls
refundAsset.
- the initiator calls
- Premium:
redeemPremiumpays the participant once they have participated and their asset has been redeemed or refunded.refundPremiumreturns it to the initiator only if the participant never joined.
So the initiator still holds the option to proceed or abort, but now pays for it. Events (SetUp, Initiated, PremiumFilled, Participated, the ...Redeemed / ...Refunded family) log each step.
Sample code
Initiator side with ethers v6 (contract address and amounts are placeholders):
import { Contract, keccak256, randomBytes, hexlify } from "ethers";
const swap = new Contract(SWAP_ADDRESS, [
"function setup(bytes32,address,address,address,uint256,address,uint256,uint256) payable",
"function initiate(bytes32,uint256) payable",
"function fillPremium(bytes32,uint256) payable",
"function redeemAsset(bytes32,bytes32)",
], initiator);
const secret = hexlify(randomBytes(32));
const secretHash = keccak256(secret); // check the hash function your implementation uses
const now = Math.floor(Date.now() / 1000);
await swap.setup(secretHash, initiator.address, TOKEN_A, TOKEN_B,
AMOUNT_A, PARTICIPANT, AMOUNT_B, PREMIUM);
await swap.initiate(secretHash, now + 48 * 3600);
await swap.fillPremium(secretHash, now + 24 * 3600);
// ...after the participant locks TOKEN_B, exercise the option:
await swap.redeemAsset(secret, secretHash);
Things to know
- Status: Last Call, with a last-call deadline of 2020-12-31 in the document. It has not moved to Final.
- The spec defines function and event shapes but leaves token handling details (ETH vs. ERC-20, approvals) to the example implementation; read that code before relying on behaviour.
- Security notes from the spec: the participant should never participate before the premium is deposited, and timelocks must be chosen so the whole process completes before they expire.
- A true cross-chain swap needs a compatible HTLC on both chains; this standard covers the Ethereum contract side.