2

I have created two basic ERC20 tokens (TokenA and TokenB) and a contract that swaps TokenA with TokenB. Everything seems working but I get the error above when I run the test file. How can BigNumber { value: "0" } be different from itself? Can anyone please help?

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);
    }
}

Swap Contract

// 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'));
  });
});

1 Answer 1

1

Object types are being compared by reference, not by content. Like:

let foo = {};
let bar = {};
console.log(foo === bar);
//> false

So, convert to simple types - number, string or 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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.