diff --git a/src/entities/CurvePool.ts b/src/entities/CurvePool.ts index f7d3384..d481e35 100644 --- a/src/entities/CurvePool.ts +++ b/src/entities/CurvePool.ts @@ -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) diff --git a/src/entities/Pool.ts b/src/entities/Pool.ts index e84b016..0b0842f 100644 --- a/src/entities/Pool.ts +++ b/src/entities/Pool.ts @@ -10,10 +10,8 @@ import { getPoolAdminFee, getPoolFee, getPoolFutureAdminFee, - getPoolLPToken, - getPoolPriceScale, + getPoolLastPrices, } from "./CurvePool" -import { getERC20TotalSupply } from "./ERC20" import { getCurveFactory } from "./Factory" class PoolDetails { @@ -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, diff --git a/src/mappings/amm.ts b/src/mappings/amm.ts index c84b248..d7feb5b 100644 --- a/src/mappings/amm.ts +++ b/src/mappings/amm.ts @@ -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" @@ -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() @@ -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() diff --git a/src/tests/mocks/CurvePool.ts b/src/tests/mocks/CurvePool.ts index 0cc8b5f..c808aba 100644 --- a/src/tests/mocks/CurvePool.ts +++ b/src/tests/mocks/CurvePool.ts @@ -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))]) @@ -153,6 +161,7 @@ export function mockCurvePoolFunctions(): void { createFutureAdminFeeCallMock(addressMock) createFutureAdminFeeChangeDeadlineCallMock(addressMock) createPriceScaleCallMock(addressMock) + createLastPricesCallMock(addressMock) createCoinsCallMock(addressMock) } ) diff --git a/src/utils/calculateAPY.ts b/src/utils/calculateAPY.ts index baf0b23..def1615 100644 --- a/src/utils/calculateAPY.ts +++ b/src/utils/calculateAPY.ts @@ -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, @@ -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)