1

I can't understand what the problem is, please help

from eth_account import Account
from eth_account.signers.local import LocalAccount
from web3 import Web3, HTTPProvider
from config import *
import json

with open("abi.json", encoding='utf-8') as f:
    abi = json.load(f)


def withdraw(contract_address: str, node: str, account: LocalAccount):
    w3 = Web3(HTTPProvider(node))
    contract = w3.eth.contract(address=w3.toChecksumAddress(contract_address), abi=abi)
    estimate_gas = get_estimate_gas(w3, contract_address, account.address)
    if estimate_gas > 21000:
        nonce_executor = w3.eth.get_transaction_count(account.address)
        tx = contract.functions.withdrawETH(*ARGUMENTS).buildTransaction(
            {
                'from': account.address,
                "gasPrice": int(w3.eth.gas_price * 1.1),
                "nonce": nonce_executor,
                "chainId": w3.eth.chainId,
                'gas': estimate_gas,
            })
        sign_tx = account.sign_transaction(tx)
        w3.eth.send_raw_transaction(sign_tx.rawTransaction)


def get_estimate_gas(w3: Web3, contract_address, executor_address):
    gas = 0
    try:
        contract = w3.eth.contract(address=w3.toChecksumAddress(contract_address),
                                   abi=abi)
        transaction = contract.functions.withdrawETH(*ARGUMENTS)
        gas = transaction.estimateGas({'from': executor_address})
    except ValueError as ex:
        print(ex)
        message = ex.args[0].get('message')
        gas = int(message.split()[-1].strip('(').strip(')'))
    except Exception as ex:
        print(ex)
    finally:
        return gas


account = Account.from_key(PRIVATE_KEY_EXECUTOR)
withdraw(CONTRACT_ADDRESS, "https://rpc.ankr.com/eth", account)
1
  • Attach exception stacktrace would be helpful for identifying the problem. Sep 7 at 2:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.