-
Notifications
You must be signed in to change notification settings - Fork 12
/
add.ts
135 lines (127 loc) · 3.97 KB
/
add.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
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
import { Registry } from "@airswap/libraries";
import { Command } from "@oclif/command";
import chalk from "chalk";
import { getTable } from "console.table";
import { ethers } from "ethers";
import { cancelled, confirm, getTokenList } from "../../lib/prompt";
import * as utils from "../../lib/utils";
import { getWallet } from "../../lib/wallet";
const IERC20 = require("@airswap/utils/build/src/abis/ERC20.json");
export default class TokensAdd extends Command {
public static description = "add supported tokens to the registry";
public async run() {
try {
const wallet = await getWallet(this, true);
const chainId = (await wallet.provider.getNetwork()).chainId;
const metadata = await utils.getMetadata(this, chainId);
utils.displayDescription(this, TokensAdd.description, chainId);
this.log(chalk.white(`Registry ${Registry.getAddress(chainId)}\n`));
const registryContract = Registry.getContract(wallet, chainId);
const stakingTokenContract = new ethers.Contract(
await registryContract.stakingToken(),
IERC20.abi,
wallet,
);
const allowance = await stakingTokenContract.allowance(
wallet.address,
Registry.getAddress(chainId),
);
if (allowance.eq(0)) {
this.log(chalk.yellow("Registry is not approved"));
this.log(
`Enable usage of the registry with ${chalk.bold(
"registry:approve",
)}\n`,
);
process.exit(0);
}
const url = (
await registryContract.getServerURLsForStakers([wallet.address])
)[0];
if (!url) {
this.log(chalk.yellow("Server URL is not set"));
this.log(`Set your server URL with ${chalk.bold("registry:url")}\n`);
process.exit(0);
} else {
this.log(chalk.white(`Server URL ${chalk.bold(url)}\n`));
}
const activatedProtocols = await registryContract.getProtocolsForStaker(
wallet.address,
);
if (!activatedProtocols.length) {
this.log(chalk.yellow("No activated protocols"));
this.log(
`Add protocols you support with ${chalk.bold("protocols:add")}\n`,
);
process.exit(0);
}
const activatedTokens = await registryContract.getTokensForStaker(
wallet.address,
);
if (activatedTokens.length) {
this.log("Tokens currently activated:\n");
const result = [];
activatedTokens.map((address) => {
const token = metadata.byAddress[address.toLowerCase()];
result.push({
address: token ? token.address : address,
symbol: token ? token.symbol : "?",
});
});
this.log(getTable(result));
}
const tokens: any = await getTokenList(
metadata,
"token symbols to activate (comma separated)",
);
const tokenAddresses = [];
const tokenLabels = [];
for (const i in tokens) {
tokenAddresses.push(tokens[i].address);
tokenLabels.push(`${tokens[i].address} (${tokens[i].symbol})`);
}
const stakingToken =
metadata.byAddress[stakingTokenContract.address.toLowerCase()];
const supportCost = await registryContract.supportCost();
const totalCost = supportCost.mul(tokenAddresses.length);
const balance = await stakingTokenContract.balanceOf(wallet.address);
if (balance.lt(totalCost)) {
this.log(
`\nInsufficient balance in staking token ${stakingToken.symbol} (${stakingToken.address})\n`,
);
this.log(
`· Balance: ${ethers.utils
.formatUnits(balance.toString(), stakingToken.decimals)
.toString()}`,
);
this.log(
`· Required: ${ethers.utils
.formatUnits(totalCost.toString(), stakingToken.decimals)
.toString()}\n`,
);
} else {
if (
await confirm(
this,
metadata,
"addTokens",
{
tokens: tokenLabels.join("\n"),
stake: `${ethers.utils
.formatUnits(totalCost.toString(), stakingToken.decimals)
.toString()} ${stakingToken.symbol}`,
},
chainId,
)
) {
registryContract
.addTokens(tokenAddresses)
.then(utils.handleTransaction)
.catch(utils.handleError);
}
}
} catch (e) {
cancelled(e);
}
}
}