3

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?

2

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);
   }
  }
  • Thankyou for your reply, However, even though i implemented all the above steps, i still see the same Hex result 0x67df93f2000000000000000000000000ce76eebdf91149284fc9c596a9bd113ec8dae871000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000048656c6c6f20576f726c64 – Nandini Neppalli Dec 29 '16 at 16:42
  • Can you post your code in a gist, github? – Rob Hitchens - B9lab Dec 29 '16 at 19:04
  • Thanks a bunch Rob, Here is the link : github.com/nandinicbit1981/hello-truffle – Nandini Neppalli Dec 29 '16 at 19:07
  • App.js. At line 49, add something like var startWatching = watchSent(); At 52, in this context, meta = Meta.deployed(); You should start seeing watcher events in the log. At line 80, tx.input is showing you the raw data you sent to the EVM. That's just what tx.input is. I didn't look at the rest very closely. – Rob Hitchens - B9lab Dec 29 '16 at 19:22
  • Thanks again, Could you please tell me in Meta.deployed(), what does Meta stand for? Is that a contact that i need to download?, also,when i added the code snippet with watcher, i get error saying LogSent is not a method, its an event. – Nandini Neppalli Dec 30 '16 at 0:07

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.