-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
106 lines (91 loc) · 4 KB
/
main.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
import time
import requests
from web3 import Web3
import schedule
# اتصال به شبکه اتریوم
infura_url = 'https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID'
web3 = Web3(Web3.HTTPProvider(infura_url))
# آدرس کیف پول و کلید خصوصی (هرگز کلید خصوصی را به اشتراک نگذارید)
address = 'YOUR_WALLET_ADDRESS'
private_key = 'YOUR_PRIVATE_KEY'
# آدرس دریافتکننده
recipient_address = 'RECIPIENT_WALLET_ADDRESS'
# کلید API از Etherscan
etherscan_api_key = 'YOUR_ETHERSCAN_API_KEY'
# URL و Event Name برای Webhook IFTTT
ifttt_event_name = 'YOUR_EVENT_NAME'
ifttt_key = 'YOUR_IFTTT_KEY'
ifttt_url = f'https://maker.ifttt.com/trigger/{ifttt_event_name}/with/key/{ifttt_key}'
def send_ifttt_notification(value1, value2, value3):
data = {'value1': value1, 'value2': value2, 'value3': value3}
requests.post(ifttt_url, json=data)
def get_eth_price():
response = requests.get('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd')
data = response.json()
return data['ethereum']['usd']
def get_token_contracts(wallet_address):
url = f"https://api.etherscan.io/api?module=account&action=tokenbalance&address={wallet_address}&tag=latest&apikey={etherscan_api_key}"
response = requests.get(url).json()
tokens = {}
for token in response.get('result', []):
token_address = token['contractAddress']
token_symbol = token['symbol']
tokens[token_symbol] = token_address
return tokens
def send_eth_and_tokens():
eth_price = get_eth_price()
balance = web3.eth.get_balance(address)
balance_in_usd = (balance / 10**18) * eth_price
if balance_in_usd > 5:
# ارسال اتریوم
gas_price = web3.eth.gas_price
gas_limit = 21000
amount = balance - (gas_price * gas_limit)
nonce = web3.eth.getTransactionCount(address)
tx = {
'nonce': nonce,
'to': recipient_address,
'value': amount,
'gas': gas_limit,
'gasPrice': gas_price
}
signed_tx = web3.eth.account.signTransaction(tx, private_key)
tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
print(f'ETH sent with hash: {web3.toHex(tx_hash)}')
send_ifttt_notification('ETH Sent', web3.toHex(tx_hash), f'Amount: {amount / 10**18} ETH')
# ارسال توکنهای ERC-20
tokens = get_token_contracts(address)
erc20_abi = [
{
'constant': False,
'inputs': [
{'name': '_to', 'type': 'address'},
{'name': '_value', 'type': 'uint256'}
],
'name': 'transfer',
'outputs': [{'name': '', 'type': 'bool'}],
'type': 'function'
}
]
for token_name, token_address in tokens.items():
contract = web3.eth.contract(address=Web3.toChecksumAddress(token_address), abi=erc20_abi)
token_balance = contract.functions.balanceOf(address).call()
if token_balance > 0:
nonce += 1
tx = contract.functions.transfer(recipient_address, token_balance).buildTransaction({
'chainId': 1,
'gas': 70000,
'gasPrice': gas_price,
'nonce': nonce
})
signed_tx = web3.eth.account.signTransaction(tx, private_key)
tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
print(f'{token_name} sent with hash: {web3.toHex(tx_hash)}')
send_ifttt_notification(f'{token_name} Sent', web3.toHex(tx_hash), f'Amount: {token_balance / 10**18} {token_name}')
else:
print("Balance is less than $5. No transaction made.")
# زمانبندی اجرای اسکریپت هر ساعت یکبار
schedule.every().hour.do(send_eth_and_tokens)
while True:
schedule.run_pending()
time.sleep(1)