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

Add State overrides to eth_call #1759

Merged
merged 22 commits into from
Feb 3, 2024
Merged

Conversation

maxencerb
Copy link
Contributor

@maxencerb maxencerb commented Jan 31, 2024

What

The goal of this PR is to add state overrides to call and eth_call EIP1193 types. These overrides are helpful in many cases. It modifies the state of selected accounts before the transaction. It is a default feature in geth. It allows to modify:

  • The balance of the account
  • The code on a given account
  • The state (key-value mapping representation of the storage) either on top of the account storage or completely override it.

Why

I only added the rpc schema and changed the "low-level" call function. But the first case scenario that comes in mind is the approve and swap transaction simulation.

If you know the mapping key for the allowances, you could change the allowance to the max allowance before a simulateContract call. This would allow a cheap onchain simulation of the rates you could get.

const { result } = await publicClient.simulateContract({
  address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
  abi: mangroveABI,
  functionName: 'marketOrderByTick',
  account,
  stateOverride: {
    // modify the state of the USDT contract
    [USDT_ADDRESS]: {
      // Allows to modify storage while keeping other storage intact
      stateDiff: {
        // modify the storage slot corresponding to allowance
        [userAllowanceSlot]: "0xffffffffff...",
      }
    }
  }
})

PR-Codex overview

Focus of this PR:

This PR focuses on adding the stateOverride property to various functions (call, simulateContract, readContract, and multicall) and introducing related types and error classes.

Detailed summary:

  • Added stateOverride property to call, simulateContract, readContract, and multicall functions.
  • Added StateOverride type defining state overrides for eth_call method.
  • Added InvalidBytesLengthErrorType and InvalidBytesLengthError classes.
  • Added RpcStateOverride and RpcStateMapping types.
  • Added StateMapping and StateOverride types.
  • Added AccountStateConflictErrorType, AccountStateConflictError, StateAssignmentConflictErrorType, and StateAssignmentConflictError classes.
  • Updated various files with the new changes.

The following files were skipped due to too many changes: src/errors/stateOverride.ts, src/actions/public/multicall.test.ts, site/pages/docs/actions/public/call.md, src/actions/public/call.ts, site/pages/docs/contract/simulateContract.md, src/actions/public/call.test.ts

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Copy link

changeset-bot bot commented Jan 31, 2024

🦋 Changeset detected

Latest commit: 99ce411

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jan 31, 2024

@maxencerb is attempting to deploy a commit to the wevm Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Member

@jxom jxom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems chill. Just a couple of comments!

src/actions/public/call.ts Outdated Show resolved Hide resolved
src/actions/public/call.ts Outdated Show resolved Hide resolved
src/types/misc.ts Outdated Show resolved Hide resolved
src/types/misc.ts Outdated Show resolved Hide resolved
src/actions/public/call.ts Outdated Show resolved Hide resolved
@maxencerb
Copy link
Contributor Author

I made some changes, added tests for readContract, multicall, and call, added some error handling.

Also added API docs on simulateContract, readContract, multicall, and call and added a usage example on simulateContract.

@maxencerb maxencerb changed the title Add States override to call Add State overrides to eth_call Feb 1, 2024
Copy link
Member

@jxom jxom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's looking good! If we can get coverage on these lines, then I'm happy to merge!

Test coverage cmd: bun run test:cov public/call.test

Screenshot 2024-02-02 at 1 02 58 pm

@maxencerb
Copy link
Contributor Author

I added coverage to these lines on call.test.ts.

@jxom jxom merged commit 7d89ce8 into wevm:main Feb 3, 2024
19 of 27 checks passed
@github-actions github-actions bot mentioned this pull request Feb 3, 2024
@tmm tmm mentioned this pull request Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants