Skip to content
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

Closed
wants to merge 52 commits into from
Closed

feat(backend): get_historical_balance endpoint #54

wants to merge 52 commits into from

Conversation

EjembiEmmanuel
Copy link
Contributor

No description provided.

Copy link

vercel bot commented Apr 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
vault ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 16, 2024 11:51am

test(backend): modify tests
Copy link
Contributor

@0xLucqs 0xLucqs left a 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

@@ -15,6 +15,7 @@ export const usdcTransfer = pgTable('transfer_usdc', {
});

export const usdcBalance = pgTable('balance_usdc', {
balanceId: text('balance_id').primaryKey(),
Copy link
Contributor

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}$/;
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright got it!

Comment on lines 21 to 32
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)
)
`,
);
Copy link
Contributor

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

Copy link
Contributor

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)

Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor Author

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',
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor Author

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
EjembiEmmanuel and others added 5 commits April 15, 2024 10:56
* 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
Comment on lines 20 to 25
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)
`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

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!

Copy link
Collaborator

@fracek fracek left a 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.

Copy link

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 80.43% 74 / 92
🔵 Statements 80.43% 74 / 92
🔵 Functions 89.47% 17 / 19
🔵 Branches 76% 19 / 25
File Coverage
File Stmts % Branch % Funcs % Lines Uncovered Lines
Unchanged Files
backend/src/app.ts 40% 100% 50% 40% 29, 31-37, 32-33, 35-36
backend/src/index.ts 0% 0% 100% 0% 5, 7-14, 16
backend/src/db/drizzle.ts 100% 100% 100% 100%
backend/src/db/plugin.ts 85.71% 50% 66.66% 85.71% 17, 22
backend/src/db/schema.ts 100% 100% 100% 100%
backend/src/routes/getBalance.ts 83.33% 100% 100% 83.33% 28-29
backend/src/routes/getHistoricalBalance.ts 83.33% 100% 100% 83.33% 53-54
backend/src/routes/getTransactionHistory.ts 86.66% 100% 100% 86.66% 53-54
backend/src/routes/index.ts 100% 100% 100% 100%
backend/src/routes/register.ts 88.88% 50% 100% 88.88% 51
Generated in workflow #52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants