Skip to content

chore(api-reference) Add Contract Manager Support #2785

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

aditya520
Copy link
Member

@aditya520 aditya520 commented Jun 14, 2025

Summary

TODO: Add a method to check if RPC is working. We can verify by calling eth_chainID and match with the present chainID.

Rationale

How has this been tested?

  • Current tests cover my changes
  • Added new tests
  • Manually tested the code

@aditya520 aditya520 requested a review from a team as a code owner June 14, 2025 13:19
Copy link

vercel bot commented Jun 14, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
api-reference ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 15, 2025 2:25pm
7 Skipped Deployments
Name Status Preview Comments Updated (UTC)
component-library ⬜️ Skipped (Inspect) Jun 15, 2025 2:25pm
developer-hub ⬜️ Skipped (Inspect) Jun 15, 2025 2:25pm
entropy-debugger ⬜️ Skipped (Inspect) Jun 15, 2025 2:25pm
entropy-explorer ⬜️ Skipped (Inspect) Jun 15, 2025 2:25pm
insights ⬜️ Skipped (Inspect) Jun 15, 2025 2:25pm
proposals ⬜️ Skipped (Inspect) Jun 15, 2025 2:25pm
staking ⬜️ Skipped (Inspect) Jun 15, 2025 2:25pm

@vercel vercel bot temporarily deployed to Preview – entropy-debugger June 15, 2025 14:22 Inactive
@vercel vercel bot temporarily deployed to Preview – insights June 15, 2025 14:22 Inactive
@vercel vercel bot temporarily deployed to Preview – component-library June 15, 2025 14:22 Inactive
@vercel vercel bot temporarily deployed to Preview – entropy-explorer June 15, 2025 14:22 Inactive
@vercel vercel bot temporarily deployed to Preview – staking June 15, 2025 14:22 Inactive
@vercel vercel bot temporarily deployed to Preview – developer-hub June 15, 2025 14:22 Inactive
@vercel vercel bot temporarily deployed to Preview – proposals June 15, 2025 14:22 Inactive
Copy link
Collaborator

@cprussin cprussin left a comment

Choose a reason for hiding this comment

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

I have a few minor suggestions, but to me the most important thing is that I really don't think NETWORK_INFO makes sense when using contract manager, so I don't think it's wise to approach this by trying to rebuild it to match the old shape. Instead, my suggestion is to get rid of NETWORK_INFO entirely, and to update the call sites to use contract manager directly.

Comment on lines +25 to +26
} else if (chain.rpcUrls.default.http[0]) {
return [chain.id, http(chain.rpcUrls.default.http[0])];
Copy link
Collaborator

Choose a reason for hiding this comment

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

This logic should live in getRpcUrl -- it doesn't make sense that we have a function that should get the rpc URL which only sometimes returns the RPC url and other times there is still a valid URL but the consumer needs to know about different logic to retrieve it.

rpcUrl: string;
networkId: number;
type: string;
nativeToken?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think you need to worry about adding types for fields that aren't in use with this app. Typescript objects are not closed (meaning they allow undeclared fields to be present), and only including fields you actually need allows typescript to be more helpful if we wanted to e.g. restructure the shape of EvmChains (typescript would be able to tell us if we've restructured things in a way that's breaking anything in use)

const contractAddress = Object.values(EvmPriceFeedContracts).find(
(contract) => contract.chain === chain.id,
);
return contractAddress?.address as `0x${string}`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should not be typecasting. For one, this is going to incorrectly mask the undefined case, and for two, if an invalid string were entered into EvmPriceFeedContracts, the typesystem would be incorrect and runtime would not operate in a way that matches what the type checker is guaranteeing.

Instead, it would be better to explicitly verify and throw exceptions if the data fails invariants you expect, e.g. something along these lines:

if (contractAddress === undefined) {
  throw new Error(`No contract found for ${chain.id}`);
} else if (isZeroXString(contractAddress.address)) {
  return contractAddress.address;
} else {
  throw new Error(`Invalid contract address for ${chain.id}: ${contractAddress.address}`);
}

...

const isZeroXString = (val: string): val is `0x${string}` => val.startsWith("0x");

const mapEvmChainsToNetworkInfo = (chains: EvmChains[]) => {
const networkInfo: Record<number, NetworkInfo> = {};

for (const chain of chains) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

A few notes:

  1. This doesn't seem to make a lot of sense to me. Why not just get rid of NETWORK_INFO entirely and just use EvmChains directly in the consuming files? This looks like it's just added indirection that just attempts to keep the NETWORK_INFO structure the same as it was before, but the indirection doesn't add any other value and it shouldn't be hard to refactor things to get rid of this file entirely (the type checker should be very helpful here)
  2. If you're going to do this, a more declarative / functional way is to use Object.fromEntries:
const mapEvmChainsToNetworkInfo = (chains: EvmChains[]) =>
  Object.fromEntries(chains.map(chain => [
    Number.parseInt(chain.networkId, 10),
    {
      name: chain.id,
      rpcUrl: chain.rpcUrl,
      isMainnet: chain.mainnet,
      contractAddress: getPriceFeedContractAddress(chain)
    }
  ]));
  1. as a minor nit, you should use Number.parseInt to convert string to int (I think that's what's happening here), not Number(), as the Number constructor is ambiguous and unclear (e.g. it's not clear to me from reading the code what conversion is actually happening)

};

// Convert EvmChains to array format
const evmChainsArray = Object.values(EvmChains) as EvmChains[];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please don't typecast -- if this does not automatically unify then it means something needs to be verified in order for the typesystem to be reliable. But I think if you follow my advice of doing away with NETWORK_INFO entirely, this will go away.

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.

2 participants