5

I'm running a simple Solity contract which can hold it's own balances for users however I cannot seem to run the transfer function

pragma solidity ^0.4.8;

contract SimpleWallet {

    address public owner;
    mapping (address => uint) balances;

    // Constructor
    function SimpleWallet(){
        owner = msg.sender;
        balances[owner] = 1000;
    }

    function transfer(address _to, uint _value) returns (bool success){
        if (balances[msg.sender] < _value) {
            return false;
        }

        balances[msg.sender] -= _value;
        balances[_to] += _value;
        return true;
    }

    function getBalance(address _user) constant returns (uint _balance) {
        return balances[_user];
    }
}

I'll then run..

x = SimpleWallet.deployed()

a1 = web3.eth.accounts[0]

x.then(i => i.getBalance(a1).then(console.log))

And get a returned error

Error: Error: VM Exception while executing eth_call: invalid JUMP at f05bf9ee42d946231b79045ab38aabe852ea0113d27e047820c05041e8c24734/7be97c83604dc80e217456e9da80bc0a2d73f847:43
    at /usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/web3-provider-engine/subproviders/vm.js:117:17
    at /usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/lib/runTx.js:59:5
    at /usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:3824:9
    at /usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:460:16
    at replenish (/usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:977:25)
    at iterateeCallback (/usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:967:17)
    at /usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:944:16
    at /usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:3821:13
    at apply (/usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:21:25)
    at /usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/ethereumjs-vm/node_modules/async/dist/async.js:56:12
    at Object.module.exports.InvalidResponse (/usr/local/lib/node_modules/truffle/node_modules/web3/lib/web3/errors.js:35:16)
    at /usr/local/lib/node_modules/truffle/node_modules/web3/lib/web3/requestmanager.js:86:36
    at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/node_modules/web3/lib/web3/httpprovider.js:118:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/node_modules/xhr2/lib/xhr2.js:509:12)
    at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/node_modules/xhr2/lib/xhr2.js:469:24)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:975:12)
8

This is how you can access/call function in truffle console.

$ truffle console

truffle(development)> compile
truffle(development)> contract_name.new('Constructor Initialization')

// Above command will return lot of things on console, second last 
// attribute would be address(hash of the contract)
// use it the next line of command

truffle(development)> contract_name.at("contract address").functionName()

Hope this helps.

3

After deployed using truffle,

x = SimpleWallet.deployed()

you can then call functions,

var balance =x.getBalance(a)

2

Simply open the Truffle console with the given command : $truffle console

and make an instance of the contract

var instance_name = ContractName.at(Contract_Address)

Contract_Address can be accessed simply by writing : ContractName.address

When the instance got created, use it to call the functions in the smart contract like :

instance_name.function_name(...)

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.