Skip to content

Instantly share code, notes, and snippets.

@mingderwang
Created January 15, 2025 03:46
Show Gist options
  • Save mingderwang/b34f910b01d7943d529b8ee3e01132a6 to your computer and use it in GitHub Desktop.
Save mingderwang/b34f910b01d7943d529b8ee3e01132a6 to your computer and use it in GitHub Desktop.
// 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