Skip to content

Commit

Permalink
merge decimals opt
Browse files Browse the repository at this point in the history
  • Loading branch information
mzywang committed Apr 2, 2024
1 parent c2d509a commit c134f7d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BigDecimal, BigInt, ethereum } from '@graphprotocol/graph-ts'

import { Transaction } from '../types/schema'
import { ZERO_BI, ZERO_BD, ONE_BD } from '../utils/constants'
import { ONE_BD, ZERO_BD, ZERO_BI } from '../utils/constants'

export function exponentToBigDecimal(decimals: BigInt): BigDecimal {
let resultString = '1'
Expand Down Expand Up @@ -29,7 +29,7 @@ export function safeDiv(amount0: BigDecimal, amount1: BigDecimal): BigDecimal {
*/
export function fastExponentiation(value: BigDecimal, power: i32): BigDecimal {
if (power < 0) {
let result = fastExponentiation(value, -power)
const result = fastExponentiation(value, -power)
return safeDiv(ONE_BD, result)
}

Expand All @@ -41,8 +41,8 @@ export function fastExponentiation(value: BigDecimal, power: i32): BigDecimal {
return value
}

let halfPower = power / 2
let halfResult = fastExponentiation(value, halfPower)
const halfPower = power / 2
const halfResult = fastExponentiation(value, halfPower)

// Use the fact that x ^ (2n) = (x ^ n) * (x ^ n) and we can compute (x ^ n) only once.
let result = halfResult.times(halfResult)
Expand Down
7 changes: 4 additions & 3 deletions src/utils/tick.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
import { fastExponentiation, safeDiv } from '.'

import { Tick } from '../types/schema'
import { Mint as MintEvent } from '../types/templates/Pool/Pool'
import { bigDecimalExponated, safeDiv } from '.'
import { fastExponentiation, safeDiv } from '.'
import { safeDiv } from '.'
import { ONE_BD, ZERO_BI } from './constants'

export function createTick(tickId: string, tickIdx: i32, poolId: string, event: MintEvent): Tick {
Expand All @@ -20,7 +21,7 @@ export function createTick(tickId: string, tickIdx: i32, poolId: string, event:
tick.price1 = ONE_BD

// 1.0001^tick is token1/token0.
let price0 = fastExponentiation(BigDecimal.fromString('1.0001'), tickIdx)
const price0 = fastExponentiation(BigDecimal.fromString('1.0001'), tickIdx)
tick.price0 = price0
tick.price1 = safeDiv(ONE_BD, price0)

Expand Down

0 comments on commit c134f7d

Please sign in to comment.