-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathswap.ts
170 lines (150 loc) · 4.32 KB
/
swap.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { SwapERC20 } from "@airswap/libraries";
import {
createOrderERC20,
createOrderERC20Signature,
orderERC20ToParams,
toDecimalString,
} from "@airswap/utils";
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 Delegate = require("@airswap/delegate/build/contracts/Delegate.sol/Delegate.json");
const delegateDeploys = require("@airswap/delegate/deploys.js");
const IERC20 = require("@airswap/utils/build/src/abis/ERC20.json");
export default class DelegateSwap extends Command {
public static description = "swap with the delegate";
public async run() {
try {
const wallet = await getWallet(this, true);
const chainId = (await wallet.provider.getNetwork()).chainId;
const metadata = await utils.getMetadata(this, chainId);
utils.displayDescription(this, DelegateSwap.description, chainId);
const delegateContract = new ethers.Contract(
delegateDeploys[chainId],
Delegate.abi,
wallet,
);
this.log(chalk.white(`Delegate contract: ${delegateContract.address}\n`));
const { senderWallet }: any = await get({
senderWallet: {
description: "delegator wallet",
type: "Address",
},
});
const { senderAmount }: any = await get({
senderAmount: {
description: "buy amount",
type: "Number",
},
});
const { senderToken }: any = await getTokens(
{ senderToken: "of token" },
metadata,
);
const { signerAmount }: any = await get({
signerAmount: {
description: "for amount",
type: "Number",
},
});
const { signerToken }: any = await getTokens(
{ signerToken: "of token" },
metadata,
);
const rules = await delegateContract.rules(
senderWallet,
senderToken.address,
signerToken.address,
);
if (rules[0] === ethers.constants.AddressZero) {
throw new Error(
"No rules found for the selected account and token pair.",
);
}
const swapContract = SwapERC20.getAddress(chainId);
const protocolFee = await SwapERC20.getContract(
wallet.provider,
chainId,
).protocolFee();
const unsignedOrder = createOrderERC20({
nonce: String(Date.now()),
expiry: String(Math.round(Date.now() / 1000) + 120),
protocolFee: protocolFee.toString(),
signerWallet: wallet.address,
signerToken: signerToken.address,
signerAmount: utils
.getAtomicValue(signerAmount, signerToken.address, metadata)
.toFixed(),
senderWallet: delegateContract.address,
senderToken: senderToken.address,
senderAmount: utils
.getAtomicValue(senderAmount, senderToken.address, metadata)
.toFixed(),
});
const signature = await createOrderERC20Signature(
unsignedOrder,
wallet.privateKey,
swapContract,
chainId,
);
const order = {
...unsignedOrder,
...signature,
};
this.log();
if (
await confirm(
this,
metadata,
"swap",
{
signerWallet: order.signerWallet,
signerToken: order.signerToken,
signerAmount: `${order.signerAmount} (${chalk.cyan(
toDecimalString(
order.signerAmount,
metadata.byAddress[signerToken.address].decimals,
),
)})`,
senderWallet: `${delegateContract.address} (${chalk.cyan("Delegate")})`,
senderToken: order.senderToken,
senderAmount: `${order.senderAmount} (${chalk.cyan(
toDecimalString(
order.senderAmount,
metadata.byAddress[senderToken.address].decimals,
),
)})`,
},
chainId,
)
) {
delegateContract
.swap(senderWallet, ...orderERC20ToParams(order))
.then(utils.handleTransaction)
.catch(utils.handleError);
}
} catch (e) {
cancelled(e);
}
}
public async validateAmount(amount: number) {
if (!Number.isInteger(amount) && amount <= 0) {
throw new Error(`${amount} is invalid.`);
}
}
public async validateSenderWallet(
address: string,
wallet: any,
delegateContract: any,
) {
if (address !== wallet.address) {
const { authorizedWallet } = await delegateContract.authorized(address);
if (!authorizedWallet || authorizedWallet !== wallet.address) {
throw new Error(`Address ${address}: Unauthorized wallet.`);
}
}
}
}