Build a token swap dApp on Nexus
Token A
and Token B
for one another.
hardhat.config.js
which will define the network configuration, RPC endpoint for Hardhat to connect to, and chainID for signature verification:
.env
in your project root directoryhardhat.config.js
if not already present:TokenSwap
contract in order to receive Token B.
Behavior:
ERC20
implementation for standard compliance and security.initialSupply
argument at deployment.msg.sender
, typically the deployer.TokenSwap
contract.
Behavior:
TokenSwap
contract with Token B before users can perform swaps.TokenSwap
contract performs the core logic of the application: exchanging Token A for Token B at a fixed 1:1 ratio.
Purpose:
Enables users to trade Token A for Token B in a single-step transaction, after approval.
Constructor:
IERC20
instances for interaction.swap(uint256 amount)
function executes the exchange.amount
of Token A from the caller to the contract using transferFrom
. This requires the user to have previously approved the contract to spend their tokens.amount
of Token B from the contract to the user.scripts/deploy.js
:
A deployment script is a programmatic way to deploy your smart contracts to a blockchain using Hardhat. Instead of manually interacting with the network through a UI or console, deployment scripts allow you to:
ether.js
in your Next.js project:
src/app/page.tsx
file to integrate the deployed smart contract with the frontend:
0x83FD4F92926AC4FB7a0b510aD5615Fe76e588cF8
0x67bae4B1E5528Fb6C92E7E0BC243341dc71330db
0xf18529580694Ff80e836132875a52505124358EC