[Python] How can I make a custom payment driver of XDC Network in Golem?

Discussão em 'Python' iniciado por Stack, Maio 3, 2025.

  1. Stack

    Stack Membro Participativo

    In the Golem network, they do not provide XDC Network drivers, so we need to connect the XDC Network with Golem to make custom drivers, but the problem is that there is no official guide about it. Can anyone help me?

    from web3 import Web3
    from config import XDC_CONFIG

    class XDCPaymentDriver:
    def __init__(self, private_key, wallet_address):
    self.w3 = Web3(Web3.HTTPProvider(XDC_CONFIG["rpc_url"]))
    self.wallet = wallet_address
    self.pk = private_key
    self.token = self.w3.eth.contract(
    address=self.w3.to_checksum_address(XDC_CONFIG["token_contract"]),
    abi=open("XDCBasicToken.abi").read()
    )

    def pay(self, to_address, amount):
    decimals = XDC_CONFIG["token_decimals"]
    amt = int(amount * (10 ** decimals))
    nonce = self.w3.eth.get_transaction_count(self.wallet)

    # XDC-specific transaction parameters
    txn = self.token.functions.transfer(
    self.w3.to_checksum_address(to_address),
    amt
    ).build_transaction({
    'from': self.wallet,
    'nonce': nonce,
    'gas': 100_000,
    # XDC Apothem recommends fixed gas price of 0.25 Gwei
    'gasPrice': self.w3.to_wei('0.25', 'gwei'),
    # Required for EIP-1559 compatibility
    'chainId': XDC_CONFIG["chain_id"]
    })

    try:
    signed = self.w3.eth.account.sign_transaction(txn, self.pk)
    tx_hash = self.w3.eth.send_raw_transaction(signed.raw_transaction)
    return tx_hash.hex()
    except Exception as e:
    print(f"Transaction failed: {str(e)}")
    return None

    Continue reading...

Compartilhe esta Página