Skip to content

Commit

Permalink
wip: data indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
gaboesquivel committed Feb 7, 2024
1 parent 9351875 commit 4982943
Show file tree
Hide file tree
Showing 27 changed files with 196 additions and 52 deletions.
2 changes: 1 addition & 1 deletion apps/faucet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"tailwind-merge": "^2.0.0",
"tailwindcss": "^3.3.5",
"tailwindcss-animate": "^1.0.7",
"smartsale-abis": "workspace:*",
"smartsale-contracts": "workspace:*",
"smartsale-chains": "workspace:*",
"viem": "^1.20.3",
"wagmi": "^1.4.12"
Expand Down
2 changes: 1 addition & 1 deletion apps/faucet/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ConnectButton } from '@rainbow-me/rainbowkit';
import { FaucetForm } from './components/faucet-form';
import { useUsdtBalance } from './hooks/use-usdt-balance';
import { TestnetUSDCred, TestnetMBOTSPL } from 'smartsale-abis';
import { TestnetUSDCred, TestnetMBOTSPL } from 'smartsale-contracts';
import { AddTokenToWallet } from './components/add-token-to-metamask';
import { Button } from './components/ui/button';

Expand Down
2 changes: 1 addition & 1 deletion apps/faucet/src/components/add-token-to-metamask.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ERC20ContractData } from "smartsale-abis";
import { ERC20ContractData } from "smartsale-contracts";


export function AddTokenToWallet({ address, symbol, decimals, image, name }: ERC20ContractData){
Expand Down
2 changes: 1 addition & 1 deletion apps/faucet/src/components/faucet-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Button } from "@/components/ui/button"
import { useAccount } from "wagmi"
import { useState } from "react"
import { useFaucet } from "@/hooks/use-faucet"
import { TestnetUSDCred } from 'smartsale-abis'
import { TestnetUSDCred } from 'smartsale-contracts'

export function FaucetForm() {
const account = useAccount()
Expand Down
2 changes: 1 addition & 1 deletion apps/faucet/src/hooks/use-faucet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useContractWrite, usePrepareContractWrite } from 'wagmi';
import { TestnetUSDCred } from 'smartsale-abis';
import { TestnetUSDCred } from 'smartsale-contracts';
import { parseUnits } from 'viem';

export function useFaucet({ recipient, amount }:UseFaucetParams) {
Expand Down
2 changes: 1 addition & 1 deletion apps/faucet/src/hooks/use-usdt-balance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TestnetUSDCred } from "smartsale-abis";
import { TestnetUSDCred } from "smartsale-contracts";
import { formatUnits } from "viem";
import { useAccount, useContractRead } from "wagmi";

Expand Down
2 changes: 1 addition & 1 deletion apps/indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"prisma": "^5.9.1"
},
"dependencies": {
"smartsale-abis": "workspace:*",
"smartsale-contracts": "workspace:*",
"smartsale-lib": "workspace:*",
"smartsale-chains": "workspace:*",
"@sentry/node": "^7.98.0",
Expand Down
101 changes: 66 additions & 35 deletions apps/indexer/src/indexer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { stringify, parseEventLogs } from 'viem'
import { parseEventLogs, Log, Address } from 'viem'
import { client } from './evm-client'
import { TestnetEasyAuction } from 'smartsale-abis'
import { runPromisesInSeries } from './lib'
import { TestnetEasyAuction } from 'smartsale-contracts'
import { bigintToPostgresTimestamp, getTokenDetails, runPromisesInSeries } from './lib'
import { PrismaClient } from '@prisma/client'
import { NewAuctionEvent } from './types'
const prisma = new PrismaClient()

export async function startIndexer() {
Expand Down Expand Up @@ -30,27 +31,31 @@ export async function startIndexer() {
events: TestnetEasyAuction.getEvents(),
onLogs: (logs) => {
const filteredlogs = logs.filter((log) => log.eventName !== 'OwnershipTransferred')
console.log(
stringify(parseEventLogs({ abi: TestnetEasyAuction.abi, logs: filteredlogs }), null, 2),
)
// console.log(
// stringify(parseEventLogs({ abi: TestnetEasyAuction.abi, logs: filteredlogs }), null, 2),
// )
processLogs(logs)
},
})
}

async function processLogs(logs: any) {
const actions = logs.map((log: any) => {
const eventName = log.eventName.toString()
if (!(eventName in eventHandlers)) return null
return async () => {
try {
eventHandlers[eventName](log)
} catch (error) {
//TODO: sent sentry reports
console.error(error)
// takes the generic logs and if the eventName matches one of the eventHandlers keys
// it passes the log to corresponding hanlder function
async function processLogs(logs: Log[]) {
const actions = logs
.map((log) => {
const eventName = (log as any).eventName.toString()
if (!(eventName in eventHandlers)) return null
return async () => {
try {
eventHandlers[eventName](log)
} catch (error) {
//TODO: sent sentry reports
console.error(error)
}
}
}
})
})
.filter((action): action is () => Promise<void> => action !== null)

runPromisesInSeries(actions)
}
Expand Down Expand Up @@ -78,24 +83,50 @@ function handleClaimedFromOrder(log: any) {
// console.log('handleClaimedFromOrder', log)
}

async function handleNewAuction(log: any) {
console.log('handleNewAuction', log)
// auctionId: 5n,
// _auctioningToken: '0x5EdB28FBa146371A5f4A1C5812111C887EC9Ae73',
// _biddingToken: '0x5b148580635E8B67184bCb496741e423F2c326bF',
// orderCancellationEndDate: 0n,
// auctionEndDate: 1711349042n,
// userId: 4n,
// _auctionedSellAmount: 100000n,
// _minBuyAmount: 50000000n,
// minimumBiddingAmountPerOrder: 10000n,
// minFundingThreshold: 0n,
// allowListContract: '0x0000000000000000000000000000000000000000',
// allowListData: '0x'
async function handleNewAuction(log: NewAuctionEvent) {
// console.log('handleNewAuction', log.args, typeof log.args)

const auctioningToken = await getTokenDetails({address: log.args._auctioningToken})
const biddingToken = await getTokenDetails({address: log.args._biddingToken})
const data = {
// 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,
symbol_auctioning_token: auctioningToken.symbol,
symbol_bidding_token: biddingToken.symbol,
address_auctioning_token: auctioningToken.address,
address_bidding_token: biddingToken.address,
decimals_auctioning_token: auctioningToken.decimals,
decimals_bidding_token: biddingToken.decimals,
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,

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

// current_volume integer null,
// current_clearing_order_sell_amount bigint null,
// current_clearing_order_buy_amount bigint null,
// current_clearing_price integer null,
// current_bidding_amount bigint null,
// is_atomic_closure_allowed boolean null,
// is_private_auction boolean null,
// interest_score integer null,
// usd_amount_traded integer null,

// TODO: add this to schema
// _auctionedSellAmount: 100000n,
// _minBuyAmount: 50000000n,
}

// return prisma.auction_details.create({
// })
console.log('handleNewAuction:: data for postgres db', data)

// return prisma.auction_details.create({ data })
}

function handleNewSellOrder(log: any) {
Expand Down
32 changes: 31 additions & 1 deletion apps/indexer/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import fs from 'fs/promises'
import { erc20Abi } from 'abitype/abis'
import { client } from './evm-client'
import { Address } from 'viem'

export async function writeToFile(data: string, filePath: string) {
try {
Expand All @@ -17,7 +20,34 @@ export function runPromisesInSeries<T>(promiseFns: (() => Promise<T>)[]): Promis
return promiseFns.reduce<Promise<T | void>>((prevPromise, currentPromiseFn) => {
// Chain the current promise to the accumulator after the previous one completes
// Here, we ignore the result of the previous promise, as we're focusing on chaining
// If collecting results is needed, an additional structure would be required
return prevPromise.then(() => currentPromiseFn())
}, Promise.resolve())
}

export async function getTokenDetails({ address }: { address: Address }) {
const results = await client.multicall({
contracts: [
{
address,
abi: erc20Abi,
functionName: 'decimals',
args: [],
},
{
address,
abi: erc20Abi,
functionName: 'symbol',
args: [],
},
],
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
}
83 changes: 83 additions & 0 deletions apps/indexer/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Address, Log } from 'viem'

export interface AuctionClearedEvent extends Log {
eventName: 'AuctionCleared'
args: {
auctionId: bigint
soldAuctioningTokens: bigint
soldBiddingTokens: bigint
clearingPriceOrder: string
}
}

export interface CancellationSellOrderEvent extends Log {
eventName: 'CancellationSellOrder'
args: {
auctionId: bigint
userId: bigint
buyAmount: bigint
sellAmount: bigint
}
}

export interface ClaimedFromOrderEvent extends Log {
eventName: 'ClaimedFromOrder'
args: {
auctionId: bigint
userId: bigint
buyAmount: bigint
sellAmount: bigint
}
}

export interface NewAuctionEvent extends Log {
eventName: 'NewAuction'
args: {
auctionId: bigint
_auctioningToken: Address
_biddingToken: Address
orderCancellationEndDate: bigint
auctionEndDate: bigint
userId: bigint
_auctionedSellAmount: bigint
_minBuyAmount: bigint
minimumBiddingAmountPerOrder: bigint
minFundingThreshold: bigint
allowListContract: Address
allowListData: Address
}
}

export interface NewSellOrderEvent extends Log {
eventName: 'NewSellOrder'
args: {
auctionId: bigint
userId: bigint
buyAmount: bigint
sellAmount: bigint
}
}

export interface NewUserEvent extends Log {
eventName: 'NewUser'
args: {
userId: bigint
userAddress: string
}
}

export interface OwnershipTransferredEvent extends Log {
eventName: 'OwnershipTransferred'
args: {
previousOwner: string
newOwner: string
}
}

export interface UserRegistrationEvent extends Log {
eventName: 'UserRegistration'
args: {
user: string
userId: bigint
}
}
2 changes: 1 addition & 1 deletion apps/webapp/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Newsletter } from '@/components/newletter'
import Participate from '@/components/pariticipate'
import { Upcoming } from '@/components/upcoming'
import _ from 'lodash'
import { TestnetEasyAuction } from 'smartsale-abis'
import { TestnetEasyAuction } from 'smartsale-contracts'

// console.log(_.groupBy(TestnetEasyAuction.abi.map(o => _.omit(o, ['inputs','outputs'])), 'type'))

Expand Down
4 changes: 2 additions & 2 deletions apps/webapp/hooks/use-auction-data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TestnetEasyAuction } from 'smartsale-abis'
import { TestnetEasyAuction } from 'smartsale-contracts'
import { useContractRead } from 'wagmi'

export function useAuctionData(auctionId: number) {
Expand All @@ -19,7 +19,7 @@ export function useAuctionData(auctionId: number) {

function mapArrayToAuctionData(data: unknown): AuctionData | undefined {
if (!Array.isArray(data)) return

return {
auctioningToken: data[0],
biddingToken: data[1],
Expand Down
2 changes: 1 addition & 1 deletion apps/webapp/hooks/use-place-order.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TestnetDepositOrder } from 'smartsale-abis'
import { TestnetDepositOrder } from 'smartsale-contracts'
import { useContractWrite } from 'wagmi'

interface DepositAndPlaceOrderParams {
Expand Down
2 changes: 1 addition & 1 deletion apps/webapp/lib/projects.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ERC20ContractData, TestnetMBOTSPL } from 'smartsale-abis'
import { ERC20ContractData, TestnetMBOTSPL } from 'smartsale-contracts'

export const projects: Project[] = [
{
Expand Down
2 changes: 1 addition & 1 deletion apps/webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hot-toast": "^2.4.1",
"smartsale-abis": "workspace:*",
"smartsale-contracts": "workspace:*",
"smartsale-lib": "workspace:*",
"smartsale-chains": "workspace:*",
"viem": "^1.20.3",
Expand Down
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion hardhat/auction/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "smartsale-contracts",
"name": "smartsale-solidity-contracts",
"version": "0.5.0",
"directories": {
"test": "test"
Expand Down
2 changes: 1 addition & 1 deletion hardhat/erc20-token/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "smartsale-tokens",
"name": "smartsale-solidity-tokens",
"version": "1.0.0",
"scripts": {
"deployUSDCredTestnet": "hardhat run scripts/deployUSDCredWithFaucet.js --network eosevm_testnet",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "smartsale-abis",
"name": "smartsale-contracts",
"version": "0.0.1",
"private": true,
"description": "SmartEVM Contract Data",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 4982943

Please sign in to comment.