0

I am trying to call the following solidity method from my smart contract in javascript:


address[] public investors;

 function addInvestor(address investor) public {
        investors.push(investor);
    }


========================Javascript code===================

const Web3 = require('web3')
const web3 = new Web3('https://goerli.infura.io/v3/c21338274bfc4a2ca6d4cb88789ed820');
const tokenContractABI = require('./abi.json');
const tokenContractAddress = '0x5fB7bFD1ac3804934E9Dbc32398C330B69826894'; 
const investor = '0xFBBd1c24C8e68831bD8c3e82b305df8fb70494a6';
const privateKey = '9XXX';

const tokenContract = new web3.eth.Contract(tokenContractABI, tokenContractAddress);


async function callAddInvestor() {
    try {
        const sender = web3.eth.accounts.privateKeyToAccount(privateKey);
        const gasLimit = 22000; 
        const nonce = await web3.eth.getTransactionCount(sender.address);

        const functionCall = tokenContract.methods.addInvestor(investor); 
        const encodedABI = functionCall.encodeABI();

        const transactionObject = {
            to: '0x5fB7bFD1ac3804934E9Dbc32398C330B69826894',
            gas: gasLimit,
            gasPrice: await web3.eth.getGasPrice(),
            data: encodedABI,
            nonce: nonce,
            chainId: 5
        };

        const signedTransaction = await sender.signTransaction(transactionObject);
        const receipt = await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
        const investors = await tokenContract.methods.investors().call();
        console.log('Get investors :', investors);
        console.log('Transaction receipt:', receipt);
    } catch (err) {
        console.error(err);
    }
}

callAddInvestor();

I am getting the error:

Error: Error: Transaction has been reverted by the EVM: { "blockHash": "0xe8ec781840bf6a4c108763d9896bb9bd15a72e4b53c7c87e42d2791fdb6b5325", "blockNumber": 9012380, "contractAddress": null, "cumulativeGasUsed": 13157800, "effectiveGasPrice": 3350701364, "from": "0x18469cf4878b00ea90bf9b9fe8b06e026d400ba7", "gasUsed": 22000, "logs": [], "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "status": false, "to": "0x5fb7bfd1ac3804934e9dbc32398c330b69826894", "transactionHash": "0x7c2a199bfc759c08a927c4b4caf23e5dc0acf77e4f1e9997c15999488b26a685", "transactionIndex": 58, "type": "0x0" }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.