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

feat: baseTokens & chainIds #43

Merged
merged 2 commits into from
Aug 12, 2024
Merged

feat: baseTokens & chainIds #43

merged 2 commits into from
Aug 12, 2024

Conversation

0xkenj1
Copy link
Collaborator

@0xkenj1 0xkenj1 commented Aug 8, 2024

🤖 Linear

Closes ZKS-151 ZKS-152

Description

  • getBaseTokens
  • getChainIds
  • Removed bridgehubAddress & sharedBridgeAddress from shared/constants.ts
  • Add bridgehubAddress , sharedBridgeAddress & stateTransitionManagerAddress as parameters for L1MetricsService

@0xkenj1 0xkenj1 requested a review from 0xnigir1 August 8, 2024 19:42
Copy link

linear bot commented Aug 8, 2024

ZKS-151 Get chainId's

In order to get chain ids we should add StateTransitionManager contract addresses as input

 ZK sync era StateTransitionManager: 0xc2eE6b6af7d616f6e27ce7F4A451Aedc2b0F5f5C

Use this method to get all chainIds from StateTransitionManager: https://github.com/matter-labs/era-contracts/blob/8a70bbbc48125f5bde6189b4e3c6a3ee79631678/l1-contracts/contracts/state-transition/StateTransitionManager.sol#L109

Steps:

  • Get STM array from inputs
  • multicall using getAllHyperchainsIds
  • return { <STM_ADDRESS>: ChainId[] }

ZKS-152 Get baseToken

we need to get `baseToken` given a chain Id.

use baseToken method from bridgehub : https://github.com/matter-labs/era-contracts/blob/8a70bbbc48125f5bde6189b4e3c6a3ee79631678/l1-contracts/contracts/bridgehub/Bridgehub.sol#L30

@0xkenj1 0xkenj1 requested a review from 0xyaco August 8, 2024 19:42
Copy link
Collaborator

@0xyaco 0xyaco left a comment

Choose a reason for hiding this comment

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

image

@@ -716,6 +731,86 @@ describe("L1MetricsService", () => {
});
});

describe("getChainIds", () => {
it("returns chainIds", async () => {
true && true;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this some kind of weird hack or is it something that was left by accident? 🔎

}),
allowFailure: false,
});
this.chainIds = chainIds.flat();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Dropping this comment just in case, from my ignorance on how/when is this service going to be instantiated.

If this is going to be used while answering requests and the chainIds are going to change at some point, let's be sure that this service is instantiated once per request or something like that, to avoid having a deprecated value of chainIds as there's no mechanism to renew them. This issue would arise, for example, if this service is instantiated only when the NestJS app starts and is reused with all the requests.

Copy link
Collaborator

Choose a reason for hiding this comment

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

i think that chainIds won't change frequently enough (for now) but it's a good point. maybe when implementing Cache we could update this implementation

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

what we are gona do is to implement an Inmemory cache during milestone Enhancements, almost every method of the L1MetricsService will have a caching strategy. In particular for getChainIds what we plan is to have a ttl of x minutes, that will ensure that the values will be updated and not get stuck.


expect(result).toEqual(mockedMulticallReturnValue);
});
it("returns baseTokens", async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
it("returns baseTokens", async () => {
it("returns empty array if chainIds is empty", async () => {

Comment on lines +768 to +780
it("returns empty array if chainIds are empty", async () => {
const mockedChainIds: bigint[] = [];
l1MetricsService = await mockMetricsModule(
mockedBridgeHubAddress,
mockedSharedBridgeAddress,
mockedSTMAddresses,
);
l1MetricsService["chainIds"] = mockedChainIds;

const result = await l1MetricsService.getChainIds();

expect(result).toEqual(mockedChainIds);
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like you wanted to test this scenario in getBaseTokens but ended up doing it in the getChainIds. This case might be already covered by the returns chainIds previously setted up on the returns chainIds previously setted up

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it was on purpose hehehehe, but yea,returns chainIds previously setted up should cover the case, just wanted to test this edge case in particular.

}),
allowFailure: false,
});
this.chainIds = chainIds.flat();
Copy link
Collaborator

Choose a reason for hiding this comment

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

i think that chainIds won't change frequently enough (for now) but it's a good point. maybe when implementing Cache we could update this implementation

* Get the base token for each chain
* @returns A map of chainId to base token address
*/
async getBaseTokens(chainIds: ChainId[]): Promise<Address[]> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we return a Token from the whitelisted from Coingecko instead of plain Address?
ie. search in the static token list the one that matches the contractAddress (or Ethereum if adresss(1)) so metadata is returned to frontend too (with image url and all that stuff)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

niceeee, lets do it

@0xkenj1 0xkenj1 requested review from 0xnigir1 and 0xyaco August 9, 2024 22:42
it("returns known tokens", async () => {
const mockedChainIds = [1n, 2n];
const knownTokenAddress1 = Object.keys(erc20Tokens)[0];
const knownTokenAddress2 = Object.keys(erc20Tokens)[0];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the duplicated usage of the [0] element intended?

@@ -0,0 +1,4 @@
import { Token, TokenType } from "@zkchainhub/shared/types";

export const isNativeToken = (token: Token<TokenType>): token is Token<"native"> =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

😎

Copy link
Collaborator

@0xnigir1 0xnigir1 left a comment

Choose a reason for hiding this comment

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

lets gooo 🚀

Copy link
Collaborator

Choose a reason for hiding this comment

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

cool 💯

Comment on lines +364 to +367
decimals: 18,
name: "unknown",
type: "erc20",
symbol: "unknown",
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can add a task to enhancements to fetch decimals, name and symbol from the contract now that i think of it but for now it's good since we know that baseTokens are permisioned and practically it'll be Ether most of the times

@0xkenj1 0xkenj1 merged commit d760463 into dev Aug 12, 2024
6 checks passed
@0xkenj1 0xkenj1 deleted the feat/chainIds-base-tokens branch August 12, 2024 13:47
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