【问题标题】:Solidity Contract Calls issueSolidity 合约调用问题
【发布时间】:2019-05-23 12:27:46
【问题描述】:

我创建了一个虚拟的 Solidity 合约 (https://learn.aion.network/docs/deploy-a-smart-contract-using-web3) 并部署了它。当我尝试使用 aion-web3 拨打电话时出现问题。

const contract = require('aion-web3-eth-contract');
contract.setProvider("https://aion.api.nodesmith.io/v1/mastery/jsonrpc?apiKey=*");
const Web3 = require('aion-web3');
const web3 = new Web3(new Web3.providers.HttpProvider("https://aion.api.nodesmith.io/v1/mastery/jsonrpc?apiKey=*"));

const account = web3.eth.accounts.privateKeyToAccount("****");

let myContract = new contract([...], "0xa0e1166A455a0d75CFC2bfa32D7f76f0e1852c106b981Acf59EDE327CFD36811");
// console.log("C a",myContract.options.address);

myContract.methods.getCount().call({from: account.address}, function (error, result) {
    if (error){
        console.log("err=>", error)
    } else {
        console.log("res=>", result)
    }
});

我希望为 0,因为它是第一次调用,但会引发以下错误:

TypeError: myContract.methods.getCount is not a function

【问题讨论】:

    标签: aion


    【解决方案1】:

    您尝试调用该函数的方式似乎不太正确。与其创建myContract 对象,不如尝试将合约地址放入事务对象中,然后调用它:

    let transactionCall = {
        from: account.address, 
        to: "0xa0bf00624C2E81de745A826052D635f5c35515F0B55df6E4b1BAaCe785C124B9", 
        gas: 54321, 
            data: contractInst.methods.getCount().encodeABI()
    };
    
    web3.eth.call(transactionCall).then((res) => console.log(web3.utils.hexToNumber(res)));
    

    另外,请确保您的帐户中有硬币。你可以在这里使用一个水龙头:https://faucets.blockxlabs.com/aion

    另外,欢迎使用 StackOverflow!

    【讨论】:

      【解决方案2】:

      尝试使用以下方法创建合约实例:

      let myContract = new web3.eth.Contract(["compile contract abi info"])

      并且

      web3.eth.call({to:YourContractAddress, data:myContract.methods.getCount().encodeABI()}).then((res) => console.log(web3.utils.hexToNumber(res)));

      【讨论】:

        猜你喜欢
        • 2017-08-19
        • 1970-01-01
        • 2018-08-13
        • 2018-02-04
        • 2021-12-16
        • 1970-01-01
        • 2019-06-16
        • 1970-01-01
        • 2022-03-29
        相关资源
        最近更新 更多