繁体   English   中英

使用 web3 调用智能合约函数的问题

[英]Issue in calling a smart contract function with web3

我正在尝试调用此智能合约中提供的 createCustomer 函数https://ropsten.etherscan.io/address/0xD3B462CbF6244ed21CD3cF334Bf8CB44A28795A9#code

我们基本上必须提供三个参数,如字符串内存_hashedEmail、字符串内存_name和字符串内存_phone。

所以我编写了以下程序来调用 createCustomer 函数

const addcustomer = async (req, res, next) => {
  try {
        const init = async() => {
            const provider = new  HDWalletProvider(
              privateKey1,
              'https://ropsten.infura.io/v3/1693cef23bd542968df2435f25726d39'
            );
            const web3 = new Web3(provider);

            let contract = new web3.eth.Contract(abi2, address3);
            contract.methods.createCustomer({_hashedemail: "a", _name: "nike", _phone: "99"}).call((err, result) => { console.log(result) });
            };
            init(); 
    }catch (err) {
        //throw error in json response with status 500. 
        return apiResponse.ErrorResponse(res, err);
    }
};

但是它给了我这个错误,这没有任何意义,因为我已经提供了三个参数。

(node:14744) UnhandledPromiseRejectionWarning: Error: Invalid number of parameters for "createCustomer". Got 1 expected 3!

删除createCustomer中的{ } 所以要清楚,这一行:

contract.methods.createCustomer({_hashedemail: "a", _name: "nike", _phone: "99"})

应该:

contract.methods.createCustomer(_hashedemail: "a", _name: "nike", _phone: "99")

另外,当您尝试与修改区块链的函数进行交互时,您不应该使用.call而是使用.send来调用它。

要了解更多信息,您应该查看 web3.js 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM