-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathadd.ts
134 lines (124 loc) · 3.74 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
import { Registry } from "@airswap/libraries";
import { ProtocolIds, protocolNames } from "@airswap/utils";
import { Command } from "@oclif/command";
import chalk from "chalk";
import { getTable } from "console.table";
import { ethers } from "ethers";
import { cancelled, confirm, get } 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 ProtocolsAdd extends Command {
public static description = "add supported protocols 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, ProtocolsAdd.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("Protocols currently activated:\n");
const result = [];
activatedProtocols.map((id) => {
result.push({
id,
label: protocolNames[id],
});
});
this.log(getTable(result));
} else {
this.log("No protocols are currently activated.\n");
}
this.log(
`${chalk.bold(
"Available protocols",
)} (More info: https://about.airswap.io/technology/protocols)\n`,
);
for (const i in ProtocolIds) {
this.log(`· ${ProtocolIds[i]} (${i})`);
}
this.log();
const { protocolId }: any = await get({
protocolId: {
type: "Protocol",
description: "protocol id to activate",
},
});
this.log();
const stakingToken =
metadata.byAddress[stakingTokenContract.address.toLowerCase()];
const supportCost = await registryContract.supportCost();
const balance = await stakingTokenContract.balanceOf(wallet.address);
if (balance.lt(supportCost)) {
this.log(
`Insufficient 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(supportCost.toString(), stakingToken.decimals)
.toString()}\n`,
);
} else {
if (
await confirm(
this,
metadata,
"addProtocols",
{
protocols: `${protocolId} (${protocolNames[protocolId]})`,
stake: `${ethers.utils
.formatUnits(supportCost.toString(), stakingToken.decimals)
.toString()} ${stakingToken.symbol}`,
},
chainId,
)
) {
registryContract
.addProtocols([protocolId])
.then(utils.handleTransaction)
.catch(utils.handleError);
}
}
} catch (e) {
cancelled(e);
}
}
}