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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.


const assetPrices = await this.coingeckoApi.getTokenPrices({
chainId: this.chainId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { DataSourceError } from '@/domain/errors/data-source.error';
import { IBalancesApi } from '@/domain/interfaces/balances-api.interface';
import { ILoggingService, LoggingService } from '@/logging/logging.interface';
import { Inject, Injectable } from '@nestjs/common';
import { getAddress } from 'viem';

export const IZerionBalancesApi = Symbol('IZerionBalancesApi');

Expand Down Expand Up @@ -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.

token: {
name: fungible_info.name ?? '',
symbol: fungible_info.symbol ?? '',
Expand Down
2 changes: 0 additions & 2 deletions src/domain.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import { HealthRepository } from '@/domain/health/health.repository';
import { HumanDescriptionApiModule } from '@/datasources/human-description-api/human-description-api.module';
import { IHumanDescriptionRepository } from '@/domain/human-description/human-description.repository.interface';
import { HumanDescriptionRepository } from '@/domain/human-description/human-description.repository';
import { BalancesValidator } from '@/domain/balances/balances.validator';
import { BalancesApiModule } from '@/datasources/balances-api/balances-api.module';

@Global()
Expand Down Expand Up @@ -93,7 +92,6 @@ import { BalancesApiModule } from '@/datasources/balances-api/balances-api.modul
SafeAppsValidator,
SafeListValidator,
SafeValidator,
BalancesValidator,
TokenValidator,
TransactionTypeValidator,
TransferValidator,
Expand Down
5 changes: 2 additions & 3 deletions src/domain/balances/balances.repository.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Inject, Injectable } from '@nestjs/common';
import { IBalancesRepository } from '@/domain/balances/balances.repository.interface';
import { Balance } from '@/domain/balances/entities/balance.entity';
import { BalancesValidator } from '@/domain/balances/balances.validator';
import { BalanceSchema } from '@/domain/balances/entities/schemas/balance.schema';
import { IBalancesApiManager } from '@/domain/interfaces/balances-api.manager.interface';

@Injectable()
export class BalancesRepository implements IBalancesRepository {
constructor(
@Inject(IBalancesApiManager)
private readonly balancesApiManager: IBalancesApiManager,
private readonly balancesValidator: BalancesValidator,
) {}

async getBalances(args: {
Expand All @@ -21,7 +20,7 @@ export class BalancesRepository implements IBalancesRepository {
}): Promise<Balance[]> {
const api = await this.balancesApiManager.getBalancesApi(args.chainId);
const balances = await api.getBalances(args);
return balances.map((balance) => this.balancesValidator.validate(balance));
return balances.map((balance) => BalanceSchema.parse(balance));
}

async clearBalances(args: {
Expand Down
36 changes: 0 additions & 36 deletions src/domain/balances/balances.validator.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/domain/balances/entities/__tests__/balance.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Builder, IBuilder } from '@/__tests__/builder';
import { faker } from '@faker-js/faker';
import { balanceTokenBuilder } from './balance.token.builder';
import { Balance } from '@/domain/balances/entities/balance.entity';
import { getAddress } from 'viem';

export function balanceBuilder(): IBuilder<Balance> {
return new Builder<Balance>()
.with('tokenAddress', faker.finance.ethereumAddress())
.with('tokenAddress', getAddress(faker.finance.ethereumAddress()))
.with('token', balanceTokenBuilder().build())
.with('balance', faker.string.numeric());
}
24 changes: 9 additions & 15 deletions src/domain/balances/entities/balance.entity.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { BalanceToken } from '@/domain/balances/entities/balance.token.entity';
import {
BalanceSchema,
Erc20BalanceSchema,
NativeBalanceSchema,
} from '@/domain/balances/entities/schemas/balance.schema';
import { z } from 'zod';

export interface NativeBalance {
tokenAddress: null;
token: null;
balance: string;
}
export type NativeBalance = z.infer<typeof NativeBalanceSchema>;

export interface Erc20Balance {
tokenAddress: string;
token: BalanceToken;
balance: string;
}
export type Erc20Balance = z.infer<typeof Erc20BalanceSchema>;

export type Balance = (NativeBalance | Erc20Balance) & {
fiatBalance: string | null;
fiatConversion: string | null;
};
export type Balance = z.infer<typeof BalanceSchema>;
10 changes: 4 additions & 6 deletions src/domain/balances/entities/balance.token.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export interface BalanceToken {
name: string;
symbol: string;
decimals: number;
logoUri: string;
}
import { BalanceTokenSchema } from '@/domain/balances/entities/schemas/balance.schema';
import { z } from 'zod';

export type BalanceToken = z.infer<typeof BalanceTokenSchema>;
Loading