Skip to content

Commit

Permalink
Allow non-eth addresses on ZerionBalanceSchema implementantion addres…
Browse files Browse the repository at this point in the history
…ses (#1646)

Allow non-eth addresses in ZerionBalanceSchema
  • Loading branch information
hectorgomezv authored Jun 12, 2024
1 parent 449ab9c commit 6d12cfb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,16 @@ describe('Zerion Balance Entity schemas', () => {
);
});

// Note: this is needed as the implementation address field can contain non-eth addresses.
it('should allow any string as address value', () => {
const zerionImplementation = zerionImplementationBuilder().build();
zerionImplementation.address = faker.string.sample();

const result = ZerionImplementationSchema.safeParse(zerionImplementation);

expect(result.success).toBe(true);
});

it('should not allow an invalid address value', () => {
const zerionImplementation = zerionImplementationBuilder().build();
// @ts-expect-error - address is expected to be a string
Expand All @@ -540,7 +550,6 @@ describe('Zerion Balance Entity schemas', () => {
const nonChecksummedAddress = faker.finance
.ethereumAddress()
.toLowerCase();
// @ts-expect-error - address is expected to be a checksummed address
zerionImplementation.address = nonChecksummedAddress;

const result = ZerionImplementationSchema.safeParse(zerionImplementation);
Expand Down
11 changes: 9 additions & 2 deletions src/datasources/balances-api/entities/zerion-balance.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Reference documentation: https://developers.zerion.io/reference/listwalletpositions
*/

import { AddressSchema } from '@/validation/entities/schemas/address.schema';
import { getAddress, isAddress } from 'viem';
import { z } from 'zod';

export type ZerionFungibleInfo = z.infer<typeof ZerionFungibleInfoSchema>;
Expand All @@ -22,7 +22,14 @@ export type ZerionBalances = z.infer<typeof ZerionBalancesSchema>;

export const ZerionImplementationSchema = z.object({
chain_id: z.string(),
address: AddressSchema.nullish().default(null),
// Note: AddressSchema can't be used here because this field can contain non-eth addresses.
address: z
.string()
.nullish()
.default(null)
.transform((value) =>
value !== null && isAddress(value) ? getAddress(value) : value,
),
decimals: z.number(),
});

Expand Down

0 comments on commit 6d12cfb

Please sign in to comment.