-
Notifications
You must be signed in to change notification settings - Fork 0
/
interact.js
193 lines (158 loc) · 6 KB
/
interact.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const OrderProxy = artifacts.require('./OneInchOrderProxy');
const IERC20 = artifacts.require('./IERC20');
const axios = require('axios').default;
const fromWei = web3.utils.fromWei;
const toWei = web3.utils.toWei;
const toBN = web3.utils.toBN;
const BN = web3.utils.BN;
const eth = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
const usdc = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
const dai = '0x6B175474E89094C44Da98b954EedeAC495271d0F'
const uni = '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984';
const esd = '0x36f3fd68e7325a35eb768f1aedaae9ea0689d723';
const userAddr = '0x2f71129b240080C638ac8d993BFF52169E3551c3';
const oneInchAddr = '0x111111125434b319222CdBf8C261674aDB56F3ae';
// ganache-cli should be called with --unlock userAddr,ERC20
// userAddr needs to have ETH, enough ERC20 and should have approved
// ERC20 on oneInch for enough amount
const ERC20 = usdc;
const REWARD_AMOUNT = 0.05;
const ERC20Contract = new web3.eth.Contract(IERC20.abi, ERC20);
const createETHToTokenTx = async ({ dstToken, srcAmount, minReturn, period, user } = {}) => {
const orderProxy = await OrderProxy.deployed();
const createEvent = await orderProxy.create(
eth,
dstToken,
toBN(toWei(`${srcAmount || 1}`)),
toBN(`${minReturn}`),
toBN(`${(period || 1) * 24 * 60 * 60}`),
{ from: user, value: toWei(`${(srcAmount + REWARD_AMOUNT) || 1.05}`) }
)
.then((x) => x.logs.find((x) => x.event == "Create"))
.then((x) => (x ? x.args : null));
return createEvent;
}
const createTokenToTokenTx = async ({ srcToken, dstToken, srcAmount, minReturn, period, user } = {}) => {
const orderProxy = await OrderProxy.deployed();
const createEvent = await orderProxy.create(
srcToken,
dstToken,
toBN("10000000"),
toBN(`${minReturn}`),
toBN(`${(period || 1) * 24 * 60 * 60}`),
{ from: user, value: toWei(`${REWARD_AMOUNT}`) }
)
.then((x) => x.logs.find((x) => x.event == "Create"))
.then((x) => (x ? x.args : null));
return createEvent;
}
async function executeTx({ id, calldata, fromAccount }) {
const orderProxy = await OrderProxy.deployed();
await orderProxy.execute(
toBN(`${id}`),
calldata,
{ from: fromAccount }
);
}
async function tokenToToken() {
const accounts = await web3.eth.getAccounts();
//let balInit = await ERC20Contract.methods.balanceOf(accounts[0]).call();
//console.log('init:');
//console.log(balInit.toString());
await ERC20Contract.methods
.transfer(accounts[0], '10000000')
.send({ from: userAddr });
const params = {
srcToken: ERC20, dstToken: uni, srcAmount: 2, minReturn: 1000, period: 2, user: accounts[0]
};
const approval = await ERC20Contract.methods
.approve(OrderProxy.address, '10000000')
.send({ from: accounts[0] });
console.log(`Approval transaction hash: ${approval.transactionHash}`);
let balBefore = await ERC20Contract.methods.balanceOf(params.user).call();
console.log('before:');
console.log(balBefore.toString());
console.log('======');
const { id } = await createTokenToTokenTx(params);
console.log('TX created: ' + id.toString());
const { data } = await axios.get('https://api.1inch.exchange/v2.0/swap', {
params: {
fromTokenAddress: params.srcToken,
toTokenAddress: params.dstToken,
amount: '10000000',
destReceiver: params.user,
//amount: toWei(`${params.srcAmount}`),
fromAddress: userAddr,
slippage: 1
}
});
//console.log(data)
//console.log(OrderProxy.address);
//const contract = OrderProxy.address.slice(2).toLowerCase();
//const replaced = data.tx.data.replace(/2f71129b240080c638ac8d993bff52169e3551c3/g, contract);
await executeTx({ id, calldata: data.tx.data, fromAccount: accounts[0] }) ;
console.log('======');
let balAfter = await ERC20Contract.methods.balanceOf(params.user).call();
console.log('after:');
console.log(balAfter.toString());
const inst = new web3.eth.Contract(IERC20.abi, uni);
const balUniBefore = await inst.methods.balanceOf(params.user).call();
console.log(balUniBefore);
}
async function ethToToken() {
const accounts = await web3.eth.getAccounts();
const params = {
srcToken: eth, dstToken: ERC20, srcAmount: 2, minReturn: 131862329, period: 2, user: accounts[1]
};
const { data } = await axios.get('https://api.1inch.exchange/v2.0/swap', {
params: {
fromTokenAddress: params.srcToken,
toTokenAddress: params.dstToken,
amount: toWei(`${params.srcAmount}`),
destReceiver: params.user,
fromAddress: userAddr,
slippage: 1
}
});
let balBefore = await ERC20Contract.methods.balanceOf(params.user).call();
console.log('before:');
console.log(balBefore.toString());
console.log('======');
const { id } = await createETHToTokenTx(params);
console.log('TX created: ' + id.toString());
await executeTx({ id, calldata: data.tx.data, fromAccount: accounts[2] }) ;
console.log('======');
let balAfter = await ERC20Contract.methods.balanceOf(params.user).call();
console.log('after:');
console.log(balAfter.toString());
}
async function checkDecoding() {
const orderProxy = await OrderProxy.deployed();
const accounts = await web3.eth.getAccounts();
const params = {
srcToken: eth, dstToken: ERC20, srcAmount: 2, minReturn: 1000, period: 2, user: accounts[0]
};
const { data } = await axios.get('https://api.1inch.exchange/v2.0/swap', {
params: {
fromTokenAddress: params.srcToken,
toTokenAddress: params.dstToken,
destReceiver: params.user,
amount: toWei(`${params.srcAmount}`),
fromAddress: userAddr,
slippage: 1
}
});
const { id } = await orderProxy._decode_(data.tx.data);
const minReturnAmount = await orderProxy._minReturnAmount();
console.log(minReturnAmount.toString());
const guaranteedAmount = await orderProxy._guaranteedAmount();
console.log(guaranteedAmount.toString());
}
async function main() {
await ethToToken();
await tokenToToken();
//await checkDecoding();
}
module.exports = async (callback) => {
main().then(() => callback()).catch(err => callback(err))
}