forked from tinymanorg/asa-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.mjs
218 lines (177 loc) · 6.24 KB
/
build.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import path from "path";
import algosdk from "algosdk";
import { readdir, mkdir, rm, appendFile } from "fs/promises";
import {
existsSync,
mkdirSync,
lstatSync,
readdirSync,
copyFileSync,
} from "fs";
import { fileURLToPath } from "url";
if (!process.env.INDEXER_TOKEN) {
throw new Error("No INDEXER_TOKEN was provided.");
}
const ALGOD_CREDENTIALS = {
algodev: {
indexerToken: process.env.INDEXER_TOKEN,
indexerServer: "https://indexer-mainnet.aws.algodev.network/",
port: 443,
},
};
const indexer = new algosdk.Indexer(
ALGOD_CREDENTIALS.algodev.indexerToken,
ALGOD_CREDENTIALS.algodev.indexerServer,
ALGOD_CREDENTIALS.algodev.port
);
const __filename = fileURLToPath(import.meta.url);
const algoIconsFolderName = "ALGO";
const ASSET_LOGO_BASE_URL = "https://asa-list.tinyman.org";
const currentDirname = path.dirname(__filename);
const buildDirectory = path.join(currentDirname, "build");
const ASSET_ICON_MAP = new Map();
const ASSET_MAP = new Map();
try {
if (existsSync(buildDirectory)) {
await rm(buildDirectory, { recursive: true });
}
await mkdir(buildDirectory);
await mkdir(`${buildDirectory}/assets`);
const files = await readdir(path.join(currentDirname, "assets"));
const assetIDs = [];
for (const file of files) {
try {
const assetFolderPath = path.join(currentDirname, "assets", file);
if (lstatSync(assetFolderPath).isDirectory()) {
const folderFiles = await readdir(assetFolderPath);
if (folderFiles.some((folderFile) => folderFile.includes("icon.png"))) {
const targetDirName = createTargetDirectoryName(path.basename(file));
copyDirectorySync(
assetFolderPath,
path.join(buildDirectory, "assets"),
targetDirName
);
// if targetDirName is an asset id, we fetch the asset information and add it to assets.json
if (Number(targetDirName) >= 0) {
assetIDs.push(Number(targetDirName));
} else {
// adds icon placeholder to ASSET_ICON_MAP
ASSET_ICON_MAP.set(targetDirName, getAssetLogo(targetDirName));
}
}
}
} catch (error) {
console.error(error);
throw error;
}
}
console.log(`┏━━━ Fetching asset data from the indexer ━━━━━━━━━━━━`);
Promise.all(assetIDs.map(getAssetInformationById)).then(async (assetData) => {
assetData.forEach((asset) => {
ASSET_MAP.set(asset.id, asset);
ASSET_ICON_MAP.set(asset.id, asset.logo.png);
});
// Create icons.json file and save it under /build directory
console.log(`\n┏━━━ Creating icons.json ━━━━━━━━━━━━`);
const assetIcons = Object.fromEntries(ASSET_ICON_MAP);
const assetIconsJSON = JSON.stringify(assetIcons, null, "\t");
await appendFile(path.join(buildDirectory, "icons.json"), assetIconsJSON);
console.log(`━━━━━━━━━━━━ Created icons.json ━━━━━━━━━━━━\n`);
// Create assets.json file and save it under /build directory
console.log(`┏━━━ Creating assets.json ━━━━━━━━━━━━`);
const assets = Object.fromEntries(ASSET_MAP);
const assetsJSON = JSON.stringify(assets, null, "\t");
await appendFile(path.join(buildDirectory, "assets.json"), assetsJSON);
console.log(`━━━━━━━━━━━━ Finished ━━━━━━━━━━━━`);
});
} catch (error) {
console.error(error);
throw error;
}
/**
* @param {string} source
* @returns Directory name that includes only asset ids
*/
function createTargetDirectoryName(source) {
// If there is a match, the last capturing group will always be the asset id
const idMatchResult = source.match(/(.*)-(\d+)/);
let targetDirName = source;
if (idMatchResult?.length) {
targetDirName = idMatchResult[idMatchResult.length - 1];
} else if (source.toLowerCase() === algoIconsFolderName.toLowerCase()) {
targetDirName = "0";
}
return targetDirName;
}
function copyDirectorySync(source, target, targetDirName) {
let files = [];
const targetFolder = path.join(target, targetDirName);
if (!existsSync(targetFolder)) {
console.log(`┏━━━ Creating ${targetFolder} ━━━━━━━━━━━━`);
mkdirSync(targetFolder);
}
if (lstatSync(source).isDirectory()) {
console.log(
`
Copying contents of ${source} into ${targetFolder} ━━━━━━━━━━━━`
);
files = readdirSync(source);
files.forEach(function (file) {
const curSource = path.join(source, file);
if (lstatSync(curSource).isDirectory()) {
copyDirectorySync(curSource, targetFolder);
} else {
console.log(`••••• Copying ${curSource}`);
copyFileSync(curSource, path.join(targetFolder, file));
}
});
console.log(`
━━━━━━━━━━━━ Finished copying ${source} ━━━━━━━━━━━━
`);
}
}
/**
* Generates URL for the asset's logo
*/
function getAssetLogo(assetId, type = "png") {
let logo = `${ASSET_LOGO_BASE_URL}/assets/${assetId}/icon.png`;
if (type === "svg") {
logo = `${ASSET_LOGO_BASE_URL}/assets/${assetId}/icon.svg`;
}
return logo;
}
/**
* Fetches asset information from the indexer
* @param {number} id
*/
async function getAssetInformationById(id) {
try {
if (id === 0) {
return {
id: `${id}`,
name: "Algorand",
unit_name: "ALGO",
decimals: 6,
url: "https://algorand.org",
total_amount: "6615503326932151",
logo: { png: getAssetLogo(id), svg: getAssetLogo(id, "svg") },
deleted: false,
};
}
console.log(`••••• Looking up asset #${id}`);
const { asset } = await indexer.lookupAssetByID(id).do();
return {
id: `${asset.index}`,
decimals: Number(asset.params.decimals),
name: asset.params.name || "",
unit_name: asset.params["unit-name"] || "",
url: "",
total_amount: String(asset.params.total),
logo: { png: getAssetLogo(id), svg: getAssetLogo(id, "svg") },
deleted: asset.deleted,
};
} catch (error) {
console.error(error);
throw new Error(`Failed to fetch information for asset #${id}`);
}
}