forked from pandaforme/FollowMint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFollowMint_unix.py
271 lines (246 loc) · 9.86 KB
/
FollowMint_unix.py
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
import json
import threading
import time
import requests
from blocknative.stream import Stream
from web3 import Web3
from eth_abi import decode_abi
import os
configExample = {
"RPC": "https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161",
"privateKey": ["", ""],
"blocknativeKey": "",
"barkKey": "",
"maxGasPrice": 50,
"maxGasLimit": 1000000,
"follow": {
"0x8888887a5e2491fec904d90044e6cd6c69f1e71c": {"start": 0, "end": 24},
"0x555555B63d1C3A8c09FB109d2c80464685Ee042B": {"start": 18, "end": 6},
"0x99999983c70de9543cdc11dB5DE66A457d241e8B": {"start": 8, "end": 20}
},
"blacklist": ["Ape", "Bear", "Duck", "Pixel", "Not", "Okay", "Woman", "Baby", "Goblin", "Ai"]
}
def print_green(message):
stime = time.strftime("%m-%d %H:%M:%S", time.localtime())
print(f'[{stime}] \033[1;32m{message}\033[0m')
def print_red(message):
stime = time.strftime("%m-%d %H:%M:%S", time.localtime())
print(f'[{stime}] \033[1;31m{message}\033[0m')
def print_blue(message):
stime = time.strftime("%m-%d %H:%M:%S", time.localtime())
print(f'[{stime}]\033[1;34m{message}\033[0m')
def print_yellow(message):
stime = time.strftime("%m-%d %H:%M:%S", time.localtime())
print(f'[{stime}]\033[1;33m{message}\033[0m')
def bark(info, data):
if barkKey != '':
requests.get('https://api.day.app/' + barkKey + '/' + info + '?url=' + data)
def getMethodName(methodSignature):
try:
if methodSignature in methodNameDict:
print_blue('mint方法:' + methodNameDict[methodSignature]['method'])
return methodNameDict[methodSignature]['isMint'], methodNameDict[methodSignature]['method']
res = requests.get('https://www.4byte.directory/api/v1/signatures/?hex_signature=' + methodSignature)
if res.status_code == 200:
method = res.json()['results'][0]['text_signature']
methodName = method.split('(')[0].lower()
print_blue('mint方法:' + method)
if 'mint' in methodName:
methodNameDict[methodSignature] = {'method': method, 'isMint': True}
return True, method
else:
methodNameDict[methodSignature] = {'method': method, 'isMint': False}
return False, None
except:
return False, None
def isBlackList(_to):
try:
NFTcon = w3.eth.contract(address=_to, abi=[nameabi])
name = NFTcon.functions.name().call()
for black in blacklist:
if black in name:
print_red(name + "黑名单" + black + ",跳过")
return False
except:
print_red('获取NFT名称失败,跳过')
return False
print_blue('NFT名称:' + name)
return True
def isMintTime(_from):
for _follow in follows:
if _follow.lower() == _from.lower():
starttime = int(follows[_follow]['start'])
endtime = int(follows[_follow]['end'])
tm_hour = time.localtime().tm_hour
if tm_hour >= starttime or tm_hour < endtime:
pass
else:
print_red("非Mint时间,跳过")
return False
return True
def minttx(_account, _privateKey, _inputData, _method, _from_address, _to_address, _gasPrice, _maxFeePerGas, _maxPriorityFeePerGas):
try:
abi = _method.split('(')[1][:-1].split(',')
if len(abi) != 0 and 'address' in abi:
params = decode_abi(abi, bytes.fromhex(_inputData[10:]))
for index in range(len(abi)):
if abi[index] == 'address':
_inputData = _inputData.replace(params[index][2:].lower(), _account.address[2:].lower())
transaction = {
'from': _account.address,
'chainId': chainId,
'to': _to_address,
'gas': 2000000,
'nonce': w3.eth.getTransactionCount(_account.address),
'data': _inputData
}
if _gasPrice > 10000:
transaction['gasPrice'] = _gasPrice + w3.toWei(0.1, 'gwei')
else:
transaction['maxFeePerGas'] = _maxFeePerGas
transaction['maxPriorityFeePerGas'] = _maxPriorityFeePerGas + w3.toWei(0.1, 'gwei')
try:
estimateGas = w3.eth.estimateGas(transaction)
if estimateGas > maxGasLimit:
print_red('超过gasLimit上限,跳过')
return
transaction['gas'] = estimateGas
signed = w3.eth.account.sign_transaction(transaction, _privateKey)
new_raw = signed.rawTransaction.hex()
tx_hash = w3.eth.sendRawTransaction(new_raw)
print_green("mint交易发送成功" + w3.toHex(tx_hash))
freceipt = w3.eth.waitForTransactionReceipt(tx_hash, 600)
if freceipt.status == 1:
try:
ETHused = freceipt.effectiveGasPrice * freceipt.gasUsed
ETHused = w3.fromWei(ETHused, 'ether')
ETHusedinfo = '本次mint:' + str(ETHused) + 'ETH'
except:
ETHusedinfo = ''
print_green("mint成功 " + ETHusedinfo)
bark('mint成功', 'https://cn.etherscan.com/tx/' + w3.toHex(tx_hash))
else:
print_red("mint失败")
bark('mint失败', 'https://cn.etherscan.com/tx/' + w3.toHex(tx_hash))
except Exception as e:
print_red('预测失败,跳过:' + str(e))
return
except Exception as e:
print_red('发送交易失败,跳过:' + str(e))
return
async def txn_handler(txn, unsubscribe):
to_address = txn['to']
from_address = txn['from']
to_address = w3.toChecksumAddress(to_address)
gasPrice = 0
maxFeePerGas = 0
maxPriorityFeePerGas = 0
if 'gasPrice' in txn:
gasPrice = int(txn['gasPrice'])
else:
maxFeePerGas = int(txn['maxFeePerGas'])
maxPriorityFeePerGas = int(txn['maxPriorityFeePerGas'])
inputData = txn['input']
value = txn['value']
print_yellow(from_address + "监控到新交易")
if not isMintTime(from_address):
return
if value != '0':
print_red("非免费,跳过")
return
if to_address in mintadd:
print_red("mint过,跳过")
return
isMint, method = getMethodName(inputData[:10])
if not isMint:
print_red('可能不是mint交易,跳过')
return
if not isBlackList(to_address):
return
if gasPrice > maxGasPrice or maxFeePerGas > maxGasPrice:
print_red('gasPrice过高,跳过')
return
mintadd.append(to_address)
for index in range(len(accounts)):
threading.Thread(target=minttx, args=(accounts[index], privateKeys[index], inputData, method, from_address, to_address, gasPrice, maxFeePerGas, maxPriorityFeePerGas)).start()
def main():
while True:
try:
stream = Stream(blocknativeKey)
print_blue(str(len(accounts)) + '个地址开始监控')
print_blue('开始监控')
for _follow in follows:
filters = [{
"status": "pending",
"from": _follow
}]
stream.subscribe_address(_follow, txn_handler, filters)
stream.connect()
except Exception as e:
print_red(str(e))
time.sleep(10)
if __name__ == '__main__':
print_red("有能力的请使用源码,不对使用者安全负责")
print_red("打狗请用小号,无法保证无bug")
print_red("开源地址:https://github.com/Fooyao/FollowMint")
print_red("代码水平较差,有任何优化建议请反馈")
if not os.path.exists('config.json'):
print_blue('请先配置config.json')
file = open('config.json', 'w')
file.write(json.dumps(configExample))
file.close()
time.sleep(10)
try:
file = open('config.json', 'r')
config = json.loads(file.read())
RPC = config['RPC']
privateKeys = config['privateKey']
if type(privateKeys) == str:
privateKeys = [privateKeys]
file = open('config.json', 'w')
config['privateKey'] = privateKeys
file.write(json.dumps(config))
file.close()
if 'blacklist' in config:
blacklist = config['blacklist']
else:
blacklist = []
file = open('config.json', 'w')
config['blacklist'] = ["Ape", "Bear", "Duck", "Pixel", "Not", "Okay", "Woman", "Baby", "Goblin", "Ai"]
file.write(json.dumps(config))
file.close()
blocknativeKey = config['blocknativeKey']
barkKey = config['barkKey']
follows = config['follow']
nameabi = {
'inputs': [],
'name': 'name',
'outputs': [{'internalType': 'string', 'name': '', 'type': 'string'}],
'stateMutability': 'view',
'type': 'function'
}
if type(follows) == list:
print_red('请重新配置follow起始时间,配置文件模板已更新')
followsDict = {}
for follow in follows:
followsDict[follow] = {'start': 0, 'end': 24}
config['follow'] = followsDict
file = open('config.json', 'w')
file.write(json.dumps(config))
file.close()
time.sleep(10)
else:
w3 = Web3(Web3.HTTPProvider(RPC))
maxGasPrice = config['maxGasPrice']
maxGasPrice = w3.toWei(maxGasPrice, 'gwei')
maxGasLimit = int(config['maxGasLimit'])
chainId = w3.eth.chainId
accounts = []
for privateKey in privateKeys:
accounts.append(w3.eth.account.privateKeyToAccount(privateKey))
mintadd = []
methodNameDict = {}
main()
except Exception as e:
print_red(str(e))
time.sleep(10)