【问题标题】:Send two-dimensional array from Nodejs server socket to Ethereum smart contract (Solidity)从 Nodejs 服务器套接字发送二维数组到以太坊智能合约(Solidity)
【发布时间】:2021-06-30 17:11:51
【问题描述】:

快速提问,有没有人知道如何将二维 (2D) 数组从 Nodejs 服务器应用程序上传或发送到智能合约 (Solidity) 中声明的函数?

我希望维护二维数组的索引,因为稍后将访问它以进行搜索。

提前谢谢你!

【问题讨论】:

    标签: node.js multidimensional-array blockchain solidity smartcontracts


    【解决方案1】:

    您可以在 Solidity 中使用多个 [] 符号定义多维数组。

    文档:https://docs.soliditylang.org/en/v0.8.6/types.html#arrays

    pragma solidity ^0.8;
    
    contract MyContract {
        function foo(uint256[][] memory _array) external pure returns (uint256[][] memory) {
            return _array;
        }
    }
    

    然后你可以使用任何库来传递JS多维数组与智能合约进行交互,例如web3jsNPMdocs)。

    const array = [
        [1, 2],
        [3, 4]
    ];
    
    const contract = new web3.eth.Contract(jsonInterface, address);
    contract.methods.foo(array).call().then((response) => {
        console.log(response);
    });
    

    【讨论】:

    • 我认为这会起作用,但是我的程序遇到了与 gas 和 gasPrice 相关的交易问题,我有空时会尝试打开一个新问题,非常感谢您的借阅你的手!
    • 我也面临同样的问题。我也收到了 gasPrice miscalculatian 的错误。即使我尽可能地遵循相同的结构。你修好了吗?
    • @Tolstenko gasPrice 计算错误通常指向一般错误,例如“出了点问题”,这可能是由与工作答案不同的细微细节引起的。请提出一个单独的问题以及重现问题的步骤。
    猜你喜欢
    • 1970-01-01
    • 2019-07-27
    • 2019-04-07
    • 2018-08-09
    • 2017-04-06
    • 1970-01-01
    • 2019-05-09
    • 2020-03-14
    • 2021-02-24
    相关资源
    最近更新 更多