Created
January 15, 2025 03:46
-
-
Save mingderwang/b34f910b01d7943d529b8ee3e01132a6 to your computer and use it in GitHub Desktop.
Uniswapv2pairlike https://chatgpt.com/share/677e0386-f0ac-800f-a922-f23ed299c55e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
import "./SimpleERC20.sol"; | |
import "./UniswapV2PairLike.sol"; | |
contract TestUniswapV2Pair { | |
SimpleERC20 public tokenA; | |
SimpleERC20 public tokenB; | |
UniswapV2PairLike public pair; | |
constructor() { | |
// Deploy two ERC20 tokens | |
tokenA = new SimpleERC20(1000 ether); // 1000 tokens with 18 decimals | |
tokenB = new SimpleERC20(1000 ether); // 1000 tokens with 18 decimals | |
// Deploy the UniswapV2PairLike contract | |
pair = new UniswapV2PairLike(address(tokenA), address(tokenB)); | |
// Approve the pair contract to spend tokens on behalf of this contract | |
tokenA.approve(address(pair), type(uint256).max); | |
tokenB.approve(address(pair), type(uint256).max); | |
// Add initial liquidity to the pool | |
pair.addLiquidity(100 ether, 200 ether); // Add 100 TokenA and 200 TokenB | |
} | |
function testSwap() public { | |
// Transfer tokens to simulate a user having them | |
tokenA.transfer(address(pair), 10 ether); // Send 10 TokenA to the pair contract | |
// Perform a swap, expecting TokenB in return | |
pair.swap(0, 5 ether); // Request 5 TokenB as output | |
} | |
function getReserves() public view returns (uint256, uint256) { | |
return pair.getReserves(); | |
} | |
function balances() public view returns (uint256 balanceA, uint256 balanceB) { | |
balanceA = tokenA.balanceOf(address(this)); | |
balanceB = tokenB.balanceOf(address(this)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment