Solidity

交換代幣後儲備金不變

  • September 21, 2022

我分叉了 Fantom 網路。我在我的測試腳本中使用了部署在 Fantom 網路上的 UniswapV2Router02、WrappedFtm、Btc 合約。

這是測試腳本。

   beforeEach(async () => {
       [owner, account1, account2] = await ethers.getSigners();
       const myContract = await ethers.getContractFactory(
         "MyContract"
       );
       MyContract = await myContract.deploy();
       await MyContract.deployed();
   
       const UniswapAbi = require("./UniswapV2Router02.json");
       UniswapRouter = new ethers.Contract(UNISWAPV2ROUTER02, UniswapAbi, owner);
   
       const FtmAbi = require("./WrappedFtm.json");
       WrappedFtm = new ethers.Contract(FTM, FtmAbi, owner);
   });
   
   describe("Test", () => {
       beforeEach(async () => {
         await WrappedFtm.deposit({ value: "2000000000000000000" });
         await WrappedFtm.approve(UniswapRouter.address, "2000000000000000000");
       });
   
       it("getCurrentPrice test", async () => {
         await MyContract.setFeedAddress(FTM_USD_FEED, BTC_USD_FEED);
         await MyContract.getCurrentPrice(FTM_BTC_LP);
         const amount = await UniswapRouter.swapExactTokensForTokens(
           "1000000000000000000",
           1,
           [FTM, WBTC],
           owner.address,
           Date.now() + 100000
         );
         console.log({ amount });
         await MyContract.getCurrentPrice(FTM_BTC_LP);
       });
   });

這是我的契約。

   // SPDX-License-Identifier: UNLICENSED
   pragma solidity ^0.8.0;
   
   import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
   import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";
   import "hardhat/console.sol";
   
   contract MyContract {
       uint public version;
       address public token0FeedAddress;
       address public token1FeedAddress;
   
       constructor() {
           version = 1;
       }
       
       function getCurrentPrice(address token) external view returns(uint) {
           address token0Address = IUniswapV2Pair(token).token0();
           address token1Address = IUniswapV2Pair(token).token1();
           (
               uint reserve0,
               uint reserve1,
               // uint blockTimestampLast
           ) = IUniswapV2Pair(token).getReserves();
           console.log(reserve0, reserve1);
       }
   
       function setFeedAddress(address feedAddress0, address feedAddress1) external {
           token0FeedAddress = feedAddress0;
           token1FeedAddress = feedAddress1;
       }
   }

在 getCurretPrice 函式中,我記錄了 token0 和 token1 的儲備,但在交換令牌後它們沒有改變。請有人幫助我。

固定的。IUniswapV2Router02 和 IUniswapV2Pair 的出廠地址不同。

交換不應改變 ERC20 代幣的總供應量。總供應量應該保持不變。只有持有代幣的賬戶會因為交換而改變,而不是有多少代幣在流通。

引用自:https://ethereum.stackexchange.com/questions/136104