I use hardhat to test a solidity contract but I get a different result with functions:
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "hardhat/console.sol";
contract TestContract is ERC721Enumerable, Ownable {
using SafeMath for uint;
...
function getContractBalance() public view onlyOwner returns (uint) {
return address(this).balance;
}
function getUserBalance(address _owner) external view returns (uint) {
return address(_owner).balance;
}
}
test.js
...
beforeEach(async function () {
Token = await ethers.getContractFactory("TestContract");
[owner, addr1, addr2, ...addrs] = await ethers.getSigners();
hardhatToken = await Token.deploy("test");
});
it("First test", async function () {
let total = await hardhatToken.totalSupply() // BigNumber { value: "0" }
let userBalance2 = await hardhatToken.getUserBalance(owner.address) // BigNumber { value: "9999979080649422213031" }
let userBalance = await hardhatToken.balanceOf(owner.address) // BigNumber { value: "0" }
let cBalance = await hardhatToken.getContractBalance() // BigNumber { value: "0" }
})
I think it's because a different unity (ether -> wei) but I don't know why. Can you explain me ?
Thank you
This function returns the amount of eth of the _owner
.
function getUserBalance(address _owner) external view returns (uint) {
return address(_owner).balance;
}
This function returns the amount of the eth that the contract own.
function getContractBalance() public view onlyOwner returns (uint) {
return address(this).balance;
}
This function returns the amount of TOKEN that the owner has:
balanceOf(owner.address)