-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from batphonghan/add_balancer_pendle
kelp: gain : tvl for blancer, pendle, spectra users
- Loading branch information
Showing
11 changed files
with
6,069 additions
and
1,120 deletions.
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,135 @@ | ||
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); | ||
let shares = convertLpToAgETH(balances, poolDetails); | ||
|
||
if (shares.length == 0) { | ||
throw new Error(`Empty share Spectra BLOCK: ${blockNumber}`); | ||
} | ||
return shares; | ||
} |
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
Oops, something went wrong.