Skip to content

Commit

Permalink
Add balancer user
Browse files Browse the repository at this point in the history
  • Loading branch information
batphonghan committed Aug 22, 2024
1 parent b699e1f commit 8f46444
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 159 deletions.
8 changes: 0 additions & 8 deletions adapters/kelp_gain_linea/jest.config.js

This file was deleted.

7 changes: 2 additions & 5 deletions adapters/kelp_gain_linea/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@
"jsbi": "^4.3.0",
"tiny-invariant": "^1.3.1",
"toformat": "^2.0.0",
"ethereum-block-by-date": "^1.4.9",
"@types/jest": "26.0.24",
"jest": "26.6.3",
"ts-jest": "26.5.6"
"ethereum-block-by-date": "^1.4.9"
},
"devDependencies": {
"@types/ethereum-block-by-date": "^1.4.1",
"@types/node": "^20.11.17",
"typescript": "4.9.5"
"typescript": "^5.3.3"
}
}
165 changes: 87 additions & 78 deletions adapters/kelp_gain_linea/src/lib/balancer.ts
Original file line number Diff line number Diff line change
@@ -1,118 +1,127 @@
import { gql } from "graphql-request";
import { subgraphFetchAllById, subgraphFetchOne } from "./subgraph";
import BigNumber from "bignumber.js";
import { ethers } from "ethers";
import { subgraphFetchAllById, subgraphFetchOne } from "./query";

const BALANCER_V2_ENDPOINT = "https://api.thegraph.com/subgraphs/id/QmQ5TT2yYBZgoUxsat3bKmNe5Fr9LW9YAtDs8aeuc1BRhj";
const AGETH_POOL_ID = "0xf1bbc5d95cd5ae25af9916b8a193748572050eb00000000000000000000006bc";
const BALANCER_V2_ENDPOINT =
"https://api.thegraph.com/subgraphs/id/QmQ5TT2yYBZgoUxsat3bKmNe5Fr9LW9YAtDs8aeuc1BRhj";
const AGETH_POOL_ID =
"0xf1bbc5d95cd5ae25af9916b8a193748572050eb00000000000000000000006bc";

interface GraphQLQuery {
query: string;
collection: string;
query: string;
collection: string;
}

interface UserAddress {
id: string;
id: string;
}

interface Share {
id: string;
userAddress: UserAddress;
balance: string;
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;
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;
tokens: Token[];
totalShares: string;
}

interface GetPoolDetailsResponse {
pool: Pool;
pool: Pool;
}

const ONE_E_18 = BigInt(10**18);

const BALANCER_POOL_SHARES_QUERY: GraphQLQuery = {
query: gql`
query GetPoolShares($poolId: ID!, $block: Int, $first: Int, $lastId: ID!) {
poolShares(
where: { poolId: $poolId, id_gt: $lastId, balance_gt: "0", userAddress_not: "0x0000000000000000000000000000000000000000" }
block: { number: $block }
first: $first
orderBy: id
orderDirection: asc
) {
id
balance
userAddress {
id
}
}
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",
}
}
`,
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
}
query: gql`
query GetPoolDetails($poolId: ID!, $block: Int) {
pool(id: $poolId, block: { number: $block }) {
tokens {
priceRate
weight
balance
symbol
}
`,
collection: "pool",
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 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,
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}
);
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 = BigNumber(agETH.balance).multipliedBy(BigNumber(ONE_E_18.toString()));
const totalLiquidity = BigNumber(poolDetails.totalShares).multipliedBy(ONE_E_18.toString());

for(let i=0; i<balances.length; i++) {
const userLpBalance = BigNumber(balances[i].balance).multipliedBy(BigNumber(ONE_E_18.toString()));
const userShare = userLpBalance.div(totalLiquidity);
const userAgETH = userShare.multipliedBy(totalPoolAgETH);
balances[i].balance = userAgETH.toString();
}
return balances;
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) {
let balances = await fetchBalancerAgEthPoolShares(blockNumber);
const poolDetails = await getPoolDetails(blockNumber);
return convertLpToAgETH(balances, poolDetails);
let balances = await fetchBalancerAgEthPoolShares(blockNumber);
const poolDetails = await getPoolDetails(blockNumber);
return convertLpToAgETH(balances, poolDetails);
}

2 changes: 0 additions & 2 deletions adapters/kelp_gain_linea/src/lib/pendle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const PendleURL =

const API_KEY = process.env.SENTIO_API_KEY || "";



export async function fetchAllPendleShare(blockNumber: number) {
const dataSize = 20000;
let page = 0;
Expand Down
21 changes: 17 additions & 4 deletions adapters/kelp_gain_linea/src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface IDwise {
id: string;
}

async function subgraphFetchAllById<T extends IDwise>(
export async function subgraphFetchAllById<T extends IDwise>(
endpoint: string,
query: string,
collection: string,
Expand Down Expand Up @@ -41,6 +41,19 @@ async function subgraphFetchAllById<T extends IDwise>(
}
return data;
}
export async function subgraphFetchOne<T>(
endpoint: string,
query: string,
collection: string,
variables: Record<string, unknown>
): Promise<T> {
const resp: { [collection: string]: T } = await request(
endpoint,
query,
variables
);
return resp[collection];
}

interface GraphQLQuery {
query: string;
Expand Down Expand Up @@ -85,7 +98,7 @@ export async function getAllAgEthHodlers(blockNumber: number) {
...pendleShares.map((e) => {
return {
id: e.user,
balance: e.share,
balance: e.share
};
})
);
Expand All @@ -94,10 +107,10 @@ export async function getAllAgEthHodlers(blockNumber: number) {
...balancerShares.map((e) => {
return {
id: e.userAddress.id,
balance: e.balance,
balance: e.balance
};
})
)
);

const balanceMap = new Map<string, bigint>();
for (const balance of [...positions]) {
Expand Down
53 changes: 0 additions & 53 deletions adapters/kelp_gain_linea/src/lib/subgraph.ts

This file was deleted.

9 changes: 0 additions & 9 deletions adapters/kelp_gain_linea/test/balancer.test.ts

This file was deleted.

0 comments on commit 8f46444

Please sign in to comment.