首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AssertionError:期望BigNumber { value:"0“}等于BigNumber { value:"0”}

AssertionError:期望BigNumber { value:"0“}等于BigNumber { value:"0”}
EN

Ethereum用户
提问于 2023-04-06 09:14:05
回答 1查看 31关注 0票数 2

我创建了两个基本的ERC20令牌(TokenA和TokenB)和一个与TokenB交换TokenA的合同。一切看起来都正常,但是当我运行测试文件时,我得到了上面的错误。BigNumber { value:"0“}如何与其自身不同?有人能帮忙吗?

TokenA

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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);
    }
}

互换合同

代码语言:javascript
复制
// 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

代码语言:javascript
复制
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'));
  });
});
EN

回答 1

Ethereum用户

回答已采纳

发布于 2023-04-06 09:58:35

对象类型是通过引用而不是内容进行比较的。比如:

代码语言:javascript
复制
let foo = {};
let bar = {};
console.log(foo === bar);
//> false

因此,转换为简单类型- numberstringbigint

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/148598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档