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

Overnight csv #94

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions adapters/overnight/src/sdk/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ export const enum CHAINS{
}
export const enum PROTOCOLS{
OVN = 1,
OVN_REBASE = 2
}

export const enum AMM_TYPES{
UNISWAPV3 = 0,
}

export const enum OVN_CONTRACTS{
FIRST = 0,
SECOND = 1,
USDPLUS = 2,
USDTPLUS = 3,
USDPLUS = 0,
USDTPLUS = 1,
}

export const SUBGRAPH_URLS = {
[CHAINS.LINEA]: {
[PROTOCOLS.OVN_REBASE]: {
[PROTOCOLS.OVN]: {
[OVN_CONTRACTS.USDPLUS]: {
url: "https://api.studio.thegraph.com/query/68020/ovn_linea_points/version/latest",
address: "0xB79DD08EA68A908A97220C76d19A6aA9cBDE4376"
Expand All @@ -29,16 +26,6 @@ export const SUBGRAPH_URLS = {
address: "0x1E1F509963A6D33e169D9497b11c7DbFe73B7F13"
},
},
[PROTOCOLS.OVN]: {
[OVN_CONTRACTS.FIRST]: {
url: "https://api.studio.thegraph.com/query/68020/linea_ovn/version/latest",
address: "0xc5f4c5c2077bbbac5a8381cf30ecdf18fde42a91"
},
[OVN_CONTRACTS.SECOND]: {
url: "https://api.studio.thegraph.com/query/68020/ovn_linea_2/version/latest",
address: "0x58aacbccaec30938cb2bb11653cad726e5c4194a"
},
}
}
}

Expand Down
120 changes: 2 additions & 118 deletions adapters/overnight/src/sdk/subgraphDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ export interface PositionRebaseNet{
value: bigint;
};

export interface PositionWithUSDValue extends Position{
token0USDValue: string;
token1USDValue: string;
token0AmountsInWei: bigint;
token1AmountsInWei: bigint;
token0DecimalValue: number;
token1DecimalValue: number;
}

const countNetRebase = async (
usersMinted: Map<string, string>,
usersRedeemed: Map<string, string>,
Expand Down Expand Up @@ -149,7 +140,7 @@ export const getRebaseForUsersByPoolAtBlock = async ({
const blockNumberFrom = 0;
const blockNumberTo = blockNumber;

const urlData = SUBGRAPH_URLS[CHAINS.LINEA][PROTOCOLS.OVN_REBASE];
const urlData = SUBGRAPH_URLS[CHAINS.LINEA][PROTOCOLS.OVN];

const blockStep = BLOCK_STEP;
const step = (blockNumberTo - blockNumberFrom) / blockStep;
Expand Down Expand Up @@ -278,86 +269,6 @@ export const getRebaseForUsersByPoolAtBlock = async ({
return listNetRebase;
}

// OVN pools
// 0x58aacbccaec30938cb2bb11653cad726e5c4194a usdc/usd+
// 0xc5f4c5c2077bbbac5a8381cf30ecdf18fde42a91 usdt+/usd+
const getPoolsData = async (blockNumber: number, blockTimestamp: number): Promise<CSVRow[]> => {
if (new BN(blockNumber).eq(0)) return [];
let whereQuery = blockNumber ? `where: { blockNumber_lt: ${blockNumber} }` : "";
const poolsData = SUBGRAPH_URLS[CHAINS.LINEA][PROTOCOLS.OVN]

let skip = 0;
let fetchNext = true;

const allPoolsRes = await Promise.all(Object.values(poolsData).map(async (_) => {
const url = _.url
const poolId = _.address
let result: Position[] = [];

while(fetchNext){
let query = `{
deposits(${whereQuery} orderBy: amount, first: 1000,skip: ${skip}) {
id
amount
user
blockNumber
}
}`;

let response = await fetch(url, {
method: "POST",
body: JSON.stringify({ query }),
headers: { "Content-Type": "application/json" },
});
let data = await response.json();

let positions = data.data.deposits;
for (let i = 0; i < positions.length; i++) {
let position = positions[i];
let transformedPosition: Position = {
id: position.id,
liquidity: BigInt(position.amount),
owner: position.user,
address: poolId,
};
result.push(transformedPosition);

}
if(positions.length < 1000){
fetchNext = false;
}else{
skip += 1000;
}
}

return result
}))

const positions = allPoolsRes.flat(1);

console.log("Positions: ", positions.length);
let lpValueByUsers = getLPValueByUserAndPoolFromPositions(positions);
const csvRows: CSVRow[] = [];

lpValueByUsers.forEach((value, key) => {
value.forEach((lpValue) => {
const lpValueStr = lpValue.toString();
// Accumulate CSV row data
csvRows.push({
block_number: blockNumber,
timestamp: blockTimestamp,
user_address: key.toLowerCase(),
token_address: LP_LYNEX,
token_balance: BigInt(lpValueStr),
token_symbol: LP_LYNEX_SYMBOL,
usd_price: 0
});
})
});

return csvRows;
}

// counting rebase by blocks range
// [0, 100, 200] -> gonna be counted like [0, 100] + [100, 200]
const getRebaseData = async (block: number, blockTimestamp: number): Promise<CSVRow[]> => {
Expand Down Expand Up @@ -406,34 +317,7 @@ export const getUserTVLByBlock = async ({
blockNumber,
blockTimestamp,
}: BlockData): Promise<CSVRow[]> => {
const poolsCsv = await getPoolsData(blockNumber, blockTimestamp);
const rebaseCsv = await getRebaseData(blockNumber, blockTimestamp);

return poolsCsv.concat(rebaseCsv);
}

export const getLPValueByUserAndPoolFromPositions = (
positions: Position[]
): Map<string, Map<string, BN>> => {
let result = new Map<string, Map<string, BN>>();
for (let i = 0; i < positions.length; i++) {
let position = positions[i];
let poolId = position.id;
let owner = position.owner;
let userPositions = result.get(owner);
if (userPositions === undefined) {
userPositions = new Map<string, BN>();
result.set(owner, userPositions);
}
let poolPositions = userPositions.get(poolId);
if (poolPositions === undefined) {
poolPositions = BN(0);
}

poolPositions = poolPositions.plus(position.liquidity.toString());
userPositions.set(poolId, poolPositions);
}
return result;
return await getRebaseData(blockNumber, blockTimestamp);
}

export const getTimestampAtBlock = async (blockNumber: number) => {
Expand Down
Loading