From 461aab83a43d4ee5591d2ba5077f619cac2c3304 Mon Sep 17 00:00:00 2001 From: Amadeo Pellicce Date: Sun, 5 Feb 2023 16:49:02 -0800 Subject: [PATCH 1/5] adding functions to help with searching for and adding binance identifier apis --- src/db/network.ts | 5 +++++ src/model/Asset.ts | 1 + src/model/Network.ts | 1 + src/repo/index.ts | 37 ++++++++++++++++++++++++++++++++++--- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/db/network.ts b/src/db/network.ts index 9db890f..0b75152 100644 --- a/src/db/network.ts +++ b/src/db/network.ts @@ -41,3 +41,8 @@ export async function findNetworkByNetworkId(networkCode: string, callback: Netw throw err; } } + +export async function getAssetsByNetworkCodeAndSymbol(networkCode: string, symbol: string) { + const assets = await getAssetsForNetwork(networkCode); + return assets.find((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'); From 1e09abe53b740d5fa0be90ab8270517b60917fe8 Mon Sep 17 00:00:00 2001 From: Amadeo Pellicce Date: Sun, 5 Feb 2023 16:49:14 -0800 Subject: [PATCH 2/5] 1.0.179 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ca48cdf..3e95fa5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@map3xyz/assets-helper", - "version": "1.0.178", + "version": "1.0.179", "description": "A library for maintaining the assets repo.", "author": "pellicceama", "keywords": [ From 2d62012768fcb47e578d02e6c2d25ace5f4a6d6e Mon Sep 17 00:00:00 2001 From: Amadeo Pellicce Date: Sun, 5 Feb 2023 16:53:06 -0800 Subject: [PATCH 3/5] fixing issue with getAssetsByNetworkCodeAndSymbol --- src/db/asset.ts | 4 ++++ src/db/network.ts | 5 ----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/db/asset.ts b/src/db/asset.ts index 0b31500..9a4376e 100644 --- a/src/db/asset.ts +++ b/src/db/asset.ts @@ -70,3 +70,7 @@ async function getMockAssets(networkId?: string): Promise { return res as Asset[]; } +export async function getAssetsByNetworkCodeAndSymbol(networkCode: string, symbol: string): Promise { + const assets = await getAssetsForNetwork(networkCode); + return assets.filter((asset) => asset.symbol === symbol); +} diff --git a/src/db/network.ts b/src/db/network.ts index 0b75152..9db890f 100644 --- a/src/db/network.ts +++ b/src/db/network.ts @@ -41,8 +41,3 @@ export async function findNetworkByNetworkId(networkCode: string, callback: Netw throw err; } } - -export async function getAssetsByNetworkCodeAndSymbol(networkCode: string, symbol: string) { - const assets = await getAssetsForNetwork(networkCode); - return assets.find((asset) => asset.symbol === symbol); -} From 6912b07b4b433e746a46a81e5980d17d2837276d Mon Sep 17 00:00:00 2001 From: Amadeo Pellicce Date: Sun, 5 Feb 2023 16:53:20 -0800 Subject: [PATCH 4/5] 1.0.180 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3e95fa5..f4c828c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@map3xyz/assets-helper", - "version": "1.0.179", + "version": "1.0.180", "description": "A library for maintaining the assets repo.", "author": "pellicceama", "keywords": [ From ced06ae21731d61404213bd0048c8adb101fabda Mon Sep 17 00:00:00 2001 From: Amadeo Pellicce Date: Sun, 5 Feb 2023 16:56:05 -0800 Subject: [PATCH 5/5] adding dir parameter to function --- src/db/asset.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/db/asset.ts b/src/db/asset.ts index 9a4376e..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,7 +71,7 @@ async function getMockAssets(networkId?: string): Promise { return res as Asset[]; } -export async function getAssetsByNetworkCodeAndSymbol(networkCode: string, symbol: string): Promise { - const assets = await getAssetsForNetwork(networkCode); +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); }