Solidity Pragma: 0.8.27
Network: Sepolia
Contract:
- TokenA
0xb5BAd729930d785F35F2CCD8E1C9284E7058c322 - TokenB
0xE13096D1C36F134C63Dd451DA43afFc0fb839eF3 - SimpleSwap
0x8A899B1F91a787fC4B30b664477b3b5D1A849166 - Verificador_Profesor Hash: 0x74d7c20e447802fa71397af1d13ecb62412f5a0e61615b73fa314cb6c060736f
- Debugger Tenderly
Owner: 0x39581f1c36CfeBfB36934E583fb3e3CE92Ba6c58
Version: 1.10
License: MIT
SimpleSwap is a foundational Decentralized Exchange (DEX) smart contract. It draws inspiration from the core Automated Market Maker (AMM) principles of Uniswap V2, providing a simplified yet functional implementation for understanding liquidity provision and token swapping on the blockchain.
This contract allows users to:
- Establish and manage liquidity pools for a specific pair of ERC-20 tokens.
- Execute direct token swaps between the two assets within the pool.
- Add and remove liquidity enabling them to earn a share of the trading fees generated by swaps.
-
Development Environment: Remix IDE (Injected Provider - Metamask for a testnet like Sepolia)
-
Dependencies: OpenZeppelin Contracts.
npm install @openzeppelin/contracts
2.1. Open Remix IDE: Go to Remix IDE.
2.2. Select Environment: In the Deploy & Run Transactions tab (the Ethereum logo icon), choose JavaScript VM for local testing. If using a public testnet like Sepolia, select Injected Provider - Metamask and connect your wallet.
2.3. Create Token Files:
- Create
MockTokenA.sol - Create
MockTokenB.sol
2.4. Deploy ERC-20 Tokens:
- Compile MockTokenA.sol.
- In the Deploy & Run Transactions tab, select MockTokenA from the dropdown and click Deploy. Copy the deployed address (e.g., 0xc475...).
- Repeat for MockTokenB.sol, copying its deployed address (e.g., 0x9dAf...).
2.5. Deploy SimpleSwap:
- Compile
SimpleSwap.sol. - In the
Deploy & Run Transactionstab, selectSimpleSwapfrom the dropdown. - In the
Deploysection, locate the constructor fields:_tokenAand_tokenB. - Paste the deployed address of your
MockTokenAinto_tokenA. - Paste the deployed address of your
MockTokenBinto_tokenB. - Click
Deploy. Copy the deployed SimpleSwap address (e.g., 0xa42b...).
Use the deployed contract addresses from your Remix session:
- Token A: 0x7c87b552996ebd58dc76f5809fb4476cf189ac00
- Token B: 0x5544A7c23557Cc9fa410Ac1Aaa43713daA477D49
- SimpleSwap: 0x216051a12dd8365dE1F4AdD61e952134562662d7
- Account: 0x39581f1c36CfeBfB36934E583fb3e3CE92Ba6c58
Before SimpleSwap can move your tokens, you need to grant it permission using the approve function on each token.
- Select your
TokenAcontract in theDeployed Contractssection. - Expand the
approvefunction.Spender:Enter0x216051a12dd8365dE1F4AdD61e952134562662d7(SimpleSwap address).amount: Enter1000000000000000000000(e.g., 1000 TKA).- Click transact.
- Repeat the process for your TokenB contract.
Now, let's add some liquidity to the SimpleSwap pool.
- Select your SimpleSwap contract in the Deployed Contracts section.
- Expand the addLiquidity function.
- _tokenA_param
- _tokenB_param:
- amountADesired: 100000000000000000000 (100 TKA)
- amountBDesired: 200000000000000000000 (200 TKB)
- amountAMin: 90000000000000000000 (90 TKA)
- amountBMin: 180000000000000000000 (180 TKB)
- to: 0x39581f1c36CfeBfB36934E583fb3e3CE92Ba6c58 (account address)
- deadline: Math.floor(Date.now() / 1000) + 600 (in Consola)
- Click transact.
- Expected Output: You should see a successful transaction and in the decoded output:
{
"0": "uint256: amountA 100000000000000000000", // 100 TKA
"1": "uint256: amountB 200000000000000000000", // 200 TKB
"2": "uint256: liquidity 141421356237309503880" // Approx. 141.42 LP tokens
}Let's exchange some tokens. Ensure you have approved enough TokenA (or TokenB if swapping the other way)
- Select your SimpleSwap contract in the Deployed Contracts section.
- Expand the swapExactTokensForTokens function.
- amountIn: 10000000000000000000 (10 TKA)
- amountOutMin: 18000000000000000000 (18 TKB - adjust this value if you get a "Slippage" or "InsufficientOutputAmount" error)
path: Crucial for Remix and Sepolia! Enter this as a JSON array string:
["0x7c87b552996ebd58dc76f5809fb4476cf189ac00", "0x5544A7c23557Cc9fa410Ac1Aaa43713daA477D49"]
❗ Make sure to format the path correctly, or the function will fail.
- Continue
- to: 0x39581f1c36CfeBfB36934E583fb3e3CE92Ba6c58 (or your account address)
- deadline: Math.floor(Date.now() / 1000) + 600 (in Consola)
- Click transact
- Expected Output: If successful, you will see a Swap event and the decoded output will be an array like:
[
"10000000000000000000", // amountIn (10 TKA)
"amountOut" // (The actual amount of TKB received)
]Finally, remove some of the liquidity you provided.
- Select your SimpleSwap contract.
- Expand the removeLiquidity function.
- _tokenA_param
- _tokenB_param:
- liquidity: The amount of LP tokens you received in Step 2 (e.g., 141421356237309503880).
- amountAMin: 0 (or a small minimum)
- amountBMin: 0 (or a small minimum)
- to: 0x39581f1c36CfeBfB36934E583fb3e3CE92Ba6c58 (account address)
- deadline: Math.floor(Date.now() / 1000) + 600 (in Consola)
- Click transact.
- Expected Output: You should see a Burn event and the decoded output will show the amounts of TokenA and TokenB returned to your address.
Student: Elisa Araya
Course: ETH Kipu - Ethereum Developer Pack
Submission: Final Assignment - Module 3