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

问题描述 投票:0回答:1

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

我希望保留二维数组的索引,因为稍后会出于搜索目的访问它。

提前谢谢您!

node.js multidimensional-array blockchain solidity smartcontracts
1个回答
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 多维数组,例如

web3js
NPMdocs)。

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

const contract = new web3.eth.Contract(jsonInterface, address);
contract.methods.foo(array).call().then((response) => {
    console.log(response);
});
© www.soinside.com 2019 - 2024. All rights reserved.