Skip to content

Fetch pools historical - #413

Closed
alfulinku wants to merge 13 commits into
balancer:developfrom
alfulinku:fetchPools-historical
Closed

Fetch pools historical#413
alfulinku wants to merge 13 commits into
balancer:developfrom
alfulinku:fetchPools-historical

Conversation

@alfulinku

@alfulinku alfulinku commented Aug 24, 2023

Copy link
Copy Markdown

EDIT: I think I found de cleaner way of doing this directly in the SDK: balancer/balancer-sdk#525

Hello Balancer team,

I wondered if I could use your offline SOR/SDK to fetch data about the balancer protocol but historically (like "what was the best price I could get on WETH/USDC at block 17 000 000") and was left without a solution so I tried to add the feature to the SOR.

I don't really understand how to work with the SDK, I have the feeling that the files that need to implements the changes are in the SDK repository (subgraphPoolDataService.ts and onChainData.ts) but because it all starts by creating a SOR object from this repository, I guess the feature must come from here first?

Anyway I added the historical capability here and created a small test file very similar to test\testScripts\swapExample.ts, the new one is: test\testScripts\swapHistoricalExample.ts

To run this test file, you need to set your env variable RPC_URL_MAINNET to an archive node RPC (my infura works well)

The idea in that file is that I first instantiate a sor object using fetchPools like this:

const targetBlockNumber = 17_000_000;
await sor.fetchPools(undefined, undefined, targetBlockNumber);

and another instance of sor using the basic fetchPool call
await sorNow.fetchPools();

and I call the getSwaps() function with both of these sor objects to see the difference, outputing these two lines in the console:

1 WETH is 1664.098257 USDC using the most recent data
1 WETH was 1862.210233 USDC at block 17000000

Etherscan gives me 1864 usd per ETH at this block so this looks like a win.

Again, I think this is only the first step, the real implementation will have to be done on the sdk repository. And it would be very much simpler to use the SDK like I do for another project :

const balancer = new BalancerSDK({
        network: 100, // gnosis
        rpcUrl: rpcUrl,
    });

const swapsService = balancer.swaps; // Swaps module
await swapsService.fetchPools();

(but with the possibility to give the blockNumber to the fetchPools function)

Using the sor directly seems harder as it would mean having to import the SubgraphPoolDataService and getOnChainBalances on my project (at least).

What do you think? Is it a feature that could be usefull and added to the SOR/SDK ?

@alfulinku
alfulinku marked this pull request as ready for review August 24, 2023 12:36
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