我创建了两个基本的ERC20令牌(TokenA和TokenB)和一个与TokenB交换TokenA的合同。一切看起来都正常,但是当我运行测试文件时,我得到了上面的错误。BigNumber { value:"0“}如何与其自身不同?有人能帮忙吗?
TokenA
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract TokenA is ERC20 {
constructor() ERC20("TokenA", "TA") {}
function mint(address to, uint256 amount) public {
_mint(to, amount);
}
}TokenB
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract TokenB is ERC20 {
constructor() ERC20("TokenB", "TB") {}
function mint(address to, uint256 amount) public {
_mint(to, amount);
}
}互换合同
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "hardhat/console.sol";
contract SwapAforB {
uint256 private exchangeRatio;
ERC20 public TokenA;
ERC20 public TokenB;
constructor(uint256 _exchangeRatio, address tokenAAddress, address tokenBAddress) {
exchangeRatio = _exchangeRatio;
TokenA = ERC20(tokenAAddress);
TokenB = ERC20(tokenBAddress);
}
function swapTokenAforB(uint256 amount) public {
//Check balance of TokenA
require(TokenA.balanceOf(msg.sender) >= amount, "Insufficient balance of TokenA");
// Approve swapContract to spend TokenA on behalf of msg.sender
require(TokenA.allowance(msg.sender, address(this)) >= amount, "TokenA allowance too low");
// Calculate the amount of TokenB to send
uint256 amountB = amount / exchangeRatio;
require(TokenB.balanceOf(address(this)) >= amount, "Exchange has insufficient balance of TokenB");
// Transfer TokenA from msg.sender to this contract
require(TokenA.transferFrom(msg.sender, address(this), amount), "TokenA Transfer failed");
console.log("amountB ", amountB);
console.log("TkB Balance ", TokenB.balanceOf(address(this)));
// Transfer TokenB from this contract to msg.sender
require(TokenB.transfer(msg.sender, amountB), "TokenB Transfer failed");
}
}testSwap.js
const { expect } = require("chai");
const { BigNumber } = require('ethers');
describe("TokenA <-> TokenB Swap", function () {
let tokenA, tokenB, swapContract;
let owner, alice;
const exchangeRatio = 2;
beforeEach(async function () {
// Deploy TokenA and TokenB contracts
const TokenA = await ethers.getContractFactory("TokenA");
tokenA = await TokenA.deploy();
const TokenB = await ethers.getContractFactory("TokenB");
tokenB = await TokenB.deploy();
//Deploy swapContract
const SwapContract = await ethers.getContractFactory("SwapAforB");
swapContract = await SwapContract.deploy(exchangeRatio, tokenA.address, tokenB.address);
// Mint initial supply of TokenA and TokenB for Owner
[owner, alice] = await ethers.getSigners();
const initialSupply = ethers.utils.parseEther("1000");
await tokenA.mint(owner.address, initialSupply);
await tokenB.mint(owner.address, initialSupply);
});
it("should swap TokenA for TokenB", async function () {
// Step 1: Transfer tokenA to Alice and tokenB to swapToken
const amount = ethers.utils.parseEther("100");
await tokenA.transfer(alice.address, amount);
await tokenB.transfer(swapContract.address, amount);
// Step 2: Alice approves swapContract to transfer TokenA
await tokenA.connect(alice).approve(swapContract.address, amount);
// Step 3: Alice swaps TokenA for TokenB
await swapContract.connect(alice).swapTokenAforB(amount);
// Check that Alice's balances have been updated correctly
const balanceA = await tokenA.balanceOf(alice.address);
const balanceB = await tokenB.balanceOf(alice.address);
expect(balanceA).to.be.equal(BigNumber.from('0'));
expect(balanceB).to.be.equal(BigNumber.from('50'));
});
});发布于 2023-04-06 09:58:35
对象类型是通过引用而不是内容进行比较的。比如:
let foo = {};
let bar = {};
console.log(foo === bar);
//> false因此,转换为简单类型- number、string或bigint
expect(balanceA.toNumber()).to.be.equal(0);
expect(balanceA.toString()).to.be.equal('0');
// if your env supports bigints
expect(balanceA.toBigInt()).to.be.equal(0n);https://ethereum.stackexchange.com/questions/148598
复制相似问题