0

I have a testrpc running on a virtual machine (Ubuntu 16.04) and it works properly when I interact with it using the truffle console.

My only goal is to call a smart contract that simply sends ether from one account to another. This is the smart contract:

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);
    }
}

I'm trying to access the function sendEther from a web, using javascript. When I was accessing my testnet locally everything worked fine, but now I can't connect to it.

The script is transformed with browserify and then included in the htm file as a simple script tag.

The script goes like this:

var Web3 = require('web3');
let web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

function getParameterByName(name, url) {
    /* irrelevant code here */
}

try
{
    // I have to define "source" this way because web3.eth.accounts[0] is undefined, most likely because there's no connection :(
    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(destinatario);


    document.write("<br/> Source: ");
    document.write(source);
    document.write("<br/> Destination: ");
    document.write(destination);
    document.write("<br/><br/><b>ABI:</b> ");
    document.write(JSON.stringify(abi));
    document.write("<br/><br/><b>Contract:</b> ");
    document.write(JSON.stringify(contract));
    document.write("<br/><br/><b>Contract address:</b> ");
    document.write(contract_addr);
    document.write("<br/><br/><b>Amount (weis):</b> ");
    document.write(amount);
    document.write("<br/><br/><b>Transaction hash:</b> ");
    document.write(JSON.stringify(hash));
    document.write("<br/><br/><b>Transaction details:</b> ");
    document.write(JSON.stringify(web3.eth.getTransaction(hash)));
} catch (e) {
    document.write("<br/></br><b>Error: ");
    document.write(e.message);
    document.write("</b>");
}

This is the output I get:

Source: 0x8691202f56859bd5362f1033fa64e71cb4eae579

Destination: 0x09b74dc58118140deec480d562651e42ae3b21ad

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} 

And that's the problem... When I interact with the truffle console, everything works fine. When I try to do the same via web, it doesn't connect.

Any ideas on what's happening here?

Thank you so much in advance! :)

1

Ok, I found the answer. I'll leave it here for whomever may find it useful...

I wasn't aware of truffle serve, and when I tried to use it, it raised an error.

That error can be solved by following the instructions indicated here: https://github.com/trufflesuite/truffle/issues/448

I finally can see my testrpc from the browser! :)

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.