A sample implementation of a cross-chain atomic swap resolver using 1inch's cross-chain SDK. This project demonstrates how to implement secure cross-chain token swaps with escrow contracts on both source and destination chains.
This project implements a cross-chain atomic swap mechanism that allows users to swap tokens across different blockchain networks securely. The system uses:
- Escrow contracts to lock funds on both source and destination chains
- Hash-time-locked contracts (HTLC) for atomic swap execution
- 1inch Limit Order Protocol (LOP) for order execution
- Cross-chain SDK for seamless integration
-
Resolver Contract (
contracts/src/Resolver.sol
)- Main contract that orchestrates cross-chain swaps
- Deploys escrow contracts on source and destination chains
- Handles order execution through 1inch LOP
- Manages withdrawal and cancellation of escrows
-
Escrow System
IBaseEscrow.sol
- Base interface for escrow contractsIEscrowFactory.sol
- Factory for creating escrow contracts- Supports both ERC-20 tokens and native tokens
-
Libraries
TakerTraitsLib.sol
- Manages taker-specific parametersTimelocksLib.sol
- Handles time-based constraintsImmutablesLib.sol
- Manages immutable escrow parameters
- Source Chain Deployment: The resolver deploys an escrow contract on the source chain and locks the maker's funds
- Order Execution: Uses 1inch LOP to execute the swap order
- Destination Chain Deployment: Deploys a corresponding escrow on the destination chain
- Atomic Swap: The taker can withdraw funds by providing the secret hash
- Safety Mechanisms: Includes timelocks and rescue functions for dispute resolution
- Node.js >= 22
- Yarn or npm
- Tron private keys for mainnet and testnet
# Install dependencies
yarn install
Create a .env
file in the root directory:
# Private keys for Tron networks
PRIVATE_KEY_TRON=0xYourTronPrivateKeyHere
PRIVATE_KEY_NILE=0xYourTronPrivateKeyHere
The project supports:
- Tron Mainnet - Production network
- Tron Nile Testnet - Test network
Network configurations are defined in tronbox-config.js
.
yarn run compile
yarn deploy:tron:main
yarn deploy:tron:nile
Deploys an escrow contract on the source chain and executes the swap order.
function deploySrc(
IBaseEscrow.Immutables calldata immutables,
IOrderMixin.Order calldata order,
bytes32 r,
bytes32 vs,
uint256 amount,
TakerTraits takerTraits,
bytes calldata args
) external payable
Deploys an escrow contract on the destination chain.
function deployDst(
IBaseEscrow.Immutables calldata dstImmutables,
uint256 srcCancellationTimestamp
) external payable
Withdraws funds from an escrow using the secret.
function withdraw(
IEscrow escrow,
bytes32 secret,
IBaseEscrow.Immutables calldata immutables
) external
Cancels an escrow and returns funds to the original owner.
function cancel(
IEscrow escrow,
IBaseEscrow.Immutables calldata immutables
) external
- Ownable Pattern: Only the contract owner can deploy escrows and make arbitrary calls
- Timelocks: Built-in time constraints for withdrawal and cancellation periods
- Hash Verification: Secure secret-based withdrawal mechanism
- Rescue Functions: Emergency fund recovery mechanisms
- Atomic Operations: Ensures either both chains complete or both fail
@1inch/cross-chain-sdk
- Cross-chain functionality@1inch/solidity-utils
- Utility libraries@openzeppelin/contracts
- Security and access controlethers
- Ethereum interaction
tronbox
- Tron blockchain development frameworkjest
- Testing frameworkeslint
- Code lintingtypescript
- Type safety
# Run tests
yarn test
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
MIT License - see LICENSE file for details
For security issues, contact: security@1inch.io
This is an example implementation. Use at your own risk and ensure proper security audits before deploying to production.