This repository contains the source code for the TokenSwap smart contract, a decentralized exchange (DEX) that allows users to swap tokens using the Constant Product Market Maker (CPMM) formula. This README provides an overview of the contract, its functions, and how to interact with it.
The TokenSwap contract includes the following features:
-
Token Swapping: Users can swap tokenA for tokenB and vice versa using the CPMM formula to calculate exchange rates.
-
Liquidity Provision: Liquidity providers can add both tokenA and tokenB to the pool to facilitate trading.
-
Liquidity Withdrawal: Liquidity providers can withdraw their liquidity from the pool.
To get started with TokenSwap, follow these steps:
-
Compile the Contract: Use a Solidity compiler (e.g., Solidity, Hardhat, Truffle) to compile the TokenSwap.sol file.
-
Deploy the Contract: Deploy the compiled contract to the Ethereum network (mainnet or a testnet). Ensure that you provide valid addresses for tokenA and tokenB as constructor arguments.
-
Interact with the Contract: Users and liquidity providers can interact with the contract functions using Ethereum wallets or dApps. Ensure that you have the contract address to interact with it.
- Description: Calculates the expected amount of tokenB to receive when swapping _amountA of tokenA.
- Input:
_amountA
- The amount of tokenA to swap. - Output: The expected amount of tokenB.
- Description: Calculates the expected amount of tokenA required to receive _amountB of tokenB.
- Input:
_amountB
- The amount of tokenB to receive. - Output: The required amount of tokenA.
- Description: Allows users to swap tokenA for tokenB.
- Input:
amountA
- The amount of tokenA to swap. - Requirements: Sufficient allowance for tokenA, and tokenA balance must cover the swap.
- Description: Allows users to swap tokenB for tokenA.
- Input:
amountB
- The amount of tokenB to swap. - Requirements: Sufficient allowance for tokenB, and tokenB balance must cover the swap.
- Description: Allows liquidity providers to add both tokenA and tokenB to the liquidity pool.
- Inputs:
_amountA
- The amount of tokenA to add,_amountB
- The amount of tokenB to add. - Requirements: Sufficient allowances for both tokens, and token balances must cover the provided amounts.
- Description: Allows liquidity providers to withdraw their liquidity from the pool.
- Inputs:
_amountA
- The amount of tokenA to withdraw,_amountB
- The amount of tokenB to withdraw. - Requirements: Liquidity provider must have sufficient balances of both tokens in the pool.