/
approve.ts
53 lines (48 loc) · 1.76 KB
/
approve.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
import chalk from 'chalk'
import { Command } from '@oclif/command'
import { ethers } from 'ethers'
import * as utils from '../../lib/utils'
import { getTokens, confirm, cancelled } from '../../lib/prompt'
import constants from '../../lib/constants.json'
const IERC20 = require('@airswap/tokens/build/contracts/IERC20.json')
const swapDeploys = require('@airswap/swap/deploys.json')
export default class TokenApprove extends Command {
static description = 'approve a token for trading'
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, TokenApprove.description, chainId)
const swapAddress = swapDeploys[chainId]
const { token }: any = await getTokens({ token: 'token' }, metadata)
this.log()
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 approved for trading\n`))
} else {
if (
await confirm(
this,
metadata,
'approve',
{
token: `${token.address} (${token.symbol})`,
spender: `${swapAddress} (Swap)`,
},
chainId,
)
) {
tokenContract
.approve(swapAddress, constants.APPROVAL_AMOUNT, { gasPrice })
.then(utils.handleTransaction)
.catch(utils.handleError)
}
}
} catch (e) {
cancelled(e)
}
}
}