javascript - 如何在 web3.js 中将 uint32[]、uint8[] 参数传递给智能合约
问题描述
我正在做估算气体。这是我的代码。
var sdate = new Date('2021-07-01 00:00:00');
var edate = new Date('2021-07-31 00:00:00');
var arrDate = [];
arrDate.push(sdate/1000);
arrDate.push(edate/1000);
var arrCategory = [1,12,14];
var param1 = web3.eth.abi.encodeParameter('uint32[]',arrDate);
var param2 = web3.eth.abi.encodeParameter('uint8[]',arrCategory);
let Contract = new web3.eth.Contract(myPack.abi, myPack.ca);
Contract.methods.createTicket(param1, param2).estimateGas({from: accounts[0]})
.then(console.log);
我遇到了错误
Uncaught TypeError: t.map is not a function
at h.formatParam (index.js:218)
at index.js:100
at Array.map ()
at h.encodeParameters (index.js:94)
at index.js:439
at Array.map ()
at Object.d._encodeMethodABI (index.js:438)
at Object.d._processExecuteArguments (index.js:701)
at Object.d._executeMethod (index.js:720)
at estimateGas (issuecfm:257)
我在 encodeParameter 之前尝试了一些东西
大数
var BN = web3.utils.BN;
arrDate = arrDate.map(item => {return new BN(item)});
arrCategory = arrCategory.map(item => {return new BN(item)});
和字符串
arrDate = arrDate.map(item => {return item.toString()});
arrCategory = arrCategory.map(item => {return item.toString()});
经过大量搜索,我尽我所能。但我仍然遇到同样的错误。如果您能教我如何修改我的代码,我将不胜感激。
解决方案
使用 web3 合约助手函数,您需要传递“原始”JS 参数,而不是 ABI 编码的数据。
let contract = new web3.eth.Contract(myPack.abi, myPack.ca);
// mind the `arrDate` and `arrCategory` instead of `param1` and `param2`
contract.methods.createTicket(arrDate, arrCategory)
.estimateGas({from: accounts[0]})
.then(console.log);
推荐阅读
- oracle - 使用 db link 查找谁正在连接到数据库
- android - 在 Android 11 / R 中将铃声设置为铃声/通知
- apache-spark - databricks delta 格式文件无法读取
- c# - 找不到用于引用“C:\SolutionName\ProjectName\filename.dll”的附属程序集。参数不正确
- spring-boot - 在 Spring Boot 中从 VIEW 中获取
- azure-pipelines - '' 不是“密码”命令行选项 mage.exe 的有效值
- javascript - React-Redux:处理同一个 redux 状态属性的多次更改
- .htaccess - 重定向 301 时出现内部服务器错误
- powershell - 接收特定传入邮件的窗口弹出消息或警报
- linux - ecryptfs-recover-private 正确的密码,但生成的目录也被加密