solidity sepolia test, the balance does not change after the function is called, add once is succ

  Kiến thức lập trình

solidity

// SPDX-License-Identifier: MIT
// Compatible with OpenZeppelin Contracts ^5.0.0
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";

contract MyTokenERC20 is ERC20, ERC20Permit {
    AggregatorV3Interface internal dataFeed =
        AggregatorV3Interface(0x694AA1769357215DE4FAC081bf1f309aDC325306);

    constructor() ERC20("MyToken", "MTK") ERC20Permit("MyToken") {}

    event Test(string val);
    uint256 min_num = 10 * 10 ** 18; 

    function mint() public payable {
        require(
            convertEthAmountToUsdValue(msg.value) >= 1 * 10 * 10 ** 18,
            "not usd enough"
        );
        _mint(msg.sender, 10 * min_num);

    }

    function convertEthAmountToUsdValue(
        uint256 amountVal
    ) public view returns (uint256) {
        (
            ,
            /* uint80 roundID */ int answer /*uint startedAt*/ /*uint timeStamp*/ /*uint80 answeredInRound*/,
            ,
            ,

        ) = dataFeed.latestRoundData();

        return amountVal * (uint256(answer) / 10 ** 8);
    }
}

test.js
in the code the log totalEnd and end will output 10 * 10 * 10 ** 18 if once function is called. but if you hide the once function then the log totalEnd and end will print 0, why?

const { network, getNamedAccounts, deployments, ethers } = require("hardhat");

describe("MyToken test", function () {
  let deployer, myToken, MyTokenBurn;
  beforeEach(async function () {
    deployer = (await getNamedAccounts()).deployer;
    await deployments.fixture(["all"]);
    //myToken = await ethers.getContract('MyToken', deployer);
    MyTokenBurn = await ethers.getContract("MyTokenERC20", deployer);
  });

  describe("test", function () {
    it("test1", async function () {
      const BASE_FEE = ethers.parseEther("0.01");
      const total = await MyTokenBurn.totalSupply();
      console.log("total:" + total);
      const st = await MyTokenBurn.balanceOf(deployer);
      console.log("st:" + st);
    //   await new Promise(async (resolve, reject) => {
    //     MyTokenBurn.once('Transfer', async (index1, index2, index3) => {
    //         console.log('index1:' + index1 + ", index2:" + index2 + ", index3:" + index3);
    //         resolve();
    //     });

    //     await MyTokenBurn.mint({ value: BASE_FEE });
    //   });
      await MyTokenBurn.mint({ value: BASE_FEE });
      const totalEnd = await MyTokenBurn.totalSupply();
      console.log("totalEnd:" + totalEnd);
      const end = await MyTokenBurn.balanceOf(deployer);
      console.log("end:" + end);
    });
  });
});

i expect the log totalEnd and End will output 10 * 10 * 10 ** 18, what should i do, please help me, thanks

New contributor

ingram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT