how to load json file from geth

743 Views Asked by At

maybe it is because I am not handy with js, but how can I load a .json file from geth console? I want this in order to avoid the clumpsy way of copy-paste each raw abi content for each one of the contracts var abi_1 = [...]; var abi_2 = [...]; .... I know the console is javascript, so I tried with require (easy with nodejs), but it doesn't work. It is impossible in geth (js console) to load an abi_1.json and store it in a variable abi_1 in the same way I easily pickle a file in python? Thank you and hope this question makes sense to the community.

1

There are 1 best solutions below

1
On

As specified in the documentation[1], the geth console uses an implementation of ECMAScript in go called goja[2]. From what I know, classical JS (so no nodejs) does not have any IO functionnalities...

However, maybe you could use the 'loadScript' function available in the console + some bash.

For instance, let's say that your JSON file is located in /tmp/abi.json. All your JS operations can be stored in another file (let's say /tmp/operations.js).

You could use geth attach http://localhost:8545 --exec "var abi = $(cat /tmp/abi.json); loadScript('/tmp/operations.js')"

For example : /tmp/file.json contains { 'test': 'Hello, world'}

geth attach http://localhost:8545 --exec "var a = $(cat /tmp/file.json); console.log(a.test)"

Would print Hello, world!

That's not a perfect solution but it could be convenient to you.

[1] https://geth.ethereum.org/docs/interface/javascript-console

[2] https://github.com/dop251/goja