I forked Fantom network. And I use UniswapV2Router02, WrappedFtm, Btc contracts deployed on Fantom network in my test scripts.
This is test scripits.
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);
});
});
This is my contract.
// 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;
}
}
In getCurretPrice function, I logged reserves of token0 and token1 but they are not changed after swap tokens. Please someone help me.