2

I'm trying to use my already deployed contract using web3.js from my console but i'm having issues.

I get my compiled contract from browser-solidity then do:

var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var MyContract = web3.eth.contract(abi);
var myContractInstance = MyContract.at('address');
web3.eth.getCode(address)  => returns my contract code

then, if I type myContractinstance, I get the list of methods that I've built in solidity.

Issue is: I'm getting this error for all of the function calls.

Uncaught Error: could not decode, json: cannot unmarshal array into Go value of type string

Anyone got any idea of what could be happening ?

Edit: Sorry for late edit

I'm using a slightly modified version of that standardized contract :

contract currency {

                struct Account {
                    uint balance;
                    mapping ( address => uint) withdrawers;
                }

                mapping ( address => Account ) accounts;

                event CoinSent(address indexed from, uint256 value, address indexed to);

                function currency() {
                    accounts[msg.sender].balance = 1000000;
                }

                function sendCoin(uint _value, address _to) returns (bool _success) {
                    if (accounts[msg.sender].balance >= _value && _value < 340282366920938463463374607431768211456) {
                        accounts[msg.sender].balance -= _value;
                        accounts[_to].balance += _value;
                        CoinSent(msg.sender, _value, _to);
                        _success = true;
                    }
                    else _success = false;
                }

                function sendCoinFrom(address _from, uint _value, address _to) returns (bool _success) {
                    uint auth = accounts[_from].withdrawers[msg.sender];
                    if (accounts[_from].balance >= _value && auth >= _value && _value < 340282366920938463463374607431768211456) {
                        accounts[_from].withdrawers[msg.sender] -= _value;
                        accounts[_from].balance -= _value;
                        accounts[_to].balance += _value;
                        CoinSent(_from, _value, _to);
                        _success = true;
                        _success = true;
                    }
                    else _success = false;
                }

                function coinBalance() constant returns (uint _r) {
                    _r = accounts[msg.sender].balance;
                }

                function coinBalanceOf(address _addr) constant returns (uint _r) {
                    _r = accounts[_addr].balance;
                }

                function approve(address _addr) {
                    accounts[msg.sender].withdrawers[_addr] = 340282366920938463463374607431768211456;
                }

                function isApproved(address _proxy) returns (bool _r) {
                    _r = (accounts[msg.sender].withdrawers[_proxy] > 0);
                }

                function approveOnce(address _addr, uint256 _maxValue) {
                    accounts[msg.sender].withdrawers[_addr] += _maxValue;
                }

                function isApprovedOnceFor(address _target, address _proxy) returns (uint256 _r) {
                    _r = accounts[_target].withdrawers[_proxy];
                }

                function disapprove(address _addr) {
                    accounts[msg.sender].withdrawers[_addr] = 0;
                }
            }

My contract abi :

    var currencyContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"_addr","type":"address"}],"name":"disapprove","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_target","type":"address"},{"name":"_proxy","type":"address"}],"name":"isApprovedOnceFor","outputs":[{"name":"_r","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_proxy","type":"address"}],"name":"isApproved","outputs":[{"name":"_r","type":"bool"}],"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_value","type":"uint256"},{"name":"_to","type":"address"}],"name":"sendCoinFrom","outputs":[{"name":"_success","type":"bool"}],"type":"function"},{"constant":false,"inputs":[{"name":"_addr","type":"address"},{"name":"_maxValue","type":"uint256"}],"name":"approveOnce","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"_addr","type":"address"}],"name":"coinBalanceOf","outputs":[{"name":"_r","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"},{"name":"_to","type":"address"}],"name":"sendCoin","outputs":[{"name":"_success","type":"bool"}],"type":"function"},{"constant":true,"inputs":[],"name":"coinBalance","outputs":[{"name":"_r","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_addr","type":"address"}],"name":"approve","outputs":[],"type":"function"},{"inputs":[],"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"},{"indexed":true,"name":"to","type":"address"}],"name":"CoinSent","type":"event"}]);

then

var currency = currencyContract.at(["0x956440edc6115aab375ab1b96603b79376484d86"]);

And whenever i call a method i get the error.(currency.coinBalance() )

Go version go version go1.5.3 darwin/amd64

  • There are a number of typographical issues with your question - it is not clear if you have modified your code for the question or if the problem lies in these typos. You for example specify the contract address as the string 'address' as opposed to the actual contract address. – Thomas Clowes Feb 3 '16 at 17:56
  • Showing a sample of the actual call will also be helpful, even if initially it is just foo with some parameters. – eth Feb 3 '16 at 20:49
  • A snippet of the solidity or just telling us what types the contract should accept would be helpful too. – Tjaden Hess Feb 3 '16 at 23:02
  • Also perhaps specify the version of the Go client you are using. – Péter Szilágyi Feb 4 '16 at 8:44
  • Updated! followed those instruction initially: github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract – jayD Feb 5 '16 at 19:26
2

I've found what i was doing wrong in my deploy process and why accessing the contract after creation with possible but would throw the error with the address.

Turns out this is not the contract ABI :

var currencyContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"_addr","type":"address"}],"name":"disapprove","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"_target","type":"address"},{"name":"_proxy","type":"address"}],"name":"isApprovedOnceFor","outputs":[{"name":"_r","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_proxy","type":"address"}],"name":"isApproved","outputs":[{"name":"_r","type":"bool"}],"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_value","type":"uint256"},{"name":"_to","type":"address"}],"name":"sendCoinFrom","outputs":[{"name":"_success","type":"bool"}],"type":"function"},{"constant":false,"inputs":[{"name":"_addr","type":"address"},{"name":"_maxValue","type":"uint256"}],"name":"approveOnce","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"_addr","type":"address"}],"name":"coinBalanceOf","outputs":[{"name":"_r","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"},{"name":"_to","type":"address"}],"name":"sendCoin","outputs":[{"name":"_success","type":"bool"}],"type":"function"},{"constant":true,"inputs":[],"name":"coinBalance","outputs":[{"name":"_r","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_addr","type":"address"}],"name":"approve","outputs":[],"type":"function"},{"inputs":[],"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"},{"indexed":true,"name":"to","type":"address"}],"name":"CoinSent","type":"event"}]);

So the fix the issue i had to do :

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

MyContract.at("0xADDRESS")

  • Please clarify/edit the answer. What is the value of currencyContract before var MyContract = web3.eth.contract(currencyContract.abi); ? – eth Feb 8 '16 at 19:26
  • the one written in the OP after " My contract abi :" – jayD Feb 8 '16 at 23:03
  • Answer would actually be clearer if it was: var MyContract = web3.eth.contract(<put correct abi here>); . (Why have two calls to web3.eth.contract when a single one is enough) – eth Feb 8 '16 at 23:39

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.