-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmorpheus.js
More file actions
312 lines (277 loc) · 9.87 KB
/
morpheus.js
File metadata and controls
312 lines (277 loc) · 9.87 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
const ethers = require('ethers');
require('dotenv').config()
const rpc = process.env.RPC
const fetch = require('node-fetch');
const keccak256 = require('keccak256')
const contractAddress = process.env.OOFAddress; // replace with your contract address
const ABI = require('./abi/morph.json')
const { Contract, BigNumber } = require("ethers");
var bigInt = require("big-integer");// store the feed inventory
let feedInventory = [];
// storage for last update timestamp
let lastUpdate = {};
const pk = process.env.PK
const provider = new ethers.providers.JsonRpcProvider(rpc);
const contract = new ethers.Contract(contractAddress, ABI, provider);
console.log('New'); const walletWithProvider = new ethers.Wallet(pk, provider); const oofContract = !!ABI && !!walletWithProvider
? new Contract(contractAddress, ABI, walletWithProvider)
: undefined; let i;
async function vrfHash(value, feedID, fl) {
let hash = keccak256(pk.toString + contractAddress).toString('hex');//ethers.utils.keccak256(pk.toString);
console.log('seed ', hash);
let hash2
if (fl == 1) {
feedID -= 1;
}
for (let i = 0; i < 100000 - feedID; i++) {
hash = ethers.utils.keccak256(hash);
}
hash2 = keccak256(hash + value + feedID).toString('hex')
console.log('VRF seed ', hash);
let hashBN = ethers.BigNumber.from(hash);
let uint256 = hashBN
hash = uint256.toString();
console.log('seed uint ', hash);
hashBN = ethers.BigNumber.from('0x' + hash2);
uint256 = hashBN
hash2 = uint256.toString()
console.log('val ', hash2);
if (fl == 1) {
submit(feedID + 1, hash);
} else {
submit(feedID, hash2);
}
}
contract.on('feedRequested', (endpoint, endpointp, dc, c, feedId,) => {
console.log('New feed requested:');
console.log(`Endpoint: ${endpoint}`);
console.log(`Endpointp: ${endpointp}`);
console.log(`Decimal: ${c}`);
console.log(`Feed ID: ${feedId}`);
if (endpoint == 'vrf' || endpoint == 'VRF') {
if (endpointp == 'proof') {
vrfHash(endpointp, feedId, 1)// code to execute if endpoint is 'vrf' or 'VRF'
} else {
vrfHash(endpointp, feedId, 0)// code to execute if endpoint is 'vrf' or 'VRF'
}
} else {
let parsingargs = []
try {
parsingargs = endpointp.split(",");
} catch { }
let tempInv = {
"feedId": feedId,
"endpoint": endpoint,
"dc": dc,
"c": c,
"parsingargs": parsingargs
}
// process into global feed array
feedInventory.push(tempInv)
processFeeds(endpoint, endpointp, parsingargs, feedId, c)
}
});
async function processFeeds(endpoint, endpointp, parsingargs, feedId, c) {
let i; let feedIdArray = []
let feedValueArray = []
console.log("checking feed APIs")
//for (i = 0; i < feedInventory.length; i++) {
const res = await fetch(endpoint);
const body = await res.json();
console.log(body)
let j;
let toParse = body;
for (j = 0; j < parsingargs.length; j++) {
toParse = toParse[parsingargs[j]]
}
console.log(toParse)
if (toParse != "") {
toParse = parseFloat(toParse) * (10 ** c)
console.log(Math.round(toParse).toLocaleString('fullwide', { useGrouping: false }))
toParse = Math.round(toParse).toLocaleString('fullwide', { useGrouping: false })
}
console.log("Submitting " + toParse)
// push values
feedIdArray.push(feedId)
feedValueArray.push(toParse)
// set new update timestamp
lastUpdate[feedId] = Date.now()
const provider = new ethers.providers.JsonRpcProvider(rpc);
const oofAddress = process.env.OOFAddress
const walletWithProvider = new ethers.Wallet(pk, provider); const oofContract = !!ABI && !!walletWithProvider
? new Contract(oofAddress, ABI, walletWithProvider)
: undefined;
let nonce = await walletWithProvider.getTransactionCount();
let gasPrice = await provider.getGasPrice()
let tx_obk = {
gasPrice: gasPrice
}
async function wait(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
} const gasL = await oofContract.estimateGas.submitFeed(feedIdArray, feedValueArray, tx_obk);
const gasF = gasL * gasPrice;
// console.log('Gas fee:', ethers.utils.formatEther(gasF.toString()), 'ETH ', ethers.utils.formatUnits(gasPrice, "gwei") + " gwei");
// console.log('Bounty ', ethers.utils.formatEther(await oofContract.feedSupport(feedId)).toString())
const gF = (await oofContract.feedSupport(feedId) - gasF).toString()
// console.log('ETH Profit', ethers.utils.formatEther(gF))
if (ethers.utils.formatEther(gF) > 0) {
submit(feedId, toParse, 0)
}
else {
console.log('not profitable')
}
}
contract.on('feedSupported', (feedd) => {
console.log('New feed Support:')
let feedId = []; feedId[0] = feedd;
const oofAddress = process.env.OOFAddress
const walletWithProvider = new ethers.Wallet(pk, provider);
const oofContract = !!ABI && !!walletWithProvider
? new Contract(oofAddress, ABI, walletWithProvider)
: undefined;
let tempInv = {
"feedId": feedId,
// "endpoint": endpoint,
// "dc": dc,
// "c": c,
// "parsingargs": parsingargs
}
// process into global feed array
feedInventory.push(tempInv)
processFds(feedId)
});
async function processFds(feedId) {
const provider = new ethers.providers.JsonRpcProvider(rpc);
const oofAddress = process.env.OOFAddress
let feedIdArray = []
let feedValueArray = []
const d = await oofContract.getFeeds(feedId)
let c
let endpoint
let endpointp
// for (i = 0; i < d.length; i++) {
c = d[2][0]
endpoint = d[3][0]
endpointp = d[4][0]
//}
console.log(`Endpoint: ${endpoint}`);
console.log(`Endpointp: ${endpointp}`);
console.log(`Decimal: ${c}`);
console.log(`Feed ID: ${feedId}`);
if (endpoint === 'vrf' || endpoint === 'VRF') {
if (endpointp == 'proof') {
vrfHash(endpointp, feedId, 1)// code to execute if endpoint is 'vrf' or 'VRF'
} else {
vrfHash(endpointp, feedId, 0)// code to execute if endpoint is 'vrf' or 'VRF'
}
} else {
let parsingargs = []
try {
parsingargs = endpointp.split(",");
} catch { }
console.log("checking feed APIs")
//for (i = 0; i < feedInventory.length; i++) {
const res = await fetch(endpoint);
const body = await res.json();
console.log(body)
let j;
let toParse = body;
console.log(toParse)
for (j = 0; j < parsingargs.length; j++) {
toParse = toParse[parsingargs[j]]
}
console.log(toParse)
if (toParse != "") {
toParse = parseFloat(toParse) * (10 ** c)
console.log(Math.round(toParse).toLocaleString('fullwide', { useGrouping: false }))
toParse = Math.round(toParse).toLocaleString('fullwide', { useGrouping: false })
}
console.log("Submitting " + toParse)
// push values
feedId = Number(feedId)
feedIdArray.push(feedId)
feedValueArray.push(toParse)
// set new update timestamp
lastUpdate[feedId] = Date.now()
let gasPrice = await provider.getGasPrice()
let tx_obk = {
gasPrice: gasPrice
}
const gasL = await oofContract.estimateGas.submitFeed(feedIdArray, feedValueArray, tx_obk);
const gasF = gasL * gasPrice;
// console.log('Gas fee:', ethers.utils.formatEther(gasF.toString()), 'ETH ', ethers.utils.formatUnits(gasPrice, "gwei") + " gwei");
//console.log('Bounty ', ethers.utils.formatEther(await oofContract.feedSupport(feedId)).toString())
//console.log('ETH Profit', ethers.utils.formatEther(ethProfit.toString()));
const gF = (await oofContract.feedSupport(feedId) - gasF).toString()
// console.log('ETH Profit ', gF);
if (ethers.utils.formatEther(gF) > 0) {
submit(feedId, toParse, 0)
}
else {
console.log('not profitable')
}
}
}
let txa = []
async function submit(feedId, value, fl) {
if (txa.length == 0 || fl == 1) {
// If not, add the new feedId and value to the queue
txa.unshift({ feedId: feedId, value: value });
const gasPrice = await provider.getGasPrice();
const tx_obk = { gasPrice };
const gasLimit = await oofContract.estimateGas.submitFeed(
[feedId],
[value],
tx_obk
);
const gasFee = gasLimit.mul(gasPrice);
let sup = await oofContract.feedSupport(feedId)
const ethProfit = sup - gasFee;
console.log('Gas fee:', ethers.utils.formatEther(gasFee.toString()), 'ETH ', ethers.utils.formatUnits(gasPrice, "gwei") + " gwei");
console.log('Bounty ', ethers.utils.formatEther(sup))
console.log('ETH Profit', ethers.utils.formatEther(ethProfit.toString()));
if (ethProfit > 0) {
console.log(
"submitting with gas price: " +
ethers.utils.formatUnits(gasPrice, "gwei") +
" gwei"
);
console.log("submitting feeds...");
const tx = await oofContract.submitFeed([feedId], [value], tx_obk);
console.log(
`submitted feed id ${feedId} with value ${value} at ${Date.now()}`
);
console.log("Transaction hash: " + tx.hash);
await tx.wait();
console.log(`Transaction confirmed at ${Date.now()}`);
// Remove the processed value from the queue
txa.shift();
// Check if there are any values left in the queue
if (txa.length > 0) {
// Submit the next value in the queue
const nextVal = txa[0]; txa.shift();
submit(nextVal.feedId, nextVal.value, 1);
}
} else {
console.log("not profitable");
// Remove the processed value from the queue
txa.shift();
// Check if there are any values left in the queue
if (txa.length > 0) {
// Submit the next value in the queue
const nextVal = txa[0]; txa.shift();
await submit(nextVal.feedId, nextVal.value, 1);
}
}
} else {
// If not, add the new feedId and value to the queue
if (txa.some((item) => item.feedId === feedId && item.value === value)) {
console.log(`Feed id ${feedId} with value ${value} already in queue`);
} else {
txa.push({ feedId: feedId, value: value });
}
console.log(`Added feed id ${feedId} with value ${value} to queue`);
}
}