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

reverse rpc fallback order #1010

Merged
merged 7 commits into from
Dec 11, 2024
Merged

reverse rpc fallback order #1010

merged 7 commits into from
Dec 11, 2024

Conversation

technophile-04
Copy link
Collaborator

Description

I am not sure what's the best solution for this is, but realized that public RPC URL's are not reliable. Especially recognized this with sepolia public rpc url which wagmi has https://rpc2.sepolia.org.

It seems to read the wrong state example balance and also not able to fetch read variables.

Demo video:
Screen.Recording.2024-12-08.at.9.41.06.AM.mov

To test the problem:

  1. Switch to main branch.

  2. Copy this inside externalContracts.ts

externalContracts.ts
import { GenericContractsDeclaration } from "~~/utils/scaffold-eth/contract";

/**
 * @example
 * const externalContracts = {
 *   1: {
 *     DAI: {
 *       address: "0x...",
 *       abi: [...],
 *     },
 *   },
 * } as const;
 */
const externalContracts = {
  11155111: {
    YourContract: {
      address: "0x4E439fde29737bD2B8a89CC2C2dDf9926c624194",
      abi: [
        {
          inputs: [
            {
              internalType: "address",
              name: "_owner",
              type: "address",
            },
          ],
          stateMutability: "nonpayable",
          type: "constructor",
        },
        {
          anonymous: false,
          inputs: [
            {
              indexed: true,
              internalType: "address",
              name: "greetingSetter",
              type: "address",
            },
            {
              indexed: false,
              internalType: "string",
              name: "newGreeting",
              type: "string",
            },
            {
              indexed: false,
              internalType: "bool",
              name: "premium",
              type: "bool",
            },
            {
              indexed: false,
              internalType: "uint256",
              name: "value",
              type: "uint256",
            },
          ],
          name: "GreetingChange",
          type: "event",
        },
        {
          inputs: [],
          name: "greeting",
          outputs: [
            {
              internalType: "string",
              name: "",
              type: "string",
            },
          ],
          stateMutability: "view",
          type: "function",
        },
        {
          inputs: [],
          name: "owner",
          outputs: [
            {
              internalType: "address",
              name: "",
              type: "address",
            },
          ],
          stateMutability: "view",
          type: "function",
        },
        {
          inputs: [],
          name: "premium",
          outputs: [
            {
              internalType: "bool",
              name: "",
              type: "bool",
            },
          ],
          stateMutability: "view",
          type: "function",
        },
        {
          inputs: [
            {
              internalType: "string",
              name: "_newGreeting",
              type: "string",
            },
          ],
          name: "setGreeting",
          outputs: [],
          stateMutability: "payable",
          type: "function",
        },
        {
          inputs: [],
          name: "totalCounter",
          outputs: [
            {
              internalType: "uint256",
              name: "",
              type: "uint256",
            },
          ],
          stateMutability: "view",
          type: "function",
        },
        {
          inputs: [
            {
              internalType: "address",
              name: "",
              type: "address",
            },
          ],
          name: "userGreetingCounter",
          outputs: [
            {
              internalType: "uint256",
              name: "",
              type: "uint256",
            },
          ],
          stateMutability: "view",
          type: "function",
        },
        {
          inputs: [],
          name: "withdraw",
          outputs: [],
          stateMutability: "nonpayable",
          type: "function",
        },
        {
          stateMutability: "payable",
          type: "receive",
        },
      ],
      inheritedFunctions: {},
    },
  },
} as const;

export default externalContracts satisfies GenericContractsDeclaration;
  1. Go to http://localhost:3000/debug

Solution:

We use alchmey URL first and then fallback to public RPC. Well I mean this is gonna burst the scaffold alchemy rpc but not sure what's the best solution here is.

@carletex
Copy link
Member

carletex commented Dec 9, 2024

but not sure what's the best solution here is.

Not sure either, but sharing some thoughts here as well.

I think the Sepolia one is the most problematic (noticed this when trying to deploy from hardhat when we had the public RPCs... it was always failing giving weird errors). The problem is that the RPC is not failing (where the fallback would work)... it's just returning incorrect stuff.

I guess wevm people noticed it too because they changed it on viem last month wevm/viem@79a1830. I guess it'd come with the newer versions of wagmi/view.

A couple of solution could be:

  1. If this is mainly Sepolia, let's just update and leave the RPC order as it was.
    • PROS:
      • Our alchemy key won't suffer
    • CONS:
      • public RPCs tend to fail
      • If people change to use their alchemy key... it'd still fail because it's the fallback (A weird hack would be to detect if a different API is being used... if so, make alchemy the default)
  2. If it's a general thing, let's tweak the order.
    • PROS:
      • Works most of the time (unless limits are hit)
    • CONS:
      • People killing our RPC

@technophile-04
Copy link
Collaborator Author

If people change to use their alchemy key... it'd still fail because it's the fallback (A weird hack would be to detect if a different API is being used... if so, make alchemy the default)

Love this! Makes sense becoz if people add their API key they would accept more reliable result and SE-2 was still using public RPC URL first (which might give wrong data)

Also updated the viem and wagmi so that we the default rpc from viem works nicely out of the box

Copy link
Member

@carletex carletex left a comment

Choose a reason for hiding this comment

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

I think this is a great solution. Thanks Shiv!!

@rin-st
Copy link
Member

rin-st commented Dec 10, 2024

Working and looking great to me, thanks! Just added a micro change, moved default alchemy API key to the variable so it could be changed from one place

@technophile-04
Copy link
Collaborator Author

Working and looking great to me, thanks! Just added a micro change, moved default alchemy API key to the variable so it could be changed from one place

Niceee and makes sense tysm!! Merging this 🙌

@technophile-04 technophile-04 merged commit 9c4d3a6 into main Dec 11, 2024
1 check passed
@technophile-04 technophile-04 deleted the rpc-fallback-reverse branch December 11, 2024 04:30
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