Skip to content

Commit

Permalink
Add generateAll.ts script
Browse files Browse the repository at this point in the history
  • Loading branch information
oktapodia committed Aug 29, 2024
1 parent 35045d3 commit dae8a11
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions generateAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
ChainType,
createConfig,
EVM,
getTokens,
Solana,
} from "@lifi/sdk";
import * as fs from 'node:fs/promises';
import * as path from 'node:path';

createConfig({
providers: [EVM(), Solana()],
integrator: "lifi",
preloadChains: false,
});

async function getNativeTokens() {
const tokens = await getTokens({
chainTypes: [ChainType.EVM, ChainType.SVM],
});

return Object.entries(tokens.tokens).map(([, value]) => {
return value?.[0];
});
}

const tokensFolderPath = path.join(__dirname, "tokens");
const outputFilePath = path.join(__dirname, 'tokens', 'all.json');

async function getChainsTopTokens() {
const files = await fs.readdir(tokensFolderPath);

let allTokens = [];

for (const file of files) {
const filePath = path.join(tokensFolderPath, file);
console.log(`Parsing file ${filePath}`);
const data = await fs.readFile(filePath, "utf8");

if (!data) {
throw new Error(`File ${file} is not found.`);
}

const parsedData = JSON.parse(data);

if (!Array.isArray(parsedData)) {
throw new Error(`File ${file} does not contains an array.`);
}

allTokens = allTokens.concat(parsedData);
}

return allTokens;
}

(async () => {
await getChainsTopTokens();

await fs.writeFile(outputFilePath, JSON.stringify([...await getNativeTokens(), ...await getChainsTopTokens()], null, 2), 'utf8');
console.log(`All tokens have been written to ${outputFilePath}`);
})();

0 comments on commit dae8a11

Please sign in to comment.