Skip to content

Commit

Permalink
refactor: use decimal instead of bigint in db
Browse files Browse the repository at this point in the history
  • Loading branch information
tmrdlt committed Apr 15, 2024
1 parent 2abcd37 commit f91146d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ CREATE TABLE "PairLiquidityInfoHistoryV2" (
"id" SERIAL NOT NULL,
"pairId" INTEGER NOT NULL,
"eventType" TEXT NOT NULL,
"reserve0" BIGINT,
"reserve1" BIGINT,
"deltaReserve0" BIGINT,
"deltaReserve1" BIGINT,
"fiatPrice" BIGINT,
"reserve0" DECIMAL(100,0),
"reserve1" DECIMAL(100,0),
"deltaReserve0" DECIMAL(100,0),
"deltaReserve1" DECIMAL(100,0),
"fiatPrice" DECIMAL(100,0),
"height" INTEGER NOT NULL,
"microBlockTime" BIGINT NOT NULL,
"logIndex" INTEGER NOT NULL,
Expand Down
10 changes: 5 additions & 5 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ model PairLiquidityInfoHistoryV2 {
pair Pair @relation(fields: [pairId], references: [id])
pairId Int
eventType String
reserve0 BigInt?
reserve1 BigInt?
deltaReserve0 BigInt?
deltaReserve1 BigInt?
fiatPrice BigInt?
reserve0 Decimal? @db.Decimal(100, 0)
reserve1 Decimal? @db.Decimal(100, 0)
deltaReserve0 Decimal? @db.Decimal(100, 0)
deltaReserve1 Decimal? @db.Decimal(100, 0)
fiatPrice Decimal? @db.Decimal(100, 0)
height Int
microBlockTime BigInt
logIndex Int
Expand Down
5 changes: 5 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Decimal } from '@prisma/client/runtime/library';

export const nonNullable = <T>(t: T | null | undefined, label?: string): T => {
if (t == null) {
throw new Error(
Expand All @@ -20,3 +22,6 @@ export const pluralize = (count: number, noun: string, suffix = 's') =>

const parseEnv = (x) => x && JSON.parse(x);
export const presentInvalidTokens = parseEnv(process.env.SHOW_INVALID_TOKENS);

export const bigIntToDecimal = (bigInt: bigint | null): Decimal | null =>
bigInt ? new Decimal(bigInt.toString()) : null;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ContractAddress } from '../../clients/sdk-client.model';
import { orderBy } from 'lodash';
import { PairLiquidityInfoHistoryV2DbService } from '../../database/pair-liquidity-info-history/pair-liquidity-info-history-v2-db.service';
import { PairLiquidityInfoHistoryV2ErrorDbService } from '../../database/pair-liquidity-info-history-error/pair-liquidity-info-history-v2-error-db.service';
import { bigIntToDecimal } from '../../lib/utils';

@Injectable()
export class PairLiquidityInfoHistoryImporterV2Service {
Expand Down Expand Up @@ -146,11 +147,11 @@ export class PairLiquidityInfoHistoryImporterV2Service {
.upsert({
pairId: pairWithTokens.id,
eventType: event.eventType,
reserve0: event.reserve0,
reserve1: event.reserve0,
deltaReserve0: event.deltaReserve0,
deltaReserve1: event.deltaReserve1,
fiatPrice: 0n,
reserve0: bigIntToDecimal(event.reserve0),
reserve1: bigIntToDecimal(event.reserve1),
deltaReserve0: bigIntToDecimal(event.deltaReserve0),
deltaReserve1: bigIntToDecimal(event.deltaReserve1),
fiatPrice: bigIntToDecimal(0n),
height: parseInt(log.height),
microBlockTime: BigInt(log.block_time),
logIndex: parseInt(log.log_idx),
Expand Down Expand Up @@ -201,11 +202,11 @@ export class PairLiquidityInfoHistoryImporterV2Service {
.upsert({
pairId: pairWithTokens.id,
eventType: 'CreatePair',
reserve0: 0n,
reserve1: 0n,
deltaReserve0: 0n,
deltaReserve1: 0n,
fiatPrice: 0n,
reserve0: bigIntToDecimal(0n),
reserve1: bigIntToDecimal(0n),
deltaReserve0: bigIntToDecimal(0n),
deltaReserve1: bigIntToDecimal(0n),
fiatPrice: bigIntToDecimal(0n),
height: parseInt(microBlock.height),
microBlockTime: BigInt(microBlock.time),
logIndex: 0,
Expand Down

0 comments on commit f91146d

Please sign in to comment.