-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(backend): get_historical_balance endpoint #54
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
test(backend): modify tests
usdcBalance shema
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we'll need more tests
backend/src/db/schema.ts
Outdated
@@ -15,6 +15,7 @@ export const usdcTransfer = pgTable('transfer_usdc', { | |||
}); | |||
|
|||
export const usdcBalance = pgTable('balance_usdc', { | |||
balanceId: text('balance_id').primaryKey(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove that
import type { FastifyInstance } from 'fastify'; | ||
import { usdcBalance } from '../db/schema'; | ||
|
||
const addressRegex = /^0x0[0-9a-fA-F]{63}$/; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this in routes/index.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What am I suppose to move here. I don't quite get it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move the regex in routes/index.ts
(as every endpoint uses it) and import it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright got it!
const historicalBalances = await fastify.db.execute( | ||
sql` | ||
SELECT balance, DATE(block_timestamp) AS date | ||
FROM ${usdcBalance} | ||
WHERE (address, block_timestamp) IN ( | ||
SELECT address, MAX(block_timestamp) AS max_timestamp | ||
FROM ${usdcBalance} | ||
WHERE address = ${address} | ||
GROUP BY address, DATE(block_timestamp) | ||
) | ||
`, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't add the query as raw sql use the ts functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also we want the last balance per day. If I had 100 usdc, i send 40 at 10 am then i receive 20 at 11pm my balance for that day would be 80 (lmk if it's not clear)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and we want that for the last 30 days
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. I assumed each entry in the database has a timestamp and corresponding balance. So I grouped the rows by address and date then extracted the entry with the latest timestamp per day assuming the entry already has the latest balance for that day.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah then this is correct, just rewrite this using the typescript functions plz
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright
balanceId: 'YfuanNvdfMfcmmdeklWDJO', | ||
address: testAddress, | ||
blockTimestamp: new Date('2024-04-10 13:03:05'), | ||
balance: '3.8AE83A109D0635A426BB', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't refer to the spec for the balance it's actually not hex but regular base 10
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the numbers should be formatted like that 1.234567
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright
usdcBalance scheme test(backend): modify mock data
routes/index.ts
* add txns history endpoint and test * address review comments
* chore(backend): add biome * fix(backend): return 0 for unknown addresses * feat(backend): add register endpoint * test(backend): register endpoint + coverage * remove cursor from registration * fix(ci): prevent segfault
test(backend): modify mock data
const subquery = sql` | ||
SELECT address, MAX(block_timestamp) AS max_timestamp | ||
FROM ${usdcBalance} | ||
WHERE address = ${address} AND block_timestamp >= NOW() - INTERVAL '30 days' | ||
GROUP BY address, DATE(block_timestamp) | ||
`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const subquery = sql` | |
SELECT address, MAX(block_timestamp) AS max_timestamp | |
FROM ${usdcBalance} | |
WHERE address = ${address} AND block_timestamp >= NOW() - INTERVAL '30 days' | |
GROUP BY address, DATE(block_timestamp) | |
`; | |
const subquery = fastify.db | |
.select({ | |
address: usdcBalance.address, | |
maxTimestamp: sql`MAX(${usdcBalance.blockTimestamp})`, | |
}) | |
.from(usdcBalance) | |
.where( | |
and( | |
eq(usdcBalance.address, address), | |
gte(usdcBalance.blockTimestamp, sql`NOW() - INTERVAL '30 days'`), | |
), | |
) | |
.groupBy(usdcBalance.address, sql`DATE(${usdcBalance.blockTimestamp})`); |
Can be rewritten in pure drizzle for type safety. Nothing else needs changing from my test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, found a way to get it to work with pure drizzle. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall. I noticed only one minor improvement.
usdcBalance scheme test(backend): modify mock data
routes/index.ts
test(backend): modify mock data
retrieval to utilize pure drizzle for type safety
Coverage Report
File Coverage
|
test(backend): modify tests
usdcBalance shema
usdcBalance scheme test(backend): modify mock data
routes/index.ts
No description provided.