Skip to content

Commit

Permalink
[backend] various changes and fixes (#92)
Browse files Browse the repository at this point in the history
* update factory address

* enable fastify logger

* fix get balance

* change tx history pagination order

* fix tx history sort

* remove useless log
  • Loading branch information
0xChqrles authored Jun 19, 2024
1 parent 2ded615 commit b03d0af
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type AppConfiguration = {
}

export async function buildApp(config: AppConfiguration) {
const app = Fastify()
const app = Fastify({ logger: true })

dotenv.config()
app.register(fastifyDrizzle, {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/constants/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const VAULT_FACTORY_ADDRESS = '0x060f7dc1dcb936fd9bff710d4a19da3870e611e014b3ceaa662d24ac87221894'
export const VAULT_FACTORY_ADDRESS = '0x8dabd7ce1da04fada30dc43aa90dee1861d62f0575601b19feaf80f801938e'
export const BLANK_ACCOUNT_CLASS_HASH = '0x022d51f548b95dda56852e1e3211ebdcc623637794baf768afff33807f8c4563'

export enum Entrypoint {
Expand Down
19 changes: 10 additions & 9 deletions backend/src/routes/getBalance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { eq, max } from 'drizzle-orm'
import { and, eq, max } from 'drizzle-orm'
import type { FastifyInstance } from 'fastify'

import { usdcBalance } from '@/db/schema'
Expand All @@ -21,19 +21,20 @@ export function getBalanceRoute(fastify: FastifyInstance) {
return reply.status(400).send({ message: 'Invalid address format.' })
}

const subQuery = fastify.db
.select({ maxCursor: max(usdcBalance.cursor).as('maxCursor') })
.from(usdcBalance)
.as('subQuery')

try {
// Use Drizzle ORM to find the balance by address
const balanceRecord = await fastify.db
.select({
cursor: max(usdcBalance.cursor),
balance: usdcBalance.balance,
})
const balanceRecords = await fastify.db
.select({ balance: usdcBalance.balance })
.from(usdcBalance)
.where(eq(usdcBalance.address, address))
.groupBy(usdcBalance.balance)
.innerJoin(subQuery, and(eq(usdcBalance.address, address), eq(usdcBalance.cursor, subQuery.maxCursor)))
.execute()

const balance = balanceRecord[0]?.balance ?? '0'
const balance = balanceRecords[0]?.balance ?? '0'

return reply.send({ balance })
} catch (error) {
Expand Down
9 changes: 5 additions & 4 deletions backend/src/routes/getTransactionHistory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { and, asc, eq, gt, or, sql } from 'drizzle-orm'
import { and, desc, eq, lt, or, sql } from 'drizzle-orm'
import type { FastifyInstance } from 'fastify'

import { usdcTransfer } from '@/db/schema'
Expand Down Expand Up @@ -31,8 +31,9 @@ export function getTransactionHistory(fastify: FastifyInstance) {
return reply.status(400).send({ error: 'Invalid address format.' })
}

const firstTimestamp = after ? Number(after) : 0xfffffffffff // a timestamp large enough to be sure that it's in the future

try {
const firstTimestamp = after ? Number(after) : Number(0)
const txs = await fastify.db
.select({
transaction_timestamp: usdcTransfer.blockTimestamp,
Expand All @@ -53,12 +54,12 @@ export function getTransactionHistory(fastify: FastifyInstance) {
.leftJoin(sql`registration AS "to_user"`, eq(usdcTransfer.toAddress, sql`"to_user"."contract_address"`))
.where(
and(
gt(usdcTransfer.blockTimestamp, new Date(firstTimestamp)),
lt(usdcTransfer.blockTimestamp, new Date(firstTimestamp)),
or(eq(usdcTransfer.fromAddress, address), eq(usdcTransfer.toAddress, address)),
),
)
.limit(first)
.orderBy(asc(usdcTransfer.blockTimestamp))
.orderBy(desc(usdcTransfer.blockTimestamp))
.execute()

return reply.status(200).send({ transactions: txs })
Expand Down
4 changes: 2 additions & 2 deletions backend/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sql } from 'drizzle-orm'
import type { FastifyInstance } from 'fastify'
import type { Account } from 'starknet'
import { ServiceListInstance } from 'twilio/lib/rest/verify/v2/service'
import { ServiceContext } from 'twilio/lib/rest/verify/v2/service'

import type { Database } from '@/db/drizzle'

Expand All @@ -18,7 +18,7 @@ import { verifyOtp } from './verifyOtp'

export const addressRegex = /^0x0[0-9a-fA-F]{63}$/

export function declareRoutes(fastify: FastifyInstance, deployer: Account, twilio_services: ServiceListInstance) {
export function declareRoutes(fastify: FastifyInstance, deployer: Account, twilio_services: ServiceContext) {
getStatusRoute(fastify)
getBalanceRoute(fastify)
getCurrentExpenseRoute(fastify)
Expand Down

0 comments on commit b03d0af

Please sign in to comment.