From 6097de2ecf16e5b1783de414fdb90dd6bcb2fff3 Mon Sep 17 00:00:00 2001 From: Michael Wang <44713145+mzywang@users.noreply.github.com> Date: Wed, 22 May 2024 15:52:09 -0400 Subject: [PATCH] add unit tests for initialize --- src/mappings/pool/initialize.ts | 28 ++++- src/utils/pricing.ts | 40 +++--- tests/constants.ts | 77 +++++++++++- tests/handleBurn.test.ts | 4 +- tests/handleCollect.test.ts | 14 ++- tests/handleInitialize.test.ts | 215 ++++++++++++++++++++++++++++++++ tests/handleMint.test.ts | 4 +- tests/handlePoolCreated.test.ts | 4 +- tests/intervalUpdates.test.ts | 77 ++++-------- 9 files changed, 380 insertions(+), 83 deletions(-) create mode 100644 tests/handleInitialize.test.ts diff --git a/src/mappings/pool/initialize.ts b/src/mappings/pool/initialize.ts index 9e2cc3be..da0e17c9 100644 --- a/src/mappings/pool/initialize.ts +++ b/src/mappings/pool/initialize.ts @@ -1,11 +1,29 @@ -import { BigInt } from '@graphprotocol/graph-ts' +import { BigDecimal, BigInt } from '@graphprotocol/graph-ts' import { Bundle, Pool, Token } from '../../types/schema' import { Initialize } from '../../types/templates/Pool/Pool' import { updatePoolDayData, updatePoolHourData } from '../../utils/intervalUpdates' -import { findEthPerToken, getEthPriceInUSD } from '../../utils/pricing' +import { + MINIMUM_ETH_LOCKED, + STABLE_COINS, + USDC_WETH_03_POOL, + WETH_ADDRESS, + findEthPerToken, + getEthPriceInUSD, +} from '../../utils/pricing' export function handleInitialize(event: Initialize): void { + handleInitializeHelper(event) +} + +export function handleInitializeHelper( + event: Initialize, + stablecoinWrappedNativePoolAddress: string = USDC_WETH_03_POOL, + stablecoinIsToken0: boolean = true, // true is stablecoin is token0, false if stablecoin is token1 + wrappedNativeAddress: string = WETH_ADDRESS, + stablecoinAddresses: string[] = STABLE_COINS, + minimumEthLocked: BigDecimal = MINIMUM_ETH_LOCKED, +): void { // update pool sqrt price and tick const pool = Pool.load(event.address.toHexString())! pool.sqrtPrice = event.params.sqrtPriceX96 @@ -18,7 +36,7 @@ export function handleInitialize(event: Initialize): void { // update ETH price now that prices could have changed const bundle = Bundle.load('1')! - bundle.ethPriceUSD = getEthPriceInUSD() + bundle.ethPriceUSD = getEthPriceInUSD(stablecoinWrappedNativePoolAddress, stablecoinIsToken0) bundle.save() updatePoolDayData(event) @@ -26,8 +44,8 @@ export function handleInitialize(event: Initialize): void { // update token prices if (token0 && token1) { - token0.derivedETH = findEthPerToken(token0 as Token) - token1.derivedETH = findEthPerToken(token1 as Token) + token0.derivedETH = findEthPerToken(token0 as Token, wrappedNativeAddress, stablecoinAddresses, minimumEthLocked) + token1.derivedETH = findEthPerToken(token1 as Token, wrappedNativeAddress, stablecoinAddresses, minimumEthLocked) token0.save() token1.save() } diff --git a/src/utils/pricing.ts b/src/utils/pricing.ts index 1ec27745..96f2243e 100644 --- a/src/utils/pricing.ts +++ b/src/utils/pricing.ts @@ -4,8 +4,8 @@ import { exponentToBigDecimal, safeDiv } from '../utils/index' import { Bundle, Pool, Token } from './../types/schema' import { ONE_BD, ZERO_BD, ZERO_BI } from './constants' -const WETH_ADDRESS = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' -const USDC_WETH_03_POOL = '0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8' +export const WETH_ADDRESS = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' +export const USDC_WETH_03_POOL = '0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8' // token where amounts should contribute to tracked volume and liquidity // usually tokens that many tokens are paired with s @@ -33,7 +33,7 @@ export const WHITELIST_TOKENS: string[] = [ '0xfe2e637202056d30016725477c5da089ab0a043a', // sETH2 ] -const STABLE_COINS: string[] = [ +export const STABLE_COINS: string[] = [ '0x6b175474e89094c44da98b954eedeac495271d0f', '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', '0xdac17f958d2ee523a2206206994597c13d831ec7', @@ -42,23 +42,28 @@ const STABLE_COINS: string[] = [ '0x4dd28568d05f09b02220b09c2cb307bfd837cb95', ] -const MINIMUM_ETH_LOCKED = BigDecimal.fromString('60') +export const MINIMUM_ETH_LOCKED = BigDecimal.fromString('60') const Q192 = BigInt.fromI32(2).pow(192 as u8) export function sqrtPriceX96ToTokenPrices(sqrtPriceX96: BigInt, token0: Token, token1: Token): BigDecimal[] { const num = sqrtPriceX96.times(sqrtPriceX96).toBigDecimal() const denom = BigDecimal.fromString(Q192.toString()) - const price1 = num.div(denom).times(exponentToBigDecimal(token0.decimals)).div(exponentToBigDecimal(token1.decimals)) + const price1 = num + .div(denom) + .times(exponentToBigDecimal(token0.decimals)) + .div(exponentToBigDecimal(token1.decimals)) const price0 = safeDiv(BigDecimal.fromString('1'), price1) return [price0, price1] } -export function getEthPriceInUSD(): BigDecimal { - // fetch eth prices for each stablecoin - const usdcPool = Pool.load(USDC_WETH_03_POOL) // dai is token0 - if (usdcPool !== null) { - return usdcPool.token0Price +export function getEthPriceInUSD( + stablecoinWrappedNativePoolAddress: string = USDC_WETH_03_POOL, + stablecoinIsToken0: boolean = true, // true is stablecoin is token0, false if stablecoin is token1 +): BigDecimal { + const stablecoinWrappedNativePool = Pool.load(stablecoinWrappedNativePoolAddress) + if (stablecoinWrappedNativePool !== null) { + return stablecoinIsToken0 ? stablecoinWrappedNativePool.token0Price : stablecoinWrappedNativePool.token1Price } else { return ZERO_BD } @@ -68,8 +73,13 @@ export function getEthPriceInUSD(): BigDecimal { * Search through graph to find derived Eth per token. * @todo update to be derived ETH (add stablecoin estimates) **/ -export function findEthPerToken(token: Token): BigDecimal { - if (token.id == WETH_ADDRESS) { +export function findEthPerToken( + token: Token, + wrappedNativeAddress: string = WETH_ADDRESS, + stablecoinAddresses: string[] = STABLE_COINS, + minimumEthLocked: BigDecimal = MINIMUM_ETH_LOCKED, +): BigDecimal { + if (token.id == wrappedNativeAddress) { return ONE_BD } const whiteList = token.whitelistPools @@ -81,7 +91,7 @@ export function findEthPerToken(token: Token): BigDecimal { // hardcoded fix for incorrect rates // if whitelist includes token - get the safe price - if (STABLE_COINS.includes(token.id)) { + if (stablecoinAddresses.includes(token.id)) { priceSoFar = safeDiv(ONE_BD, bundle.ethPriceUSD) } else { for (let i = 0; i < whiteList.length; ++i) { @@ -96,7 +106,7 @@ export function findEthPerToken(token: Token): BigDecimal { // get the derived ETH in pool if (token1) { const ethLocked = pool.totalValueLockedToken1.times(token1.derivedETH) - if (ethLocked.gt(largestLiquidityETH) && ethLocked.gt(MINIMUM_ETH_LOCKED)) { + if (ethLocked.gt(largestLiquidityETH) && ethLocked.gt(minimumEthLocked)) { largestLiquidityETH = ethLocked // token1 per our token * Eth per token1 priceSoFar = pool.token1Price.times(token1.derivedETH as BigDecimal) @@ -108,7 +118,7 @@ export function findEthPerToken(token: Token): BigDecimal { // get the derived ETH in pool if (token0) { const ethLocked = pool.totalValueLockedToken0.times(token0.derivedETH) - if (ethLocked.gt(largestLiquidityETH) && ethLocked.gt(MINIMUM_ETH_LOCKED)) { + if (ethLocked.gt(largestLiquidityETH) && ethLocked.gt(minimumEthLocked)) { largestLiquidityETH = ethLocked // token0 per our token * ETH per token0 priceSoFar = pool.token0Price.times(token0.derivedETH as BigDecimal) diff --git a/tests/constants.ts b/tests/constants.ts index 39e45c6b..4993604a 100644 --- a/tests/constants.ts +++ b/tests/constants.ts @@ -3,10 +3,14 @@ import { assert, createMockedFunction, newMockEvent } from 'matchstick-as' import { handlePoolCreatedHelper } from '../src/mappings/factory' import { PoolCreated } from '../src/types/Factory/Factory' +import { Pool, Token } from '../src/types/schema' +import { ZERO_BD, ZERO_BI } from '../src/utils/constants' const USDC_MAINNET_ADDRESS = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' const WETH_MAINNET_ADDRESS = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' +const WBTC_MAINNET_ADDRESS = '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599' export const USDC_WETH_03_MAINNET_POOL = '0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8' +export const WBTC_WETH_03_MAINNET_POOL = '0xcbcdf9626bc03e24f779434178a73a0b4bad62ed' export const POOL_FEE_TIER_03 = 3000 export const POOL_TICK_SPACING_03 = 60 @@ -34,13 +38,21 @@ export const WETH_MAINNET_FIXTURE: TokenFixture = { decimals: '18', } +export const WBTC_MAINNET_FIXTURE: TokenFixture = { + address: WBTC_MAINNET_ADDRESS, + symbol: 'WBTC', + name: 'Wrapped Bitcoin', + totalSupply: '200', + decimals: '8', +} + export const TEST_ETH_PRICE_USD = BigDecimal.fromString('2000') export const TEST_USDC_DERIVED_ETH = BigDecimal.fromString('1').div(BigDecimal.fromString('2000')) export const TEST_WETH_DERIVED_ETH = BigDecimal.fromString('1') export const MOCK_EVENT = newMockEvent() -export const createTestPool = ( +export const invokePoolCreatedWithMockedEthCalls = ( mockEvent: ethereum.Event, factoryAddress: string, token0: TokenFixture, @@ -91,6 +103,69 @@ export const createTestPool = ( handlePoolCreatedHelper(poolCreatedEvent, factoryAddress, [token0.address, token1.address]) } +// More lightweight than the method above which invokes handlePoolCreated. This +// method only creates the pool entity while the above method also creates the +// relevant token and factory entities. +export const createAndStoreTestPool = ( + poolAddress: string, + token0Address: string, + token1Address: string, + feeTier: i32, +): Pool => { + const pool = new Pool(poolAddress) + pool.createdAtTimestamp = ZERO_BI + pool.createdAtBlockNumber = ZERO_BI + pool.token0 = token0Address + pool.token1 = token1Address + pool.feeTier = BigInt.fromI32(feeTier) + pool.liquidity = ZERO_BI + pool.sqrtPrice = ZERO_BI + pool.token0Price = ZERO_BD + pool.token1Price = ZERO_BD + pool.tick = ZERO_BI + pool.observationIndex = ZERO_BI + pool.volumeToken0 = ZERO_BD + pool.volumeToken1 = ZERO_BD + pool.volumeUSD = ZERO_BD + pool.untrackedVolumeUSD = ZERO_BD + pool.feesUSD = ZERO_BD + pool.txCount = ZERO_BI + pool.collectedFeesToken0 = ZERO_BD + pool.collectedFeesToken1 = ZERO_BD + pool.collectedFeesUSD = ZERO_BD + pool.totalValueLockedToken0 = ZERO_BD + pool.totalValueLockedToken1 = ZERO_BD + pool.totalValueLockedUSD = ZERO_BD + pool.totalValueLockedETH = ZERO_BD + pool.totalValueLockedUSDUntracked = ZERO_BD + pool.liquidityProviderCount = ZERO_BI + + pool.save() + return pool +} + +export const createAndStoreTestToken = (tokenFixture: TokenFixture): Token => { + const token = new Token(tokenFixture.address) + token.symbol = tokenFixture.symbol + token.name = tokenFixture.name + token.decimals = BigInt.fromString(tokenFixture.decimals) + token.totalSupply = BigInt.fromString(tokenFixture.totalSupply) + token.volume = ZERO_BD + token.volumeUSD = ZERO_BD + token.untrackedVolumeUSD = ZERO_BD + token.feesUSD = ZERO_BD + token.txCount = ZERO_BI + token.poolCount = ZERO_BI + token.totalValueLocked = ZERO_BD + token.totalValueLockedUSD = ZERO_BD + token.totalValueLockedUSDUntracked = ZERO_BD + token.derivedETH = ZERO_BD + token.whitelistPools = [] + + token.save() + return token +} + // Typescript for Subgraphs do not support Record types so we use a 2D string array to represent the object instead. export const assertObjectMatches = (entityType: string, id: string, obj: string[][]): void => { for (let i = 0; i < obj.length; i++) { diff --git a/tests/handleBurn.test.ts b/tests/handleBurn.test.ts index 89ddb76d..618d8c55 100644 --- a/tests/handleBurn.test.ts +++ b/tests/handleBurn.test.ts @@ -8,7 +8,7 @@ import { convertTokenToDecimal, fastExponentiation, safeDiv } from '../src/utils import { FACTORY_ADDRESS, ONE_BD, ZERO_BI } from '../src/utils/constants' import { assertObjectMatches, - createTestPool, + invokePoolCreatedWithMockedEthCalls, MOCK_EVENT, POOL_FEE_TIER_03, POOL_TICK_SPACING_03, @@ -59,7 +59,7 @@ const BURN_EVENT = new Burn( describe('handleBurn', () => { beforeAll(() => { - createTestPool( + invokePoolCreatedWithMockedEthCalls( MOCK_EVENT, FACTORY_ADDRESS, USDC_MAINNET_FIXTURE, diff --git a/tests/handleCollect.test.ts b/tests/handleCollect.test.ts index 92f31db5..f61ef015 100644 --- a/tests/handleCollect.test.ts +++ b/tests/handleCollect.test.ts @@ -8,7 +8,7 @@ import { convertTokenToDecimal } from '../src/utils' import { FACTORY_ADDRESS, ZERO_BD } from '../src/utils/constants' import { assertObjectMatches, - createTestPool, + invokePoolCreatedWithMockedEthCalls, MOCK_EVENT, POOL_FEE_TIER_03, POOL_TICK_SPACING_03, @@ -59,7 +59,7 @@ const COLLECT_EVENT = new Collect( describe('handleMint', () => { beforeAll(() => { - createTestPool( + invokePoolCreatedWithMockedEthCalls( MOCK_EVENT, FACTORY_ADDRESS, USDC_MAINNET_FIXTURE, @@ -123,7 +123,10 @@ describe('handleMint', () => { ['totalValueLocked', collectedAmountToken0.neg().toString()], [ 'totalValueLockedUSD', - collectedAmountToken0.times(TEST_USDC_DERIVED_ETH.times(TEST_ETH_PRICE_USD)).neg().toString(), + collectedAmountToken0 + .times(TEST_USDC_DERIVED_ETH.times(TEST_ETH_PRICE_USD)) + .neg() + .toString(), ], ]) @@ -132,7 +135,10 @@ describe('handleMint', () => { ['totalValueLocked', collectedAmountToken1.neg().toString()], [ 'totalValueLockedUSD', - collectedAmountToken1.times(TEST_WETH_DERIVED_ETH.times(TEST_ETH_PRICE_USD)).neg().toString(), + collectedAmountToken1 + .times(TEST_WETH_DERIVED_ETH.times(TEST_ETH_PRICE_USD)) + .neg() + .toString(), ], ]) diff --git a/tests/handleInitialize.test.ts b/tests/handleInitialize.test.ts new file mode 100644 index 00000000..e976a10b --- /dev/null +++ b/tests/handleInitialize.test.ts @@ -0,0 +1,215 @@ +import { assert, beforeAll, beforeEach, clearStore, describe, test } from 'matchstick-as' +import { + MOCK_EVENT, + POOL_FEE_TIER_03, + TEST_ETH_PRICE_USD, + USDC_MAINNET_FIXTURE, + USDC_WETH_03_MAINNET_POOL, + WBTC_MAINNET_FIXTURE, + WBTC_WETH_03_MAINNET_POOL, + WETH_MAINNET_FIXTURE, + assertObjectMatches, + createAndStoreTestPool, + createAndStoreTestToken, +} from './constants' +import { Address, BigDecimal, BigInt, ethereum } from '@graphprotocol/graph-ts' +import { findEthPerToken, getEthPriceInUSD } from '../src/utils/pricing' +import { ADDRESS_ZERO, ZERO_BD } from '../src/utils/constants' +import { Bundle, Pool, Token } from '../src/types/schema' +import { safeDiv } from '../src/utils' +import { handleInitializeHelper } from '../src/mappings/pool/initialize' +import { Initialize } from '../src/types/templates/Pool/Pool' + +class InitializeFixture { + sqrtPriceX96: BigInt + tick: i32 +} + +const INITIALIZE_FIXTURE: InitializeFixture = { + sqrtPriceX96: BigInt.fromString('1111111111111111'), + tick: 194280, +} + +const INITIALIZE_EVENT = new Initialize( + Address.fromString(USDC_WETH_03_MAINNET_POOL), + MOCK_EVENT.logIndex, + MOCK_EVENT.transactionLogIndex, + MOCK_EVENT.logType, + MOCK_EVENT.block, + MOCK_EVENT.transaction, + [ + new ethereum.EventParam('sqrtPriceX96', ethereum.Value.fromUnsignedBigInt(INITIALIZE_FIXTURE.sqrtPriceX96)), + new ethereum.EventParam('tick', ethereum.Value.fromI32(INITIALIZE_FIXTURE.tick)), + ], + MOCK_EVENT.receipt, +) + +describe('handleInitialize', () => { + test('success', () => { + const pool = createAndStoreTestPool( + USDC_WETH_03_MAINNET_POOL, + USDC_MAINNET_FIXTURE.address, + WETH_MAINNET_FIXTURE.address, + POOL_FEE_TIER_03, + ) + + const token0 = createAndStoreTestToken(USDC_MAINNET_FIXTURE) + const token1 = createAndStoreTestToken(WETH_MAINNET_FIXTURE) + + const bundle = new Bundle('1') + bundle.ethPriceUSD = TEST_ETH_PRICE_USD + bundle.save() + + const stablecoinWrappedNativePoolAddress = USDC_WETH_03_MAINNET_POOL + const stablecoinIsToken0 = true + const wrappedNativeAddress = WETH_MAINNET_FIXTURE.address + const stablecoinAddresses = [USDC_MAINNET_FIXTURE.address] + const minimumEthLocked = ZERO_BD + + handleInitializeHelper( + INITIALIZE_EVENT, + stablecoinWrappedNativePoolAddress, + stablecoinIsToken0, + wrappedNativeAddress, + stablecoinAddresses, + minimumEthLocked, + ) + + assertObjectMatches('Pool', USDC_WETH_03_MAINNET_POOL, [ + ['sqrtPrice', INITIALIZE_FIXTURE.sqrtPriceX96.toString()], + ['tick', INITIALIZE_FIXTURE.tick.toString()], + ]) + + const expectedEthPrice = getEthPriceInUSD(USDC_WETH_03_MAINNET_POOL, true) + assertObjectMatches('Bundle', '1', [['ethPriceUSD', expectedEthPrice.toString()]]) + + const expectedToken0Price = findEthPerToken( + token0 as Token, + wrappedNativeAddress, + stablecoinAddresses, + minimumEthLocked, + ) + assertObjectMatches('Token', USDC_MAINNET_FIXTURE.address, [['derivedETH', expectedToken0Price.toString()]]) + + const expectedToken1Price = findEthPerToken( + token1 as Token, + wrappedNativeAddress, + stablecoinAddresses, + minimumEthLocked, + ) + assertObjectMatches('Token', WETH_MAINNET_FIXTURE.address, [['derivedETH', expectedToken1Price.toString()]]) + }) +}) + +describe('getEthPriceInUSD', () => { + beforeEach(() => { + clearStore() + const pool = createAndStoreTestPool( + USDC_WETH_03_MAINNET_POOL, + USDC_MAINNET_FIXTURE.address, + WETH_MAINNET_FIXTURE.address, + POOL_FEE_TIER_03, + ) + }) + + test('success - stablecoin is token0', () => { + const pool = Pool.load(USDC_WETH_03_MAINNET_POOL)! + pool.token0Price = BigDecimal.fromString('1') + pool.save() + + const ethPriceUSD = getEthPriceInUSD(USDC_WETH_03_MAINNET_POOL, true) + + assert.assertTrue(ethPriceUSD == BigDecimal.fromString('1')) + }) + + test('success - stablecoin is token1', () => { + const pool = Pool.load(USDC_WETH_03_MAINNET_POOL)! + pool.token1Price = BigDecimal.fromString('1') + pool.save() + + const ethPriceUSD = getEthPriceInUSD(USDC_WETH_03_MAINNET_POOL, false) + + assert.assertTrue(ethPriceUSD == BigDecimal.fromString('1')) + }) + + test('failure - pool not found', () => { + const pool = Pool.load(USDC_WETH_03_MAINNET_POOL)! + pool.token0Price = BigDecimal.fromString('1') + pool.token1Price = BigDecimal.fromString('1') + pool.save() + + const ethPriceUSD = getEthPriceInUSD(ADDRESS_ZERO) + assert.assertTrue(ethPriceUSD == BigDecimal.fromString('0')) + }) +}) + +describe('findEthPerToken', () => { + beforeEach(() => { + clearStore() + + const bundle = new Bundle('1') + bundle.ethPriceUSD = TEST_ETH_PRICE_USD + bundle.save() + }) + + test('success - token is wrapped native', () => { + const token = createAndStoreTestToken(WETH_MAINNET_FIXTURE) + const ethPerToken = findEthPerToken(token as Token, WETH_MAINNET_FIXTURE.address) + assert.assertTrue(ethPerToken == BigDecimal.fromString('1')) + }) + + test('success - token is stablecoin', () => { + const token = createAndStoreTestToken(USDC_MAINNET_FIXTURE) + const ethPerToken = findEthPerToken(token as Token, WETH_MAINNET_FIXTURE.address, [USDC_MAINNET_FIXTURE.address]) + const expectedStablecoinPrice = safeDiv(BigDecimal.fromString('1'), TEST_ETH_PRICE_USD) + assert.assertTrue(ethPerToken == expectedStablecoinPrice) + }) + + test('success - token is not wrapped native or stablecoin', () => { + const pool = createAndStoreTestPool( + WBTC_WETH_03_MAINNET_POOL, + WBTC_MAINNET_FIXTURE.address, + WETH_MAINNET_FIXTURE.address, + POOL_FEE_TIER_03, + ) + + const minimumEthLocked = BigDecimal.fromString('0') + + pool.liquidity = BigInt.fromString('100') + pool.totalValueLockedToken1 = BigDecimal.fromString('100') + pool.token1Price = BigDecimal.fromString('5') + pool.save() + + const token0 = createAndStoreTestToken(WBTC_MAINNET_FIXTURE) + token0.whitelistPools = [WBTC_WETH_03_MAINNET_POOL] + token0.save() + + const token1 = createAndStoreTestToken(WETH_MAINNET_FIXTURE) + token1.derivedETH = BigDecimal.fromString('10') + token1.save() + + const ethPerToken = findEthPerToken( + token0 as Token, + WETH_MAINNET_FIXTURE.address, + [USDC_MAINNET_FIXTURE.address], + minimumEthLocked, + ) + + assert.assertTrue(ethPerToken == BigDecimal.fromString('50')) + }) + + test('success - token is not wrapped native or stablecoin, but has no pools', () => { + const token0 = createAndStoreTestToken(WBTC_MAINNET_FIXTURE) + const ethPerToken = findEthPerToken(token0 as Token) + assert.assertTrue(ethPerToken == BigDecimal.fromString('0')) + }) + + test('success - token is not wrapped native or stablecoin, but has no pools with liquidity', () => { + const token0 = createAndStoreTestToken(WBTC_MAINNET_FIXTURE) + token0.whitelistPools = [WBTC_WETH_03_MAINNET_POOL] + token0.save() + + const ethPerToken = findEthPerToken(token0 as Token) + assert.assertTrue(ethPerToken == BigDecimal.fromString('0')) + }) +}) diff --git a/tests/handleMint.test.ts b/tests/handleMint.test.ts index 195f0316..4248787c 100644 --- a/tests/handleMint.test.ts +++ b/tests/handleMint.test.ts @@ -8,7 +8,7 @@ import { convertTokenToDecimal, fastExponentiation, safeDiv } from '../src/utils import { FACTORY_ADDRESS, ONE_BD } from '../src/utils/constants' import { assertObjectMatches, - createTestPool, + invokePoolCreatedWithMockedEthCalls, MOCK_EVENT, POOL_FEE_TIER_03, POOL_TICK_SPACING_03, @@ -62,7 +62,7 @@ const MINT_EVENT = new Mint( describe('handleMint', () => { beforeAll(() => { - createTestPool( + invokePoolCreatedWithMockedEthCalls( MOCK_EVENT, FACTORY_ADDRESS, USDC_MAINNET_FIXTURE, diff --git a/tests/handlePoolCreated.test.ts b/tests/handlePoolCreated.test.ts index 801a67f6..74fad2d4 100644 --- a/tests/handlePoolCreated.test.ts +++ b/tests/handlePoolCreated.test.ts @@ -8,7 +8,7 @@ import { StaticTokenDefinition } from '../src/utils/staticTokenDefinition' import { fetchTokenDecimals, fetchTokenName, fetchTokenSymbol, fetchTokenTotalSupply } from '../src/utils/token' import { assertObjectMatches, - createTestPool, + invokePoolCreatedWithMockedEthCalls, MOCK_EVENT, POOL_FEE_TIER_03, POOL_TICK_SPACING_03, @@ -24,7 +24,7 @@ describe('handlePoolCreated', () => { assert.notInStore('Token', USDC_MAINNET_FIXTURE.address) assert.notInStore('Token', USDC_MAINNET_FIXTURE.address) - createTestPool( + invokePoolCreatedWithMockedEthCalls( MOCK_EVENT, FACTORY_ADDRESS, USDC_MAINNET_FIXTURE, diff --git a/tests/intervalUpdates.test.ts b/tests/intervalUpdates.test.ts index 39d00932..9bd91ba4 100644 --- a/tests/intervalUpdates.test.ts +++ b/tests/intervalUpdates.test.ts @@ -19,6 +19,8 @@ import { USDC_MAINNET_FIXTURE, USDC_WETH_03_MAINNET_POOL, WETH_MAINNET_FIXTURE, + createAndStoreTestPool, + createAndStoreTestToken, } from './constants' describe('uniswap interval data', () => { @@ -78,36 +80,12 @@ describe('uniswap interval data', () => { describe('pool interval data', () => { beforeEach(() => { clearStore() - - const pool = new Pool(USDC_WETH_03_MAINNET_POOL) - pool.createdAtTimestamp = ZERO_BI - pool.createdAtBlockNumber = ZERO_BI - pool.token0 = USDC_MAINNET_FIXTURE.address - pool.token1 = WETH_MAINNET_FIXTURE.address - pool.feeTier = BigInt.fromI32(POOL_FEE_TIER_03) - pool.liquidity = ZERO_BI - pool.sqrtPrice = ZERO_BI - pool.token0Price = ZERO_BD - pool.token1Price = ZERO_BD - pool.tick = ZERO_BI - pool.observationIndex = ZERO_BI - pool.volumeToken0 = ZERO_BD - pool.volumeToken1 = ZERO_BD - pool.volumeUSD = ZERO_BD - pool.untrackedVolumeUSD = ZERO_BD - pool.feesUSD = ZERO_BD - pool.txCount = ZERO_BI - pool.collectedFeesToken0 = ZERO_BD - pool.collectedFeesToken1 = ZERO_BD - pool.collectedFeesUSD = ZERO_BD - pool.totalValueLockedToken0 = ZERO_BD - pool.totalValueLockedToken1 = ZERO_BD - pool.totalValueLockedUSD = ZERO_BD - pool.totalValueLockedETH = ZERO_BD - pool.totalValueLockedUSDUntracked = ZERO_BD - pool.liquidityProviderCount = ZERO_BI - - pool.save() + createAndStoreTestPool( + USDC_WETH_03_MAINNET_POOL, + USDC_MAINNET_FIXTURE.address, + WETH_MAINNET_FIXTURE.address, + POOL_FEE_TIER_03, + ) }) test('success - create and update poolDayData', () => { @@ -127,7 +105,10 @@ describe('pool interval data', () => { const dayId = poolEvent.block.timestamp.toI32() / 86400 const dayStartTimestamp = dayId * 86400 - const dayPoolID = poolEvent.address.toHexString().concat('-').concat(dayId.toString()) + const dayPoolID = poolEvent.address + .toHexString() + .concat('-') + .concat(dayId.toString()) assertObjectMatches('PoolDayData', dayPoolID, [ ['date', dayStartTimestamp.toString()], @@ -219,7 +200,10 @@ describe('pool interval data', () => { const hourIndex = poolEvent.block.timestamp.toI32() / 3600 const hourStartUnix = hourIndex * 3600 - const hourPoolID = poolEvent.address.toHexString().concat('-').concat(hourIndex.toString()) + const hourPoolID = poolEvent.address + .toHexString() + .concat('-') + .concat(hourIndex.toString()) assertObjectMatches('PoolHourData', hourPoolID, [ ['periodStartUnix', hourStartUnix.toString()], @@ -299,24 +283,7 @@ describe('token interval data', () => { beforeEach(() => { clearStore() - const token = new Token(WETH_MAINNET_FIXTURE.address) - token.symbol = WETH_MAINNET_FIXTURE.symbol - token.name = WETH_MAINNET_FIXTURE.name - token.decimals = BigInt.fromString(WETH_MAINNET_FIXTURE.decimals) - token.totalSupply = BigInt.fromString(WETH_MAINNET_FIXTURE.totalSupply) - token.volume = ZERO_BD - token.volumeUSD = ZERO_BD - token.untrackedVolumeUSD = ZERO_BD - token.feesUSD = ZERO_BD - token.txCount = ZERO_BI - token.poolCount = ZERO_BI - token.totalValueLocked = ZERO_BD - token.totalValueLockedUSD = ZERO_BD - token.totalValueLockedUSDUntracked = ZERO_BD - token.derivedETH = ZERO_BD - token.whitelistPools = [] - - token.save() + createAndStoreTestToken(WETH_MAINNET_FIXTURE) const bundle = new Bundle('1') bundle.ethPriceUSD = ZERO_BD @@ -338,7 +305,10 @@ describe('token interval data', () => { const dayId = MOCK_EVENT.block.timestamp.toI32() / 86400 const dayStartTimestamp = dayId * 86400 - const tokenDayID = token.id.toString().concat('-').concat(dayId.toString()) + const tokenDayID = token.id + .toString() + .concat('-') + .concat(dayId.toString()) assertObjectMatches('TokenDayData', tokenDayID, [ ['date', dayStartTimestamp.toString()], @@ -419,7 +389,10 @@ describe('token interval data', () => { const hourIndex = MOCK_EVENT.block.timestamp.toI32() / 3600 const hourStartUnix = hourIndex * 3600 - const tokenHourID = token.id.toString().concat('-').concat(hourIndex.toString()) + const tokenHourID = token.id + .toString() + .concat('-') + .concat(hourIndex.toString()) assertObjectMatches('TokenHourData', tokenHourID, [ ['periodStartUnix', hourStartUnix.toString()],