-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathurl.ts
99 lines (91 loc) · 2.67 KB
/
url.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
import { Command } from "@oclif/command";
import chalk from "chalk";
import { ethers } from "ethers";
import { cancelled, confirm, get } from "../../lib/prompt";
import * as utils from "../../lib/utils";
import { getWallet } from "../../lib/wallet";
import { Registry } from "@airswap/libraries";
const IERC20 = require("@airswap/utils/build/src/abis/ERC20.json");
export default class RegistryURL extends Command {
public static description = "set server url on 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, RegistryURL.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`,
);
} else {
const url = (
await registryContract.getServerURLsForStakers([wallet.address])
)[0];
const stakingToken =
metadata.byAddress[stakingTokenContract.address.toLowerCase()];
const stakingCost = await registryContract.stakingCost();
if (url) {
this.log(`Current server url: ${url}\n`);
} else {
const balance = await stakingTokenContract.balanceOf(wallet.address);
if (balance.lt(stakingCost)) {
this.log(
`Insufficient balance in staking token (${stakingTokenContract.address})\n`,
);
this.log(
`· Balance: ${ethers.utils
.formatUnits(balance.toString(), stakingToken.decimals)
.toString()}`,
);
this.log(
`· Required: ${ethers.utils
.formatUnits(stakingCost.toString(), stakingToken.decimals)
.toString()}\n`,
);
} else {
const { newURL }: any = await get({
newURL: {
type: "Locator",
description: "server url",
},
});
this.log();
if (
await confirm(
this,
metadata,
"setURL",
{
url: newURL,
},
chainId,
)
) {
registryContract
.setServerURL(newURL)
.then(utils.handleTransaction)
.catch(utils.handleError);
}
}
}
}
} catch (e) {
cancelled(e);
}
}
}