0

I was managed to use transfer() function of the token from my contract, however, the balanceOf() function will only return 0. (or it seems on MyEtherWallet)

I was testing a case where the smart contract receives a token from EOA(Bittrex account) and transfers to another EOA(MyEtherWallet account).

So I've deployed my smart contract below from MyEtherWallet and sent tokens (Golem in this case) to this contract.

Then, I have called transfer() function of the token(Golem) and successfully transferred the token to my MyEtherWallet account.

I have checked the balance by visiting the (Golem) token's contract on Etherscan which has been successfully transferred to my account(MyEtherWallet account).

Reading balance from token contract

However, whenever I try to read the balance of the token from my contract, it only shows 0.

I thought this was due to the decimals so I have tried multiplying, dividing the amount that would have returned from balanceOf() function. However, none of them works. It only shows 0.

I am so lost here and asking for more advanced solidity developer's help. What can I do to make a contract that reads the balance of the token?

Thank you for listening.

Here's the smart contract code that I have made.

P.S. Thought the problem was constant, tried using view, did not work. Also, this code does not have a function that multiplies the returned balance.

pragma solidity ^0.4.24;

contract ERC20Basic{

function balanceOf(address _owner) external constant returns (uint256 balance);
function transfer(address to, uint256 value) public returns (bool);


}

library SafeMath {//SafeMath Code}

contract TokenTransferContract{

using SafeMath for uint256;

address _owner;
address _tokenAddress;
ERC20Basic erc20Instance;

constructor () public {

    _owner = msg.sender; //Contract distributor is the owner of this contract.

}

// Setting the instance to call functions from different contract (In this case ERC20 Token's function)

function setTokenInstance(address _tokenContractAddress) public {

    _tokenAddress = _tokenContractAddress;
    erc20Instance = ERC20Basic(_tokenContractAddress);

}

//transfer function works.

function transferContractTokenToOwner(uint256 _tokenAmount) public {
    require(_owner == msg.sender);

    erc20Instance.transfer(_owner, _tokenAmount); //Transfering the amount of token that was allocated with the address of this(UserTokenWalletContract) address to Owner(EOA).

}

function getAmountOfTokenOfOwner() view public returns (uint256 _amount){
    require(_owner == msg.sender);

    _amount = (erc20Instance.balanceOf(_owner)).div(1000000000000000000);

}

function getAmountOfTokenOfContract() view public returns (uint256 _amount){

    require(_owner == msg.sender);

    _amount = (erc20Instance.balanceOf(address(this))).div(1000000000000000000);

}

 function noneDecimalGetAmountOfTokenOfOwner() view public returns (uint256 _amount){
    require(_owner == msg.sender);

    _amount = erc20Instance.balanceOf(_owner);

}

function noneDecimalgetAmountOfTokenOfWalletContract() view public returns (uint256 _amount){
    require(_owner == msg.sender);

    _amount = erc20Instance.balanceOf(address(this));

}


function getOwnerAddress() view public returns (address _ownerAddress){
    _ownerAddress = _owner;

}

function getContractAddress() view public returns (address _contractAddress){
    _contractAddress = address(this);

}

function getContractInstanceAddress() view public returns (address _contractInstanceAddress){ 
    _contractInstanceAddress = _tokenAddress;

}

}
1

Found the answer.

The problem was the require() state in the balanceOf function.

The balanceOf function does not need transaction, which means it has no msg.sender. The msg.sender is 0x00000000000000000.

It really doesn't need to have a require() statement. It's a caller.

So thanks to me I guess.

Good day!

Good day to you too!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.