Skip to content

Commit

Permalink
Add type validation to FT metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
tifrel committed Dec 19, 2023
1 parent 452df90 commit 8f584b9
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions packages/rpc/src/methods/ftMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,45 @@ export type FtMetadata = {
decimals: number;
}

export const ftBalance = async ({ contractId }): Promise<FtMetadata> => {
return callViewMethod<FtMetadata>({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isFtMetadata(x: any): x is FtMetadata {
if (typeof x.spec !== 'string') {
return false;
}
if (typeof x.name !== 'string') {
return false;
}
if (typeof x.symbol !== 'string') {
return false;
}
if (!isStringOrNull(x.icon)) {
return false;
}
if (!isStringOrNull(x.reference)) {
return false;
}
if (!isStringOrNull(x.reference_hash)) {
return false;
}
return typeof x.decimals === 'number';
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isStringOrNull(x: any): x is string | null {
if (typeof x === 'string') {
return true;
}
if (x === null) {
return true;
}
return false;
}

export const ftBalance = async ({ contractId }): Promise<FtMetadata | null> => {
const res = callViewMethod<FtMetadata>({
contractId,
method: 'ft_metadata',
});

return isFtMetadata(res) ? res : null;
};

0 comments on commit 8f584b9

Please sign in to comment.