-1

I am using openzepplin library and trying to transfer amount from owner to 2 diffrent wallets. All is working fine but I am getting error when I am checking the initial and final balance of owners after transaction.

Here is my token.sol code

// contracts/OceanToken.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";

contract OceanToken is ERC20Capped, ERC20Burnable {
    address payable public owner;
    uint256 public blockReward;

    constructor(
        uint256 cap,
        uint256 reward
    ) ERC20("OceanToken", "OCT") ERC20Capped(cap * (10 ** decimals())) {
        owner = payable(msg.sender);
        _mint(owner, 70000000 * (10 ** decimals()));
        blockReward = reward * (10 ** decimals());
    }

    function _mint(
        address account,
        uint256 amount
    ) internal virtual override(ERC20Capped, ERC20) {
        require(
            ERC20.totalSupply() + amount <= cap(),
            "ERC20Capped: cap exceeded"
        );
        super._mint(account, amount);
    }

    function _mintMinerReward() internal {
        _mint(block.coinbase, blockReward);
    }

    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 value
    ) internal virtual override {
        if (
            from != address(0) &&
            to != block.coinbase &&
            block.coinbase != address(0)
        ) {
            _mintMinerReward();
        }
        super._beforeTokenTransfer(from, to, value);
    }

    function setBlockReward(uint256 reward) public onlyOwner {
        blockReward = reward * (10 ** decimals());
    }

    function destroy() public onlyOwner {
        selfdestruct(owner);
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Only the owner can call this function");
        _;
    }
}

here is my test.js code

const { expect } = require("chai");
const hre = require("hardhat");

describe("OceanToken contract", function () {
    // global vars
    let Token;
    let oceanToken;
    let owner;
    let addr1;
    let addr2;
    let tokenCap = 100000000;
    let tokenBlockReward = 50;

    beforeEach(async function () {
        // Get the ContractFactory and Signers here.
        Token = await ethers.getContractFactory("OceanToken");
        [owner, addr1, addr2] = await hre.ethers.getSigners();

        oceanToken = await Token.deploy(tokenCap, tokenBlockReward);
    });

    describe("Deployment", function () {
        it("Should set the right owner", async function () {
            expect(await oceanToken.owner()).to.equal(owner.address);
        });

        it("Should assign the total supply of tokens to the owner", async function () {
            const ownerBalance = await oceanToken.balanceOf(owner.address);
            expect(await oceanToken.totalSupply()).to.equal(ownerBalance);
        });

        it("Should set the max capped supply to the argument provided during deployment", async function () {
            const cap = await oceanToken.cap();
            expect(Number(hre.ethers.formatEther(cap))).to.equal(tokenCap);
        });

        it("Should set the blockReward to the argument provided during deployment", async function () {
            const blockReward = await oceanToken.blockReward();
            expect(Number(hre.ethers.formatEther(blockReward))).to.equal(
                tokenBlockReward
            );
        });
    });

    describe("Transactions", function () {
        it("Should transfer tokens between accounts", async function () {
            // Transfer 50 tokens from owner to addr1
            await oceanToken.transfer(addr1.address, 50);
            const addr1Balance = await oceanToken.balanceOf(addr1.address);
            expect(addr1Balance).to.equal(50);

            // Transfer 50 tokens from addr1 to addr2
            // We use .connect(signer) to send a transaction from another account
            await oceanToken.connect(addr1).transfer(addr2.address, 50);
            const addr2Balance = await oceanToken.balanceOf(addr2.address);
            expect(addr2Balance).to.equal(50);
        });

        it("Should fail if sender doesn't have enough tokens", async function () {
            const initialOwnerBalance = await oceanToken.balanceOf(
                owner.address
            );
            // Try to send 1 token from addr1 (0 tokens) to owner (1000000 tokens).
            // `require` will evaluate false and revert the transaction.
            await expect(
                oceanToken.connect(addr1).transfer(owner.address, 1)
            ).to.be.revertedWith("ERC20: transfer amount exceeds balance");

            // Owner balance shouldn't have changed.
            expect(await oceanToken.balanceOf(owner.address)).to.equal(
                initialOwnerBalance
            );
        });

        it("Should update balances after transfers", async function () {
            const initialOwnerBalance = await oceanToken.balanceOf(
                owner.address
            );
            console.log(initialOwnerBalance);
            console.log(
                initialOwnerBalance - Number(hre.ethers.formatEther(150))
            );

            // Transfer 100 tokens from owner to addr1.
            await oceanToken.transfer(addr1.address, 100);

            // Transfer another 50 tokens from owner to addr2.
            await oceanToken.transfer(addr2.address, 50);

            // Check balances.
            const finalOwnerBalance = await oceanToken.balanceOf(owner.address);
            expect(finalOwnerBalance).to.equal(initialOwnerBalance.sub(150));

            const addr1Balance = await oceanToken.balanceOf(addr1.address);
            expect(addr1Balance).to.equal(100);

            const addr2Balance = await oceanToken.balanceOf(addr2.address);
            expect(addr2Balance).to.equal(50);
        });
    });
});

I am getting error in this line expect(finalOwnerBalance).to.equal(initialOwnerBalance.sub(150)); It says .sub() function not available. Can someone help me?

1 Answers1

0

In your test, you use sub(), which is a Bignumber function. Now here initialOwnerBalance is already a Bignumber but the other number you are adding is simply a number which is 150. If you look into the source code of sub(), it expects a bignumberish type. Please have a look Gihub code for sub()

Now to make it work you cam convert it to a Bignumber like this : Bignumber.from(150)