/
deposit.ts
61 lines (56 loc) · 2.08 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
import chalk from 'chalk'
import { ethers } from 'ethers'
import { Command } from '@oclif/command'
import * as utils from '../../lib/utils'
import { get, confirm, cancelled } from '../../lib/prompt'
import { toDecimalString } from '@airswap/utils'
const WETH9 = require('@airswap/tokens/build/contracts/WETH9.json')
export default class IntentUnset extends Command {
static description = 'deposit eth to weth'
async run() {
try {
const wallet = await utils.getWallet(this, true)
const chainId = String((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.bySymbol['WETH']
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)
}
}
}