-
Notifications
You must be signed in to change notification settings - Fork 91
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
kelp: gain : tvl for blancer, pendle, spectra users #294
Merged
Merged
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
fafec39
Update pendle users
batphonghan b592f8b
get balancer agETH/rsETH user balances
aqua-regia dfb4693
added pagination to subgraph query
aqua-regia b699e1f
convert user lp balances to agETH balances
aqua-regia 8f46444
Add balancer user
batphonghan 630ad9b
Update pendle query
batphonghan 317a5cd
Alway query from history 1 hour
batphonghan 93c523e
Update env name
batphonghan 51bd9d7
Merge remote-tracking branch 'origin/main' into add_balancer_pendle
batphonghan 9141d58
Fix compile error
batphonghan a38c578
Convert share pendle to agETH
batphonghan f97cb06
Refactor
batphonghan 9d39f21
Update pendle endpoint
batphonghan 25bd868
Update test file
batphonghan 4320481
Update agEth/rsEth rate
batphonghan 4757933
Update pendle hourly snapshot
batphonghan 4f58de1
Merge remote-tracking branch 'origin/main' into add_balancer_pendle
batphonghan 949c95a
Add spectra
batphonghan 38833b6
Update provider
batphonghan f0e808c
Retry
batphonghan fe3bb31
Revert "Add spectra"
batphonghan cf8f102
Revert "Revert "Add spectra""
batphonghan b1e7ecc
Remove debug
batphonghan ad32877
rename
batphonghan 97a8e8d
Update spectra query
batphonghan ee4344d
Merge branch 'add_spectra' into add_balancer_pendle
batphonghan 80551c2
Add start block
batphonghan 589878f
Refactor
batphonghan a5839b9
Throw error when query for higher block
batphonghan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { gql } from "graphql-request"; | ||
import { ethers } from "ethers"; | ||
import { subgraphFetchAllById, subgraphFetchOne } from "./query"; | ||
import { BALANCER_START_BLOCK } from "./utils"; | ||
|
||
const BALANCER_V2_ENDPOINT = | ||
"https://api.thegraph.com/subgraphs/id/QmQ5TT2yYBZgoUxsat3bKmNe5Fr9LW9YAtDs8aeuc1BRhj"; | ||
const AGETH_POOL_ID = | ||
"0xf1bbc5d95cd5ae25af9916b8a193748572050eb00000000000000000000006bc"; | ||
interface GraphQLQuery { | ||
query: string; | ||
collection: string; | ||
} | ||
|
||
interface UserAddress { | ||
id: string; | ||
} | ||
|
||
interface Share { | ||
id: string; | ||
userAddress: UserAddress; | ||
balance: string; | ||
} | ||
|
||
interface Token { | ||
priceRate: string; // or number, depending on the actual data type | ||
weight: string; // or number | ||
balance: string; // or number | ||
symbol: string; | ||
} | ||
|
||
interface Pool { | ||
tokens: Token[]; | ||
totalShares: string; | ||
} | ||
|
||
interface GetPoolDetailsResponse { | ||
pool: Pool; | ||
} | ||
|
||
const BALANCER_POOL_SHARES_QUERY: GraphQLQuery = { | ||
query: gql` | ||
query GetPoolShares($poolId: ID!, $block: Int, $lastId: ID!) { | ||
poolShares( | ||
where: { | ||
poolId: $poolId | ||
id_gt: $lastId | ||
balance_gt: "0" | ||
userAddress_not: "0x0000000000000000000000000000000000000000" | ||
} | ||
block: { number: $block } | ||
first: 1000 | ||
orderBy: id | ||
orderDirection: asc | ||
) { | ||
id | ||
balance | ||
userAddress { | ||
id | ||
} | ||
} | ||
} | ||
`, | ||
collection: "poolShares" | ||
}; | ||
|
||
const POOL_DETAILS_QUERY: GraphQLQuery = { | ||
query: gql` | ||
query GetPoolDetails($poolId: ID!, $block: Int) { | ||
pool(id: $poolId, block: { number: $block }) { | ||
tokens { | ||
priceRate | ||
weight | ||
balance | ||
symbol | ||
} | ||
totalShares | ||
} | ||
} | ||
`, | ||
collection: "pool" | ||
}; | ||
|
||
export async function getPoolDetails(block: number): Promise<Pool> { | ||
return await subgraphFetchOne<Pool>( | ||
BALANCER_V2_ENDPOINT, | ||
POOL_DETAILS_QUERY.query, | ||
POOL_DETAILS_QUERY.collection, | ||
{ poolId: AGETH_POOL_ID, block: block } | ||
); | ||
} | ||
|
||
export async function fetchBalancerAgEthPoolShares( | ||
block: number | ||
): Promise<Share[]> { | ||
return await subgraphFetchAllById<Share>( | ||
BALANCER_V2_ENDPOINT, | ||
BALANCER_POOL_SHARES_QUERY.query, | ||
BALANCER_POOL_SHARES_QUERY.collection, | ||
{ poolId: AGETH_POOL_ID, block: block } | ||
); | ||
} | ||
|
||
function convertLpToAgETH(balances: Share[], poolDetails: Pool) { | ||
const agETH = poolDetails.tokens.filter( | ||
(token) => token.symbol == "agETH" | ||
)[0]; | ||
const totalPoolAgETH = ethers.utils.parseEther(agETH.balance).toBigInt(); | ||
const totalLiquidity = ethers.utils | ||
.parseEther(poolDetails.totalShares) | ||
.toBigInt(); | ||
|
||
for (let i = 0; i < balances.length; i++) { | ||
const userLpBalance = ethers.utils | ||
.parseEther(balances[i].balance) | ||
.toBigInt(); | ||
const userAgETH = (userLpBalance * totalPoolAgETH) / totalLiquidity; | ||
balances[i].balance = userAgETH.toString(); | ||
} | ||
return balances; | ||
} | ||
|
||
export async function fetchAllBalancerShare(blockNumber: number) { | ||
if (blockNumber < BALANCER_START_BLOCK) { | ||
return []; | ||
} | ||
let balances = await fetchBalancerAgEthPoolShares(blockNumber); | ||
const poolDetails = await getPoolDetails(blockNumber); | ||
return convertLpToAgETH(balances, poolDetails); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type Result = { | ||
rows: Row[]; | ||
}; | ||
|
||
export type Row = { | ||
user: string; | ||
share: string; | ||
block_number: string; | ||
day: string; | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currently this is just checking if sum of rseth in csv is more than total supply. but my concern here is that one of the function might return 0 result when it is not suppose to, which we ended up ingesting that.
can we modify this check to make sure those function that is suppose to return data in the specific block is doing so, and throw error if it isnt?