从 Nodejs 服务器套接字发送二维数组到以太坊智能合约Solidity

问题描述

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

我希望保留二维数组的索引,因为稍后将对其进行访问以进行搜索

先谢谢你!

解决方法

您可以在 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 多维数组以与智能合约进行交互,例如 web3js (NPM,docs)。

const array = [
    [1,2],[3,4]
];

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