Skip to content

Commit

Permalink
improving address formatting function
Browse files Browse the repository at this point in the history
  • Loading branch information
pellicceama committed Nov 3, 2022
1 parent 099b331 commit b9cebf9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/repo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ export function getAssetMaps(networkCode: string, address: string): AssetMap[] {
}

export function addAssetMap(map: AssetMap, repoPath: string = DEFAULT_REPO_DISK_LOCATION) {
const assetMapInfoFile = path.join(repoPath, getDirPathForTokenlist(map.fromNetwork, map.fromAddress), 'maps.json');
const assetDir = path.join(repoPath, getDirPathForTokenlist(map.fromNetwork, map.fromAddress));
const assetMapInfoFile = path.join(assetDir, 'maps.json');

if(!fs.existsSync(assetMapInfoFile)) {
if(fs.existsSync(assetDir) && !fs.existsSync(assetMapInfoFile)) {
fs.writeFileSync(assetMapInfoFile, JSON.stringify([map], null, 2));
return;
}
Expand Down
13 changes: 10 additions & 3 deletions src/utils/addresses.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { toChecksumAddress, checkAddressChecksum } from 'ethereum-checksum-address';
import { toChecksumAddress } from 'ethereum-checksum-address';

export function formatAddress(address: string): string {
if(!address) {
return null;
}

address = address.replace(/\s/g, "");

if(address.toLowerCase().startsWith('0x')) {
return toChecksumAddress(address);
try {
return toChecksumAddress(address);
} catch (err) {
return null;
}
}

// TODO; in future expand for other non EVM use cases
return address.toLowerCase();
return address.replace(/\s/g, "");
}

export function parseAssetId(assetId: string): { networkCode: string, address: string } {
Expand Down

0 comments on commit b9cebf9

Please sign in to comment.