【问题标题】:calling a smart contract functons as a a string in javascript web3在 javascript web3 中调用智能合约函数作为字符串
【发布时间】:2018-12-09 16:42:26
【问题描述】:

这是我的问题:不要像这样调用智能合约函数

//assuming the name of the contract function is called "balanceOf"
contract.methods.balanceOf("0x", "0x").call(err, balance) => {
      console.log({err, balance});
})

我想知道是否可以这样调用:

var funcName = "balanceOf";
var parameter = "0x, 0x";
contract.methods.funcName(parameter).call(err, balance) => {
     console.log({err, balance}];
})

谢谢。

【问题讨论】:

    标签: javascript smartcontracts web3


    【解决方案1】:

    通过使用键作为索引,可以像数组一样访问 javascript 中的对象。所以你的情况是:

    var funcName = "balanceOf";
    var parameter = "0x, 0x";
    contract.methods[funcName]("0x", "0x").call(err, balance) => {
       console.log({err, balance}];
    })
    

    但是你传递参数的方式不会像那样工作。您只是将字符串作为参数传递。可以把它想象成将参数传递给任何其他函数。

    【讨论】:

    • 我试过了,我得到一个错误,说 contract.methods 不是一个函数。
    猜你喜欢
    • 2019-01-24
    • 2022-09-29
    • 2019-12-26
    • 2019-08-27
    • 2018-07-10
    • 2018-07-07
    • 2018-12-28
    • 2019-01-17
    • 2017-08-07
    相关资源
    最近更新 更多