【问题标题】:how to load json file from geth如何从geth加载json文件
【发布时间】:2020-10-07 21:06:30
【问题描述】:

可能是因为我不熟悉 js,但是如何从 geth 控制台加载 .json 文件?我希望这样做是为了避免为每个合同var abi_1 = [...]; var abi_2 = [...]; ... 复制粘贴每个原始 abi 内容的笨拙方式。我知道控制台是javascript,所以我尝试了require(使用nodejs很容易),但它不起作用。在geth(js控制台)中不可能加载abi_1.json并将其存储在变量abi_1中,就像我在python中轻松腌制文件一样?谢谢你,希望这个问题对社区有意义。

【问题讨论】:

    标签: javascript json solidity geth


    【解决方案1】:

    如文档[1] 中所述,geth 控制台在 go 中使用 ECMAScript 的实现,称为 goja[2]。据我所知,经典 JS(所以没有 nodejs)没有任何 IO 功能......

    但是,也许您可​​以使用控制台中可用的“loadScript”功能 + 一些 bash。

    例如,假设您的 JSON 文件位于 /tmp/abi.json。您所有的 JS 操作都可以存储在另一个文件中(比如/tmp/operations.js)。

    你可以使用geth attach http://localhost:8545 --exec "var abi = $(cat /tmp/abi.json); loadScript('/tmp/operations.js')"

    例如: /tmp/file.json 包含 { 'test': 'Hello, world'}

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

    将打印Hello, world!

    这不是一个完美的解决方案,但对您来说可能很方便。

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

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

    【讨论】:

    • 感谢您的贡献。这个想法似乎是一个很好的回旋处!例如,在 --exec 之后使用具有所有必要 abi 的 arr_abis 的数组,或者在 operations.js 中包含此类操作..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多