7

I have a JavaScript snippet to get the latest transaction to/from an account. After starting the geth instance, I did this in the interactive JS console:

jsFunction is a function in filename.js

>loadScript("/path/to/file/filename.js")
true
>jsFunction(<parameters>)
<Returns the results as expected>

But when I try to do the same using the Non-interactive mode, it fails:

$ geth --exec 'loadScript("/path/to/file/filename.js")' attach ipc:/path/to/ipc
true
$ geth --exec 'jsFunction(<parameters>)' attach ipc:/path/to/ipc
ReferenceError: 'jsFunction' is not defined
    at <anonymous>:1:1

I ran the same command (geth --exec) with normal web3.js functions like eth.getTransaction, eth.getBlock and it works fine. I don't understand what's wrong with executing custom scripts. Does geth client support this functionality? Am I missing something?

P.S: I am running a private blockchain but didn't mention --datadir, --networkid, and --genesis etc. in the above commands for readability.

2
  • 2
    I think you should do everything in a single line geth --exec 'loadscript("/path/to/file/filename.js"); jsFunction(<params>); ' attach ipc:/path/to/ipc
    – Ismael
    Jun 24, 2016 at 19:24
  • 1
    No it is a typo error. It is LoadScript not loadscript
    – Ellis
    Jun 24, 2016 at 19:27

1 Answer 1

8

Each geth exec start one interpreter. So if you define a function into the first, it is not available on the second... Please merge them into one exec:

To sum up:

 geth --exec 'loadScript("/path/to/file/filename.js"); jsFunction(...)' attach

This also worked:

geth --exec "loadscript(\"/path/to/file/filename.js\");jsFunction(<params>)" attach ipc:/path/to/ipc
4
  • 1
    I am not trying to execute a contract. My question is specific to custom JS code. The eth.compile.solidity is a built-in web3.js function.
    – galahad
    Jun 24, 2016 at 19:14
  • 1
    Tried changing ' to " and also tried changing 'loadScript("/path/to/file/filename.js")' to "loadScript(\"/path/to/file/filename.js\")". Still getting the same error.
    – galahad
    Jun 24, 2016 at 19:22
  • 1
    Oops. Please ignore the typo. I executed it correctly with a capital S. Still no luck.
    – galahad
    Jun 24, 2016 at 19:27
  • 1
    Warning: if you have two different exec, these are two different context. So it is like playing with two distinct interpreters.
    – Ellis
    Jun 24, 2016 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.