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

Migrate Balance validation to zod #1257

Merged
merged 7 commits into from
Mar 11, 2024
Merged

Migrate Balance validation to zod #1257

merged 7 commits into from
Mar 11, 2024

Conversation

iamacook
Copy link
Member

@iamacook iamacook commented Mar 6, 2024

Summary

This migrates the validation of Balance (and associated sub-types) to zod.

Changes

  • Removes BalancesValidator and associated schema
  • Adds BalanceSchema and infers type(s) from it
  • Propagate required changes in codebase
  • Update associated tests

@iamacook iamacook self-assigned this Mar 6, 2024
@@ -125,7 +125,7 @@ export class SafeBalancesApi implements IBalancesApi {
): Promise<Balance[]> {
const tokenAddresses = balances
.map((balance) => balance.tokenAddress)
.filter((address): address is string => address !== null);
.filter((address): address is `0x${string}` => address !== null);
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a requirement as the schema checksums the address.

@@ -230,7 +231,7 @@ export class ZerionBalancesApi implements IBalancesApi {
): Erc20Balance {
const { fungible_info, quantity } = zerionBalanceAttributes;
return {
tokenAddress,
tokenAddress: getAddress(tokenAddress),
Copy link
Member Author

Choose a reason for hiding this comment

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

As before regarding the checksumming of addresses. I will not highlight the other instances of this, but everywhere an address is wrapped in a getAddress call, it is the same.

@coveralls
Copy link

coveralls commented Mar 6, 2024

Pull Request Test Coverage Report for Build 8230510014

Details

  • 12 of 12 (100.0%) changed or added relevant lines in 5 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.02%) to 93.387%

Files with Coverage Reduction New Missed Lines %
src/routes/transactions/entities/tests/human-description.builder.ts 1 80.0%
Totals Coverage Status
Change from base Build 8185873789: -0.02%
Covered Lines: 6417
Relevant Lines: 6636

💛 - Coveralls

Comment on lines +159 to +161
address: transactionApiBalancesResponse[1].tokenAddress
? getAddress(transactionApiBalancesResponse[1].tokenAddress)
: transactionApiBalancesResponse[1].tokenAddress,
Copy link
Member Author

Choose a reason for hiding this comment

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

Type-wise, tokenAddress may be 0x${string} or null, hence the ternary requirement.

@iamacook iamacook marked this pull request as ready for review March 6, 2024 10:41
@iamacook iamacook requested a review from a team as a code owner March 6, 2024 10:41
Base automatically changed from zod-union-errors to main March 6, 2024 15:23
@iamacook iamacook marked this pull request as draft March 7, 2024 08:54
@iamacook iamacook marked this pull request as ready for review March 7, 2024 09:19
@iamacook iamacook marked this pull request as draft March 7, 2024 09:22
@iamacook iamacook marked this pull request as ready for review March 7, 2024 11:31
Copy link
Member

@hectorgomezv hectorgomezv left a comment

Choose a reason for hiding this comment

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

👏🏻👏🏻👏🏻

{
code: 'invalid_union',
unionErrors: [
// @ts-expect-error - inferred types don't allow optional fields
Copy link
Member

Choose a reason for hiding this comment

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

Is this comment accurate? The error I get when removing the @ts-expect-error annotation is:

Type '{ issues: { code: "invalid_type"; expected: "string"; received: "undefined"; path: string[]; message: string; }[]; name: string; }' is missing the following properties from type 'ZodError<any>': errors, format, message, isEmpty, and 4 more.

(this is also applicable to other lines: 193, 253, 266)

Copy link
Member Author

Choose a reason for hiding this comment

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

True - it looks like the error is not on our end. I updated the comment in 67f7390.

@iamacook iamacook merged commit fe2ff13 into main Mar 11, 2024
16 checks passed
@iamacook iamacook deleted the zod-balances-validation branch March 11, 2024 16:09
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.

3 participants