-
-
Save edwinosky/8c518b34290f3892fa1cdaaa7903396e to your computer and use it in GitHub Desktop.
run.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from web3 import Web3 | |
# Configuración de Web3 | |
w3 = Web3(Web3.HTTPProvider('https://testnet.storyrpc.io')) | |
# Direcciones y claves | |
wallet_address = Web3.to_checksum_address('0x3b3b872dfb380fc2feb97438971bc947bce9199f') | |
private_key = 'dee3b12b2a830a6e07f5f469872b5b21ca44861eb68e8061c1a6e0d1cf1e7787' | |
contract_address = Web3.to_checksum_address('0xB4a7Ea1f7874D0C55f19CB6a37aeB3F41a910276') | |
weth_contract_address = Web3.to_checksum_address('0x968B9a5603ddEb2A78Aa08182BC44Ece1D9E5bf0') | |
# ABI del contrato ERC-20 | |
erc20_abi = [ | |
{ | |
"constant": True, | |
"inputs": [ | |
{"name": "owner", "type": "address"}, | |
{"name": "spender", "type": "address"} | |
], | |
"name": "allowance", | |
"outputs": [ | |
{"name": "remaining", "type": "uint256"} | |
], | |
"payable": False, | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"constant": False, | |
"inputs": [ | |
{"name": "_spender", "type": "address"}, | |
{"name": "_value", "type": "uint256"} | |
], | |
"name": "approve", | |
"outputs": [ | |
{"name": "", "type": "bool"} | |
], | |
"payable": False, | |
"stateMutability": "nonpayable", | |
"type": "function" | |
}, | |
{ | |
"constant": True, | |
"inputs": [ | |
{"name": "account", "type": "address"} | |
], | |
"name": "balanceOf", | |
"outputs": [ | |
{"name": "balance", "type": "uint256"} | |
], | |
"payable": False, | |
"stateMutability": "view", | |
"type": "function" | |
} | |
] | |
# Obtener balance de WETH en la wallet | |
def get_weth_balance(): | |
erc20_contract = w3.eth.contract(address=weth_contract_address, abi=erc20_abi) | |
balance = erc20_contract.functions.balanceOf(wallet_address).call() | |
return balance | |
# Obtener balance de ETH en la wallet | |
def get_eth_balance(): | |
balance = w3.eth.get_balance(wallet_address) | |
return balance | |
# Calcular el monto a suministrar (5%-10% del balance) | |
def calculate_supply_amount(balance): | |
return int(balance * random.uniform(0.05, 0.10)) | |
# Verificar si ya hay tokens aprobados suficientes | |
def check_existing_allowance(amount): | |
erc20_contract = w3.eth.contract(address=weth_contract_address, abi=erc20_abi) | |
allowance = erc20_contract.functions.allowance(wallet_address, contract_address).call() | |
return allowance >= amount | |
# Aprobar el contrato para gastar tokens | |
def approve_tokens(amount): | |
nonce = w3.eth.get_transaction_count(wallet_address) | |
erc20_contract = w3.eth.contract(address=weth_contract_address, abi=erc20_abi) | |
transaction = erc20_contract.functions.approve(contract_address, amount).build_transaction({ | |
'chainId': w3.eth.chain_id, | |
'gas': 200000, | |
'gasPrice': w3.to_wei('30', 'gwei'), | |
'nonce': nonce | |
}) | |
signed_txn = w3.eth.account.sign_transaction(transaction, private_key) | |
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction) | |
print(f"Transacción de aprobación enviada con hash: {tx_hash.hex()}") | |
return tx_hash | |
# Construir la transacción de suministro | |
def build_supply_transaction(supply_amount): | |
nonce = w3.eth.get_transaction_count(wallet_address) | |
function_selector = '0x2b627736' # Método Supply | |
reserve = Web3.to_checksum_address('0x968B9a5603ddEb2A78Aa08182BC44Ece1D9E5bf0') | |
on_behalf_of = wallet_address | |
referral_code = 0 | |
data = function_selector + \ | |
reserve[2:].rjust(64, '0') + \ | |
wallet_address[2:].rjust(64, '0') + \ | |
on_behalf_of[2:].rjust(64, '0') + \ | |
Web3.to_hex(supply_amount).lstrip('0x').rjust(64, '0') + \ | |
Web3.to_hex(referral_code).lstrip('0x').rjust(4, '0') | |
transaction = { | |
'to': contract_address, | |
'data': data, | |
'nonce': nonce, | |
'gas': 500000, | |
'gasPrice': w3.to_wei('30', 'gwei'), | |
'chainId': w3.eth.chain_id | |
} | |
try: | |
gas_estimate = w3.eth.estimate_gas(transaction) | |
transaction['gas'] = gas_estimate | |
except Exception as e: | |
print(f"Error al estimar gas: {e}") | |
raise | |
return transaction | |
# Firmar y enviar la transacción | |
def send_transaction(transaction): | |
signed_txn = w3.eth.account.sign_transaction(transaction, private_key) | |
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction) | |
print(f"Transacción enviada con hash: {tx_hash.hex()}") | |
return tx_hash | |
# Ejecutar el flujo | |
balance = get_weth_balance() | |
eth_balance = get_eth_balance() | |
supply_amount = calculate_supply_amount(balance) | |
print(f"Balance de WETH en la wallet: {w3.from_wei(balance, 'ether')} WETH") | |
print(f"Balance de ETH en la wallet: {w3.from_wei(eth_balance, 'ether')} ETH") | |
print(f"Monto a suministrar: {w3.from_wei(supply_amount, 'ether')} WETH") | |
if not check_existing_allowance(supply_amount): | |
approve_tx_hash = approve_tokens(supply_amount) | |
print(f"Esperando confirmación de la transacción de aprobación {approve_tx_hash.hex()}...") | |
w3.eth.wait_for_transaction_receipt(approve_tx_hash) | |
print("Aprobación completada.") | |
supply_transaction = build_supply_transaction(supply_amount) | |
supply_tx_hash = send_transaction(supply_transaction) | |
print(f"Esperando confirmación de la transacción de suministro {supply_tx_hash.hex()}...") | |
w3.eth.wait_for_transaction_receipt(supply_tx_hash) | |
print("Suministro completado.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment