Skip to content

Commit

Permalink
Use last_prices to compute pool spotPrice
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanchambras committed May 24, 2024
1 parent e86ff9b commit fad3928
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/entities/CurvePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ export const getPoolPriceScale = (poolAddress: Address): BigInt => {
return ZERO_BI
}

export const getPoolLastPrices = (poolAddress: Address): BigInt => {
let curvePoolContract = CurvePool.bind(poolAddress)

let lastPricesCall = curvePoolContract.try_last_prices()

if (!lastPricesCall.reverted) {
return lastPricesCall.value
}

log.warning("last_prices() call reverted for {}", [poolAddress.toHex()])

return ZERO_BI
}

export const getPoolCoins = (poolAddress: Address): Address[] => {
let curvePoolContract = CurvePool.bind(poolAddress)

Expand Down
6 changes: 2 additions & 4 deletions src/entities/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import {
getPoolAdminFee,
getPoolFee,
getPoolFutureAdminFee,
getPoolLPToken,
getPoolPriceScale,
getPoolLastPrices,
} from "./CurvePool"
import { getERC20TotalSupply } from "./ERC20"
import { getCurveFactory } from "./Factory"

class PoolDetails {
Expand Down Expand Up @@ -101,7 +99,7 @@ export function createPool(params: PoolDetails): Pool {
}
}

let spotPrice = getPoolPriceScale(params.poolAddress)
let spotPrice = getPoolLastPrices(params.poolAddress)
if (pool.futureVault) {
let poolAPY = createAPYInTimeForPool(
params.poolAddress,
Expand Down
6 changes: 3 additions & 3 deletions src/mappings/amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getAccount } from "../entities/Account"
import { updateAccountAssetBalance } from "../entities/AccountAsset"
import { getAsset } from "../entities/Asset"
import { getAssetAmount } from "../entities/AssetAmount"
import { getPoolPriceScale, getPoolLPToken } from "../entities/CurvePool"
import { getPoolLastPrices, getPoolLPToken } from "../entities/CurvePool"
import { getERC20Decimals, getERC20TotalSupply } from "../entities/ERC20"
import { updateFutureDailyStats } from "../entities/FutureDailyStats"
import { createTransaction } from "../entities/Transaction"
Expand Down Expand Up @@ -419,7 +419,7 @@ export function handleTokenExchange(event: TokenExchange): void {
pool.totalFees = pool.totalFees.plus(fee)
pool.totalAdminFees = pool.totalAdminFees.plus(adminFee)

let spotPrice = getPoolPriceScale(event.address)
let spotPrice = getPoolLastPrices(event.address)
pool.spotPrice = spotPrice

pool.save()
Expand Down Expand Up @@ -583,7 +583,7 @@ export function handleRemoveLiquidityOne(event: RemoveLiquidityOne): void {
const lpTotalSupply = getERC20TotalSupply(lpTokenAddress)
pool.lpTotalSupply = lpTotalSupply

let spotPrice = getPoolPriceScale(event.address)
let spotPrice = getPoolLastPrices(event.address)
pool.spotPrice = spotPrice

pool.save()
Expand Down
9 changes: 9 additions & 0 deletions src/tests/mocks/CurvePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ const createPriceScaleCallMock = (addressMock: Address): void => {
).returns([ethereum.Value.fromSignedBigInt(POOL_PRICE_SCALE_MOCK)])
}

const createLastPricesCallMock = (addressMock: Address): void => {
createMockedFunction(
addressMock,
"last_prices",
"last_prices():(uint256)"
).returns([ethereum.Value.fromSignedBigInt(POOL_PRICE_SCALE_MOCK)])
}

const createCoinsCallMock = (addressMock: Address): void => {
createMockedFunction(addressMock, "coins", "coins(uint256):(address)")
.withArgs([ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(0))])
Expand Down Expand Up @@ -153,6 +161,7 @@ export function mockCurvePoolFunctions(): void {
createFutureAdminFeeCallMock(addressMock)
createFutureAdminFeeChangeDeadlineCallMock(addressMock)
createPriceScaleCallMock(addressMock)
createLastPricesCallMock(addressMock)
createCoinsCallMock(addressMock)
}
)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/calculateAPY.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Address, BigDecimal, BigInt, log } from "@graphprotocol/graph-ts"
import { LPVault } from "../../generated/schema"
import { SECONDS_PER_YEAR, ZERO_BD } from "../constants"
import { createAPYInTimeForPool } from "../entities/APYInTime"
import { getPoolPriceScale } from "../entities/CurvePool"
import { getPoolLastPrices } from "../entities/CurvePool"
import {
getIBTRate,
getPTRate,
Expand All @@ -27,7 +27,7 @@ export function updatePoolAPY(

const expirationTimestamp = getExpirationTimestamp(principalToken)
const timeLeft = expirationTimestamp.minus(currentTimestamp)
const spotPrice = getPoolPriceScale(poolAddress)
const spotPrice = getPoolLastPrices(poolAddress)

poolAPY.spotPrice = spotPrice
const ibtRate = getIBTRate(principalToken)
Expand Down

0 comments on commit fad3928

Please sign in to comment.