0

I trying to create and test an ERC-20 Token on a local geth blockchain.

I tried compiling and testing my contract on Remix's Javascript VM environment and it works fine.

Then, I wanted to try deploying the contract on my local chain so I changed the environment to Web3, set it to local host with the correct port of the miner process, and deployed it (the deployment transaction was successful and I can view the contract object on Remix).

I use a totalSupply variable which is given in the constructor and saved in a local variable.

However, when I try reading this variable it always returns 0 rather than the value that I have provided (this works as intended in the Javascript VM) and I even tried giving totalSupply a default value of 1 but the getter function still returns 0 so I reckon it is a reading problem rather than a writing one.

Here is my code but I don't think the code is the problem:

pragma solidity ^0.5.0;

import "./IERC20.sol";
import "./SafeMath.sol";


contract EnergyToken is IERC20{
using SafeMath for uint;
mapping(address => uint256) balances;
mapping(address => mapping(address => uint256)) allowed;
uint256 totalSupply_;
address minter;

constructor(uint256 total) public{


totalSupply_ = total;
balances[msg.sender] = totalSupply_;
minter = msg.sender;

}

  modifier onlyMinter() {
    require(msg.sender == minter);
    _;
  }
function totalSupply() public view returns(uint256){
    return totalSupply_;
}
function balanceOf(address tokenOwner)  public view returns(uint256){
    return balances[tokenOwner];
}

function transfer(address receiver, uint numTokens) public returns (bool) {

  require(numTokens <= balances[msg.sender]);
  balances[msg.sender] = balances[msg.sender].sub(numTokens);
  balances[receiver] = balances[receiver].add(numTokens);
  emit Transfer(msg.sender, receiver, numTokens);
  return true;

}

function approve(address delegate, uint numTokens) public returns (bool){

    allowed[msg.sender][delegate] = numTokens;
    emit Approval(msg.sender, delegate, numTokens);
    return true;

}

function allowance(address owner, address delegate) public view returns (uint){

    return allowed[owner][delegate];

}

function increaseAllowance(address delegate, uint numTokens) public returns (bool){

    allowed[msg.sender][delegate] = allowed[msg.sender][delegate].add(numTokens);
    return true;
}

function decreaseAllowance(address delegate, uint numTokens) public returns (bool){

    allowed[msg.sender][delegate] = allowed[msg.sender][delegate].sub(numTokens);
    return true;

}

function transferFrom(address owner, address buyer, uint numTokens) public returns (bool)
 {

    require(balances[owner]>= numTokens);
    require(allowed[owner][msg.sender] >= numTokens);
    allowed[owner][msg.sender] = allowed[owner][msg.sender].sub(numTokens);
  balances[owner] = balances[owner].sub(numTokens);
  balances[buyer] = balances[buyer].add(numTokens);
  emit Transfer(owner, buyer, numTokens);
  return true;

  }


function _mint(address account, uint256 amount) public  {

        require(account != address(0), "ERC20: mint to the zero address");
        require(msg.sender == minter, "ERC20: unauthorised mint!");
        totalSupply_ = totalSupply_.add(amount);
        balances[account] = balances[account].add(amount);
        emit Transfer(address(0), account, amount);
    }

    function _burn(address account, uint256 amount) public onlyMinter 
{

        require(account != address(0), "ERC20: burn from the zero address");

        balances[account] = balances[account].sub(amount, "ERC20: burn amount exceeds balance");
        totalSupply_ = totalSupply_.sub(amount);
        emit Transfer(account, address(0), amount);

    }
}
5
  • 1
    when I try reading this variable - where is the code for that??? – goodvibration Jan 23 '20 at 18:07
  • Solidity code seems to be working - likely a problem with how you are trying to read it. – Steven V Jan 23 '20 at 18:40
  • The code is working fine also with geth. I think the problem is how you are reading the variable. – Mahesh Rajput Jan 24 '20 at 5:57
  • I was reading it using the totalSupply function in Remix which returns 0 always. However, when i call that function in the javascript VM it works fine which is weird.I also tried deploying the contract using truffle and attempting to call the function that way but that returned an error "Returned values aren't valid, did it run Out of Gas?" as well – Aleksi Daskalov Jan 24 '20 at 10:04
  • You're likely to fine more on your problem in the logs of the node, so check them if you have access. Truffle's "Returned values aren't valid, did it run Out of Gas?" is a bit deceiving error since RPCs endpoints do not provide enough information as to what happened and Truffle simply assumes the most common mistake. – fixanoid Jan 27 '20 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.