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:
- testrpc
- truffle compile
- 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! :)