1

I have a contract function that I cannot call from the front-end using ethers.js for some reason. I cannot even debug this call because it doesnt produce any errors. I tried to break the call by changing gas price or passed arguments, then I get errors. When I get no errors, metamask doesnt popup to ask for confirmation as i'm 100% sure there is gas price for this transaction.

Here is how i call the function:

    const provider = new ethers.providers.JsonRpcProvider("https://rpc-...", 80001); ///rpc url
    const signer = new ethers.Wallet("0x....", provider); ///private key
    const administrativeSidechainContract = new ethers.Contract(ContractAddress, abi, signer)


    async function updateUI() {
        //const gasPrice = await provider.getGasPrice() //gets the gas price but doesnt fire transaction
        const addingAllWinnings = await administrativeSidechainContract.addAllPotentialWinnings(
            props.eventNumber,
            props.winningsList,
            props.loopedOverChainsList,
            props.winnersList
            , { gasPrice: ethers.utils.parseUnits('100', 'gwei'), gasLimit: ethers.utils.parseUnits('10000000', 'wei') })
    }
    updateUI()

I have written all the unit tests with hardhat + ethers.js and they all pass.

Here is the contract function:

    function addAllPotentialWinnings(
        uint256 _eventNumber,
        uint256[][] memory _potentialWinnings,
        uint256[] memory _chainsList,
        address[] memory _winnersList
    ) public onlyOwner {
        winnersList[_eventNumber] = _winnersList;
        updateChains(_eventNumber, _chainsList);
        for (uint i = 0; i < _winnersList.length; i++) {
            updatePotentialWinnings(
                _eventNumber,
                _winnersList[i],
                _potentialWinnings[i]);}}

Please note that i cannot call all the other functions that are state-changing due to the similar reasons. However all the view functions work just fine.

What am I missing about calling state changing functions with ethers.js?

2 Answers 2

1

IF you want use metamask, you not need to create wallet with private key. Metamask will sign tx itself (with popup confirmation). In addition, before signing, metamask will populate your tx with nonce, gasprice, and gasLimit (latery you can correct this values inside metamask).

Try this:

const provider = new ethers.providers.JsonRpcProvider("https://rpc-...", 80001); ///rpc url
    const signer = provider.getSigner(0)
    const administrativeSidechainContract = new ethers.Contract(ContractAddress, abi, signer)


        async function updateUI() {
                const addingAllWinnings = await administrativeSidechainContract.addAllPotentialWinnings(
                    props.eventNumber,
                    props.winningsList,
                    props.loopedOverChainsList,
                    props.winnersList
    )}
1
  • Your answer led me to actually seeing error messages and I was able to debug it form there. Thank you! Commented Aug 22, 2022 at 9:25
0

There were two problems: how I get the signer and how the rpc provider is detected. I changed both things - (1) I get the signer using the wallet and private key, (2) instead of providing rpc provider with a link, it is detected automatically with web3.

    const provider = new ethers.providers.Web3Provider(web3.currentProvider);
    const wallet = new ethers.Wallet(privateKey, provider);
    const signer = wallet.provider.getSigner(wallet.address);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.