-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(packages): added zod for api transformers (#2474)
Co-authored-by: stackchain <30806844+stackchain@users.noreply.github.com>
- Loading branch information
1 parent
ed20db2
commit f58294b
Showing
21 changed files
with
526 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
apps/wallet-mobile/src/yoroi-wallets/cardano/api/assetSuply.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {z} from 'zod' | ||
|
||
import {BackendConfig} from '../../types' | ||
import {createTypeGuardFromSchema} from '../../utils' | ||
import fetchDefault from './fetch' | ||
import {toAssetName, toPolicyId} from './utils' | ||
|
||
export const fetchTokensSupplies = async ( | ||
tokenIds: string[], | ||
config: BackendConfig, | ||
): Promise<Record<string, number | null>> => { | ||
const assets = tokenIds.map((tokenId) => ({policy: toPolicyId(tokenId), name: toAssetName(tokenId) || ''})) | ||
const response = await fetchDefault<unknown>('multiAsset/supply', {assets}, config) | ||
|
||
if (!isAssetSupplyEntry(response)) { | ||
return {} | ||
} | ||
|
||
const supplies = assets.map((asset) => { | ||
const key = `${asset.policy}.${asset.name}` | ||
return response.supplies[key] || null | ||
}) | ||
|
||
return Object.fromEntries(tokenIds.map((tokenId, index) => [tokenId, supplies[index]])) | ||
} | ||
|
||
type AssetSupplyEntry = { | ||
supplies: {[key: string]: number | null} | ||
} | ||
|
||
const AssetSupplySchema: z.ZodSchema<AssetSupplyEntry> = z.object({ | ||
supplies: z.record(z.number().nullable()), | ||
}) | ||
|
||
export const isAssetSupplyEntry = createTypeGuardFromSchema(AssetSupplySchema) |
29 changes: 29 additions & 0 deletions
29
apps/wallet-mobile/src/yoroi-wallets/cardano/api/assetSupply.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {isAssetSupplyEntry} from './assetSuply' | ||
|
||
describe('isAssetSupplyEntry', () => { | ||
it('returns true if object has supplies key of type object', () => { | ||
expect(isAssetSupplyEntry({supplies: {}})).toEqual(true) | ||
expect(isAssetSupplyEntry({supplies: 1})).toEqual(false) | ||
expect(isAssetSupplyEntry({supplies: []})).toEqual(false) | ||
expect(isAssetSupplyEntry({supplies: null})).toEqual(false) | ||
expect(isAssetSupplyEntry({supplies: undefined})).toEqual(false) | ||
expect(isAssetSupplyEntry({supplies: true})).toEqual(false) | ||
}) | ||
|
||
it('returns false if not given object with supplies key', () => { | ||
expect(isAssetSupplyEntry(null)).toEqual(false) | ||
expect(isAssetSupplyEntry(1)).toEqual(false) | ||
expect(isAssetSupplyEntry([])).toEqual(false) | ||
expect(isAssetSupplyEntry(true)).toEqual(false) | ||
expect(isAssetSupplyEntry('hello')).toEqual(false) | ||
expect(isAssetSupplyEntry(undefined)).toEqual(false) | ||
expect(isAssetSupplyEntry({})).toEqual(false) | ||
}) | ||
|
||
it('requires supply value to be a number or null', () => { | ||
expect(isAssetSupplyEntry({supplies: {a: 1}})).toEqual(true) | ||
expect(isAssetSupplyEntry({supplies: {a: null}})).toEqual(true) | ||
expect(isAssetSupplyEntry({supplies: {a: '1'}})).toEqual(false) | ||
expect(isAssetSupplyEntry({supplies: {a: true}})).toEqual(false) | ||
}) | ||
}) |
55 changes: 55 additions & 0 deletions
55
apps/wallet-mobile/src/yoroi-wallets/cardano/api/metadata.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {TokenInfo} from '../../types' | ||
import {parseNFTs} from './metadata' | ||
|
||
const storageUrl = 'https://example.com' | ||
describe('parseNFTs', () => { | ||
it('throws when given a value that is not an object', () => { | ||
expect(() => parseNFTs(null, storageUrl)).toThrow() | ||
expect(() => parseNFTs(1, storageUrl)).toThrow() | ||
expect(() => parseNFTs([], storageUrl)).toThrow() | ||
expect(() => parseNFTs(true, storageUrl)).toThrow() | ||
expect(() => parseNFTs('hello', storageUrl)).toThrow() | ||
expect(() => parseNFTs(undefined, storageUrl)).toThrow() | ||
}) | ||
|
||
it('returns empty array given an object that does not have an array for a value', () => { | ||
expect(parseNFTs({policyId: 1}, storageUrl)).toEqual([]) | ||
expect(parseNFTs({policyId: 'world'}, storageUrl)).toEqual([]) | ||
expect(parseNFTs({policyId: null}, storageUrl)).toEqual([]) | ||
expect(parseNFTs({policyId: true}, storageUrl)).toEqual([]) | ||
expect(parseNFTs({policyId: {}}, storageUrl)).toEqual([]) | ||
expect(parseNFTs({policyId: undefined}, storageUrl)).toEqual([]) | ||
}) | ||
|
||
it('returns empty array if no assets have key 721', () => { | ||
expect(parseNFTs({policyId: []}, storageUrl)).toEqual([]) | ||
expect(parseNFTs({policyId: [{}]}, storageUrl)).toEqual([]) | ||
expect(parseNFTs({policyId: [{key: 'hello'}]}, storageUrl)).toEqual([]) | ||
expect(parseNFTs({policyId: [{key: 'hello'}, {key: 'world'}]}, storageUrl)).toEqual([]) | ||
}) | ||
|
||
it('resolves with placeholder data if key 721 is present and metadata is not', () => { | ||
const result = parseNFTs({'8e2c7604711faef7c84c91b286c7327d17df825b7f0c88ec0332c0b4.0': [{key: '721'}]}, storageUrl) | ||
const expectedValue: Partial<TokenInfo> = { | ||
id: '8e2c7604711faef7c84c91b286c7327d17df825b7f0c88ec0332c0b4.30', | ||
name: '0', | ||
} | ||
expect(result[0]).toEqual(expect.objectContaining(expectedValue)) | ||
}) | ||
|
||
it('resolves with NFT when key and metadata are present', () => { | ||
const result = parseNFTs( | ||
{ | ||
'8e2c7604711faef7c84c91b286c7327d17df825b7f0c88ec0332c0b4.0': [ | ||
{key: '721', metadata: {'8e2c7604711faef7c84c91b286c7327d17df825b7f0c88ec0332c0b4': {0: {name: 'Name'}}}}, | ||
], | ||
}, | ||
storageUrl, | ||
) | ||
const expectedValue: Partial<TokenInfo> = { | ||
id: '8e2c7604711faef7c84c91b286c7327d17df825b7f0c88ec0332c0b4.30', | ||
name: 'Name', | ||
} | ||
expect(result[0]).toEqual(expect.objectContaining(expectedValue)) | ||
}) | ||
}) |
Oops, something went wrong.