-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathunset.ts
96 lines (85 loc) · 2.51 KB
/
unset.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
import { Command } from "@oclif/command";
import chalk from "chalk";
import { ethers } from "ethers";
import { cancelled, confirm, get, getTokens } from "../../lib/prompt";
import * as utils from "../../lib/utils";
import { getWallet } from "../../lib/wallet";
const Delegate = require("@airswap/delegate/build/contracts/Delegate.sol/Delegate.json");
const delegateDeploys = require("@airswap/delegate/deploys.js");
export default class DelegateUnsetRule extends Command {
public static description = "unset a delegate rule";
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, DelegateUnsetRule.description, chainId);
const delegateContract = new ethers.Contract(
delegateDeploys[chainId],
Delegate.abi,
wallet,
);
this.log(chalk.white(`Delegate contract: ${delegateContract.address}\n`));
const { senderWallet }: any = await get({
senderWallet: {
description: "delegator wallet",
type: "Address",
},
});
await this.validateSenderWallet(senderWallet, wallet, delegateContract);
const { senderToken }: any = await getTokens(
{ senderToken: "of token" },
metadata,
);
const { signerToken }: any = await getTokens(
{ signerToken: "for token" },
metadata,
);
const rules = await delegateContract.rules(
senderWallet,
senderToken.address,
signerToken.address,
);
if (rules[0] === ethers.constants.AddressZero) {
throw new Error(
"No rules found for the selected account and token pair.",
);
}
this.log();
if (
await confirm(
this,
metadata,
"unsetRule",
{
senderWallet,
senderToken: senderToken.address,
signerToken: signerToken.address,
},
chainId,
)
) {
delegateContract
.unsetRule(senderWallet, senderToken.address, signerToken.address)
.then(utils.handleTransaction)
.catch(utils.handleError);
}
} catch (e) {
cancelled(e);
}
}
public async validateSenderWallet(
address: string,
wallet: any,
delegateContract: any,
) {
if (address !== wallet.address) {
const { authorizedWallet } = await delegateContract.authorized(address);
if (!authorizedWallet || authorizedWallet !== wallet.address) {
throw new Error(
`Current account is not a manager for ${address}. Use ${chalk.bold("delegate:authorize")} to authorize managers.`,
);
}
}
}
}