0

I have deployed a smart contract over my private ethereum blockchain using geth.

Smart Contract

pragma solidity >=0.4.22 <0.6.0;

contract GreenPoints {

    string public name;
    string public symbol;
    uint8 public decimals = 18;
    uint256 public totalSupply;

    mapping (address => bool) public frozenAccount;
    mapping (address => uint256) public balanceOf;

    event Transfer(address indexed from, address indexed to, uint256 value);
    event FrozenFunds(address target, bool frozen);
    event Burn(address indexed from, uint256 value);

    constructor(
        uint256 initialSupply,
        string memory tokenName,
        string memory tokenSymbol
    ) public {
        totalSupply = initialSupply * 10 ** uint256(decimals);  
        balanceOf[msg.sender] = totalSupply;                
        name = tokenName;                                   
        symbol = tokenSymbol;                               
    }

    function _transfer(address _from, address _to, uint _value) internal {
        require(_to != address(0x0));
        require(balanceOf[_from] >= _value);
        require(balanceOf[_to] + _value >= balanceOf[_to]);
        uint previousBalances = balanceOf[_from] + balanceOf[_to];
        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(_from, _to, _value);
        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
    }

    function transfer(address _to, uint256 _value) public returns (bool success) {
        require(!frozenAccount[_to]);                           
        require (balanceOf[msg.sender] >= _value);                

        _transfer(msg.sender, _to, _value);
        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {       
        require (_to != address(0x0));                          
        require (balanceOf[_from] >= _value);                   
        require (balanceOf[_to] + _value >= balanceOf[_to]);    
        require(!frozenAccount[_from]);                         
        require(!frozenAccount[_to]);                           

        _transfer(_from, _to, _value);
        return true;
    }

    function burn(uint256 _value) public returns (bool success) {
        require(balanceOf[msg.sender] >= _value);   
        balanceOf[msg.sender] -= _value;            
        totalSupply -= _value;                      
        emit Burn(msg.sender, _value);
        return true;
    }

    function burnFrom(address _from, uint256 _value) public returns (bool success) {
        require(balanceOf[_from] >= _value);                
        balanceOf[_from] -= _value;                         
        totalSupply -= _value;                              
        emit Burn(_from, _value);
        return true;
    }

    function freezeAccount(address target, bool freeze) public {
        frozenAccount[target] = freeze;
        emit FrozenFunds(target, freeze);
    }
}

This is how i have initialized variables on geth console.

var abi=[ { "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_value", "type": "uint256" } ], "name": "burn", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "burnFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "frozenAccount", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "target", "type": "address" }, { "name": "freeze", "type": "bool" } ], "name": "freezeAccount", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "name": "initialSupply", "type": "uint256" }, { "name": "tokenName", "type": "string" }, { "name": "tokenSymbol", "type": "string" } ], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "name": "target", "type": "address" }, { "indexed": false, "name": "frozen", "type": "bool" } ], "name": "FrozenFunds", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Burn", "type": "event" } ];

var contract=web3.eth.contract(abi);

var contractinstance=contract.at('0x6621b0431ed6f533575380f2ba4788f44b5049b6')

I am able to check balance of address by using contractinstance.balanceOf('0x029285ee26b9da62f138133a9fe40cb8c2219c82')

But i am unable to utilize transferFrom function of my contract

It results in following error

contractinstance.transferFrom('0x029285ee26b9da62f138133a9fe40cb8c2219c82', '0xe149cb26b99f176367e32c4ce33bc3bcb455cec6', '100')
Error: invalid address
    at web3.js:3930:15
    at web3.js:3756:20
    at web3.js:5025:28
    at map (<native code>)
    at web3.js:5024:12
    at web3.js:5050:18
    at web3.js:5075:23
    at web3.js:4137:16
    at apply (<native code>)
    at web3.js:4223:16

Although wallets do exist

 personal.listWallets
[{
    accounts: [{
        address: "0x029285ee26b9da62f138133a9fe40cb8c2219c82",
        url: "keystore:///home/lubuntu/Project/data/keystore/UTC--2019-02-20T06-42-47.223857724Z--029285ee26b9da62f138133a9fe40cb8c2219c82"
    }],
    status: "Unlocked",
    url: "keystore:///home/lubuntu/Project/data/keystore/UTC--2019-02-20T06-42-47.223857724Z--029285ee26b9da62f138133a9fe40cb8c2219c82"
}, {
    accounts: [{
        address: "0xe149cb26b99f176367e32c4ce33bc3bcb455cec6",
        url: "keystore:///home/lubuntu/Project/data/keystore/UTC--2019-02-20T06-43-46.766062805Z--e149cb26b99f176367e32c4ce33bc3bcb455cec6"
    }],
    status: "Locked",
    url: "keystore:///home/lubuntu/Project/data/keystore/UTC--2019-02-20T06-43-46.766062805Z--e149cb26b99f176367e32c4ce33bc3bcb455cec6"
}]

I am trying to transfer tokens from my main account to another account that was created before deploying contract over my private blockchain.

  • Which version of web3 are you using? – goodvibration Feb 26 at 9:22
  • Also, contractinstance.transferFrom(...) should continue with .send({from: someApprovedAccount}). – goodvibration Feb 26 at 9:26
  • @goodvibration I am using web3 1.0, i have tried it with .send, but still the same error is produced. – Saeedi Feb 26 at 9:43
  • Are you sure? contractinstance.transferFrom should be undefined in that case (you should use contractinstance.methods.transferFrom). Can you please run npm ls web3 and say what you're getting? – goodvibration Feb 26 at 9:55
  • I am getting web3@1.0.0-beta.46 and also in geth it shows modules: admin:1.0 clique:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0 When i run contractinstance.methods.transferFrom, it produces following TypeError: Cannot access member 'transferFrom' of undefined – Saeedi Feb 26 at 10:04
0

It worked after resetting default account with

web3.eth.defaultAccount = eth.accounts[0]

And then

myContractInstance.transferFrom("0x029285ee26b9da62f138133a9fe40cb8c2219c82","0xe149cb26b99f176367e32c4ce33bc3bcb455cec6","10000",{from: eth.accounts[0]})

works fine.

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.