Skip to content

Commit

Permalink
Merge pull request #29 from map3xyz/network-codes
Browse files Browse the repository at this point in the history
Get Networks with Fungible Assets API
  • Loading branch information
pellicceama authored Sep 20, 2022
2 parents 296cafb + af7e07b commit be25b59
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 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.135",
"version": "1.0.136",
"description": "A library for maintaining the assets repo.",
"author": "pellicceama",
"keywords": [
Expand Down
11 changes: 10 additions & 1 deletion src/networks/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from "ava";
import { getAssetMapping, getNetworks } from ".";
import { getAssetMapping, getNetworks, getNetworksWithAssets } from ".";

test("networks includes ethereum", async (t) => {
const networks = await getNetworks();
Expand All @@ -11,3 +11,12 @@ test("Mappings can be read successfully", async (t) => {
t.truthy(mappings.length > 0);
});

test("networks with assets includes polygon", async (t) => {
const networks = await getNetworksWithAssets();
t.truthy(networks.find((n) => n.name === "Polygon"));
});

test("networks with assets does not include Bitcoin", async (t) => {
const networks = await getNetworksWithAssets();
t.falsy(networks.find((n) => n.name === "Bitcoin"));
});
38 changes: 38 additions & 0 deletions src/networks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,44 @@ export async function getNetworks(dir?: string): Promise<Network[]> {
}
}

export async function getNetworksWithAssets(dir?: string): Promise<Network[]> {
if (!dir) {
dir = DEFAULT_REPO_DISK_LOCATION;
}

const res: Network[] = [];

try {
await cloneOrPullRepoAndUpdateSubmodules(REPO_CLONE_URL, dir, true, "master");

const directories = await getDirectories(dir);

directories.forEach((directory) => {
const split = directory.split("/");
const baseDirIsNetworkDir = split[split.length - 2] === "networks";
const dirIsGitDir = directory.includes(".git")

if (baseDirIsNetworkDir && !dirIsGitDir) {
const networkName = split[split.length - 1];

const networkHasAssets = fs.existsSync(`${directory}/assets/${networkName}-tokenlist`);

if(networkHasAssets) {
res.push(readAndParseJson(`${directory}/info.json`));
}
}
});

if (res.length === 0) {
throw new Error(`getNetworks No networks found in ${dir}`);
}

return res;
} catch (err) {
throw err;
}
}

export async function getAssetsForNetwork(network: string, dir?: string): Promise<Asset[]> {
if (!dir) {
dir = DEFAULT_REPO_DISK_LOCATION;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/git.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from "ava";
import fs from "fs";
import { cloneOrPullRepoAndUpdateSubmodules, getCommitId } from "./git";
import { cloneOrPullRepoAndUpdateSubmodules, commit, getCommitId } from "./git";

const REPO_CLONE_URL = "[email protected]:map3xyz/assets.git";
const DEFAULT_REPO_DISK_LOCATION = "./tmp/map3xyz-assets";
Expand All @@ -21,7 +21,7 @@ test("we are able to get the commit id of a tag", async (t) => {
try {
await cloneOrPullRepoAndUpdateSubmodules(REPO_CLONE_URL, DEFAULT_REPO_DISK_LOCATION, true, "master");
const commitId = await getCommitId(DEFAULT_REPO_DISK_LOCATION, "HEAD");
t.true(commitId.length === 16);
t.true(commitId.length === 16 || commitId.length === 8);
} catch (err) {
t.fail(err.message);
}
Expand Down

0 comments on commit be25b59

Please sign in to comment.