From 4197e8355be2728e65ce3a04b99649438fff79d9 Mon Sep 17 00:00:00 2001 From: shashwatS22 <46936675+shashwatS22@users.noreply.github.com> Date: Fri, 15 Sep 2023 11:32:14 +0530 Subject: [PATCH 1/6] added function call for token balance --- subgraphs/level-finance/abis/Pool.json | 9 +++++ .../src/common/constants.mustache | 2 +- .../level-finance/src/modules/transaction.ts | 33 +++++++++++++++---- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/subgraphs/level-finance/abis/Pool.json b/subgraphs/level-finance/abis/Pool.json index 9e6f909950..6da3f0a024 100644 --- a/subgraphs/level-finance/abis/Pool.json +++ b/subgraphs/level-finance/abis/Pool.json @@ -3003,6 +3003,15 @@ "stateMutability": "view", "type": "function" }, + { + "inputs": [ + { "internalType": "address", "name": "token", "type": "address" } + ], + "name": "poolBalances", + "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], + "stateMutability": "view", + "type": "function" + }, { "inputs": [ { diff --git a/subgraphs/level-finance/src/common/constants.mustache b/subgraphs/level-finance/src/common/constants.mustache index f4e0eb2a70..73a0b75962 100644 --- a/subgraphs/level-finance/src/common/constants.mustache +++ b/subgraphs/level-finance/src/common/constants.mustache @@ -203,7 +203,7 @@ export const DEFAULT_DECIMALS_PRECISION = BigInt.fromI32(10).pow(DEFAULT_DECIMALS); export const PRICE_FEED_PRECISION = BigInt.fromI32(10).pow(PRICE_FEED_DECIMALS); - +export const FUNDING_RATE_PRECISION = BigDecimal.fromString("0.00001"); export enum Side { LONG, SHORT, diff --git a/subgraphs/level-finance/src/modules/transaction.ts b/subgraphs/level-finance/src/modules/transaction.ts index 3ba71a558a..987fc56cc6 100644 --- a/subgraphs/level-finance/src/modules/transaction.ts +++ b/subgraphs/level-finance/src/modules/transaction.ts @@ -4,7 +4,14 @@ import * as constants from "../common/constants"; import { SDK } from "../sdk/protocols/perpfutures"; import { Pool } from "../sdk/protocols/perpfutures/pool"; import { getOrCreateAccount } from "../common/initializers"; -import { Address, BigInt, Bytes } from "@graphprotocol/graph-ts"; +import { + Address, + BigInt, + Bytes, + dataSource, + log, +} from "@graphprotocol/graph-ts"; +import { Pool as PoolContract } from "../../generated/Pool/Pool"; import { TransactionType } from "../sdk/protocols/perpfutures/enums"; export function transaction( @@ -19,7 +26,7 @@ export function transaction( ): void { const account = getOrCreateAccount(accountAddress, pool, sdk); const token = sdk.Tokens.getOrCreateToken(tokenAddress); - if (!token._isWhitelisted) return; + if (token._isWhitelisted === false) return; if (transactionType === TransactionType.DEPOSIT) { utils.checkAndUpdateInputTokens(pool, token, amount); @@ -31,11 +38,23 @@ export function transaction( const poolInputTokens = pool.getInputTokens(); const idx = pool.getInputTokens().indexOf(token.id); const inputTokenBalances = pool.pool.inputTokenBalances; - if (transactionType === TransactionType.DEPOSIT) { - inputTokenBalances[idx] = inputTokenBalances[idx].plus(amount); - } - if (transactionType === TransactionType.WITHDRAW) { - inputTokenBalances[idx] = inputTokenBalances[idx].minus(amount); + log.warning("[Network] {}", [dataSource.network()]); + if (dataSource.network() === "arbitrum-one") { + const poolContract = PoolContract.bind( + Address.fromBytes(pool.getBytesID()) + ); + const tokenBalance = utils.readValue( + poolContract.try_poolBalances(Address.fromBytes(token.id)), + constants.BIGINT_ZERO + ); + inputTokenBalances[idx] = tokenBalance; + } else { + if (transactionType === TransactionType.DEPOSIT) { + inputTokenBalances[idx] = inputTokenBalances[idx].plus(amount); + } + if (transactionType === TransactionType.WITHDRAW) { + inputTokenBalances[idx] = inputTokenBalances[idx].minus(amount); + } } const amountsArray = new Array(poolInputTokens.length).fill( constants.BIGINT_ZERO From 6e78130d8ca8cd83134686e3444a071d4f6c3d77 Mon Sep 17 00:00:00 2001 From: shashwatS22 <46936675+shashwatS22@users.noreply.github.com> Date: Fri, 15 Sep 2023 12:43:03 +0530 Subject: [PATCH 2/6] constant for network --- .../level-finance/src/common/constants.mustache | 3 ++- subgraphs/level-finance/src/modules/transaction.ts | 12 +++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/subgraphs/level-finance/src/common/constants.mustache b/subgraphs/level-finance/src/common/constants.mustache index 73a0b75962..75083258df 100644 --- a/subgraphs/level-finance/src/common/constants.mustache +++ b/subgraphs/level-finance/src/common/constants.mustache @@ -217,4 +217,5 @@ export namespace MasterChef { } export const INFLATION_INTERVAL = "TIMESTAMP"; export const STARTING_INFLATION_RATE = BIGINT_ZERO; -export const IS_COLLATERAL_IN_USD = {{isCollateralInUSD}} \ No newline at end of file +export const IS_COLLATERAL_IN_USD = {{isCollateralInUSD}}; +export const NETWORK_ARBITRUM = "arbitrum-one"; \ No newline at end of file diff --git a/subgraphs/level-finance/src/modules/transaction.ts b/subgraphs/level-finance/src/modules/transaction.ts index 987fc56cc6..7280cd405a 100644 --- a/subgraphs/level-finance/src/modules/transaction.ts +++ b/subgraphs/level-finance/src/modules/transaction.ts @@ -4,13 +4,7 @@ import * as constants from "../common/constants"; import { SDK } from "../sdk/protocols/perpfutures"; import { Pool } from "../sdk/protocols/perpfutures/pool"; import { getOrCreateAccount } from "../common/initializers"; -import { - Address, - BigInt, - Bytes, - dataSource, - log, -} from "@graphprotocol/graph-ts"; +import { Address, BigInt, Bytes, dataSource } from "@graphprotocol/graph-ts"; import { Pool as PoolContract } from "../../generated/Pool/Pool"; import { TransactionType } from "../sdk/protocols/perpfutures/enums"; @@ -38,8 +32,8 @@ export function transaction( const poolInputTokens = pool.getInputTokens(); const idx = pool.getInputTokens().indexOf(token.id); const inputTokenBalances = pool.pool.inputTokenBalances; - log.warning("[Network] {}", [dataSource.network()]); - if (dataSource.network() === "arbitrum-one") { + + if (dataSource.network() == constants.NETWORK_ARBITRUM) { const poolContract = PoolContract.bind( Address.fromBytes(pool.getBytesID()) ); From f786de9ba311c14b6562a00e07c18b56569ee4a8 Mon Sep 17 00:00:00 2001 From: shashwatS22 <46936675+shashwatS22@users.noreply.github.com> Date: Fri, 15 Sep 2023 12:59:15 +0530 Subject: [PATCH 3/6] cleanup --- subgraphs/level-finance/abis/Pool.json | 9 +++++++ .../src/common/constants.mustache | 4 +-- subgraphs/level-finance/src/common/utils.ts | 3 +++ .../level-finance/src/modules/transaction.ts | 25 ++++++++++++++----- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/subgraphs/level-finance/abis/Pool.json b/subgraphs/level-finance/abis/Pool.json index 9e6f909950..6da3f0a024 100644 --- a/subgraphs/level-finance/abis/Pool.json +++ b/subgraphs/level-finance/abis/Pool.json @@ -3003,6 +3003,15 @@ "stateMutability": "view", "type": "function" }, + { + "inputs": [ + { "internalType": "address", "name": "token", "type": "address" } + ], + "name": "poolBalances", + "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], + "stateMutability": "view", + "type": "function" + }, { "inputs": [ { diff --git a/subgraphs/level-finance/src/common/constants.mustache b/subgraphs/level-finance/src/common/constants.mustache index f4e0eb2a70..45fad8e275 100644 --- a/subgraphs/level-finance/src/common/constants.mustache +++ b/subgraphs/level-finance/src/common/constants.mustache @@ -203,7 +203,7 @@ export const DEFAULT_DECIMALS_PRECISION = BigInt.fromI32(10).pow(DEFAULT_DECIMALS); export const PRICE_FEED_PRECISION = BigInt.fromI32(10).pow(PRICE_FEED_DECIMALS); - +export const FUNDING_RATE_PRECISION = BigDecimal.fromString("0.00001"); export enum Side { LONG, SHORT, @@ -217,4 +217,4 @@ export namespace MasterChef { } export const INFLATION_INTERVAL = "TIMESTAMP"; export const STARTING_INFLATION_RATE = BIGINT_ZERO; -export const IS_COLLATERAL_IN_USD = {{isCollateralInUSD}} \ No newline at end of file +export const IS_COLLATERAL_IN_USD = {{isCollateralInUSD}}; diff --git a/subgraphs/level-finance/src/common/utils.ts b/subgraphs/level-finance/src/common/utils.ts index 1e0792a262..0eba4430d0 100644 --- a/subgraphs/level-finance/src/common/utils.ts +++ b/subgraphs/level-finance/src/common/utils.ts @@ -15,6 +15,9 @@ import { LpToken as LpTokenContract } from "../../generated/Pool/LpToken"; export function enumToPrefix(snake: string): string { return snake.toLowerCase().replace("_", "-") + "-"; } +export function equalsIgnoreCase(a: string, b: string): boolean { + return a.replace("-", "_").toLowerCase() == b.replace("-", "_").toLowerCase(); +} export function readValue( callResult: ethereum.CallResult, diff --git a/subgraphs/level-finance/src/modules/transaction.ts b/subgraphs/level-finance/src/modules/transaction.ts index 3ba71a558a..44dba6c277 100644 --- a/subgraphs/level-finance/src/modules/transaction.ts +++ b/subgraphs/level-finance/src/modules/transaction.ts @@ -4,7 +4,8 @@ import * as constants from "../common/constants"; import { SDK } from "../sdk/protocols/perpfutures"; import { Pool } from "../sdk/protocols/perpfutures/pool"; import { getOrCreateAccount } from "../common/initializers"; -import { Address, BigInt, Bytes } from "@graphprotocol/graph-ts"; +import { Address, BigInt, Bytes, dataSource } from "@graphprotocol/graph-ts"; +import { Pool as PoolContract } from "../../generated/Pool/Pool"; import { TransactionType } from "../sdk/protocols/perpfutures/enums"; export function transaction( @@ -31,11 +32,23 @@ export function transaction( const poolInputTokens = pool.getInputTokens(); const idx = pool.getInputTokens().indexOf(token.id); const inputTokenBalances = pool.pool.inputTokenBalances; - if (transactionType === TransactionType.DEPOSIT) { - inputTokenBalances[idx] = inputTokenBalances[idx].plus(amount); - } - if (transactionType === TransactionType.WITHDRAW) { - inputTokenBalances[idx] = inputTokenBalances[idx].minus(amount); + const network = dataSource.network(); + if (utils.equalsIgnoreCase(network, constants.Network.ARBITRUM_ONE)) { + const poolContract = PoolContract.bind( + Address.fromBytes(pool.getBytesID()) + ); + const tokenBalance = utils.readValue( + poolContract.try_poolBalances(Address.fromBytes(token.id)), + constants.BIGINT_ZERO + ); + inputTokenBalances[idx] = tokenBalance; + } else { + if (transactionType === TransactionType.DEPOSIT) { + inputTokenBalances[idx] = inputTokenBalances[idx].plus(amount); + } + if (transactionType === TransactionType.WITHDRAW) { + inputTokenBalances[idx] = inputTokenBalances[idx].minus(amount); + } } const amountsArray = new Array(poolInputTokens.length).fill( constants.BIGINT_ZERO From 11ca2cc1e97cfcb711461a3a26b8a71111aa420c Mon Sep 17 00:00:00 2001 From: shashwatS22 <46936675+shashwatS22@users.noreply.github.com> Date: Fri, 15 Sep 2023 13:02:26 +0530 Subject: [PATCH 4/6] token whitelisted check update --- subgraphs/level-finance/src/modules/transaction.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subgraphs/level-finance/src/modules/transaction.ts b/subgraphs/level-finance/src/modules/transaction.ts index 918b4bf472..44dba6c277 100644 --- a/subgraphs/level-finance/src/modules/transaction.ts +++ b/subgraphs/level-finance/src/modules/transaction.ts @@ -20,7 +20,7 @@ export function transaction( ): void { const account = getOrCreateAccount(accountAddress, pool, sdk); const token = sdk.Tokens.getOrCreateToken(tokenAddress); - if (token._isWhitelisted === false) return; + if (!token._isWhitelisted) return; if (transactionType === TransactionType.DEPOSIT) { utils.checkAndUpdateInputTokens(pool, token, amount); From 38f0151069626e9ccfd8feee6353ab12d1f007ec Mon Sep 17 00:00:00 2001 From: shashwatS22 <46936675+shashwatS22@users.noreply.github.com> Date: Sat, 30 Sep 2023 14:11:56 +0530 Subject: [PATCH 5/6] increment subgraph version --- deployment/deployment.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deployment/deployment.json b/deployment/deployment.json index 7ec0d6cdf8..46a646e8a3 100644 --- a/deployment/deployment.json +++ b/deployment/deployment.json @@ -9154,7 +9154,7 @@ "status": "prod", "versions": { "schema": "1.3.1", - "subgraph": "1.0.1", + "subgraph": "1.0.2", "methodology": "1.0.0" }, "services": { From 8d5379d1a49ecfb9bf086dfb4c31ce62d860c275 Mon Sep 17 00:00:00 2001 From: shashwatS22 <46936675+shashwatS22@users.noreply.github.com> Date: Fri, 13 Oct 2023 10:48:56 +0530 Subject: [PATCH 6/6] added token balance from contract --- .../level-finance/src/modules/transaction.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/subgraphs/level-finance/src/modules/transaction.ts b/subgraphs/level-finance/src/modules/transaction.ts index 44dba6c277..0ab5d66f23 100644 --- a/subgraphs/level-finance/src/modules/transaction.ts +++ b/subgraphs/level-finance/src/modules/transaction.ts @@ -33,21 +33,23 @@ export function transaction( const idx = pool.getInputTokens().indexOf(token.id); const inputTokenBalances = pool.pool.inputTokenBalances; const network = dataSource.network(); + const poolContract = PoolContract.bind(Address.fromBytes(pool.getBytesID())); if (utils.equalsIgnoreCase(network, constants.Network.ARBITRUM_ONE)) { - const poolContract = PoolContract.bind( - Address.fromBytes(pool.getBytesID()) - ); const tokenBalance = utils.readValue( poolContract.try_poolBalances(Address.fromBytes(token.id)), constants.BIGINT_ZERO ); inputTokenBalances[idx] = tokenBalance; } else { - if (transactionType === TransactionType.DEPOSIT) { - inputTokenBalances[idx] = inputTokenBalances[idx].plus(amount); - } - if (transactionType === TransactionType.WITHDRAW) { - inputTokenBalances[idx] = inputTokenBalances[idx].minus(amount); + const poolTokenResult = poolContract.try_poolTokens( + Address.fromBytes(token.id) + ); + if (!poolTokenResult.reverted) { + let tokenBalance = poolTokenResult.value.getPoolBalance(); + if (!tokenBalance) { + tokenBalance = constants.BIGINT_ZERO; + } + inputTokenBalances[idx] = tokenBalance; } } const amountsArray = new Array(poolInputTokens.length).fill(