使用INFURA调用智能合约功能

时间:2019-02-15 14:29:00

标签: node.js ethereum solidity smartcontracts evm

我无法理解如何准备交易,签名并使用INFURA发送。我希望发布的NodeJS代码出问题。谁能帮我解决这个问题?

注意:我可以通过智能合约发送交易,但是EVM会恢复执行。

智能合约(Solidity)(我要调用的功能)的一部分:

function testFunction() public pure returns (string memory) {
    return "testSuccess";
}

NodeJS中使用的函数:

      const printRequestTransaction = async (gcodeHash) => {

    log(`Preparing a transaction to register that the gcode has been sent`.yellow);


    // With every new transaction send using a specific wallet address, it needed to increase a nonce which is tied to the sender wallet
    let nonce = await web3.eth.getTransactionCount(web3.eth.defaultAccount);

    // Fetch the current transaction gas prices from https://ethgasstation.info/
    let gasPrices = await getCurrentGasPrices();

    // Build a new transaction object and sign it locally
    let details = {
        "to": process.env.SMART_CONTRACT_ADDRESS,
        //"value": web3.utils.toHex(web3.utils.toWei(amountToSend.toString(), 'ether')),
        "gas": 210000,
        "gasPrice": gasPrices.low * 1000000000, // converts the gwei price to wei
        "nonce": nonce,
        //"data": gcodeHash,
        "function": "testFunction",
        "chainId": 3 // EIP 155 chainId - mainnet: 1, ropsten: 3, rinkeby: 4 (https://ethereum.stackexchange.com/questions/17051/how-to-select-a-network-id-or-is-there-a-list-of-network-ids/17101#17101)
    };
    const transaction = new EthereumTx(details);

    // This is where the transaction is authorized on Local PC behalf. The private key is what unlocks Local PC wallet.
    transaction.sign(Buffer.from(process.env.WALLET_PRIVATE_KEY, 'hex'));

    // Compress the transaction info down into a transportable object
    const serializedTransaction = transaction.serialize();

    // The Web3 library is able to automatically determine the "from" address based on Local PC private key

    // Submit the raw transaction details to the provider configured above (INFURA)

    const transactionHash = await web3.eth.sendSignedTransaction('0x' + serializedTransaction.toString('hex')).then(function (receipt) {
        log(`result of the invokation: ${receipt})`.red);
        return receipt.transactionHash;
    }).catch((err) => {
        log(`error occurred: ${err})`.red);
    });

    //const transactionHash = "exampleHash";
    _transactionHash = transactionHash;

    // Now it is known the transaction ID, so let's build the public Etherscan url where the transaction details can be viewed.
    const url = `https://ropsten.etherscan.io/tx/${transactionHash}`;
    log(url.cyan);
    log(`Note: please allow for 30 seconds before transaction appears on Etherscan`.yellow)
};

输出:

  发生

错误:错误:事务已被EVM恢复:                           {                             “ blockHash”:“ 0x6205c1b8ce2fde3693e85843dce684ff11472e9df01fd850bc99e5771b3262d5”,                             “ blockNumber”:5024524,                             “ contractAddress”:null,                             “ cumulativeGasUsed”:“ 0x50170f”,                             “来自”:“ 0x8882528C7104e146E0500203C353C09922575385”,                             “ gasUsed”:“ 0x5236”,                             “日志”:[],                             “ logsBloom”:“ 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000百万                             “ status”:“ 0x0”,                             “至”:“ 0x1732089F6F6EeFA942871707c5AE1909B60774EB”,                             “ transactionHash”:“ 0xf748c9a6bdb19e776db4d8a9802d2bf5f8b588158da387029529249aca00a499”,                             “ transactionIndex”:1                           })

我要特别注意let details = {...};对象,但我不确定它是否正确,对此我有些困惑。

0 个答案:

没有答案