从零开始构建交易

从零开始构建交易

单击顶部栏上的🚀 -> Binder在线运行此示例!

from conflux_web3 import Web3

w3 = Web3(Web3.HTTPProvider("https://test.confluxrpc.com"))
account = w3.account.from_key("0x....") # fill your secret key here

这里解释了每个字段的含义。下面展示了各个字段的常见构造方法。

prebuilt_tx = {
    'from': account.address,
    'nonce': w3.cfx.get_next_nonce(account.address),
    'to': w3.account.create().address,
    'value': 100,
    'gasPrice': w3.cfx.gas_price,
    'chainId': w3.cfx.chain_id,
    # 'gas': 21000, 
    # 'storageLimit': 0,
    'epochHeight': w3.cfx.epoch_number
}

# estimate
estimate_result = w3.cfx.estimate_gas_and_collateral(prebuilt_tx)

prebuilt_tx['gas'] = estimate_result['gasLimit']
prebuilt_tx['storageLimit'] = estimate_result['storageCollateralized']

w3.cfx.send_raw_transaction(
    account.sign_transaction(prebuilt_tx).rawTransaction 
).executed()