A cross-chain decentralized exchange (DEX) enabling seamless cross chain token swaps between Polygon Mainnet and NEAR Protocol using Hash Time Locked Contracts (HTLCs) and the 1inch Fusion+ cross-chain infrastructure.
Watch the Full Demo - See Near-FusionSwap in action with a complete walkthrough of cross-chain swaps between Polygon and NEAR Protocol.
- Cross-Chain Swaps: Seamlessly swap tokens between Polygon and NEAR Protocol
- HTLC Security: Trustless atomic swaps using Hash Time Locked Contracts
- 1inch Integration: Leverages 1inch's cross-chain SDK for optimal routing
- Modern UI: Clean, responsive interface built with Next.js and Tailwind CSS
- Real-time Pricing: Live token price feeds with automatic rate calculations
- Wallet Integration: Support for MetaMask and NEAR Wallet
- Test Environment: Complete testing setup with hardcoded test accounts
Near-FusionSwap/
├── frontend/ # Next.js React application
├── backend/ # NestJS API server
├── evm-near-fusion+/ # Smart contracts
│ ├── polygon/ # Polygon smart contracts (Solidity)
│ └── near/ # NEAR smart contracts (Rust)
- Frontend: Next.js application with TypeScript, Tailwind CSS, and Wagmi for wallet interactions
- Backend: NestJS API handling cross-chain swap logic and order management
- Smart Contracts:
- Polygon: Solidity contracts for escrow and resolution
- NEAR: Rust contracts for HTLC implementation
- Cross-Chain Bridge: 1inch cross-chain SDK integration
The cross-chain swap process operates in two distinct phases using Hash Time Locked Contracts (HTLCs) to ensure atomic, trustless swaps between Polygon and NEAR Protocol:
When users want to swap tokens FROM Polygon TO NEAR:
- [NEAR] User Account:
goldrogerswap.testnet(wants WETH tokens) - [NEAR] Resolver Account:
htlc.testnet(facilitates the swap on NEAR) - [POLYGON] User Account:
0xC15e658AC13a89E8D2E5adBBcf29D5d168554553(wants NEAR tokens) - [NEAR] Resolver Account:
0x77ed0fef5e9DFB34e776adb11c29dd19d382745C(facilitates the swap on POLYGON)
https://polygonscan.com/address/0x77ed0fef5e9dfb34e776adb11c29dd19d382745c
Detailed Process Flow:
- HTLC Creation: Resolver (
htlc.testnet) creates a new HTLC contract on NEAR Protocol - Funding: Resolver deposits NEAR tokens into the HTLC and sets
is_destination = true - Secret Reveal: User reveals the secret hash to claim NEAR tokens from the HTLC
- Cross-Chain Claim: Using the revealed secret, Resolver claims USDC/WETH from the Polygon HTLC
- Completion: Atomic swap completed - User receives NEAR, Resolver receives Polygon tokens
When users want to swap tokens FROM NEAR TO Polygon:
- User Account:
goldrogerswap.testnet(wants Polygon tokens) - Resolver Account:
htlc.testnet(facilitates the swap)
Detailed Process Flow:
- HTLC Creation: User (
goldrogerswap.testnet) creates a new HTLC contract on NEAR Protocol - Funding: User deposits NEAR tokens into the HTLC and sets
is_destination = false - Secret Reveal: Resolver reveals the secret hash to claim NEAR tokens from the HTLC
- Cross-Chain Claim: Using the revealed secret, User claims ETH/USDC tokens from the Polygon HTLC
- Completion: Atomic swap completed - Resolver receives NEAR, User receives Polygon tokens
- Time Locks: All HTLCs have expiration times for automatic refunds if swaps fail
- Hash Secrets: Cryptographic secrets ensure atomic execution across both chains
- No Counterparty Risk: Neither party can steal funds due to HTLC constraints
- Framework: Next.js 14 with TypeScript
- Styling: Tailwind CSS + Radix UI components
- Wallet Integration: Wagmi, MetaMask SDK
- State Management: React hooks
- HTTP Client: Fetch API with custom error handling
- Framework: NestJS with TypeScript
- Blockchain: Ethers.js for Polygon, near-api-js for NEAR
- Cross-Chain: 1inch Cross-Chain SDK
- API: RESTful endpoints with CORS support
- Polygon: Solidity contracts using Foundry framework
- NEAR: Rust contracts with cargo-near build system
- Security: HTLC pattern for atomic swaps
- Node.js: v18.0.0 or higher
- npm/yarn: Latest version
- Rust: For NEAR contract development
- Foundry: For Polygon contract development
- Git: For version control
git clone https://github.com/SudeepGowda55/Near-FusionSwap.git
cd Near-FusionSwapcd frontend
npm install
cp .env.example .env.local
# Edit .env.local to set NEXT_PUBLIC_BACKEND_URL if different from default
npm run devThe frontend will be available at http://localhost:3000
cd ../backend
npm install
yarn start:devThe backend API will be available at https://37623a0a3f91.ngrok-free.app
cd ../evm-near-fusion+/polygon
npm install
forge build
npm testcd ../near
cargo build --target wasm32-unknown-unknown --releaseNEXT_PUBLIC_BACKEND_URL=https://37623a0a3f91.ngrok-free.app
NEXT_PUBLIC_CHAIN_ID=137PORT=3001
POLYGON_RPC_URL=https://polygon-rpc.com
NEAR_NETWORK=testnet
PRIVATE_KEY=your_private_key_hereThe application supports:
- Polygon Mainnet (Chain ID: 137)
- NEAR Mainnet (for production)
- Connect Wallet: Connect your MetaMask wallet to Polygon network
- Select Tokens: Choose WETH on Polygon to swap for NEAR tokens
- Enter Amount: Specify the amount you want to swap
- Approve Token: Approve token spending (one-time setup)
- Cross-Chain Details: Provide NEAR account details
- Execute Swap: Confirm the cross-chain transaction
- Monitor Status: Track swap progress in real-time
- WETH (Polygon) ↔ NEAR (NEAR Protocol)
- Additional ERC-20 tokens (USDC, USDT, DAI) coming soon
For testing purposes, the application includes pre-configured test accounts:
- Test NEAR Account:
goldrogerswap.testnet - Test Address:
0xC15e658AC13a89E8D2E5adBBcf29D5d168554553
cd frontend
npm test
npm run test:coveragecd backend
npm test
npm run test:e2e# Polygon contracts
cd evm-near-fusion+/polygon
npm test
# NEAR contracts
cd ../near
cargo testInitiates a cross-chain swap from Polygon to NEAR.
Request Body:
{
"makerPk": "string",
"srcChainId": 137,
"makerAssetAddress": "0x7ceb23fd6bc0add59e62ac25578270cff1b9f619",
"takerAssetAddress": "0x0000000000000000000000000000000000000000",
"makingAmount": 0.001,
"takingAmount": 0.014,
"makerNearAccountId": "account.testnet"
}Response:
{
"success": true,
"message": "Swap from Polygon to NEAR successfully",
"data": {
"orderHash": "0x...",
"blockHash": "0x...",
"txHash": "0x..."
}
}- HTLC Pattern: Ensures atomic swaps without counterparty risk
- Time Locks: Automatic refunds if swaps aren't completed
- Multi-signature: Support for multi-sig wallet integration
- CORS Protection: Configured for secure cross-origin requests
- Input Validation: Comprehensive validation of all API inputs
- Rate Limiting: Protection against spam and DoS attacks
- Private keys are never logged or exposed
- All transactions are signed client-side
- Secure random number generation for HTLC secrets
We welcome contributions! Please follow these steps:
- Fork the Repository
- Create Feature Branch:
git checkout -b feature/amazing-feature - Commit Changes:
git commit -m 'Add amazing feature' - Push to Branch:
git push origin feature/amazing-feature - Open Pull Request
- Follow TypeScript/JavaScript style guides
- Write comprehensive tests for new features
- Update documentation for API changes
- Use conventional commit messages
- Ensure backend is running on port 3001
- Check that frontend is using the API proxy route
- Verify MetaMask is installed and unlocked
- Switch to Polygon network (Chain ID: 137)
- Check wallet permissions
- Ensure sufficient gas and token balances
- Verify contract addresses are correct
- Check network connectivity
This project is licensed under the MIT License - see the LICENSE file for details.
- 1inch Network: For cross-chain infrastructure and SDK
- NEAR Protocol: For blockchain infrastructure and developer tools
- Polygon: For scalable Ethereum infrastructure
- Community: All contributors and testers
- GitHub: @SudeepGowda55
- Project Link: Near-FusionSwap
Built with ❤️ for the cross-chain future
