Skip to content

Commit

Permalink
implementing addIdentifierToAsset repo function
Browse files Browse the repository at this point in the history
  • Loading branch information
pellicceama committed Nov 2, 2022
1 parent be25b59 commit 5244f31
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Binary file modified .yarn/install-state.gz
Binary file not shown.
41 changes: 41 additions & 0 deletions src/repo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { GITHUB_USER_CONTENT_BASE_URL, REPO_CLONE_URL } from '../utils/constants';
import { getDirectories } from '../utils/filesystem';
import { push } from '../utils/git';
import fs from 'fs';
import { sortObjectKeys } from '../utils';

export async function pushAssetsRepoModuleChangesAndCreatePullRequests(dir: string) {
try {
Expand Down Expand Up @@ -60,4 +62,43 @@ export function getGithubHostedFileUrl (dir: string, fileName: string) {
}

throw new Error('Cannot compute github hosted file url for directory ' + dir + ' and file name ' + fileName);
}

export function getDirPathForNetworkCode (network: string) {
return `/networks/${network}`;
}

export function getDirPathForTokenlist (network: string, address?: string) {
return getDirPathForNetworkCode(network) + `/assets/${network}-tokenlist` + (address ? `/${address}` : '');
}

export async function addIdentifierToAsset(path: 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');
}

const assetInfoFilePath = path + getDirPathForTokenlist(networkCode, address);

if(!fs.existsSync(assetInfoFilePath)) {
return { addedIdentifier: false };
}

const assetInfoFile = JSON.parse(fs.readFileSync(assetInfoFilePath, 'utf8'));

if(assetInfoFile.identifiers[identifierKey]) {
return { addedIdentifier: false };
}

assetInfoFile.identifiers[identifierKey] = identifierValue;
assetInfoFile.identifiers = sortObjectKeys(assetInfoFile.identifiers);
fs.writeFileSync(assetInfoFilePath, JSON.stringify(assetInfoFile, null, 2));

return {
addedIdentifier: true
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"rootDir": "src",
"outDir": "dist",
"module": "commonjs",
"target": "es6",
"target": "ES2016",
"esModuleInterop": true,
"sourceMap": true,
"declaration": true,
Expand Down

0 comments on commit 5244f31

Please sign in to comment.