Skip to content

Commit

Permalink
db(indexer): store auction details
Browse files Browse the repository at this point in the history
  • Loading branch information
gaboesquivel committed Feb 7, 2024
1 parent 73994bb commit d261327
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 31 deletions.
7 changes: 4 additions & 3 deletions apps/indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
"prisma": "^5.9.1"
},
"dependencies": {
"smartsale-contracts": "workspace:*",
"smartsale-lib": "workspace:*",
"smartsale-chains": "workspace:*",
"@sentry/node": "^7.100.1",
"bn.js": "^5.2.1",
"lodash": "^4.17.21",
"smartsale-chains": "workspace:*",
"smartsale-contracts": "workspace:*",
"smartsale-lib": "workspace:*",
"viem": "latest"
}
}
12 changes: 7 additions & 5 deletions apps/indexer/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ generator client {
}

datasource db {
provider = "postgresql"
url = env("DIRECT_URL")
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
}

/// This model contains row level security and requires additional setup for migrations. Visit https://pris.ly/d/row-level-security for more info.
model auction_details {
exact_order_id BigInt
chain_id BigInt
@@id([exact_order_id, chain_id])
created_at DateTime @default(now()) @db.Timestamptz(6)
exact_order_id BigInt
symbol_auctioning_token String?
symbol_bidding_token String?
address_auctioning_token String?
Expand All @@ -35,6 +34,9 @@ model auction_details {
is_private_auction Boolean?
interest_score Int?
usd_amount_traded Int?
chain_id BigInt
@@id([exact_order_id, chain_id])
}

/// This model contains row level security and requires additional setup for migrations. Visit https://pris.ly/d/row-level-security for more info.
Expand Down
47 changes: 30 additions & 17 deletions apps/indexer/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { Log } from 'viem'
import { client } from './evm-client'
import { TestnetEasyAuction } from 'smartsale-contracts'
import { bigintToPostgresTimestamp, getEvents, getTokenDetails, runPromisesInSeries } from './lib'
import { PrismaClient } from '@prisma/client'
import { PrismaClient, Prisma } from '@prisma/client'
import { NewAuctionEvent } from './types'
import { eosEvmTestnet } from 'smartsale-chains'
import BN from 'bn.js'

const prisma = new PrismaClient()

export async function startIndexer() {
Expand Down Expand Up @@ -89,11 +92,11 @@ async function handleNewAuction(log: NewAuctionEvent) {

const auctioningToken = await getTokenDetails({ address: log.args._auctioningToken })
const biddingToken = await getTokenDetails({ address: log.args._biddingToken })
const data = {
const data: Prisma.auction_detailsCreateInput = {
// id bigint generated by default as identity,
// created_at timestamp with time zone not null default now(),
exact_order_id: log.args.auctionId,
// chain_id text null,
exact_order_id: new BN(log.args.auctionId.toString()).toNumber(),
chain_id: eosEvmTestnet.id,
symbol_auctioning_token: auctioningToken.symbol,
symbol_bidding_token: biddingToken.symbol,
address_auctioning_token: auctioningToken.address,
Expand All @@ -103,12 +106,14 @@ async function handleNewAuction(log: NewAuctionEvent) {
end_time_timestamp: bigintToPostgresTimestamp(log.args.auctionEndDate),
order_cancellation_end_date: bigintToPostgresTimestamp(log.args.orderCancellationEndDate),
// starting_time_stamp timestamp without time zone null,
minimum_bidding_amount_per_order: log.args.minimumBiddingAmountPerOrder,
min_funding_threshold: log.args.minFundingThreshold,
// minimum_bidding_amount_per_order: new BN(
// log.args.minimumBiddingAmountPerOrder.toString(),
// ).toNumber(),
// min_funding_threshold: new BN(log.args.minFundingThreshold.toString()).toNumber(),

// review this values.
allow_list_manager: log.args.allowListContract,
allow_list_signer: log.args.allowListData,
// allow_list_manager: log.args.allowListContract,
// allow_list_signer: log.args.allowListData,

// current_volume integer null,
// current_clearing_order_sell_amount bigint null,
Expand All @@ -125,15 +130,23 @@ async function handleNewAuction(log: NewAuctionEvent) {
// _minBuyAmount: 50000000n,
}

console.log('handleNewAuction:: data for postgres db', data)

return prisma.auction_details.upsert({
where: {
exact_order_id: data.exact_order_id,
},
update: data,
create: data,
})
// console.log('handleNewAuction:: data for postgres db', data)
try {
const result = await prisma.auction_details.upsert({
where: {
exact_order_id_chain_id: {
exact_order_id: data.exact_order_id,
chain_id: eosEvmTestnet.id,
},
},
update: data,
create: data,
})
// console.log('result', result)
} catch (error) {
console.log(error)
process.exit(0)
}
}

function handleNewSellOrder(log: any) {
Expand Down
10 changes: 4 additions & 6 deletions apps/indexer/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@ export async function getTokenDetails({ address }: { address: Address }) {
args: [],
},
],
multicallAddress: '0xcA11bde05977b3631167028862bE2a173976CA11'
multicallAddress: '0xcA11bde05977b3631167028862bE2a173976CA11',
})

return { address, decimals: Number(results[0].result), symbol: String(results[1].result) }
}


export function bigintToPostgresTimestamp(timestamp: bigint): string {
const date = new Date(Number(timestamp) * 1000); // Convert seconds to milliseconds
return date.toISOString().replace('T', ' ').replace('Z', ''); // Convert to PostgreSQL timestamp format
const date = new Date(Number(timestamp) * 1000) // Convert seconds to milliseconds
return date.toISOString()
}


export const getEvents = (abi: Abi) => abi.filter(item => item.type === 'event');
export const getEvents = (abi: Abi) => abi.filter((item) => item.type === 'event')
10 changes: 10 additions & 0 deletions apps/indexer/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { Address, Log } from 'viem'

declare global {
interface BigInt {
toJSON(): string
}
}

BigInt.prototype.toJSON = function (): string {
return String(this)
}

export interface AuctionClearedEvent extends Log {
eventName: 'AuctionCleared'
args: {
Expand Down
Binary file modified bun.lockb
Binary file not shown.

0 comments on commit d261327

Please sign in to comment.