0

I'm having a problem with a smart contract I have deployed on a local testrpc network. I'm using truffle, nodejs and web3js.

I am running that testrpc network on Ubuntu 16.04, and into it there's is a simple contract deployed, which I had succesfully tested on another similar machine.

So, what I'm trying to do is sending Ether from one account to another via a simple script (which will be called from inside an empty html page that receives the following parameters: contract address, destination address and Ether amount).

The script gets those parameters from the browser and then calls the method "sendEther", which should in fact send the Ether... but it doesn't. Funny thing is that it doesn't raise an error, either.

The smart contract looks like this:

pragma solidity ^0.4.4;

contract Transfer {
    address owner;

    function Transfer () {
        owner = msg.sender;
    }

    function sendEther (address dst) payable {
        if(msg.value<=0) revert();
        dst.transfer(msg.value);
    }
}

This is the part of the script that should do the sending (every input parameter is correct):

try
{
    var source = "0x8691202f56859bd5362f1033fa64e71cb4eae579";
    var abi = [{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}];
    var contract_addr = getParameterByName("contract");
    var amount = getParameterByName("amount");
    var destination = getParameterByName("destination");
    var contract = new web3.eth.Contract(abi, contract_addr, {from: source, to: destination, value: parseInt(amount)});
    var hash = contract.methods.sendEther(destination);

    ...lots of "document.write"...

} catch (e) {
  ...error handling...
}

The output I get indicates that there's been no error, but when I go to a truffle console on the machine that hosts the script, it does not reflect the changes of balance on the involved accounts (source and destination).

This is the output (the results of all the "document.write"):

ABI: [{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function","signature":"0x48c981e2"},{"inputs":[],"payable":false,"type":"constructor"}]

Contract: {"currentProvider":{"host":"http://localhost:8545","timeout":0,"connected":false},"_requestManager":{"provider":{"host":"http://localhost:8545","timeout":0,"connected":false},"providers":{},"subscriptions":{}},"givenProvider":null,"providers":{},"_provider":{"host":"http://localhost:8545","timeout":0,"connected":false},"_ethereumCall":{},"options":{"from":"0x8691202F56859BD5362f1033FA64E71cb4EaE579","to":"0x09b74dc58118140deec480d562651e42ae3b21ad","value":1000000000000,"address":"0x377D5848c8A7B68D20CcE114f23AD959a62CC9f7","jsonInterface":[{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function","signature":"0x48c981e2"},{"inputs":[],"payable":false,"type":"constructor"}]},"defaultAccount":null,"defaultBlock":"latest","methods":{},"events":{},"_address":"0x377D5848c8A7B68D20CcE114f23AD959a62CC9f7","_jsonInterface":[{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function","signature":"0x48c981e2"},{"inputs":[],"payable":false,"type":"constructor"}]}

Contract address: 0x377d5848c8a7b68d20cce114f23ad959a62cc9f7

Amount (weis): 1000000000000

Transaction hash: {"arguments":["0x09b74dc58118140deec480d562651e42ae3b21ad"],"_method":{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function","signature":"0x48c981e2"},"_parent":{"currentProvider":{"host":"http://localhost:8545","timeout":0,"connected":false},"_requestManager":{"provider":{"host":"http://localhost:8545","timeout":0,"connected":false},"providers":{},"subscriptions":{}},"givenProvider":null,"providers":{},"_provider":{"host":"http://localhost:8545","timeout":0,"connected":false},"_ethereumCall":{},"options":{"from":"0x8691202F56859BD5362f1033FA64E71cb4EaE579","to":"0x09b74dc58118140deec480d562651e42ae3b21ad","value":1000000000000,"address":"0x377D5848c8A7B68D20CcE114f23AD959a62CC9f7","jsonInterface":[{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function","signature":"0x48c981e2"},{"inputs":[],"payable":false,"type":"constructor"}]},"defaultAccount":null,"defaultBlock":"latest","methods":{},"events":{},"_address":"0x377D5848c8A7B68D20CcE114f23AD959a62CC9f7","_jsonInterface":[{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function","signature":"0x48c981e2"},{"inputs":[],"payable":false,"type":"constructor"}]}}

Transaction details: {"isFulfilled":false,"isRejected":false} 

Can you tell me what's not working here, please? I'm pretty lost right now...

Please ask me for any additional information if necessary.

Thanks in advance! :)

  • Ok, looks like I found something. I can't even access web3.eth.accounts[0], it says "undefined". Looks like there's something really wrong... I'll try to solve it and then give an update. – JaVinci Aug 29 '17 at 7:26
1

It seems to me there's a little mess around here:

var contract = new web3.eth.Contract(abi, contract_addr, {from: source, to: destination, value: parseInt(amount)});
var hash = contract.methods.sendEther(destination);

Try this:

var contract = web3.eth.contract(abi).at(contract_addr);
var hash = contract.sendEther(destination, {from: source, value: parseInt(amount)});

Please note you also need to unlock the account source to be able to send transactions from there.

You can find in the official documentation more informations about contracts methods and how to call them.

  • Thank you so much, Guiseppe! The problem is that they've changed the interface (check web3js.readthedocs.io/en/1.0/web3-eth-contract.html) and now it seems that things are done that way... In fact my previous code was almost identical to the one you've written :) I guess I should try to go back to web3's previous version... – JaVinci Aug 29 '17 at 6:54
  • Web3 1.0 is still in beta, I suggest to continue to work on the lastest stable release (it's 0.19, github.com/ethereum/web3.js/releases/tag/0.19.0) until 1.0 will be released. – Giuseppe Bertone Aug 29 '17 at 15:48
  • I tried to, but the problem is not the code itself (I think it would work on the new version of web3), but the fact that I can't connect to my testrpc... still working on that, I can't figure out the reason why it doesn't connect. Thanks a lot! – JaVinci Aug 30 '17 at 8:34
0

Ok, the reason why the contract was not doing anything was because I was totally disconnected from the testnet, and needed to do a truffle serve or something similar (in fact it was npm run dev) that let me connect to the testnet via web.

I finally figured out what happened and answered to myself here.

I will leave this reply as accepted in hopes that it can be helpful to anyone :)

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.