Skip to content

Latest commit

 

History

28 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Course Mode Developer State

📜 SimpleSwap

📌 Essential Information

Solidity Pragma: 0.8.27

Network: Sepolia

Contract:

Owner: 0x39581f1c36CfeBfB36934E583fb3e3CE92Ba6c58

Version: 1.10

License: MIT


General Description

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.

Setup and Usage

1. Requirements

  • Development Environment: Remix IDE (Injected Provider - Metamask for a testnet like Sepolia)

  • Dependencies: OpenZeppelin Contracts.

    npm install @openzeppelin/contracts

2. Deployment (Step-by-step in Remix)

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 Transactions tab, select SimpleSwap from the dropdown.
  • In the Deploy section, locate the constructor fields: _tokenA and _tokenB.
  • Paste the deployed address of your MockTokenA into _tokenA.
  • Paste the deployed address of your MockTokenB into _tokenB.
  • Click Deploy. Copy the deployed SimpleSwap address (e.g., 0xa42b...).

3. Step-by-Step Testing in Remix

Use the deployed contract addresses from your Remix session:

  • Token A: 0x7c87b552996ebd58dc76f5809fb4476cf189ac00
  • Token B: 0x5544A7c23557Cc9fa410Ac1Aaa43713daA477D49
  • SimpleSwap: 0x216051a12dd8365dE1F4AdD61e952134562662d7
  • Account: 0x39581f1c36CfeBfB36934E583fb3e3CE92Ba6c58

3.1. Approve Tokens for SimpleSwap

Before SimpleSwap can move your tokens, you need to grant it permission using the approve function on each token.

  • Select your TokenA contract in the Deployed Contracts section.
  • Expand the approve function.
    • Spender:Enter 0x216051a12dd8365dE1F4AdD61e952134562662d7 (SimpleSwap address).
    • amount: Enter 1000000000000000000000 (e.g., 1000 TKA).
    • Click transact.
  • Repeat the process for your TokenB contract.

3.2. Add Liquidity

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
}

3.3. Swap Exact Tokens For 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)
]

3.4. Remove Liquidity

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.

📇 Project Information

Student: Elisa Araya
Course: ETH Kipu - Ethereum Developer Pack
Submission: Final Assignment - Module 3

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages