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

fix: add schema validation for stamps #5348

Merged
merged 1 commit into from
May 7, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
"varuint-bitcoin": "1.1.2",
"webextension-polyfill": "0.10.0",
"yup": "1.3.3",
"zod": "3.23.6",
"zxcvbn": "4.4.2"
},
"devDependencies": {
Expand Down
10 changes: 7 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 69 additions & 55 deletions src/app/query/bitcoin/stamps/stamps-by-address.query.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,74 @@
import { useQuery } from '@tanstack/react-query';
import axios from 'axios';
import { ZodError, z } from 'zod';

import { analytics } from '@shared/utils/analytics';

import { AppUseQueryConfig } from '@app/query/query-config';
import { QueryPrefixes } from '@app/query/query-prefixes';

export interface Stamp {
stamp: number;
block_index: number;
cpid: string;
asset_longname: string;
creator: string;
divisible: number;
keyburn: number;
locked: number;
message_index: number;
stamp_base64: string;
stamp_mimetype: string;
stamp_url: string;
supply: number;
timestamp: string;
tx_hash: string;
tx_index: number;
src_data: string;
ident: string;
creator_name: string;
stamp_gen: string;
stamp_hash: string;
is_btc_stamp: number;
is_reissue: number;
file_hash: string;
}
const stampSchema = z.object({
stamp: z.number(),
block_index: z.number(),
cpid: z.string(),
asset_longname: z.string(),
creator: z.string(),
divisible: z.number(),
keyburn: z.number(),
locked: z.number(),
message_index: z.number(),
stamp_base64: z.string(),
stamp_mimetype: z.string(),
stamp_url: z.string(),
supply: z.number(),
timestamp: z.string(),
tx_hash: z.string(),
tx_index: z.number(),
src_data: z.string(),
ident: z.string(),
creator_name: z.string(),
stamp_gen: z.string(),
stamp_hash: z.string(),
is_btc_stamp: z.number(),
is_reissue: z.number(),
file_hash: z.string(),
});

export interface Src20Token {
id: string;
address: string;
cpid: string;
p: string;
tick: string;
amt: number;
block_time: string;
last_update: number;
}
export type Stamp = z.infer<typeof stampSchema>;

interface StampsByAddressQueryResponse {
page: number;
limit: number;
totalPages: number;
total: number;
last_block: number;
btc: {
address: string;
balance: number;
txCount: number;
unconfirmedBalance: number;
unconfirmedTxCount: number;
};
data: {
stamps: Stamp[];
src20: Src20Token[];
};
}
const src20TokenSchema = z.object({
id: z.string(),
address: z.string(),
cpid: z.string(),
p: z.string(),
tick: z.string(),
amt: z.number(),
block_time: z.string(),
last_update: z.number(),
});

export type Src20Token = z.infer<typeof src20TokenSchema>;

const stampsByAdressSchema = z.object({
page: z.number(),
limit: z.number(),
totalPages: z.number(),
total: z.number(),
last_block: z.number().optional(),
btc: z.object({
address: z.string(),
balance: z.number(),
txCount: z.number(),
unconfirmedBalance: z.number(),
unconfirmedTxCount: z.number(),
}),
data: z.object({
stamps: z.array(stampSchema),
src20: z.array(src20TokenSchema),
}),
});

type StampsByAddressQueryResponse = z.infer<typeof stampsByAdressSchema>;

/**
* @see https://stampchain.io/docs#/default/get_api_v2_balance__address_
Expand All @@ -68,7 +77,12 @@ async function fetchStampsByAddress(address: string): Promise<StampsByAddressQue
const resp = await axios.get<StampsByAddressQueryResponse>(
`https://stampchain.io/api/v2/balance/${address}`
);
return resp.data;
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Maybe this is a pattern we can apply to a generic query wrapper?

return stampsByAdressSchema.parse(resp.data);
} catch (e) {
if (e instanceof ZodError) void analytics.track('schema_fail', e);
throw e;
fbwoolf marked this conversation as resolved.
Show resolved Hide resolved
}
}

type FetchStampsByAddressResp = Awaited<ReturnType<typeof fetchStampsByAddress>>;
Expand Down
Loading