Skip to content

Commit

Permalink
Merge pull request #37 from map3xyz/helper-tweaks
Browse files Browse the repository at this point in the history
WIP Helper tweaks
  • Loading branch information
pellicceama authored Jan 6, 2023
2 parents c122b89 + aceeaf5 commit 12afbc5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 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.172",
"version": "1.0.177",
"description": "A library for maintaining the assets repo.",
"author": "pellicceama",
"keywords": [
Expand Down
10 changes: 7 additions & 3 deletions src/model/AssetMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

export class AssetMap extends RepoObject {

fromAddress: string;
fromAddress?: string;
fromNetwork: string;
toAddress: string;
toAddress?: string;
toNetwork: string;
type: MapType;

Expand All @@ -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);
}
}
Expand Down
31 changes: 22 additions & 9 deletions src/repo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}> {
Expand All @@ -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 };
}

Expand All @@ -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 [];
Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/utils/addresses.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down

0 comments on commit 12afbc5

Please sign in to comment.