Search code examples
javascriptethereumsmartcontractsweb3jshardhat

How to get the correct balance using a smart contract?


I have simple smartcontract:

pragma solidity ^0.7.0;

contract Token {
    string public name = "My Hardhat Token";
    string public symbol = "MHT";
    uint256 public totalSupply = 1000000;
    address public owner;
    mapping(address => uint256) balances;

    constructor() {
        balances[msg.sender] = totalSupply;
        owner = msg.sender;
    }

    function transfer(address to, uint256 amount) external {
        require(balances[msg.sender] >= amount, "Not enough tokens");
        balances[msg.sender] -= amount;
        balances[to] += amount;
    }

    function balanceOf(address account) external view returns (uint256) {
        return balances[account];
    }
}

I take this contract from

And I have successfully compiled and uploaded this smart contract to the ropsten testnet. I use hardhat frameworc for this actions. https://hardhat.org/tutorial/deploying-to-a-live-network.html

After that, I wrote a script that should receive the balance, perform the transfer and create transactions.

var provider = "https://eth-ropsten.alchemyapi.io/v2/iwxxx";
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider(provider));
const abi = [{...}];
const address = "0xC1xxx";    // after deploy smart contract
const senderAddress  = "0x2bxxx";
const receiverAddress  = "0x39xxx";
const privKey = "0xf5xxx";

const token = new web3.eth.Contract(abi, address);
token.setProvider(web3.currentProvider)
token.methods.balanceOf(address).call( (err, res) => { console.log("Address Balance: ", res); }); // 0
token.methods.balanceOf(senderAddress).call((err, res) => { console.log("SenderAddress Balance: ", res); }); // 0

token.methods.transfer(receiverAddress, "1").send({ "from": address }, (err, res) => {
    if (err) {console.log("Error occured ! ", err);return}
    console.log("Hash transaction: " + res);
});

send_tr();
async function send_tr(){
    let transactionNonce = await web3.eth.getTransactionCount(senderAddress);
    let raw = {"to":receiverAddress, "value":"1", "gas":2000000, "nonce":web3.utils.toHex(transactionNonce)};
    const signedTx = await web3.eth.accounts.signTransaction(raw, privKey);
    await web3.eth.sendSignedTransaction(signedTx.rawTransaction, (err, ret) => {
        if (err) {console.log("An error occurred", err);return;}
        console.log("Success, txHash is: ", ret);
    });
}

The question is why my balance is zero even though the contract is underdeployed?

the transaction at the end is successful (as far as I understand, the transaction is needed so that the tokens transferred from address to address are successfully transferred)


Solution

  • In the solidity constructor, you're setting the balance of the deployer address - not of the contract address.

    constructor() {
        balances[msg.sender] = totalSupply;
        owner = msg.sender;
    }
    

    But then in the JS script, you're asking about balance of the contract address.

    const token = new web3.eth.Contract(abi, address);
    token.methods.balanceOf(address)