-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-startArbitrage.js
More file actions
338 lines (298 loc) · 13.6 KB
/
script-startArbitrage.js
File metadata and controls
338 lines (298 loc) · 13.6 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const { FlashbotsBundleProvider } = require("@flashbots/ethers-provider-bundle");
const args = require('args-parser')(process.argv) //Expect "collection", "gas"
const MEM = require('./script-updateLocalPools.js')
const GET = require('./script-getQuotes.js')
const readline = require("readline");
const ethers = require('ethers')
const Web3 = require(`web3`)
require("dotenv").config();
const fs = require('fs')
const ethProvider = new ethers.providers.JsonRpcProvider(`https://eth-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`);
const web3 = new Web3(`https://eth-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`);
const toHex = web3.utils.toHex
//@param string, length less than 64
//@return string, appended 64-len 0's to the left
function completeLeft(str){
if (typeof str !== 'string'){throw 'conpleteLeft: ', str, 'is not a string'}
str = str.replace('0x', '')
if (str.length > 64){throw 'comepleteLeft: ', str, 'is already bigger than 64 chars long'}
while(str.length < 64){
str = `0${str}`
}
return str.toLowerCase()
}
//@param string
//@return string, appended 0's to the right so length multiple of 64
function complete64(str){
str = str.replace('0x', '')
const toLen = Math.ceil(str.length/64)
console.log("str: ", str)
console.log("toLen: ", toLen)
while (str.length != 64*toLen){
str = str.concat("0")
}
return str.toLowerCase()
}
//@param listing in seaPortV2 'orders' object
//@return 'txData': raw hex transaction input data string
//@return 'parameters' paramaters object used for input data
function createOSTxData(listing){
let basicOrderParameters = {
"considerationToken": '0x',
"considerationIdentifier": `0x`,
"considerationAmount": listing.orders[0].protocol_data.parameters.consideration[0].endAmount,
"offerer": listing.orders[0].protocol_data.parameters.offerer,
"zone": listing.orders[0].protocol_data.parameters.zone,
"offerToken": listing.orders[0].protocol_data.parameters.offer[0].token,
"offerIdentifier": listing.orders[0].protocol_data.parameters.offer[0].identifierOrCriteria, //check that not more than 1
"offerAmount": 1,
"basicOrderType": listing.orders[0].protocol_data.parameters.orderType,
"startTime": listing.orders[0].protocol_data.parameters.startTime,
"endTime": listing.orders[0].protocol_data.parameters.endTime,
"zoneHash": listing.orders[0].protocol_data.parameters.zoneHash,
"salt": listing.orders[0].protocol_data.parameters.salt,
"offererConduitKey": listing.orders[0].protocol_data.parameters.conduitKey,
"fulfillerConduitKey": listing.orders[0].protocol_data.parameters.conduitKey,
"totalOriginalAdditionalRecipients": listing.orders[0].maker_fees.length,
"additionalRecipients": listing.orders[0].protocol_data.parameters.consideration,
"signature":listing.orders[0].protocol_data.signature,
"current_price": listing.orders[0].current_price,
}
let txData = `0xfb0f3ee1` + "0000000000000000000000000000000000000000000000000000000000000020"
+ `${completeLeft(basicOrderParameters.considerationToken)}` //has to be ETH
+ `${completeLeft(basicOrderParameters.considerationIdentifier)}`
+ `${completeLeft(toHex(basicOrderParameters.considerationAmount))}`
+ `${completeLeft(basicOrderParameters.offerer)}`
+ `${completeLeft(basicOrderParameters.zone)}`
+ `${completeLeft(basicOrderParameters.offerToken)}`
+ `${completeLeft(toHex(basicOrderParameters.offerIdentifier))}`
+ `${completeLeft(toHex(basicOrderParameters.offerAmount))}` //has to be 1
+ `${completeLeft(toHex(basicOrderParameters.basicOrderType))}` //has to be 2
+ `${completeLeft(toHex(basicOrderParameters.startTime))}`
+ `${completeLeft(toHex(basicOrderParameters.endTime))}`
+ `${completeLeft(basicOrderParameters.zoneHash)}`
+ `${completeLeft(basicOrderParameters.salt)}`
+ `${completeLeft(basicOrderParameters.offererConduitKey)}`
+ `${completeLeft(basicOrderParameters.fulfillerConduitKey)}`
+ `${completeLeft(toHex(basicOrderParameters.totalOriginalAdditionalRecipients))}`
+ `0000000000000000000000000000000000000000000000000000000000000240`
+ `00000000000000000000000000000000000000000000000000000000000002e0`;
const additionalRecips = basicOrderParameters.additionalRecipients.filter(obj => obj.recipient.toLowerCase() !== basicOrderParameters.offerer.toLowerCase())
txData = txData + `${completeLeft(toHex(additionalRecips.length))}`
for (recip of additionalRecips){
txData = txData + `${completeLeft(toHex(recip.endAmount))}`
txData = txData + `${completeLeft(recip.recipient)}`
}
txData = txData + `${completeLeft(toHex((basicOrderParameters.signature.length-2)/2))}`
+ `${complete64(basicOrderParameters.signature)}`
return {
"txData": txData.toLowerCase(),
"parameters": basicOrderParameters};
}
//@param Object: {minExpectedOutputAmount, recipient, tokenId}
//@return raw hex tx Data string for sudoswap selling off sudoSwap pool
function createSudoTxData(inputData){
//swapNFTsForToken(uint256[],uint256,address,bool,address)
let txData = '0xb1d3f1c1'
+ `${completeLeft('a0')}`
+ `${completeLeft(toHex(inputData.minExpectedOutputAmount))}`
+ `${completeLeft(inputData.recipient)}`
+ `${completeLeft('0')}`
+ `${completeLeft('0')}`
+ `${completeLeft('1')}`
+ `${completeLeft(toHex(inputData.tokenId))}`
return txData
}
//@param Array of V1 listing objects in form: {market, token_id, price, expiration, object}
//@param Array of pool objects in form: {addy, balance, outputAmount, newSpotPrice, newBalance}
//@param process args
//returns [OS V2 listing object, pool addy] if found, else null
async function findProfitableListing(listings, pools, args){
pools = pools.sort((a,b) => -(a.outputAmount-b.outputAmount))
listings = listings.sort((a,b) => (a.price-b.price))
if (pools.length == 0){
console.log("No pools with liq for this collection")
return null
}
//TODO: args.gas. For this, Gas usage must be known. This is specific to Collection.
//Simulation would be required.
if (pools[0].outputAmount > listings[0].price){ //+ transferEstimate
console.log("FOUND ARBI!!!!!!!!!!!!!!")
console.log("Listing: ", listings[0], "Pool: ", pools[0])
console.log(listings[0].object.asset.asset_contract.address, listings[0].token_id)
const osSellQuote = await GET.getOSTokenIdSellQuote(listings[0].object.asset.asset_contract.address, listings[0].token_id)
console.log("OSSELLQUOTE HERE: ", osSellQuote)
if (osSellQuote.orders.length == 0){
console.log("Too bad, this Item has already been sniped. Adding listing ID to blacklist.")
await addCnfgBlacklist(listings[0].object.asset.id)
return null
}
return [osSellQuote, pools[0]] //return OsSellquote here, keep in mind multiple listings, orders[0]
}
console.log("Arbi not found.\n")
return null
}
//@param raw tx data for sniping off OS
//@param OS basicorderParameters
//@param address of sudo pool
//@param raw tx data for selling off LSSVMPair
//@param marketplace, 'OS', 'LR', 'XY'
//@return signed Flashbots Bundle
async function createBundle(osTxData, parameters, poolAddy, sudoTxData, marketplace='OS'){
const authSigner = new ethers.Wallet(`${process.env.FB_AUTH_KEY}`);
const wallet3 = new ethers.Wallet(`${process.env.WALLET_PRIV_KEY}`)
const nonce3 = web3.eth.getTransactionCount(wallet3.address)
let market = 'OS'
switch(marketplace){
case 'OS':
market = '0x00000000006c3852cbef3e08e8df289169ede581'
break;
case 'LR':
break;
case 'XY':
break;
}
const flashbotsProvider = await FlashbotsBundleProvider.create(
provider,
authSigner
);
const tx_buy_from_marketplace = {
'from': wallet3,
'to': market,
'value': parameters.current_price,
'gas': 'Number',
'type': '2',
'maxFeePerGas': '40',
'maxPriorityFeePerGas': '10',
'data': osTxData,
'nonce': nonce3,
}
const tx_sell_to_sudoPool = {
'from': wallet3,
'to': poolAddy,
'value': '0',
'gas': 'Number',
'type': '2',
'maxFeePerGas': '40',
'maxPriorityFeePerGas': '10',
'data': sudoTxData,
'nonce': nonce3 + 1,
}
console.log("buy FB tx: ", tx_buy_from_marketplace)
console.log("sell FB tx: ", tx_sell_to_sudoPool)
const signedBundle = await flashbotsProvider.signBundle(
[
{
signer: wallet3,
transaction: tx_buy_from_marketplace,
},
{
signer: wallet3,
transaction: tx_sell_to_sudoPool,
},
]);
return signedBundle
}
async function sendBundle(signedBundle){
//Thx Kfish otherdeeds this is good copipe material
const simulation = await flashbotsProvider.simulate(signedTransactions, targetBlock)
if ('error' in simulation) {
console.warn(`Simulation Error: ${simulation.error.message}`)
process.exit(1)
} else {
console.log(`Simulation Success: ${JSON.stringify(simulation, null, 2)}`)
}
const bundleReceipt = await flashbotsProvider.sendRawBundle(
signedBundle,
TARGET_BLOCK_NUMBER
);
console.log('bundle submitted, waiting')
if ('error' in bundleSubmission) {
throw new Error(bundleSubmission.error.message)
}
const waitResponse = await bundleSubmission.wait()
console.log(`Wait Response: ${FlashbotsBundleResolution[waitResponse]}`)
}
async function addCnfgBlacklist(id){
let cnfg = await MEM.readConfig()
if (!("blackList" in cnfg)){
cnfg.blackList = []
}
cnfg.blackList.push(id)
await MEM.updateConfig(cnfg)
}
async function main(){
//TODO Think about: If using Setinterval, when inspecting listings already in memory and this is changed, what to do?
// ---->> Set ratio fetch/findArbi? Ask KFish.
//TODO Error handling on every fetch pls
//Save args in config for reusage
let cnfg = await MEM.readConfig()
for (arg of Object.keys(args)){
cnfg.args[arg] = args[arg]
}
cnfg.blackList = []
//Args parser
if (!("collection" in args || "collection" in cnfg.args)){
console.log("ERROR: Collection not specified")
return;
}
cnfg.args.collection = ("collection" in args)? args.collection : cnfg.args.collection
//Default gas 15 prio & max
cnfg.args.gas = ("gas" in args)? args.gas : ("gas" in cnfg.args)? cnfg.args.gas : "15" //Gwei
//Default loop interval 10sec
cnfg.args.listloop = ("listloop" in args)? parseInt(args.listloop) : ("listloop" in cnfg.args)? cnfg.args.listloop : 0 //Milisecs //TODO
//Default listing fetch delta 1h
cnfg.args.listdelta = ("listdelta" in args)? parseInt(args.listdelta) : ("listdelta" in cnfg.args)? cnfg.args.listdelta : 3600//Seconds //TODO
const rl = readline.createInterface({input: process.stdin,output: process.stdout,});
const question1 = () => {
return new Promise((resolve, reject) => {
rl.question("PLEASE CONFIRM ARGS", function (answer) {
if (!(answer === 'y' || answer === 'yes' || answer === '')){process.exit(1)}
resolve()
});
})
}
console.log("args: ", cnfg.args)
await question1()
rl.close
await MEM.updateConfig(cnfg)
let listings = []
let osListings = []
// await MEM.updatePools(); // await once in case the update is big, such as the first time
while(true){
arbi = null
console.log("\nLooking for new arbi...")
while (arbi === null){
await MEM.updatePools();
console.log("\nFetching OS listing Events")
osListings = await GET.getOsEvents(cnfg.args.collection, cnfg.args.listdelta)
console.log("Updating listings in Memory")
listings = GET.concatListingsNoDuplicate(listings, osListings)
listings = await GET.updateListings(listings) //[{market, token_id, price, expiration, object}]
console.log("\nFetching collection pools Quotes")//Takes forever //Takes really forever
//----> TODO A LOOOOTTT REJECTED. Proper error handling pls. Or faster Node or wateva
pools = await GET.getPoolsQuotes(cnfg.args.collection) //[{addy, balance, outputAmount, newSpotPrice, newBalance},{},{}]
if (pools == null){continue}
// console.log("listing Events: ", listings) //[{market, token_id, price, expiration, listing_object},{},{}]
// console.log("final pools: ", pools)
console.log("\nChecking for arbi opportunity")
arbi = await findProfitableListing(listings, pools, cnfg.args) //TODO: Include gas in calculation.
}
const osTxData = createOSTxData(arbi[0]).txData
const sudoTxData = createSudoTxData(arbi[1])
const signedBundle = await createBundle(osTxData, arbi[0], sudoTxData, arbi[1], gas)
const txReceipt = await sendBundle(signedBundle)
console.log(txReceipt)
return
}
// const listingA = await GET.getOSTokenIdSellQuote("0xedf6d3c3664606fe9ee3a9796d5cc75e3b16e682", 4165)
// await fs.promises.writeFile(`./listingA.json`, JSON.stringify(listingA, null, 2), (errr) => {
// if (errr) {console.log(errr);}
// });
// const A = require('./listingA.json')
// console.log(createOSTxData(A))
// create bundle
// send bundle
// see if successful and show profit
}
main();