-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathpublic.js
145 lines (124 loc) · 4.5 KB
/
public.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
const AlchemyWeb3 = require("@alch/alchemy-web3");
const _ = require("lodash");
const Tx = require('ethereumjs-tx').Transaction
const abiDecoder = require('abi-decoder');
const ethers = require('ethers'); // Require the ethers library
const utils = require('ethers').utils;
const config = require('./config.js')
let json = require('./abi.json');
abiDecoder.addABI(json);
const { spawn, exec } = require("child_process");
function getJSON() {
console.log(json); // this will show the info it in firebug console
};
async function signTx(web3, fields = {}) {
const nonce = await web3.eth.getTransactionCount(config.fromAddress, 'latest');
console.log('nonce',nonce)
const transaction = {
'nonce': nonce,
...fields,
};
return await web3.eth.accounts.signTransaction(transaction, config.privateKey);
}
async function sendTx(web3, fields = {}) {
const signedTx = await signTx(web3, fields);
web3.eth.sendSignedTransaction(signedTx.rawTransaction, function(error, hash) {
if (!error) {
console.log("Transaction sent!", hash);
const interval = setInterval(function() {
console.log("Attempting to get transaction receipt...");
web3.eth.getTransactionReceipt(hash, function(err, rec) {
if (rec) {
console.log(rec);
clearInterval(interval);
}
});
}, 1000);
} else {
console.log("Something went wrong while submitting your transaction:", error);
}
});
}
function sendMinimalLondonTx(web3,data,toAddress,price) {
console.log('data',data, typeof data)
web3.eth.estimateGas({
from: config.fromAddress,
data: data,
to: toAddress,
value: web3.utils.toWei(price, 'ether'),
}).then((estimatedGas) => {
console.log("estimatedGas", estimatedGas);
sendTx(web3, {
gas: estimatedGas,
maxPriorityFeePerGas: web3.utils.toHex(web3.utils.toWei(config.maxPriorityFeePerGas, 'gwei')),
maxFeePerGas: web3.utils.toHex(web3.utils.toWei(config.maxFeePerGas, 'gwei')),
to: toAddress,
value: web3.utils.toWei(price, 'ether'),
data: web3.utils.toHex(data)
});
});
}
const pendingTrasactions = async () => {
let web3URL;
let targetContract;
let creator;
console.log('Network Mode:', config.network);
switch(config.network) {
//---------------- TEST USAGE-------------------------
case 'Rinkeby':{
web3URL = config.wssRinkeby;
targetContract = "";
creator = "";
break;
}
case 'Goerli':{
web3URL = config.wssGoerli;
targetContract = "";
creator = "";
break;
}
//-----------------------------------------------------
default: {
web3URL = config.wssMainnet;
targetContract = config.toAddress;
creator = config.creatorAddress;
}
}
console.log('Web3URL:', web3URL);
const web3 = AlchemyWeb3.createAlchemyWeb3(web3URL);
var contract = new web3.eth.Contract(json, targetContract);
//-----------------------------------------------------------------
//--------------- Change this function every time------------------
let extraData = await contract.methods.mintSAC(config.number);
//-----------------------------------------------------------------
//-----------------------------------------------------------------
let data = extraData.encodeABI();
// DEBUG SECTION
//sendMinimalLondonTx(web3,data,targetContract,config.price);
web3.eth
.subscribe("alchemy_filteredNewFullPendingTransactions", {
address: targetContract.toLocaleLowerCase(),
})
.on("data", async (blockHeader) => {
//console.log('xxxxxx blockHeader:', blockHeader);
const auther = blockHeader.from;
const decodedData = abiDecoder.decodeMethod(blockHeader.input);
//console.log('xxxxxx decodedData:', decodedData);
if(auther.toLowerCase() === creator.toLowerCase()){
if(decodedData){
//console.log(decodedData);
//-----------------------------------------------------------------
//--------------- Change this function every time------------------
if((decodedData.name == 'flipPublicSaleState')){
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// your code
console.log("Found out the public sales",decodedData.name);
console.log("INPUT DATA",data);
sendMinimalLondonTx(web3,data,targetContract,config.price);
}
}
}
});
};
pendingTrasactions();