6

I'm starting to get a handle of Geth, especially the scripts, following this and this.

I have scripts that create contracts and call functions, but I don't know how to pass arguments to a script. Argv[] is not recognized, and most javascript doc I found is for html.

For now I call scripts with geth --testnet js script.js 2>>geth.log

Is there a way to pass arguments ?

5

The best way, and the one i would recommend to you, to handle scripts in geth is.

  1. First: start geth with console. Forget for now scripts.
  2. Then in console use command: loadScript("source");

That way if you got for example this simple script:

function getBalance(fromAddress)
{
var Balance = web3.fromWei(web3.getBalance(fromAddress), "ether");
console.log(Balance)
}

in a file named myScripts.js (in which you can have more functions), you would do that:

  • Load geth console.
  • loadScript('home/TeleportingGoat/Desktop/Scripts/myScript.js');
  • getBalance('0x1231afd1289a...deff');

That's the way i would suggest you to procceed.

|improve this answer|||||
  • Thank you for your answer. How do you you call console commands from outsite geth ? I know how to call geth scripts from shell, but I don't know how to use console commands from the outside... The script isn't there to save me time but to automate a process, I can't touch the console myself. – Teleporting Goat Nov 16 '16 at 12:25
  • There are just few ways: JSON-RPC API, WEB3.JS API, etc etc... check this Ethereum Geth for the whole documentation. The easier and most used is web3.js – KanekiDev Nov 16 '16 at 12:36
  • I found a solution to my problem (that I posted) but my question was a little general so yours technically answer my question, I'm not sure which one I should accept. – Teleporting Goat Nov 16 '16 at 12:58
  • oh, now i understand what were you asking about exactly :) nice way to solve it :) – KanekiDev Nov 16 '16 at 13:03
  • Thank you ! I'll mark my answer as accepted then. (Damn I have to wait 21h, why SE why ?) – Teleporting Goat Nov 16 '16 at 13:40
3

EDIT : I will leave my old answer for legacy, but user BokkyPooBah gave me an awesome answer to another problem, somewhat related to this one.

His solution solved both, so go give him points on his answer there !

I'll quickly sum it up here:

You need to have an instance of Geth running, and with a bash script instead of running geth --testnet js script.js 2>>geth.log you'll use geth attach. You can then write javascript directly, and use $1 and $2 like in any bash script.

#!/bin/bash
arg1=$1
arg2=$2
geth attach << EOF | grep "Data: " | sed "s/Data: //"

<content of your js script>

EOF

This will hide the console output. If you need to see it for debugging, use geth attach << EOF instead.

You may need to add the path to you ipc file if it's not default, between geth and attach. I tried to be short but if you need more detailed explanation see his answer.


My question was not perfectly asked and isn't clear about what I needed. I tried to be general, but since there is "no easy way" to pass arguments to a js script, you have to find a trick that is very dependent on the way you're calling your script (as I said, there are very specific answers for when the script is called from a html).

I found a solution to my problem so I'll post the answer because I think it could be useful to others.

What I was doing was calling a simple bash script from a web page, which executed geth --testnet script.js, so I already had two levels of scripts : bash and javascript. Js can't take arguments (easily), but bash scripts can.

So here's what I did. The following is my bash script file.

echo "primary = eth.accounts[0]; personal.unlockAccount ...<content of the js script> ... address ='"$1"'); ..." > gethScript.js
geth --testnet js gethScript.js 2>>gethScript.log 

The argument is $1 and is written directly in the js script. Then the js script works perfectly as if the argument was hard coded. The bash script can be run from console with bash bashScript "0x00..00" or in php for example with : exec('./bashScript '.$address);

|improve this answer|||||

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.