Skip to content

Commit

Permalink
Merge pull request #39 from map3xyz/binance-identifier-apis
Browse files Browse the repository at this point in the history
Binance identifier apis
  • Loading branch information
pellicceama authored Mar 10, 2023
2 parents 779293d + ced06ae commit 9942fce
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
5 changes: 5 additions & 0 deletions src/db/asset.ts
Original file line number Diff line number Diff line change
@@ -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<void>;
Expand Down Expand Up @@ -70,3 +71,7 @@ async function getMockAssets(networkId?: string): Promise<Asset[]> {
return res as Asset[];
}

export async function getAssetsByNetworkCodeAndSymbol(networkCode: string, symbol: string, dir = DEFAULT_REPO_DISK_LOCATION): Promise<Asset[]> {
const assets = await getAssetsForNetwork(networkCode, dir);
return assets.filter((asset) => asset.symbol === symbol);
}
1 change: 1 addition & 0 deletions src/model/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class Asset extends ChainObject {
type: 'asset';
identifiers: {
coinmarketcap?: number;
binance?: string;
} | null

constructor(info: Partial<Asset>) {
Expand Down
1 change: 1 addition & 0 deletions src/model/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class Network extends ChainObject {
bip44?: number;
chainId?: number;
coinmarketcap?: number;
binance?: string;
} | null
regex: {
address: string;
Expand Down
37 changes: 34 additions & 3 deletions src/repo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 9942fce

Please sign in to comment.