1

In contract, there are always some storage change like token transfer. I want to detect the token transfer event. Token always store in contract mapping struct. So how can i detect the mapping change or the variables change in contract storage and recognize this is a token change event?

rong jialei
  • 761
  • 6
  • 16

2 Answers2

1

You want to detect this from outside the contract, right? For example in an application using web3.js?

In the contract, create an event with the information you want and call it when the token transfer happens.

See here for how you can then listen for that event: https://github.com/ethereum/wiki/wiki/JavaScript-API#contract-events

Edmund Edgar
  • 16,897
  • 1
  • 29
  • 58
  • Actually, i want parse all those token transfer in blockchain. And the method i think is parse from the event logs. If there is no event be set while coding, maybe parse this kind of balance change can be really hard – rong jialei Aug 01 '16 at 05:58
1

You can use this script:

function checkAllBalances() { 
  var i =0; 
  eth.accounts.forEach( function(e){
    console.log("  eth.accounts["+i+"]: " +  e + " \tbalance: " + web3.fromWei(eth.getBalance(e), "ether") + " ether"); 
    i++; 
  })
};

taken from this thread.

Edit:

From inside the contract you can't. The only block variables you have access to are mentioned here.

Sebi
  • 5,294
  • 6
  • 27
  • 52
  • Thanks for you answer. I want parse all the token balance in contract. I found it can be obtain in etherscan.io, so i am confused how to get it. – rong jialei Aug 01 '16 at 07:39
  • etherscan.io is used for the main net meaning that balances have an equivalent in real currency. Have a look at their API: https://etherscan.io/apis – Sebi Aug 01 '16 at 07:52
  • Yes, i know that. I want do the same api with etherscan.io. And got some problem. – rong jialei Aug 01 '16 at 08:48