-
Notifications
You must be signed in to change notification settings - Fork 12
/
revoke.ts
75 lines (69 loc) · 1.89 KB
/
revoke.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
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 IERC20 = require("@airswap/utils/build/src/abis/ERC20.json");
const swapDeploys = require("@airswap/swap-erc20/deploys.js");
export default class Revoke extends Command {
public static description = "revoke a token approval";
public async run() {
try {
const wallet = await getWallet(this, true);
const chainId = (await wallet.provider.getNetwork()).chainId;
const metadata = await utils.getMetadata(this, chainId);
const gasPrice = await utils.getGasPrice(this);
utils.displayDescription(this, Revoke.description, chainId);
const { token }: any = await getTokens({ token: "token" }, metadata);
const { contract }: any = await get({
contract: {
description: "swap contract",
type: "Address",
default: swapDeploys[chainId],
},
});
this.log();
let swapAddress = contract;
if (!contract && swapDeploys[chainId]) {
swapAddress = swapDeploys[chainId];
}
const tokenContract = new ethers.Contract(
token.address,
IERC20.abi,
wallet,
);
const allowance = await tokenContract.allowance(
wallet.address,
swapAddress,
);
if (allowance.eq(0)) {
this.log(
chalk.yellow(
`${token.symbol} is already revoked (swap contract: ${swapAddress})\n`,
),
);
} else {
if (
await confirm(
this,
metadata,
"revoke",
{
token: `${token.address} (${token.symbol})`,
spender: `${swapAddress} (Swap)`,
},
chainId,
)
) {
tokenContract
.approve(swapAddress, "0", { gasPrice })
.then(utils.handleTransaction)
.catch(utils.handleError);
}
}
} catch (e) {
cancelled(e);
}
}
}