Conflux-Chain/python-conflux-sdk

Getting started example(s) need to be updated for cfx.gas_price token units and estimate_gas_and_collateral()

Closed this issue · 3 comments

The getting started example below doesn't work right out of the box. It requires conversion from token unit type to int

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': account.address,
    'value': 100,
    'gasPrice': web3.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)

The above code results in:
TypeError: Unsupported type: '<class 'cfx_utils.token_unit.Drip'>'. Must be one of: bool, str, bytes, bytearrayor int.

A simple workaround is to use cfx_utils.token_unit.to_int_if_drip_units() when first constructing the tx but I look to the owners for best practice guidance

from cfx_utils.token_unit import (
    to_int_if_drip_units
)

prebuilt_tx = {
    ...
    'gasPrice': to_int_if_drip_units(web3.cfx.gas_price),
    ....
}

Tested with:

conflux-web3==1.2.2
web3==6.8.0

Sorry for that this seems to be a bug occured during code iteration because of incomplete code tests. Will resolve this issue during next version

I think this issue is resolved

Thanks @darwintree , tested working as desired