I am trying to run a simple contract in solidity :
contract Coin {
// The keyword "public" makes those variables
// readable from outside.
address public minter;
mapping (address => uint) public balances;
// Events allow light clients to react on
// changes efficiently.
event Sent(address from, address to, uint amount, uint data);
// This is the constructor whose code is
// run only when the contract is created.
function Coin() {
balances[tx.origin] = 1000000000000000000000000000000000000;
minter = msg.sender;
}
function mint(address receiver, uint amount) {
if (msg.sender != minter) return;
balances[receiver] += amount;
}
function send(address receiver, uint amount, uint data) {
if (balances[msg.sender] < amount) return;
balances[msg.sender] -= amount;
balances[receiver] += amount;
return Sent(msg.sender, receiver, amount, data);
}
}
calling the send method using this :
function sendCoinTest() {
var meta = Coin.deployed();
var amount = 1;
var receiver = accounts[1];ElementById("receiver").value;
var data = web3.toHex("HelloWorld");
console.log("data : " + data);
setStatus("Initiating transaction... (please wait)");
meta.send(receiver, amount, data, {from: account}).then(function(result) {
setStatus("Transaction complete!");
console.log("Here I MA");
var tx = web3.eth.getTransaction(result);
console.log(tx);
console.log("input" + tx.input);
}).catch(function(e) {
console.log(e);
setStatus("Error sending coin; see log.");
});
};
the last console gives me:
> input0x67df93f2000000000000000000000000ce76eebdf91149284fc9c596a9bd113ec8dae871000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000048656c6c6f20576f726c64
However I get a huge hex string on the last console statement, when I am trying to decode it using web3j.toAscii(), I get some garbage values like ^^&*(*%%bb^*HelloWorld
What is wrong with my code?
Best Answer
You're missing a step. Your result is going to be the unmined transaction receipt. tx.input is the data you sent.
The next step is to wait for the transaction to be mined, and then get the mined transaction with the returned values.
Xavier well-solved example code here: https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6
The "data" you're sending is Hex. "uint data" should probably be "bytes32 data".
tx.origin is security risk. Avoid it unless you really know what you're doing. msg.sender will work.
Instead of returning on insufficient funds, it's almost always better to throw. This is the opposite of other situations where we tend to want to detect errors and then decide what to do.
On success return true indicating the transaction was executed as requested. No need to return values the client already knows. Include "returns" in the function so there is no doubt about what's coming back. I called it "success".
Add event logs to emit state notices about state changes. I added one for send(). There should also be one for mint().
In practice, you'll find that you can often use watchers to listen for LogSent and other events and maintain the UI. Watchers will fire when the mined transactions are detected.
A watcher looks something like this: