diff --git a/package.json b/package.json index 7de7ddd..6e1b7a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@map3xyz/assets-helper", - "version": "1.0.172", + "version": "1.0.177", "description": "A library for maintaining the assets repo.", "author": "pellicceama", "keywords": [ diff --git a/src/model/AssetMap.ts b/src/model/AssetMap.ts index 234228a..fbe174d 100644 --- a/src/model/AssetMap.ts +++ b/src/model/AssetMap.ts @@ -3,9 +3,9 @@ export class AssetMap extends RepoObject { - fromAddress: string; + fromAddress?: string; fromNetwork: string; - toAddress: string; + toAddress?: string; toNetwork: string; type: MapType; @@ -18,9 +18,13 @@ this.type = i.type; } - deserialise(): string { + deserialise(array = false): string { let parsed = JSON.parse(JSON.stringify(this)); parsed = sortObjectKeys(parsed); + + if(array) { + parsed = [parsed]; + } return JSON.stringify(parsed, undefined, 2); } } diff --git a/src/repo/index.ts b/src/repo/index.ts index 8b2c551..51b8e17 100644 --- a/src/repo/index.ts +++ b/src/repo/index.ts @@ -71,7 +71,12 @@ export function getDirPathForNetworkCode (network: string) { } export function getDirPathForTokenlist (network: string, address?: string) { - return path.join(getDirPathForNetworkCode(network),'assets',`${network}-tokenlist`, (address ? `/${address}` : '')); + let addr = address; + + if(address) { + addr = formatAddress(addr); + } + return path.join(getDirPathForNetworkCode(network),'assets',`${network}-tokenlist`, (addr ? `/${addr}` : '')); } export async function addIdentifierToAsset(dir: string, networkCode: string, address: string, identifierKey: string, identifierValue: string | number): Promise<{addedIdentifier: boolean}> { @@ -93,7 +98,9 @@ export async function addIdentifierToAsset(dir: string, networkCode: string, add const assetInfoFile = JSON.parse(fs.readFileSync(assetInfoFilePath, 'utf8')); - if(assetInfoFile.identifiers[identifierKey]) { + if(!assetInfoFile.identifiers + || Object.keys(assetInfoFile.identifiers).length === 0 + || assetInfoFile.identifiers[identifierKey]) { return { addedIdentifier: false }; } @@ -106,10 +113,8 @@ export async function addIdentifierToAsset(dir: string, networkCode: string, add } } -export function getAssetMaps(networkCode: string, address: string, repoPath: string = DEFAULT_REPO_DISK_LOCATION): AssetMap[] { - const formattedAddress = formatAddress(address); - - const assetMapInfoFile = path.join(repoPath, getDirPathForTokenlist(networkCode, formattedAddress), 'maps.json'); +export function getAssetMaps(networkCode: string, address?: string, repoPath: string = DEFAULT_REPO_DISK_LOCATION): AssetMap[] { + const assetMapInfoFile = path.join(repoPath, getDirPathForTokenlist(networkCode, address), 'maps.json'); if(!fs.existsSync(assetMapInfoFile)) { return []; @@ -123,16 +128,24 @@ export function addAssetMap(map: AssetMap, repoPath: string = DEFAULT_REPO_DISK_ const assetDir = path.join(repoPath, getDirPathForTokenlist(map.fromNetwork, map.fromAddress)); const assetMapInfoFile = path.join(assetDir, 'maps.json'); - if(fs.existsSync(assetDir) && !fs.existsSync(assetMapInfoFile)) { - fs.writeFileSync(assetMapInfoFile, JSON.stringify([map], null, 2)); + if(!fs.existsSync(assetDir)) { + throw new Error(`Cannot add map to ${assetDir} as it does not exist`) + } + + if(!fs.existsSync(assetMapInfoFile)) { + fs.writeFileSync(assetMapInfoFile, map.deserialise(true)); return; } const assetMaps = readAndParseJson(assetMapInfoFile) .map(map => new AssetMap(map)); + const existingMap = assetMaps.find( + _map => _map.fromAddress === map.fromAddress + && _map.fromNetwork === map.fromNetwork + && _map.toAddress === map.toAddress + && _map.toNetwork === map.toNetwork); - const existingMap = assetMaps.find(_map => _map.fromAddress === map.fromAddress && _map.fromNetwork === map.fromNetwork && _map.toAddress === map.toAddress && _map.toNetwork === map.toNetwork); if(!existingMap) { assetMaps.push(map); fs.writeFileSync(assetMapInfoFile, JSON.stringify(assetMaps, null, 2)); diff --git a/src/utils/addresses.ts b/src/utils/addresses.ts index 5b37047..e46db78 100644 --- a/src/utils/addresses.ts +++ b/src/utils/addresses.ts @@ -1,6 +1,6 @@ import { toChecksumAddress } from 'ethereum-checksum-address'; -export function formatAddress(address: string): string { +export function formatAddress(address?: string): string { if(!address) { return null; }