-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdeposit.ts
75 lines (69 loc) · 2.21 KB
/
deposit.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 { toDecimalString } from "@airswap/utils";
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";
const wethDeploys = require("@airswap/wrapper/deploys-weth.js");
const WETH9 = require("@airswap/wrapper/build/contracts/WETH9.sol/WETH9.json");
export default class IntentUnset extends Command {
public static description = "deposit eth to weth";
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, IntentUnset.description, chainId);
const WETH = metadata.byAddress[wethDeploys[chainId].toLowerCase()];
if (!WETH) {
throw new Error("Wrapped token not found for the selected chain.");
}
const balance = await wallet.provider.getBalance(wallet.address);
const balanceDecimal = toDecimalString(
balance.toString(),
metadata.byAddress[WETH.address].decimals,
);
this.log(`ETH available to deposit: ${chalk.bold(balanceDecimal)}`);
this.log(
chalk.gray("Some ETH must be saved to execute the transaction.\n"),
);
const { amount }: any = await get({
amount: {
description: "amount to deposit",
type: "Number",
},
});
const atomicAmount = utils.getAtomicValue(amount, WETH.address, metadata);
if (atomicAmount.eq(0)) {
cancelled("Amount must be greater than zero.");
} else if (balance.lt(atomicAmount.toString())) {
cancelled("Insufficient balance.");
} else {
this.log();
if (
await confirm(
this,
metadata,
"deposit",
{
"[value]": `${atomicAmount} (${chalk.cyan(amount)})`,
},
chainId,
)
) {
new ethers.Contract(WETH.address, WETH9.abi, wallet)
.deposit({
value: ethers.BigNumber.from(atomicAmount.toFixed()),
gasPrice,
})
.then(utils.handleTransaction)
.catch(utils.handleError);
}
}
} catch (e) {
cancelled(e);
}
}
}