-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from map3xyz/arshadm/get-mapping
Added support for reading asset mappings
- Loading branch information
Showing
9 changed files
with
148 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export class AssetMapping { | ||
primaryId: string; // id of the asset on the primary network | ||
primaryNetwork: string; // the primary network code (e.g. ethereum) | ||
mapping: { | ||
[network: string]: [string]; | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import test from 'ava'; | ||
import { getNetworks } from '.'; | ||
import test from "ava"; | ||
import { getAssetMapping, getNetworks } from "."; | ||
|
||
test("networks includes ethereum", async (t) => { | ||
const networks = await getNetworks(); | ||
t.truthy(networks.find((n) => n.name === "Ethereum")); | ||
}); | ||
|
||
test("Mappings can be read successfully", async (t) => { | ||
const mappings = await getAssetMapping(); | ||
t.truthy(mappings.length > 0); | ||
}); | ||
|
||
test('networks includes ethereum', async t => { | ||
const networks = await getNetworks(); | ||
t.truthy(networks.find(n => n.name === 'Ethereum')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,109 @@ | ||
import { parse } from "csv-parse/sync"; | ||
import fs from "fs"; | ||
import { Asset } from "../model"; | ||
import { AssetMapping } from "../model/AssetMapping"; | ||
import { Network } from "../model/Network"; | ||
import { cloneOrPullRepoAndUpdateSubmodules } from "../utils"; | ||
import { DEFAULT_REPO_DISK_LOCATION, REPO_CLONE_URL } from "../utils/constants"; | ||
import { getDirectories, readAndParseJson } from "../utils/filesystem"; | ||
|
||
export async function getNetworks(dir?: string): Promise<Network[]> { | ||
if(!dir) { | ||
dir = DEFAULT_REPO_DISK_LOCATION | ||
} | ||
if (!dir) { | ||
dir = DEFAULT_REPO_DISK_LOCATION; | ||
} | ||
|
||
const res: Network[] = []; | ||
const res: Network[] = []; | ||
|
||
try { | ||
try { | ||
await cloneOrPullRepoAndUpdateSubmodules(REPO_CLONE_URL, dir, true, "master"); | ||
|
||
await cloneOrPullRepoAndUpdateSubmodules(REPO_CLONE_URL, dir, true, 'master'); | ||
const directories = await getDirectories(dir); | ||
|
||
const directories = await getDirectories(dir); | ||
directories.forEach((directory) => { | ||
const split = directory.split("/"); | ||
|
||
directories.forEach(directory => { | ||
const split = directory.split('/'); | ||
if (split[split.length - 2] === "networks" && !directory.includes(".git")) { | ||
res.push(readAndParseJson(`${directory}/info.json`)); | ||
} | ||
}); | ||
|
||
if(split[split.length - 2] === 'networks' && !directory.includes('.git')) { | ||
res.push(readAndParseJson(`${directory}/info.json`)); | ||
} | ||
}); | ||
|
||
if(res.length === 0) { | ||
throw new Error(`getNetworks No networks found in ${dir}`); | ||
} | ||
if (res.length === 0) { | ||
throw new Error(`getNetworks No networks found in ${dir}`); | ||
} | ||
|
||
return res; | ||
} catch (err) { | ||
throw err; | ||
} | ||
return res; | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
|
||
export async function getAssetsForNetwork(network: string, dir?: string): Promise<Asset[]> { | ||
if(!dir) { | ||
dir = DEFAULT_REPO_DISK_LOCATION | ||
} | ||
|
||
const res: Asset[] = []; | ||
|
||
try { | ||
await cloneOrPullRepoAndUpdateSubmodules(REPO_CLONE_URL, dir, true, 'master'); | ||
|
||
const tokenlistDir = `${dir}/networks/${network}/assets/${network}-tokenlist`; | ||
|
||
if(!fs.existsSync(tokenlistDir)) { | ||
return []; | ||
} | ||
|
||
// TODO, make it work for multiple tokenlists | ||
const assetDirs = await getDirectories(tokenlistDir); | ||
|
||
assetDirs.forEach(directory => { | ||
const split = directory.split('/'); | ||
|
||
if(split[split.length - 2] === `${network}-tokenlist` && !directory.includes('.git')) { | ||
res.push(readAndParseJson(`${directory}/info.json`)); | ||
} | ||
}); | ||
|
||
return res; | ||
}catch (err) { | ||
throw err; | ||
if (!dir) { | ||
dir = DEFAULT_REPO_DISK_LOCATION; | ||
} | ||
|
||
const res: Asset[] = []; | ||
|
||
try { | ||
await cloneOrPullRepoAndUpdateSubmodules(REPO_CLONE_URL, dir, true, "master"); | ||
|
||
const tokenlistDir = `${dir}/networks/${network}/assets/${network}-tokenlist`; | ||
|
||
if (!fs.existsSync(tokenlistDir)) { | ||
return []; | ||
} | ||
} | ||
|
||
// TODO, make it work for multiple tokenlists | ||
const assetDirs = await getDirectories(tokenlistDir); | ||
|
||
assetDirs.forEach((directory) => { | ||
const split = directory.split("/"); | ||
|
||
if (split[split.length - 2] === `${network}-tokenlist` && !directory.includes(".git")) { | ||
res.push(readAndParseJson(`${directory}/info.json`)); | ||
} | ||
}); | ||
|
||
return res; | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
|
||
export async function getAssetMapping(dir: string = DEFAULT_REPO_DISK_LOCATION): Promise<AssetMapping[]> { | ||
const parseRecord = (networks, record) => { | ||
const assetMapping = new AssetMapping(); | ||
assetMapping.primaryId = record.primaryId; | ||
assetMapping.primaryNetwork = record.primaryNetwork; | ||
assetMapping.mapping = {}; | ||
|
||
networks | ||
.filter((network) => network != record.primaryNetwork) | ||
.filter((network) => record[network].length > 0) | ||
.forEach((network) => { | ||
const identifiers = record[network].split(";"); | ||
assetMapping.mapping[network] = identifiers; | ||
}); | ||
|
||
return assetMapping; | ||
}; | ||
|
||
const filename = `${dir}/assets.tsv`; | ||
const data = fs.readFileSync(filename); | ||
const records = parse(data, { | ||
delimiter: "\t", | ||
columns: true, | ||
skip_empty_lines: true, | ||
}); | ||
|
||
const networks = Object.keys(records[0]).filter( | ||
(key) => !["primaryId", "primaryNetwork", "name", "symbol"].includes(key) | ||
); | ||
|
||
return Promise.resolve( | ||
records | ||
.map((record) => parseRecord(networks, record)) | ||
.filter((assetMapping) => Object.keys(assetMapping.mapping).length > 0) | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters