Skip to content

Commit

Permalink
Refactor TVI/TVE with helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
itsjerryokolo committed May 23, 2023
1 parent 81fd39e commit 0a77c5c
Show file tree
Hide file tree
Showing 2 changed files with 241 additions and 370 deletions.
206 changes: 7 additions & 199 deletions subgraphs/hop-protocol/protocols/hop-protocol/src/L2/amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,15 @@ import {
} from '@graphprotocol/graph-ts'
import {
TokenSwap,
L2_Amm,
AddLiquidity,
RemoveLiquidity,
RemoveLiquidityOne,
} from '../../../../generated/HopL2Amm/L2_Amm'
import { Token } from '../../../../generated/schema'
import { getUsdPricePerToken, getUsdPrice } from '../../../../src/prices/index'
import { bigIntToBigDecimal } from '../../../../src/sdk/util/numbers'
import { BIGDECIMAL_ZERO, BIGINT_ONE } from '../../../../src/sdk/util/constants'
import {
BIGINT_TEN_TO_EIGHTEENTH,
BIGINT_TEN_TO_SIX,
SIX_DECIMAL_TOKENS,
} from '../../../../src/common/constants'
import { BIGINT_TEN_TO_EIGHTEENTH } from '../../../../src/common/constants'
import { updateAMMTVE } from '../../../../src/common/tokens'

class Pricer implements TokenPricer {
getTokenPrice(token: Token): BigDecimal {
Expand Down Expand Up @@ -139,54 +134,7 @@ export function handleTokenSwap(event: TokenSwap): void {
)
}

const Amm = L2_Amm.bind(event.address)
const inputBalanceCallA = Amm.try_getTokenBalance(BigInt.zero().toI32())
const inputBalanceCallB = Amm.try_getTokenBalance(BIGINT_ONE.toI32())

if (!inputBalanceCallA.reverted && !inputBalanceCallB.reverted) {
pool.setInputTokenBalance(inputBalanceCallA.value)

hPool.setInputTokenBalance(inputBalanceCallB.value)
hPool.setNetValueExportedUSD(BIGDECIMAL_ZERO)

if (SIX_DECIMAL_TOKENS.includes(tokenOne.id.toHexString())) {
let poolTVE = inputBalanceCallA.value
.plus(inputBalanceCallB.value)
.minus(pool.pool._inputTokenLiquidityBalance!)
pool.setNetValueExportedUSD(
poolTVE.div(BIGINT_TEN_TO_SIX).toBigDecimal()
)
log.warning('TVE1: {}, TVE2: {}, iTB-A: {}, iTB-B: {}, iTLB: {}', [
poolTVE.toString(),
poolTVE
.div(BIGINT_TEN_TO_SIX)
.toBigDecimal()
.toString(),
inputBalanceCallA.value.toString(),
inputBalanceCallB.value.toString(),
pool.pool._inputTokenLiquidityBalance!.toString(),
])
} else {
let poolTVE = inputBalanceCallA.value
.plus(inputBalanceCallB.value)
.minus(pool.pool._inputTokenLiquidityBalance!)
pool.setNetValueExportedUSD(
poolTVE.div(BIGINT_TEN_TO_EIGHTEENTH).toBigDecimal()
)
log.warning('TVE1: {}, TVE2: {}, iTB-A: {}, iTB-B: {}, iTLB: {}', [
poolTVE.toString(),
poolTVE
.div(BIGINT_TEN_TO_EIGHTEENTH)
.toBigDecimal()
.toString(),
inputBalanceCallA.value.toString(),
inputBalanceCallB.value.toString(),
pool.pool._inputTokenLiquidityBalance!.toString(),
])
}
} else {
log.warning('InputBalanceCall Reverted', [])
}
updateAMMTVE(event.address, tokenOne.id, hPool, pool)

pool.pool.relation = hPool.getBytesID()
hPool.pool.relation = hPool.getBytesID()
Expand Down Expand Up @@ -265,54 +213,7 @@ export function handleAddLiquidity(event: AddLiquidity): void {
pool.pool.relation = hPool.getBytesID()
hPool.pool.relation = hPool.getBytesID()

const Amm = L2_Amm.bind(event.address)
const inputBalanceCallA = Amm.try_getTokenBalance(BigInt.zero().toI32())
const inputBalanceCallB = Amm.try_getTokenBalance(BIGINT_ONE.toI32())

if (!inputBalanceCallA.reverted && !inputBalanceCallB.reverted) {
pool.setInputTokenBalance(inputBalanceCallA.value)

hPool.setInputTokenBalance(inputBalanceCallB.value)
hPool.setNetValueExportedUSD(BIGDECIMAL_ZERO)

if (SIX_DECIMAL_TOKENS.includes(token.id.toHexString())) {
let poolTVE = inputBalanceCallA.value
.plus(inputBalanceCallB.value)
.minus(pool.pool._inputTokenLiquidityBalance!)
pool.setNetValueExportedUSD(
poolTVE.div(BIGINT_TEN_TO_SIX).toBigDecimal()
)
log.warning('TVE1: {}, TVE2: {}, iTB-A: {}, iTB-B: {}, iTLB: {}', [
poolTVE.toString(),
poolTVE
.div(BIGINT_TEN_TO_SIX)
.toBigDecimal()
.toString(),
inputBalanceCallA.value.toString(),
inputBalanceCallB.value.toString(),
pool.pool._inputTokenLiquidityBalance!.toString(),
])
} else {
let poolTVE = inputBalanceCallA.value
.plus(inputBalanceCallB.value)
.minus(pool.pool._inputTokenLiquidityBalance!)
pool.setNetValueExportedUSD(
poolTVE.div(BIGINT_TEN_TO_EIGHTEENTH).toBigDecimal()
)
log.warning('TVE1: {}, TVE2: {}, iTB-A: {}, iTB-B: {}, iTLB: {}', [
poolTVE.toString(),
poolTVE
.div(BIGINT_TEN_TO_EIGHTEENTH)
.toBigDecimal()
.toString(),
inputBalanceCallA.value.toString(),
inputBalanceCallB.value.toString(),
pool.pool._inputTokenLiquidityBalance!.toString(),
])
}
} else {
log.warning('InputBalanceCall Reverted', [])
}
updateAMMTVE(event.address, token.id, hPool, pool)

log.warning(
`LA ${token.id.toHexString()} - lpTokenSupply: {}, amount: {}, hash: {}, feeUsd: {}`,
Expand Down Expand Up @@ -383,54 +284,8 @@ export function handleRemoveLiquidity(event: RemoveLiquidity): void {

acc.liquidityWithdraw(pool, liquidity)

const Amm = L2_Amm.bind(event.address)
const inputBalanceCallA = Amm.try_getTokenBalance(BigInt.zero().toI32())
const inputBalanceCallB = Amm.try_getTokenBalance(BIGINT_ONE.toI32())

if (!inputBalanceCallA.reverted && !inputBalanceCallB.reverted) {
pool.setInputTokenBalance(inputBalanceCallA.value)

hPool.setInputTokenBalance(inputBalanceCallB.value)
hPool.setNetValueExportedUSD(BIGDECIMAL_ZERO)

if (SIX_DECIMAL_TOKENS.includes(token.id.toHexString())) {
let poolTVE = inputBalanceCallA.value
.plus(inputBalanceCallB.value)
.minus(pool.pool._inputTokenLiquidityBalance!)
pool.setNetValueExportedUSD(
poolTVE.div(BIGINT_TEN_TO_SIX).toBigDecimal()
)
log.warning('TVE1: {}, TVE2: {}, iTB-A: {}, iTB-B: {}, iTLB: {}', [
poolTVE.toString(),
poolTVE
.div(BIGINT_TEN_TO_SIX)
.toBigDecimal()
.toString(),
inputBalanceCallA.value.toString(),
inputBalanceCallB.value.toString(),
pool.pool._inputTokenLiquidityBalance!.toString(),
])
} else {
let poolTVE = inputBalanceCallA.value
.plus(inputBalanceCallB.value)
.minus(pool.pool._inputTokenLiquidityBalance!)
pool.setNetValueExportedUSD(
poolTVE.div(BIGINT_TEN_TO_EIGHTEENTH).toBigDecimal()
)
log.warning('TVE1: {}, TVE2: {}, iTB-A: {}, iTB-B: {}, iTLB: {}', [
poolTVE.toString(),
poolTVE
.div(BIGINT_TEN_TO_EIGHTEENTH)
.toBigDecimal()
.toString(),
inputBalanceCallA.value.toString(),
inputBalanceCallB.value.toString(),
pool.pool._inputTokenLiquidityBalance!.toString(),
])
}
} else {
log.warning('InputBalanceCall Reverted', [])
}
updateAMMTVE(event.address, token.id, hPool, pool)

pool.pool.relation = hPool.getBytesID()
hPool.pool.relation = hPool.getBytesID()

Expand Down Expand Up @@ -513,54 +368,7 @@ export function handleRemoveLiquidityOne(event: RemoveLiquidityOne): void {

acc.liquidityWithdraw(pool, amount.div(BIGINT_TEN_TO_EIGHTEENTH))

const Amm = L2_Amm.bind(event.address)
const inputBalanceCallA = Amm.try_getTokenBalance(BigInt.zero().toI32())
const inputBalanceCallB = Amm.try_getTokenBalance(BIGINT_ONE.toI32())

if (!inputBalanceCallA.reverted && !inputBalanceCallB.reverted) {
pool.setInputTokenBalance(inputBalanceCallA.value)

hPool.setInputTokenBalance(inputBalanceCallB.value)
hPool.setNetValueExportedUSD(BIGDECIMAL_ZERO)

if (SIX_DECIMAL_TOKENS.includes(token.id.toHexString())) {
let poolTVE = inputBalanceCallA.value
.plus(inputBalanceCallB.value)
.minus(pool.pool._inputTokenLiquidityBalance!)
pool.setNetValueExportedUSD(
poolTVE.div(BIGINT_TEN_TO_SIX).toBigDecimal()
)
log.warning('TVE1: {}, TVE2: {}, iTB-A: {}, iTB-B: {}, iTLB: {}', [
poolTVE.toString(),
poolTVE
.div(BIGINT_TEN_TO_SIX)
.toBigDecimal()
.toString(),
inputBalanceCallA.value.toString(),
inputBalanceCallB.value.toString(),
pool.pool._inputTokenLiquidityBalance!.toString(),
])
} else {
let poolTVE = inputBalanceCallA.value
.plus(inputBalanceCallB.value)
.minus(pool.pool._inputTokenLiquidityBalance!)
pool.setNetValueExportedUSD(
poolTVE.div(BIGINT_TEN_TO_EIGHTEENTH).toBigDecimal()
)
log.warning('TVE1: {}, TVE2: {}, iTB-A: {}, iTB-B: {}, iTLB: {}', [
poolTVE.toString(),
poolTVE
.div(BIGINT_TEN_TO_EIGHTEENTH)
.toBigDecimal()
.toString(),
inputBalanceCallA.value.toString(),
inputBalanceCallB.value.toString(),
pool.pool._inputTokenLiquidityBalance!.toString(),
])
}
} else {
log.warning('InputBalanceCall Reverted', [])
}
updateAMMTVE(event.address, token.id, hPool, pool)

pool.pool.relation = hPool.getBytesID()
hPool.pool.relation = hPool.getBytesID()
Expand Down
Loading

0 comments on commit 0a77c5c

Please sign in to comment.