-
Notifications
You must be signed in to change notification settings - Fork 12
/
transfer.ts
70 lines (65 loc) · 1.79 KB
/
transfer.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
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");
export default class Transfer extends Command {
public static description = "transfer tokens to another account";
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, Transfer.description, chainId);
const { token }: any = await getTokens({ token: "token" }, metadata);
const { amount, recipient }: any = await get({
amount: {
description: "amount",
type: "Number",
},
recipient: {
description: "recipient",
type: "Address",
},
});
const atomicAmount = utils.getAtomicValue(
amount,
token.address,
metadata,
);
const tokenContract = new ethers.Contract(
token.address,
IERC20.abi,
wallet,
);
const tokenBalance = await tokenContract.balanceOf(wallet.address);
if (tokenBalance.lt(atomicAmount.toString())) {
cancelled("Insufficient balance.");
} else {
this.log();
if (
await confirm(
this,
metadata,
"transfer",
{
to: recipient,
value: `${atomicAmount} (${chalk.cyan(amount)})`,
},
chainId,
)
) {
tokenContract
.transfer(recipient, atomicAmount.toFixed(), { gasPrice })
.then(utils.handleTransaction)
.catch(utils.handleError);
}
}
} catch (e) {
cancelled(e);
}
}
}