Example Token Swap on Nexus

To learn how to build a decentralized exchange (DEX) application on Nexus, take a look at the demo project below:

Overview

This project demonstrates how to build a simple token swap application on the Nexus blockchain. Users can swap Token A for Token B in a 1:1 ratio, showcasing essential DeFi interactions like:

  • Token approvals and transfers
  • Wallet connection
  • Multi-step transactions
  • Balance updates
  • Event handling

The Smart Contracts

The project uses three main smart contracts:

  1. Token A Contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract TokenA is ERC20 {
    constructor(uint256 initialSupply) ERC20("Token A", "TKNA") {
        _mint(msg.sender, initialSupply);
    }
}
  1. Token B Contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract TokenB is ERC20 {
    constructor(uint256 initialSupply) ERC20("Token B", "TKNB") {
        _mint(msg.sender, initialSupply);
    }
}
  1. Token Swap Contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract TokenSwap {
    IERC20 public tokenA;
    IERC20 public tokenB;
    
    constructor(address _tokenA, address _tokenB) {
        tokenA = IERC20(_tokenA);
        tokenB = IERC20(_tokenB);
    }
    
    function swap(uint256 amount) external returns (bool) {
        require(tokenA.transferFrom(msg.sender, address(this), amount), "Transfer failed");
        require(tokenB.transfer(msg.sender, amount), "Swap failed");
        return true;
    }
}

Project Structure

The project is organized into two main packages:

  • contracts: A Hardhat project containing the ERC20 token contracts and swap contract
  • frontend: A Next.js project with a clean UI for token swapping
nexus-swap-example/
├── contracts/
   ├── contracts/
   ├── TokenA.sol
   ├── TokenB.sol
   └── TokenSwap.sol
   └── ignition/
       └── modules/
           └── deploy.ts
├── frontend/
   ├── src/
   └── app/
       ├── page.tsx
       └── layout.tsx
   └── lib/
       └── deployedAddresses.json
└── README.md

Key Features

Smart Contract Architecture

  • Uses OpenZeppelin’s ERC20 implementation for tokens
  • Simple 1:1 swap mechanism between Token A and Token B
  • Two-step approval and swap process for security

Frontend Implementation

  • Clean, minimal UI built with Next.js 14 and Tailwind CSS
  • Real-time balance updates
  • Wallet connection handling
  • Transaction status feedback
  • Error handling and user notifications

DeFi Concepts Demonstrated

  • Token allowances and approvals
  • Multi-step DeFi transactions
  • Balance tracking
  • Contract interactions
  • Event monitoring

Getting Started

  1. Clone the repository and install dependencies:
git clone https://github.com/nexus-xyz/nexus-swap-example
cd nexus-swap-example
npm install
  1. Deploy the contracts:
cd contracts
npx hardhat run scripts/deploy.ts --network nexus
  1. Start the frontend:
cd frontend
npm run dev

Using the dApp

  1. Connect your wallet using the “Connect Wallet” button
  2. View your Token A and Token B balances
  3. Enter the amount of Token A you want to swap
  4. Click “Swap” and approve the token spending
  5. Confirm the swap transaction
  6. Wait for confirmation and see your updated balances

The contracts are deployed at:

  • TokenA: 0x83FD4F92926AC4FB7a0b510aD5615Fe76e588cF8
  • TokenB: 0x67bae4B1E5528Fb6C92E7E0BC243341dc71330db
  • TokenSwap: 0xf18529580694Ff80e836132875a52505124358EC