Skip to content

Commit

Permalink
Merge pull request #9 from terraswap/fix/number-to-bignumber
Browse files Browse the repository at this point in the history
fix: number to big number
  • Loading branch information
jbamlee authored Jun 23, 2023
2 parents e3944af + 3af2571 commit dd3b37e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 49 deletions.
10 changes: 1 addition & 9 deletions src/collector/indexer/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ async function _terra2TokenPrice(manager: EntityManager,
const price: { [key: string]: UstPrice } = {};
price[token] = { price: "0", liquidity: "0" };

const possible = await manager.getRepository(PairInfoEntity).createQueryBuilder()
.where(`token_0 = :token0 OR token_1 = :token1`, { token0: baseCurrency, token1: baseCurrency })
.getCount();
if (!possible) {
return price[token];
}

const paths: Map<string, Set<PairData>> = new Map();
const pairs = await manager.getRepository(PairDayDataEntity).createQueryBuilder()
.distinctOn(['pair'])
Expand All @@ -121,7 +114,6 @@ async function _terra2TokenPrice(manager: EntityManager,
.addOrderBy("timestamp", "DESC")
.getMany();


let biggestLiquidity = num(0);
price[baseCurrency] = { price: "1", liquidity: biggestLiquidity.toString() };

Expand All @@ -148,7 +140,7 @@ async function _terra2TokenPrice(manager: EntityManager,

while (tokenQueue.length > 0) {
const target = tokenQueue.shift();
paths.get(target)?.forEach(p => {
paths.get(target)?.forEach(p => {
const other = p.assets[0].token === target ? p.assets[1].token : p.assets[0].token;
const otherPrice = _calculatePrice(p.assets, target, price[target].price);
if (!price[other] || num(price[other].liquidity).lt(num(p.liquidity))) {
Expand Down
20 changes: 10 additions & 10 deletions src/collector/indexer/transferUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export async function getLatestReserve(manager: EntityManager, pair: string): Pr
export function addReserve(reserve: Reserve, transformedAsset: Asset): Reserve {
const token0Reserve =
reserve.token0 === transformedAsset.token
? (Number(reserve.token0Reserve) + Number(transformedAsset.amount)).toString()
? num(reserve.token0Reserve).plus(num(transformedAsset.amount)).toString()
: reserve.token0Reserve

const token1Reserve =
reserve.token1 === transformedAsset.token
? (Number(reserve.token1Reserve) + Number(transformedAsset.amount)).toString()
? num(reserve.token1Reserve).plus(num(transformedAsset.amount)).toString()
: reserve.token1Reserve

return {
Expand Down Expand Up @@ -208,15 +208,15 @@ export async function updateTerraswapData(
.getRawMany()

let sum = {
liquidity: 0,
volume: 0,
liquidity: num(0),
volume: num(0),
txns: 0,
}

for (const data of todayData) {
sum.liquidity += Number(data.liquidity_ust)
sum.liquidity = sum.liquidity.plus(num(data.liquidity_ust))
if (data.timestamp.valueOf() === lastData[0].timestamp.valueOf()) {
sum.volume += Number(data.volume_ust)
sum.volume = sum.volume.plus(num(data.volume_ust))
sum.txns += data.txns
}
}
Expand All @@ -238,15 +238,15 @@ export async function updateTerraswapData(
.getRawMany()

sum = {
liquidity: 0,
volume: 0,
liquidity: num(0),
volume: num(0),
txns: 0,
}

for (const data of lastDayData) {
sum.liquidity += Number(data.liquidity_ust)
sum.liquidity = sum.liquidity.plus(num(data.liquidity_ust))
if (data.timestamp.valueOf() === lastData[1].timestamp.valueOf()) {
sum.volume += Number(data.volume_ust)
sum.volume = sum.volume.plus(num(data.volume_ust))
sum.txns += data.txns
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/collector/indexer/txHistoryUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ export async function updateVolume24h(
token0: isRightOrder ? transformed.assets[0].token : transformed.assets[1].token,
token1: isRightOrder ? transformed.assets[1].token : transformed.assets[0].token,
token0Volume: isRightOrder
? Math.abs(Number(transformed.assets[0].amount)).toString()
: Math.abs(Number(transformed.assets[1].amount)).toString(),
? num(transformed.assets[0].amount).abs().toString()
: num(transformed.assets[1].amount).abs().toString(),
token1Volume: isRightOrder
? Math.abs(Number(transformed.assets[1].amount)).toString()
: Math.abs(Number(transformed.assets[0].amount)).toString(),
? num(transformed.assets[1].amount).abs().toString()
: num(transformed.assets[0].amount).abs().toString(),
volumeUst: await changeVolumeAsUST(manager, new Date(timestamp), transformed, exchangeRate),
})
)
Expand Down Expand Up @@ -110,7 +110,7 @@ export async function updateLpTokenShare(

if (!lastData) return

lastData.totalLpTokenShare = (Number(lastData.totalLpTokenShare) + Number(shareDiff)).toString()
lastData.totalLpTokenShare = num(lastData.totalLpTokenShare).plus(num(shareDiff)).toString()

return pairRepo.save(lastData)
}
Expand Down Expand Up @@ -235,13 +235,13 @@ async function updatePairVolume(
])

lastData.token0Volume = (
Number(lastData.token0Volume) +
Math.abs(Number(isRightOrder ? transformed.assets[0].amount : transformed.assets[1].amount))
num(lastData.token0Volume).plus(
num(isRightOrder ? transformed.assets[0].amount : transformed.assets[1].amount).abs())
).toString()

lastData.token1Volume = (
Number(lastData.token1Volume) +
Math.abs(Number(isRightOrder ? transformed.assets[1].amount : transformed.assets[0].amount))
num(lastData.token1Volume).plus(
num(isRightOrder ? transformed.assets[1].amount : transformed.assets[0].amount).abs())
).toString()

const newVolumeUST = await changeVolumeAsUST(
Expand All @@ -251,7 +251,7 @@ async function updatePairVolume(
exchangeRate
)

lastData.volumeUst = (Number(lastData.volumeUst) + Number(newVolumeUST)).toString()
lastData.volumeUst = num(lastData.volumeUst).plus(num(newVolumeUST)).toString()

return pairRepo.save(lastData)
}
Expand Down
6 changes: 3 additions & 3 deletions src/collector/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'reflect-metadata'
import * as bluebird from 'bluebird'
import { initORM } from 'orm'
import { init as initErrorHandler, errorHandler, errorHandlerWithSentry } from 'lib/error'
import { init as initErrorHandler, errorHandlerWithSentry } from 'lib/error'
import * as logger from 'lib/logger'
import { validateConfig } from 'config'
import { collect } from './collect'
Expand Down Expand Up @@ -36,7 +36,7 @@ async function loop(
async function main(): Promise<void> {
logger.info(`Initialize collector, start_block_height: ${config.START_BLOCK_HEIGHT}`)

initErrorHandler({ sentryDsn: process.env.SENTRY })
initErrorHandler({ sentryDsn: process.env.SENTRY_DSN })

validateConfig()

Expand All @@ -56,5 +56,5 @@ async function main(): Promise<void> {
}

if (require.main === module) {
main().catch(errorHandler)
main().catch(errorHandlerWithSentry)
}
9 changes: 5 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Cycle } from 'types'
import { ParamsError } from '../lib/error'
import { num } from './num'

interface AssetInfo {
token: string
Expand All @@ -13,7 +14,7 @@ export function addMinus(n: string): string {
export function trimAssets(rawAssets: string, inflow: boolean): AssetInfo[] {
// if assets === null
if (!rawAssets) return []

const assets = rawAssets.split(',').map((e) => e.trim())
return assets
.map((e) => {
Expand Down Expand Up @@ -53,8 +54,8 @@ export function numberToDate(timestamp: number, cycle: number): Date {
}

export function stringToDate(timestamp: string, cycle: number): Date {
const timestampNumber = new Date(timestamp).valueOf()
return new Date(timestampNumber- (timestampNumber % cycle))
const timestampNumber = new Date(timestamp).valueOf()
return new Date(timestampNumber - (timestampNumber % cycle))
}

export function dateToNumber(timestamp: Date): number {
Expand All @@ -69,7 +70,7 @@ export function floorDate(timestamp: number, cycle: number): number {
export function compareLiquidity(liquidity0: string, liquidity1: string): boolean {
if (liquidity0 === 'native') return true
if (liquidity1 === 'native') return false
return Number(liquidity0) > Number(liquidity1)
return num(liquidity0) > num(liquidity1)
}

export function rangeLimit(from: number, to: number, cycle: Cycle, limit: number): void {
Expand Down
9 changes: 5 additions & 4 deletions src/services/TerraswapService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { Recent24hEntity, TerraswapDayDataEntity } from 'orm'
import { TerraswapData, TerraswapHistoricalData } from 'graphql/schema/TerraswapDayData'
import { dateToNumber, numberToDate } from 'lib/utils'
import { Cycle } from 'types'
import { num } from 'lib/num'

@Service()
export class TerraswapService {
constructor(
@InjectRepository(TerraswapDayDataEntity) private readonly terraswapRepo: Repository<TerraswapDayDataEntity>,
@InjectRepository(Recent24hEntity) private readonly recent24hRepo: Repository<Recent24hEntity>
) {}
) { }

async getTerraswap(
recent24hRepo = this.recent24hRepo,
Expand All @@ -27,14 +28,14 @@ export class TerraswapService {
select: ['volumeUst']
})

let voluem24h = 0
let volume24h = num(0)

for (const volume of recent24hData) {
voluem24h += Number(volume.volumeUst)
volume24h = volume24h.plus(num(volume.volumeUst))
}

return {
volumeUST24h: voluem24h.toString(),
volumeUST24h: volume24h.toString(),
liquidityUST: latestData.totalLiquidityUst
}
}
Expand Down
17 changes: 8 additions & 9 deletions src/services/Volume24hService.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import memoize from 'memoizee-decorator'
import { Service, Inject, Container } from 'typedi'
import { Service, Container } from 'typedi'
import { Repository } from 'typeorm'
import { InjectRepository } from 'typeorm-typedi-extensions'
import { PairInfoEntity, Recent24hEntity } from 'orm'
import { Volume24h } from 'graphql/schema'
import { TokenService } from './TokenService'
import { num } from 'lib/num'

@Service()
export class Volume24hService {
constructor(
@InjectRepository(Recent24hEntity) private readonly repo: Repository<Recent24hEntity>,
@InjectRepository(PairInfoEntity) private readonly pairRepo: Repository<PairInfoEntity>,
@Inject((type) => TokenService) private readonly tokenService: TokenService
) {}
@InjectRepository(PairInfoEntity) private readonly pairRepo: Repository<PairInfoEntity>
) { }

@memoize({ promise: true, maxAge: 600000, primitive: true, length: 1 })
async getVolume24h(
Expand All @@ -23,14 +22,14 @@ export class Volume24hService {
const recent = await repo.find({ where: { pair } })
const pairInfo = await pairRepo.findOne({ where: { pair } })
if (!pairInfo) return
if (!recent[0]){
if (!recent[0]) {
return {
token0Volume: '0',
token1Volume: '0',
volumeUST: '0',
}
}

return {
token0Volume: sumVolume(Key.TOKEN_0_VOLUME, recent).toString(),
token1Volume: sumVolume(Key.TOKEN_1_VOLUME, recent).toString(),
Expand All @@ -40,9 +39,9 @@ export class Volume24hService {
}

function sumVolume(key: Key, recentData: Recent24hEntity[]) {
let sum = 0
let sum = num(0)
for (const tx of recentData) {
sum += Number(tx[key])
sum = sum.plus(num(tx[key]))
}
return sum
}
Expand Down

0 comments on commit dd3b37e

Please sign in to comment.