-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlookup.ts
66 lines (57 loc) · 1.6 KB
/
lookup.ts
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
import * as path from "node:path";
import { chainLabels, explorerUrls } from "@airswap/utils";
import { Command } from "@oclif/command";
import chalk from "chalk";
import * as fs from "fs-extra";
import { cancelled, get } from "../../lib/prompt";
import * as utils from "../../lib/utils";
export default class MetadataLookup extends Command {
public static description = "lookup token in local metadata";
public async run() {
try {
const { chainId } = await utils.getConfig(this);
this.log();
utils.displayDescription(this, MetadataLookup.description, chainId);
const metadataPath = path.join(
this.config.configDir,
`metadata-${chainLabels[chainId]}.json`,
);
const { needle }: any = await get({
needle: {
description: "ticker or address",
type: "String",
},
});
let metadata = {
byAddress: {},
bySymbol: {},
};
if (await fs.pathExists(metadataPath)) {
metadata = require(metadataPath);
}
let token: {
address: string;
symbol: string;
name: string;
decimals: number;
};
if (needle.toUpperCase() in metadata.bySymbol) {
token = metadata.bySymbol[needle.toUpperCase()];
}
if (needle in metadata.byAddress) {
token = metadata.byAddress[needle];
}
this.log();
if (!token) {
this.log(chalk.yellow("Token not found in metadata"));
this.log(`Add a new token with ${chalk.bold("metadata:add")}\n`);
} else {
this.log(
`${token.symbol} (${token.name}) · ${explorerUrls[chainId]}/address/${token.address} · ${token.decimals} decimals\n`,
);
}
} catch (e) {
cancelled(e);
}
}
}