Skip to content

Commit

Permalink
add unit tests for intervalUpdates (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzywang authored May 29, 2024
1 parent 517c4a3 commit b22849c
Show file tree
Hide file tree
Showing 9 changed files with 507 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": false,
"singleQuote": true,
"printWidth": 120,
"trailingComma": "all"
}
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,5 @@
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"typescript": "^3.5.2"
},
"prettier": {
"printWidth": 120,
"semi": false,
"singleQuote": true
}
}
2 changes: 1 addition & 1 deletion src/mappings/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function handlePoolCreatedHelper(
event: PoolCreated,
factoryAddress: string = FACTORY_ADDRESS,
whitelistTokens: string[] = WHITELIST_TOKENS,
staticTokenDefinitions: StaticTokenDefinition[] = STATIC_TOKEN_DEFINITIONS
staticTokenDefinitions: StaticTokenDefinition[] = STATIC_TOKEN_DEFINITIONS,
): void {
// temp fix
if (event.params.pool == Address.fromHexString('0x8fe8d9bb8eeba3ed688069c3d6b556c9ca258248')) {
Expand Down
6 changes: 3 additions & 3 deletions 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 All @@ -20,7 +20,7 @@ export function handleCollect(event: CollectEvent): void {
export function handleCollectHelper(
event: CollectEvent,
factoryAddress: string = FACTORY_ADDRESS,
whitelistTokens: string[] = WHITELIST_TOKENS
whitelistTokens: string[] = WHITELIST_TOKENS,
): void {
const bundle = Bundle.load('1')!
const pool = Pool.load(event.address.toHexString())
Expand All @@ -44,7 +44,7 @@ export function handleCollectHelper(
token0 as Token,
collectedAmountToken1,
token1 as Token,
whitelistTokens
whitelistTokens,
)

// Reset tvl aggregates until new amounts calculated
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/pool/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function handleSwap(event: SwapEvent): void {

// get amount that should be tracked only - div 2 because cant count both input and output as volume
const amountTotalUSDTracked = getTrackedAmountUSD(amount0Abs, token0 as Token, amount1Abs, token1 as Token).div(
BigDecimal.fromString('2')
BigDecimal.fromString('2'),
)
const amountTotalETHTracked = safeDiv(amountTotalUSDTracked, bundle.ethPriceUSD)
const amountTotalUSDUntracked = amount0USD.plus(amount1USD).div(BigDecimal.fromString('2'))
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
11 changes: 4 additions & 7 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 Expand Up @@ -136,7 +133,7 @@ export function getTrackedAmountUSD(
token0: Token,
tokenAmount1: BigDecimal,
token1: Token,
whitelistTokens: string[] = WHITELIST_TOKENS
whitelistTokens: string[] = WHITELIST_TOKENS,
): BigDecimal {
const bundle = Bundle.load('1')!
const price0USD = token0.derivedETH.times(bundle.ethPriceUSD)
Expand Down
4 changes: 2 additions & 2 deletions tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const createTestPool = (
token1: TokenFixture,
poolAddressHexString: string,
feeTier: number,
tickSpacing: number
tickSpacing: number,
): void => {
const mockEvent = newMockEvent()
const token0Address = Address.fromString(token0.address)
Expand All @@ -68,7 +68,7 @@ export const createTestPool = (
mockEvent.block,
mockEvent.transaction,
parameters,
mockEvent.receipt
mockEvent.receipt,
)
// create mock contract calls for token0
createMockedFunction(token0Address, 'symbol', 'symbol():(string)').returns([ethereum.Value.fromString(token0.symbol)])
Expand Down
Loading

0 comments on commit b22849c

Please sign in to comment.