diff --git a/package.json b/package.json index ca48cdf..f4c828c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@map3xyz/assets-helper", - "version": "1.0.178", + "version": "1.0.180", "description": "A library for maintaining the assets repo.", "author": "pellicceama", "keywords": [ diff --git a/src/db/asset.ts b/src/db/asset.ts index 0b31500..33f1884 100644 --- a/src/db/asset.ts +++ b/src/db/asset.ts @@ -1,6 +1,7 @@ import { Asset } from "../model"; import { AssetMapping } from "../model/AssetMapping"; import { getAssetMapping, getAssetsForNetwork, getNetworks } from "../networks"; +import { DEFAULT_REPO_DISK_LOCATION } from "../utils/constants"; import { ETHEREUM_ASSETS, POLYGON_ASSETS } from "./assets.json"; type AssetInfoCallback = (assetInfo: Asset) => Promise; @@ -70,3 +71,7 @@ async function getMockAssets(networkId?: string): Promise { return res as Asset[]; } +export async function getAssetsByNetworkCodeAndSymbol(networkCode: string, symbol: string, dir = DEFAULT_REPO_DISK_LOCATION): Promise { + const assets = await getAssetsForNetwork(networkCode, dir); + return assets.filter((asset) => asset.symbol === symbol); +} diff --git a/src/model/Asset.ts b/src/model/Asset.ts index 11c707e..21a809f 100644 --- a/src/model/Asset.ts +++ b/src/model/Asset.ts @@ -11,6 +11,7 @@ export class Asset extends ChainObject { type: 'asset'; identifiers: { coinmarketcap?: number; + binance?: string; } | null constructor(info: Partial) { diff --git a/src/model/Network.ts b/src/model/Network.ts index 14127f0..671114f 100644 --- a/src/model/Network.ts +++ b/src/model/Network.ts @@ -6,6 +6,7 @@ export class Network extends ChainObject { bip44?: number; chainId?: number; coinmarketcap?: number; + binance?: string; } | null regex: { address: string; diff --git a/src/repo/index.ts b/src/repo/index.ts index 51b8e17..62c16d4 100644 --- a/src/repo/index.ts +++ b/src/repo/index.ts @@ -79,11 +79,42 @@ export function getDirPathForTokenlist (network: string, address?: string) { return path.join(getDirPathForNetworkCode(network),'assets',`${network}-tokenlist`, (addr ? `/${addr}` : '')); } +const ALLOWED_IDENTIFIER_KEYS_FOR_ASSETS = [ + 'coinmarketcap', + 'binance' +] + +export async function addIdentifierToNetwork(dir: string, networkCode: string, identifierKey: string, identifierValue: string): Promise<{addedIdentifier: boolean}> { + if(!ALLOWED_IDENTIFIER_KEYS_FOR_ASSETS.includes(identifierKey)) { + throw new Error('Identifier key ' + identifierKey + ' is not allowed for networks'); + } + + const networkInfoFilePath = path.join(dir, getDirPathForTokenlist(networkCode), 'info.json'); + + if(!fs.existsSync(networkInfoFilePath)) { + console.error('addIdentifierToNetwork Cannot find asset info file for network ' + networkCode + ' in directory ' + dir); + return { addedIdentifier: false }; + } + + const networkInfoFile = JSON.parse(fs.readFileSync(networkInfoFilePath, 'utf8')); + + if(!networkInfoFile.identifiers + || Object.keys(networkInfoFile.identifiers).length === 0 + || networkInfoFile.identifiers[identifierKey]) { + return { addedIdentifier: false }; + } + + networkInfoFile.identifiers[identifierKey] = identifierValue; + networkInfoFile.identifiers = sortObjectKeys(networkInfoFile.identifiers); + fs.writeFileSync(networkInfoFilePath, JSON.stringify(networkInfoFile, null, 2)); + + return { + addedIdentifier: true + } +} + export async function addIdentifierToAsset(dir: string, networkCode: string, address: string, identifierKey: string, identifierValue: string | number): Promise<{addedIdentifier: boolean}> { - const ALLOWED_IDENTIFIER_KEYS_FOR_ASSETS = [ - 'coinmarketcap' - ] if(!ALLOWED_IDENTIFIER_KEYS_FOR_ASSETS.includes(identifierKey)) { throw new Error('Identifier key ' + identifierKey + ' is not allowed for assets');