-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathFollowMint_unix.py
217 lines (197 loc) · 7.26 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
import json
import sys
import time
import requests
from blocknative.stream import Stream
from web3 import Web3
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 methodNameList:
return methodNameList[methodSignature]
res = requests.get('https://www.4byte.directory/api/v1/signatures/?hex_signature=' + methodSignature)
if res.status_code == 200:
methodName = res.json()['results'][0]['text_signature'].split('(')[0].lower()
print_yellow(res.json()['results'][0]['text_signature'])
if 'mint' in methodName:
methodNameList[methodSignature] = True
return True
methodNameList[methodSignature] = False
return False
except:
return False
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 + "监控到新交易")
NFTcon = w3.eth.contract(address=to_address, abi=[nameabi])
try:
name = NFTcon.functions.name().call()
for black in blacklist:
if black.lower() in name.lower():
print_yellow(name + "黑名单,跳过")
return
except:
print_yellow('获取NFT名称失败,跳过')
return
for follow in follows:
if follow.lower() == from_address.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_yellow("非Mint时间,跳过")
return
if value != '0':
print_yellow("非免费,跳过")
return
if to_address in mintadd:
print_yellow("mint过,跳过")
return
inputData = inputData.replace(from_address[2:].lower(), account.address[2:].lower())
if not getMethodName(inputData[:10]):
print_yellow('可能不是mint交易,跳过')
return
if gasPrice > maxGasPrice or maxFeePerGas > maxGasPrice:
print_yellow('gasPrice过高,跳过')
return
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
else:
transaction['maxFeePerGas'] = maxFeePerGas
transaction['maxPriorityFeePerGas'] = maxPriorityFeePerGas
try:
estimateGas = w3.eth.estimateGas(transaction)
if estimateGas > maxGasLimit:
print_yellow('超过gasLimit上限,跳过')
return
transaction['gas'] = estimateGas
signed = w3.eth.account.sign_transaction(transaction, privateKey)
new_raw = signed.rawTransaction.hex()
if to_address in mintadd:
print_yellow("mint过,跳过")
return
tx_hash = w3.eth.sendRawTransaction(new_raw)
mintadd.append(to_address)
print_green("mint交易发送成功" + w3.toHex(tx_hash))
freceipt = w3.eth.waitForTransactionReceipt(tx_hash, 600)
if freceipt.status == 1:
print_green("mint成功")
bark('mint成功', 'https://cn.etherscan.com/tx/' + w3.toHex(tx_hash))
else:
print_green("mint失败")
bark('mint失败', 'https://cn.etherscan.com/tx/' + w3.toHex(tx_hash))
except:
print_yellow('预测失败,跳过')
return
def main():
try:
stream = Stream(blocknativeKey)
filters = [{"status": "pending"}]
print_blue(account.address)
print_blue('开始监控')
for follow in follows:
stream.subscribe_address(follow, txn_handler, filters)
stream.connect()
except Exception as e:
print_red(str(e))
time.sleep(10)
if __name__ == '__main__':
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']
privateKey = config['privateKey']
if 'blacklist' in config:
blacklist = config['blacklist']
else:
blacklist = []
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
account = w3.eth.account.privateKeyToAccount(privateKey)
mintadd = []
methodNameList = {}
main()
except Exception as e:
print_red(str(e))
time.sleep(10)