1

我创建了一个虚拟的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
4

2 回答 2

0

看起来您尝试调用该函数的方式不太正确。与其创建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!

于 2019-05-24T14:43:15.140 回答
0

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

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)));

于 2019-06-11T16:05:32.270 回答