7

I have made a contract which generates a public event on blockchain that will notify client about a transaction. To listen to the event I have written following snippet in geth console:

var event = token.CoinTransfer({}, '', function(error, result){
  if (!error)
    console.log("Coin transfer: " + result.args.amount + " tokens were sent. Balances now are as following: \n Sender:\t" + result.args.sender + " \t" + token.coinBalanceOf.call(result.args.sender) + " tokens \n Receiver:\t" + result.args.receiver + " \t" + token.coinBalanceOf.call(result.args.receiver) + " tokens" )
});

Now instead of console.log() I want these transaction to be stored in a text file TransactionHistory.txt. How can we do this? Is there a better way than listening to event, to maintain transaction logs?

q9f
  • 32,913
  • 47
  • 156
  • 395
Sukhmaninder
  • 491
  • 2
  • 6
  • 14

2 Answers2

3

Probably is best that you exec the script from a file anyway

 geth --exec 'loadScript("/tmp/test.js")' attach 

Then you can direct the output, for example in Linux and Mac:

 geth --exec 'loadScript("/tmp/checkbalances.js")' attach > log.txt
Roland Kofler
  • 11,638
  • 3
  • 44
  • 83
  • It would be better if there is a command replacing "console.log". In that way I will be able to get records which I want, in the format which I like. Is there such a command(s)? – Sukhmaninder Jul 04 '16 at 09:54
  • wondering about the same thing.. – TMOTTM Jul 10 '16 at 10:32
3

The Geth console is basically a subset of Javascript, so you are limited to console.log and @Roland's answer of redirecting to a file could be done.

You have more flexibility by using Ethereum JSON-RPC (see filters). You can use a wide range of languages for JSON-RPC (some here) and its capabilities for I/O: for example you can use Python.

eth
  • 85,679
  • 53
  • 285
  • 406