Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while calling contract function #6075

Open
anurag-iitk opened this issue Dec 24, 2024 · 4 comments
Open

Error while calling contract function #6075

anurag-iitk opened this issue Dec 24, 2024 · 4 comments
Assignees

Comments

@anurag-iitk
Copy link

Version of Hardhat

2.22.17

What happened?

This is smart contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract CeroSharedSequence {
    error InvalidAddress();
    error CeroProxyAddressNotSet();
    error FailedToFetchBatch();
    address public proxyContractAddress;

    function setProxyContractAddress(address _proxyAddress) external {
        if (_proxyAddress == address(0)) {
            revert InvalidAddress();
        }
        proxyContractAddress = _proxyAddress;
    }

    function fetchBatches(uint256 batchIndex)
        external
        view
        returns (bytes32, uint64)
    {
        if (proxyContractAddress == address(0)) {
            revert CeroProxyAddressNotSet();
        }
        (bool success, bytes memory data) = proxyContractAddress.staticcall(
            abi.encodeWithSignature(
                "getBatchByRollupIdAndIndex(uint64,uint256)",
                1,
                batchIndex
            )
        );

        if (!success) {
            revert FailedToFetchBatch();
        }

        (bytes32 batchHash, uint64 timestamp) = abi.decode(
            data,
            (bytes32, uint64)
        );
        return (batchHash, timestamp);
    }
}

And this is my test script


import { expect } from "chai";
import { ethers } from "hardhat";

describe("CeroSharedSequence", function () {
  let owner: any;
  let ceroSharedSequence: any;
  const hardcodedProxyAddress = "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512";
  
  beforeEach(async function () {
    [owner] = await ethers.getSigners();
    const CeroSharedSequence = await ethers.getContractFactory("CeroSharedSequence");
    ceroSharedSequence = await CeroSharedSequence.deploy();
  });

  it("should set the proxy contract address to the hardcoded address", async function () {
    await ceroSharedSequence.setProxyContractAddress(hardcodedProxyAddress);
  });

  it("should fetch batches", async function () {
    await ceroSharedSequence.setProxyContractAddress(hardcodedProxyAddress);
    const batchIndex = 0;
    const trx = await ceroSharedSequence.fetchBatches(batchIndex);
    console.log(trx);
  });
});

Minimal reproduction steps

While running the testcase got this issue

should fetch batches:
     Error: Transaction reverted and Hardhat couldn't infer the reason.
    at CeroSharedSequence.fetchBatches (contracts/Cero.sol:37)
    at CeroSharedSequence.fetchBatches (contracts/Cero.sol:37)
    at EdrProviderWrapper.request (node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:444:41)
    at async staticCallResult (node_modules/ethers/src.ts/contract/contract.ts:337:22)
    at async staticCall (node_modules/ethers/src.ts/contract/contract.ts:303:24)
    at async Proxy.fetchBatches (node_modules/ethers/src.ts/contract/contract.ts:351:41)
    at async Context.<anonymous> (test/Lock.ts:22:17)
    ```
    
but it's working fine with remix

### Search terms

_No response_
@sisco0
Copy link
Contributor

sisco0 commented Dec 25, 2024

It looks like the root cause is that, in your Hardhat environment, the address you’re passing (0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512) does not actually contain a contract that implements the function getBatchByRollupIdAndIndex(uint64,uint256), causing the staticcall to fail. In Remix, you probably did deploy the correct contract and wired it up, so it worked there.

To fix this in Hardhat, you can deploy a quick mock (or your actual proxy contract) that includes the getBatchByRollupIdAndIndex function, then modify your test script to use the newly deployed mock’s address, like so:

  const MockProxy = await ethers.getContractFactory("MockProxy");
  const mockProxy = await MockProxy.deploy();
  await mockProxy.waitForDeployment();

  await ceroSharedSequence.setProxyContractAddress(await mockProxy.getAddress());

@anurag-iitk
Copy link
Author

I'm using Diamond Proxy Pattern and I've not deploy the proxy contract using remix. I've used same address 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 and pass it to setProxyContractAddress function. And it's work.
This address contract is in my other repository and above contract I've mentioned is in other repository. Any idea how can i do
@sisco0

@sisco0
Copy link
Contributor

sisco0 commented Dec 26, 2024

I would let other community members to provide a solution for your case @anurag-iitk .

@anurag-iitk
Copy link
Author

@kanej

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Backlog
Development

No branches or pull requests

3 participants