-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-arbitrage.js
238 lines (211 loc) · 6.98 KB
/
run-arbitrage.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
require('dotenv').config();
const HDWalletProvider = require('@truffle/hdwallet-provider');
const Web3 = require('web3');
const abis = require('./abis');
const { mainnet: addresses } = require('./addresses');
//const { testnet: addresses } = require('./addresses');
const wsProvider = new Web3.providers.WebsocketProvider(process.env.WSS_URL);
HDWalletProvider.prototype.on = wsProvider.on.bind(wsProvider);
let provider = new HDWalletProvider({
mnemonic: {
phrase: process.env.MNEMONIC,
},
providerOrUrl: wsProvider,
});
const web3 = new Web3(provider);
getAccounts = async () => {
let accounts = await web3.eth.getAccounts();
//console.log(accounts);
return accounts;
};
const amountIn = web3.utils.toBN(web3.utils.toWei('100'));
//const Flashloan = require('./build/contracts/Flashloan.json');
const DIRECTION = {
BAKERY_TO_PANCAKE: 0,
PANCAKE_TO_BAKERY: 1,
};
const PancakeSwap = new web3.eth.Contract(
abis.pancakeSwap.router,
addresses.pancakeSwap.router
);
const BakerySwap = new web3.eth.Contract(
abis.bakerySwap.router,
addresses.bakerySwap.router
);
const repayAmount = amountIn - amountIn * 0.997 + amountIn;
const tokens = Object.keys(addresses.tokens).map((key) => [
key,
addresses.tokens[key],
]);
const init = async () => {
const accounts = await getAccounts();
const admin = accounts[0];
//console.log(admin);
const networkId = await web3.eth.net.getId();
//console.log(tokens);
//console.log(tokens[0][1]);
/* const flashloan = new web3.eth.Contract(
Flashloan.abi,
Flashloan.networks[networkId].address
); */
web3.eth
.subscribe('newBlockHeaders')
.on('data', async (block) => {
console.log(`New block received. Block # ${block.number}`);
//console.log(tokens.length);
try {
await scan();
} catch (error) {
console.log(error);
}
/*
if (BTP.gt(amountIn)) {
const gasPrice = await web3.eth.getGasPrice();
//200000 is picked arbitrarily, have to be replaced by actual tx cost in next lectures, with Web3 estimateGas()
/* const txCost = flashloan.methods
.startArbitrage(
addresses.tokens.DAI,
addresses.tokens.WBNB,
amountInDai,
0,
DIRECTION.BAKERY_TO_PANCAKE,
repayAmount.toString()
)
.estimateGas(
{
from: admin,
gasPrice: gasPrice,
},
function (error, estimatedGas) {}
);
const txCost = 200000 * parseInt(gasPrice);
const profit = amountsOut2[1].sub(amountIn).sub(txCost);
if (profit > 0) {
console.log('Arb opportunity found Bakery -> Pancake!');
console.log(
`Expected profit: ${web3.utils.fromWei(profit.toString)} Dai`
);
/* let tx = flashloan.methods.startArbitrage(
addresses.tokens.DAI,
addresses.tokens.WBNB,
amountInDai,
0,
DIRECTION.BAKERY_TO_PANCAKE,
repayAmount.toString()
);
const data = tx.encodeABI();
const txData = {
from: admin,
to: flashloan.options.address,
data,
gas: gas,
gasPrice: gasPrice,
};
const receipt = await web3.eth.sendTransaction(txData);
console.log(`Transaction hash: ${receipt.transactionHash}`);
}
}
if (PTB.gt(amountIn)) {
const gasPrice = await web3.eth.getGasPrice();
//200000 is picked arbitrarily, have to be replaced by actual tx cost in next lectures, with Web3 estimateGas()
const txCost = 200000 * parseInt(gasPrice);
const profit = amountsOut4[1].sub(amountIn).sub(txCost);
if (profit > 0) {
console.log('Arb opportunity found Pancake -> Bakery!');
console.log(
`Expected profit: ${web3.utils.fromWei(profit.toString())} Dai`
);
/* let tx = flashloan.methods.startArbitrage(
addresses.tokens.DAI,
addresses.tokens.WBNB,
amountInDai,
0,
DIRECTION.PANCAKE_TO_BAKERY,
repayAmount.toString()
);
const data = tx.encodeABI();
const txData = {
from: admin,
to: flashloan.options.address,
data,
gas: gas,
gasPrice: gasPrice,
};
const receipt = await web3.eth.sendTransaction(txData);
console.log(`Transaction hash: ${receipt.transactionHash}`);
}
}*/
})
.on('error', (error) => {
console.log(error);
});
};
init();
const scan = async () => {
try {
for (let i = 1; i < tokens.length; i++) {
const amountsOut1 = await PancakeSwap.methods
.getAmountsOut(amountIn, [addresses.tokens.WBNB, tokens[i][1]])
.call();
const amountsOut2 = await BakerySwap.methods
.getAmountsOut(amountsOut1[1], [tokens[i][1], addresses.tokens.WBNB])
.call();
const amountsOut3 = await BakerySwap.methods
.getAmountsOut(amountIn, [addresses.tokens.WBNB, tokens[i][1]])
.call(); // dai to Wbnb bakeryswap
const amountsOut4 = await PancakeSwap.methods
.getAmountsOut(amountsOut3[1], [tokens[i][1], addresses.tokens.WBNB])
.call(); // Wbnb to dai pancakeswap
/* console.log(
`PancakeSwap -> BakerySwap ${
tokens[i][0]
}. WBNB input / output: ${web3.utils.fromWei(
amountIn.toString()
)} / ${web3.utils.fromWei(amountsOut2[1].toString())}`
);
console.log(
`BakerySwap -> PancakeSwap ${
tokens[i][0]
}. WBNB input / output: ${web3.utils.fromWei(
amountIn.toString()
)} / ${web3.utils.fromWei(amountsOut4[1].toString())}`
); */
const BTP = web3.utils.toBN(amountsOut2[1]);
const PTB = web3.utils.toBN(amountsOut4[1]);
if (BTP.gt(amountIn)) {
console.log(
`PancakeSwap -> BakerySwap ${
tokens[i][0]
}. WBNB input / output: ${web3.utils.fromWei(
amountIn.toString()
)} / ${web3.utils.fromWei(amountsOut2[1].toString())}`
);
console.log(
`BakerySwap -> PancakeSwap ${
tokens[i][0]
}. WBNB input / output: ${web3.utils.fromWei(
amountIn.toString()
)} / ${web3.utils.fromWei(amountsOut4[1].toString())}`
);
}
if (PTB.gt(amountIn)) {
console.log(
`PancakeSwap -> BakerySwap ${
tokens[i][0]
}. WBNB input / output: ${web3.utils.fromWei(
amountIn.toString()
)} / ${web3.utils.fromWei(amountsOut2[1].toString())}`
);
console.log(
`BakerySwap -> PancakeSwap ${
tokens[i][0]
}. WBNB input / output: ${web3.utils.fromWei(
amountIn.toString()
)} / ${web3.utils.fromWei(amountsOut4[1].toString())}`
);
}
}
} catch (error) {
console.log(error);
}
};