[Ethereum] Why is the string converting to hex

solidityweb3js

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.

contract Coin { 
  address public minter; 
  mapping (address => uint) public balances;

  event LogSent(address from, address to, uint amount, bytes32 data);

  function Coin() {
    balances[msg.sender] = 1000000000000000000000000000000000000;
    minter = msg.sender;
  }

  function mint(address receiver, uint amount) {
    if (msg.sender != minter) return;
    balances[receiver] += amount;
  }

  function send(address receiver, uint amount, bytes32 data) 
    returns(bool success) 
  {
    if (balances[msg.sender] < amount) throw;
    balances[msg.sender] -= amount;
    balances[receiver] += amount;
    LogSent(msg.sender, receiver, amount, data);
    return true;
  }

}

A watcher looks something like this:

var startWatching = watchSent;

function watchSent(address) {
  var meta = Coin.at(address); // address of contract to watch
  var watcher = meta.LogSent( {}, {fromBlock: 0}) // Log name, filter, from start
  .watch(function(err,sent) {
   if(err) {
    console.error("Something went wrong with the watcher", err);
   } else {
    // returned arguments are visible
    console.log(sent.args.sent, sent.args.receiver, sent.args.amount, sent.args.data);
   }
  }
Related Topic