-
Notifications
You must be signed in to change notification settings - Fork 12
/
delete.ts
83 lines (72 loc) · 2.04 KB
/
delete.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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 MetadataDelete extends Command {
public static description = "delete token from local metadata";
public async run() {
try {
const provider = await utils.getProvider(this);
const chainId = (await provider.getNetwork()).chainId;
this.log();
utils.displayDescription(this, MetadataDelete.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("Token not found in metadata.\n");
} else {
this.log(
`${token.symbol} (${token.name}) · ${explorerUrls[chainId]}/address/${token.address} · ${token.decimals} decimals`,
);
const { confirm }: any = await get({
confirm: {
description: chalk.white(
`\nType "yes" to remove this token (${token.symbol}) from local metadata`,
),
},
});
if (confirm === "yes") {
delete metadata.byAddress[token.address];
delete metadata.bySymbol[token.symbol];
await fs.outputJson(metadataPath, metadata);
this.log(chalk.green("Local metadata updated\n"));
} else {
this.log("\nCancelled.\n");
}
}
} catch (e) {
cancelled(e);
}
}
}