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

Introduce registry helper contracts to speed up pair discovery & bulk pair refresh #31

Merged
merged 8 commits into from
Jun 20, 2022

Conversation

diwu1989
Copy link
Contributor

@diwu1989 diwu1989 commented Feb 14, 2022

Registry helper to speed up:

  • initial pair discovery, getting pair tokens & state
  • per block bulk pair refresh, given a list of pairs

Example deployment of earlier draft of this contract, some naming changed, but functionality is the same:
Uniswap Helper https://explorer.celo.org/address/0x4d416fE42BA21C59fc853bd887B996D8C05A0e20/contracts
Balancer Helper https://explorer.celo.org/address/0xba2d6Ec829d1026B2E8ae93c06112A457918c44e/read-contract

These registry helpers are absolutely necessary to scaling the per-block pair refresh() to 2k+ pairs.

TODO: make it possible for a registry to bulk-refresh all of its pairs with the registry helper contract
Probably will create pair specific Snapshot from the on-chain fetched batch data, and use the current restore(snapshot) api to batch initialize / refresh the pairs

struct PairState {
uint reserve0;
uint reserve1;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

reserve information for the pair state information we need for refresh()

address token0;
address token1;
PairState state;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

initial Pair information for initialization, address of pair, tokens, and the initial state

IUniswapV2Factory factory,
address[] calldata tokenWhitelist,
uint offset,
uint limit
Copy link
Contributor Author

Choose a reason for hiding this comment

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

needed to introduce offset and limit so that the amount of work done per call is capped
otherwise, Forno complains about out of gas when trying to fetch info for > 200 pairs.

The safe limit I've found is to fetch in chunks of 100.

// allocate a buffer array with the upper bound of the number of pairs returned
PairInfo[] memory buffer = new PairInfo[](limit);
uint found = 0;
for (uint i = offset; i < allPairsUpTo; i++) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

loop through starting from the given offset

@zviadm
Copy link
Collaborator

zviadm commented Jun 20, 2022

I am going to take over this diff, since I need to integrate the registry helper into the dpeloyer too.
Thanks for all the PRs. I finally got around to merging most of them and releasing new version of Swappa too.

@zviadm zviadm merged commit 3fa5228 into terminal-fi:main Jun 20, 2022
@diwu1989 diwu1989 deleted the registryHelper branch June 20, 2022 20:14
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