sndefi
Trustless token escrow & instant swaps via HTLC on Starknet connection Fusion +
Problem Statement
This project implements a Hashed-Timelock Contract (HTLC) escrow on Starknet so two parties can swap any ERC-20–compatible tokens without needing to trust each other or a central exchange. 1. Create escrow – the sender locks tokens in the contract, supplying the receiver’s address, a Poseidon hash of a secret, a timelock, and a unique order_id. 2. Withdraw – the receiver reveals the secret before the timelock expires and the contract automatically transfers the funds. 3. Cancel & refund – if the timelock passes without the secret being revealed, only the sender can cancel and reclaim the tokens.The contract emits rich events (EscrowCreated, EscrowWithdrawn, EscrowCancelled) so off-chain indexers or front-ends can track state in real time. Mapping order_id → escrow_id makes integration with order-book or marketplace UIs straightforward.
Solution
Smart contract – Written in Cairo 1 for Starknet. It stores each escrow in a custom Escrow struct inside a Map, calculates a deterministic escrow_id with Poseidon hashing, and safeguards every state transition with explicit assert guards and custom error messages. • Token transfers – Uses OpenZeppelin’s IERC20Dispatcher interface so any token that follows the OZ ERC-20 standard works out-of-the-box. Balances are checked before locking funds and before refunds to prevent race conditions. • Security levers • u256 amounts → accommodates L2-sized transfers. • Timelock & secret‐hash pattern → guarantees either atomic settlement or full refund. • Order-ID mapping → prevents duplicate orders and simplifies cross-contract integrations. • Tooling – Compiled and tested with Scarb + Cairo-test, deployed via Starkli. • Front-end (optional) – A lightweight React/MobX dashboard consumes Starknet.js event streams, letting users generate secrets off-line, fund the escrow in one click, and watch the countdown timer until either withdrawal or refund is possible.
Hackathon
ETHGlobal Unite
2025
Contributors
- PedroRosalba
4 contributions