Skip to content

Commit

Permalink
add metadata-mapper for type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
0xa3k5 committed Aug 18, 2024
1 parent f468182 commit 9231e0b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
30 changes: 19 additions & 11 deletions packages/utils/src/scripts/add-icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
TVariant,
IWalletMetadata,
} from '../types'
import geckoNetworks from './gecko/gecko-networks.json'
import geckoCoins from './gecko/gecko-coins.json'
import customTokens from './gecko/custom-tokens.json'
import customNetworks from './gecko/custom-networks.json'
import _geckoNetworks from './gecko/gecko-networks.json'
import _geckoCoins from './gecko/gecko-coins.json'
import _customTokens from './gecko/custom-tokens.json'
import _customNetworks from './gecko/custom-networks.json'
import getCoinByID from './gecko/get-coin-by-id'
import {
CUSTOM_NETWORKS_METADATA_PATH,
Expand All @@ -29,6 +29,8 @@ import {
validateSvg,
getTypeAndVariant,
findByFileName,
mapRawNetworkToINetwork,
mapRawWalletToIWallet,
} from '../utils'
import {
addManualMetadata,
Expand Down Expand Up @@ -92,18 +94,24 @@ const findRawData = (
type: TType,
): INetworkRaw[] | ITokenRaw[] | undefined => {
if (type === 'token') {
const geckoCoin = findByFileName(type, name, geckoCoins)
const customCoin = findByFileName(type, name, customTokens)
const geckoCoin = findByFileName(type, name, _geckoCoins)
const customCoin = findByFileName(type, name, _customTokens)
return customCoin ?? geckoCoin
} else if (type === 'network') {
const geckoNetwork = findByFileName(type, name, geckoNetworks)
const customNetwork = findByFileName(type, name, customNetworks)
return customNetwork ?? geckoNetwork
const geckoNetworks = findByFileName(type, name, _geckoNetworks)
const customNetworks = findByFileName(type, name, _customNetworks)

if (geckoNetworks) {
return geckoNetworks?.map((n) => mapRawNetworkToINetwork(n))
} else if (customNetworks) {
return customNetworks.map((n) => mapRawNetworkToINetwork(n))
}
} else if (type === 'wallet') {
// prettier-ignore
const walletsMetadata = JSON.parse(fs.readFileSync(WALLETS_METADATA_PATH, 'utf-8'))
const customWallet = findByFileName(type, name, walletsMetadata)
return customWallet
const customWallets = findByFileName(type, name, walletsMetadata)

return customWallets?.map((w) => mapRawWalletToIWallet(w))
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './ensure-dir-exists'
export * from './remove-duplicates'
export * from './get-type-and-variant'
export * from './svg-validation'
export * from './metadata-mapper'
24 changes: 24 additions & 0 deletions packages/utils/src/utils/metadata-mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { INetworkMetadata, INetworkRaw } from '../types'

export const mapRawNetworkToINetwork = (raw: INetworkRaw): INetworkMetadata => {
const network: INetworkMetadata = {
id: raw.id,
name: raw.name,
shortname: raw.shortname,
chainId: raw.chain_identifier,
nativeCoinId: raw.native_coin_id,
variants: [],
}

return network
}

export const mapRawWalletToIWallet = (raw: INetworkRaw): INetworkMetadata => {
const wallet: INetworkMetadata = {
id: raw.id,
name: raw.name,
variants: [],
}

return wallet
}

0 comments on commit 9231e0b

Please sign in to comment.