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

Get rid of repeated web3.eth.getChainId() calls #32

Merged
merged 1 commit into from
Mar 22, 2022

Conversation

diwu1989
Copy link
Contributor

@diwu1989 diwu1989 commented Feb 15, 2022

Every single pair was making a getChainID() call to load its network address inside init().

This is unnecessary, lift it up to the registry level as part of findPairs() to retrieve the chain ID and pass into constructor of Pair.

Make initialization faster and more scalable for thousands of pairs.
This also makes it easier for me to bulk instantiate and refresh all pairs now that the swappa pair address is not part of async init()

@@ -17,21 +17,23 @@ export abstract class Pair {
public pairKey: string | null = null;
public tokenA: Address = "";
public tokenB: Address = "";
protected swappaPairAddress: Address = "";
private swappaPairAddress: Address = "";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

make private now that the child classes do not need to set this directly anymore


public async init(): Promise<void> {
const r = await this._init()
this.pairKey = r.pairKey
this.tokenA = r.tokenA
this.tokenB = r.tokenB
this.swappaPairAddress = r.swappaPairAddress
Copy link
Contributor Author

Choose a reason for hiding this comment

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

constructor should have already set up the correct pair addr

@@ -25,6 +25,7 @@ export class RegistryUniswapV2 extends Registry {
}

findPairs = async (tokenWhitelist: Address[]): Promise<Pair[]> => {
const chainId = await this.web3.eth.getChainId()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

make each registry as part of findPairs grab the chainID exactly once, and instantiate all pairs with that chainId

@@ -17,12 +17,7 @@ interface AddressesByNetwork {
alfajores?: Address,
}

export const selectAddress = async (web3: Web3, addresses: AddressesByNetwork) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

selectAddress is sync now that we require the chainId to be passed in directly
caller is responsible to make the minimal number of getChainId() calls

@zviadm zviadm merged commit 2ce093c into terminal-fi:main Mar 22, 2022
@diwu1989 diwu1989 deleted the chainIdFix branch March 23, 2022 05:31
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