0

I'm trying to develop a simple smart contract that sends Ether from one address to another and I can't make it work.

I'm using Ubuntu 16.04, truffle 3.4.7, node 7.10.1 and solidity 0.4.13.

The contract is compiled and published in a local testrpc, but when I try to access it, it just doesn't seem to work.

I've searched for solutions everywhere, but I just can't figure out what's wrong, so I hope anyone here can help me :)

The smart contract code goes like this:

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

The sequence I follow to deploy it is as follows:

  1. testrpc
  2. truffle compile
  3. truffle deploy

Then I have a simple html file which includes a js file which should make the transfer:

< script src="/usr/local/lib/node_modules/ethereum-console/node_modules/web3/dist/web3.min.js" >< /script >
< script src="test.js" >< /script >
< /head >
< body >
< /body >
< /html >

And here's the contents of the test.js file:

var Web3 = require('web3');
let web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var account_one = web3.eth.accounts[0]; // testrpc address
var account_two = web3.eth.accounts[9]; // testrpc address
/*
// This works fine, makes the transfer:
web3.eth.sendTransaction({from: account_two, to: account_one, value: 100000000000});
*/
// These don't work:
try
{
// First try:
    obj = Transfer.deployed();
    obj.sendEther(account_two, {from: account_one, to: objTransfer, value: 100000000000});
// Second try:
    Transfer.deployed().then(
        function(instance) {
            obj = instance;
            return obj.sendEther.call(account_two, {from: account_one, to: objTransfer, value: 100000000000});
        }
    );
} catch (e) {
    document.write("< br/ >< b >Error: ");
    document.write(e.message);
    document.write("< /b >");
}

Of course, first and second tries are the two things I've tried, not executed both at the same time, when trying one of them, the other one is commented.

Either way, the output I get when I display the html file in a browser is:

Error: Transfer is not defined

Obviously, the Transfer smart contract cannot be created, but I just don't understand why. Any help on how I can make this work?

Thank you so much in advance! :)

1

You need to get create contract instance in javascript file using its ABI and the address at which it is deployed as below [replace the ABI and address of your contract]:

// Try to use as below
try {
    // First try:
    var abi = <your contract ABI>;
    var Contract = web3.eth.contract(abi);
    var contractInstance = Contract.at('<Your contract address>');
    contractInstance.sendTransaction({from: account_two, to: account_one, value: 100000000000});
} catch (e) {
    document.write("< br/ >< b >Error: ");
    document.write(e.message);
    document.write("< /b >");
}
  • Thanks a lot! I'm going to try it right now, to see if it works :) Isn't there a more... "elegant" solution to this? I mean, is it totally necessary to add the whole ABI to the js file? – JaVinci Aug 16 '17 at 10:36
  • 1
    Ok... it doesn't work for me either :( It shows this error: t.abi.filter is not a function – JaVinci Aug 16 '17 at 11:22
  • you ABI should be be a javascript array instead of string. Try JSON.stringify('<ABI>'). ABI would be public when you deploy the contract so you can keep that in javascript or you can save it in a server and get it through ajax call. it all depends on you. :) – Abhishek Aug 16 '17 at 12:02
  • Wow... finally worked!!! Can't thank you enough!! :) – JaVinci Aug 16 '17 at 12:23
  • I finally got enough reputation to upvote your answer!!! :) – JaVinci Sep 14 '17 at 9:13

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.