Conflux-Chain / python-conflux-sdk

Conflux Python SDK
https://python-conflux-sdk.readthedocs.io/en/latest/
14 stars 3 forks source link

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

Closed simulacrumb-glm closed 2 weeks ago

simulacrumb-glm commented 2 months ago

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

https://github.com/Conflux-Chain/python-conflux-sdk/blob/3151662143f4db04f8ca83bf7b6b7b59570bdf08/docs/en/examples/11-construct_transaction_from_scratch.ipynb#L38

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

darwintree commented 1 month ago

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

darwintree commented 2 weeks ago

I think this issue is resolved