Skip to content

Commit

Permalink
add unit tests for intervalUpdates
Browse files Browse the repository at this point in the history
  • Loading branch information
mzywang committed May 29, 2024
1 parent 517c4a3 commit 7a4c2b1
Show file tree
Hide file tree
Showing 10 changed files with 507 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/mappings/pool/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
updatePoolHourData,
updateTokenDayData,
updateTokenHourData,
updateUniswapDayData
updateUniswapDayData,
} from '../../utils/intervalUpdates'
import { getTrackedAmountUSD, WHITELIST_TOKENS } from '../../utils/pricing'

Expand Down
5 changes: 3 additions & 2 deletions src/utils/intervalUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { FACTORY_ADDRESS } from './constants'
* Tracks global aggregate data over daily windows
* @param event
*/
export function updateUniswapDayData(event: ethereum.Event): UniswapDayData {
const uniswap = Factory.load(FACTORY_ADDRESS)!
export function updateUniswapDayData(event: ethereum.Event, factoryAddress: string = FACTORY_ADDRESS): UniswapDayData {
const uniswap = Factory.load(factoryAddress)!
const timestamp = event.block.timestamp.toI32()
const dayID = timestamp / 86400 // rounded
const dayStartTimestamp = dayID * 86400
Expand Down Expand Up @@ -72,6 +72,7 @@ export function updatePoolDayData(event: ethereum.Event): PoolDayData {
poolDayData.sqrtPrice = pool.sqrtPrice
poolDayData.token0Price = pool.token0Price
poolDayData.token1Price = pool.token1Price
poolDayData.close = pool.token0Price
poolDayData.tick = pool.tick
poolDayData.tvlUSD = pool.totalValueLockedUSD
poolDayData.txCount = poolDayData.txCount.plus(ONE_BI)
Expand Down
9 changes: 3 additions & 6 deletions src/utils/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const WHITELIST_TOKENS: string[] = [
'0x956f47f50a910163d8bf957cf5846d573e7f87ca', // FEI
'0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0', // MATIC
'0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9', // AAVE
'0xfe2e637202056d30016725477c5da089ab0a043a' // sETH2
'0xfe2e637202056d30016725477c5da089ab0a043a', // sETH2
]

const STABLE_COINS: string[] = [
Expand All @@ -39,7 +39,7 @@ const STABLE_COINS: string[] = [
'0xdac17f958d2ee523a2206206994597c13d831ec7',
'0x0000000000085d4780b73119b644ae5ecd22b376',
'0x956f47f50a910163d8bf957cf5846d573e7f87ca',
'0x4dd28568d05f09b02220b09c2cb307bfd837cb95'
'0x4dd28568d05f09b02220b09c2cb307bfd837cb95',
]

const MINIMUM_ETH_LOCKED = BigDecimal.fromString('60')
Expand All @@ -48,10 +48,7 @@ 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]
Expand Down
2 changes: 1 addition & 1 deletion src/utils/staticTokenDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class StaticTokenDefinition {

export const getStaticDefinition = (
tokenAddress: Address,
staticDefinitions: Array<StaticTokenDefinition>,
staticDefinitions: Array<StaticTokenDefinition>
): StaticTokenDefinition | null => {
const tokenAddressHex = tokenAddress.toHexString()

Expand Down
6 changes: 3 additions & 3 deletions src/utils/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getStaticDefinition, STATIC_TOKEN_DEFINITIONS, StaticTokenDefinition }

export function fetchTokenSymbol(
tokenAddress: Address,
staticTokenDefinitions: StaticTokenDefinition[] = STATIC_TOKEN_DEFINITIONS,
staticTokenDefinitions: StaticTokenDefinition[] = STATIC_TOKEN_DEFINITIONS
): string {
const contract = ERC20.bind(tokenAddress)
const contractSymbolBytes = ERC20SymbolBytes.bind(tokenAddress)
Expand Down Expand Up @@ -39,7 +39,7 @@ export function fetchTokenSymbol(

export function fetchTokenName(
tokenAddress: Address,
staticTokenDefinitions: StaticTokenDefinition[] = STATIC_TOKEN_DEFINITIONS,
staticTokenDefinitions: StaticTokenDefinition[] = STATIC_TOKEN_DEFINITIONS
): string {
const contract = ERC20.bind(tokenAddress)
const contractNameBytes = ERC20NameBytes.bind(tokenAddress)
Expand Down Expand Up @@ -80,7 +80,7 @@ export function fetchTokenTotalSupply(tokenAddress: Address): BigInt {

export function fetchTokenDecimals(
tokenAddress: Address,
staticTokenDefinitions: StaticTokenDefinition[] = STATIC_TOKEN_DEFINITIONS,
staticTokenDefinitions: StaticTokenDefinition[] = STATIC_TOKEN_DEFINITIONS
): BigInt | null {
const contract = ERC20.bind(tokenAddress)
// try types uint8 for decimals
Expand Down
4 changes: 2 additions & 2 deletions tests/handleBurn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const BURN_EVENT = new Burn(
new ethereum.EventParam('amount0', ethereum.Value.fromUnsignedBigInt(BURN_FIXTURE.amount0)),
new ethereum.EventParam('amount1', ethereum.Value.fromUnsignedBigInt(BURN_FIXTURE.amount1)),
],
MOCK_EVENT.receipt,
MOCK_EVENT.receipt
)

describe('handleBurn', () => {
Expand All @@ -66,7 +66,7 @@ describe('handleBurn', () => {
WETH_MAINNET_FIXTURE,
USDC_WETH_03_MAINNET_POOL,
POOL_FEE_TIER_03,
POOL_TICK_SPACING_03,
POOL_TICK_SPACING_03
)

const bundle = new Bundle('1')
Expand Down
8 changes: 4 additions & 4 deletions tests/handleCollect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const COLLECT_EVENT = new Collect(
new ethereum.EventParam('amount0', ethereum.Value.fromUnsignedBigInt(COLLECT_FIXTURE.amount0)),
new ethereum.EventParam('amount1', ethereum.Value.fromUnsignedBigInt(COLLECT_FIXTURE.amount1)),
],
MOCK_EVENT.receipt,
MOCK_EVENT.receipt
)

describe('handleMint', () => {
Expand All @@ -66,7 +66,7 @@ describe('handleMint', () => {
WETH_MAINNET_FIXTURE,
USDC_WETH_03_MAINNET_POOL,
POOL_FEE_TIER_03,
POOL_TICK_SPACING_03,
POOL_TICK_SPACING_03
)

const bundle = new Bundle('1')
Expand All @@ -90,11 +90,11 @@ describe('handleMint', () => {

const collectedAmountToken0 = convertTokenToDecimal(
COLLECT_FIXTURE.amount0,
BigInt.fromString(USDC_MAINNET_FIXTURE.decimals),
BigInt.fromString(USDC_MAINNET_FIXTURE.decimals)
)
const collectedAmountToken1 = convertTokenToDecimal(
COLLECT_FIXTURE.amount1,
BigInt.fromString(WETH_MAINNET_FIXTURE.decimals),
BigInt.fromString(WETH_MAINNET_FIXTURE.decimals)
)
const collectedAmountETH = collectedAmountToken0
.times(TEST_USDC_DERIVED_ETH)
Expand Down
4 changes: 2 additions & 2 deletions tests/handleMint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const MINT_EVENT = new Mint(
new ethereum.EventParam('amount0', ethereum.Value.fromUnsignedBigInt(MINT_FIXTURE.amount0)),
new ethereum.EventParam('amount1', ethereum.Value.fromUnsignedBigInt(MINT_FIXTURE.amount1)),
],
MOCK_EVENT.receipt,
MOCK_EVENT.receipt
)

describe('handleMint', () => {
Expand All @@ -69,7 +69,7 @@ describe('handleMint', () => {
WETH_MAINNET_FIXTURE,
USDC_WETH_03_MAINNET_POOL,
POOL_FEE_TIER_03,
POOL_TICK_SPACING_03,
POOL_TICK_SPACING_03
)

const bundle = new Bundle('1')
Expand Down
2 changes: 1 addition & 1 deletion tests/handlePoolCreated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('handlePoolCreated', () => {
WETH_MAINNET_FIXTURE,
USDC_WETH_03_MAINNET_POOL,
POOL_FEE_TIER_03,
POOL_TICK_SPACING_03,
POOL_TICK_SPACING_03
)

assertObjectMatches('Factory', FACTORY_ADDRESS, [
Expand Down
Loading

0 comments on commit 7a4c2b1

Please sign in to comment.