Metadata-Version: 2.1
Name: conflux
Version: 0.2.0b1
Summary: python SDK for Conflux network
Home-page: https://github.com/conflux-chain/python-conflux-sdk
Author: Conflux-Dev
Author-email: wenda.zhang@confluxnetwork.org
License: UNKNOWN
Keywords: python,conflux,blockchain
Platform: UNKNOWN
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: Microsoft :: Windows
Description-Content-Type: text/markdown
Requires-Dist: web3 (>=6.0.0b4)
Requires-Dist: cfx-address (>=1.0.0b1)
Requires-Dist: cfx-account (>=0.1.0b3)
Requires-Dist: cfx-utils (>=1.0.0b3)
Provides-Extra: dev
Requires-Dist: docker (<6,>=5.0.0) ; extra == 'dev'
Requires-Dist: pytest (<7,>=6.2.5) ; extra == 'dev'
Requires-Dist: sphinx (<5,>=4.2.0) ; extra == 'dev'
Requires-Dist: wheel ; extra == 'dev'
Provides-Extra: docs
Requires-Dist: sphinx (<5,>=4.2.0) ; extra == 'docs'
Requires-Dist: wheel ; extra == 'docs'
Provides-Extra: linter
Provides-Extra: tester
Requires-Dist: docker (<6,>=5.0.0) ; extra == 'tester'
Requires-Dist: pytest (<7,>=6.2.5) ; extra == 'tester'

# python-conflux-sdk
Python SDK for interacting with Conflux network.

Note: this SDK still is in developing, and the API may change in the future.

- [python-conflux-sdk](#python-conflux-sdk)
  - [Quick start](#quick-start)
  - [Overview](#overview)
    - [Code Examples](#code-examples)
      - [initialization with providers](#initialization-with-providers)
      - [Send a transaction](#send-a-transaction)
      - [Contract Interaction](#contract-interaction)
      - [RPC support](#rpc-support)
      - [Base32 Address Operation](#base32-address-operation)
    - [APIs to support in the near future](#apis-to-support-in-the-near-future)

## Quick start

```shell
$ pip3 install conflux
```

```python
from conflux_web3.dev import get_testnet_web3
from conflux_web3.middleware import WalletMiddleware

w3 = get_testnet_web3()
# fill your secret key
# and you can claim testnet token from https://faucet.confluxnetwork.org/
acct = w3.account.from_key("0xxxxxxxxxxxxxx") 
w3.middleware_onion.add(WalletMiddleware(acct, network_id=1))
w3.cfx.default_account = acct.address

w3.cfx.send_transaction({
    'to': w3.cfx.account.create().address,
    'value': 22,
}).executed()
```

## Overview

Python-conflux-sdk is a wrapping layer over [web3.py](https://github.com/ethereum/web3.py), and we are trying to make most of its APIs consistent with web3.py. 

### Code Examples

Python-conflux-sdk is still in developing, and currently supports these features:

#### initialization with providers

```python
# same as web3.py
from conflux_web3 import Web3
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:12537"))

# or
# conflux_web3's additional functions
# using local default HTTP provider or Conflux's public RPC FOR DEVELOPMENT
from conflux_web3.dev import (
    get_local_web3, # "http://127.0.0.1:12537"
    get_testnet_web3, # "https://test.confluxrpc.com"
    get_mainnet_web3, # "https://main.confluxrpc.com"
)
w3 = get_testnet_web3()
```

#### Send a transaction

Code example

``` python
# modified from https://web3py.readthedocs.io/en/latest/middleware.html?highlight=construct_sign_and#web3.middleware.construct_sign_and_send_raw_middleware
import os
from conflux_web3.dev import get_testnet_web3
from conflux_web3.middleware import construct_sign_and_send_raw_middleware

w3 = get_testnet_web3()
acct = w3.cfx.account.from_key(os.environ.get('PRIVATE_KEY'))
# construct_sign_and_send_raw_middleware(acct) is same as WalletMiddleware(acct)
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acct))
w3.cfx.default_account = acct.address

transaction = {
    'to': w3.cfx.account.create().address, # a random address encoded in base32 format
    'value': 22,
}

# or you can simply use """w3.cfx.send_transaction(transaction).executed()"""
w3.cfx.wait_for_transaction_receipt(w3.cfx.send_transaction(transaction))
```

#### Contract Interaction

``` py
import os, json
from conflux_web3.dev import get_testnet_web3
from conflux_web3.middleware import construct_sign_and_send_raw_middleware

w3 = get_testnet_web3()
acct = w3.cfx.account.from_key(os.environ.get('PRIVATE_KEY'))
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acct))
w3.cfx.default_account = acct.address

erc20_metadata = json.load(open("path/to/metadata/ERC20.json"))

# deploy contract
erc20 = w3.cfx.contract(bytecode=erc20_metadata["bytecode"], abi=erc20_metadata["abi"])
hash = erc20.constructor(name_="ERC20", symbol_="C", totalSupply=10**18).transact()
contract_address = w3.cfx.wait_for_transaction_receipt(hash)["contractCreated"]
assert contract_address is not None
contract = w3.cfx.contract(contract_address, abi=erc20_metadata["abi"])

# transfer
random_account = w3.account.create()
hash = contract.functions.transfer(random_account.address, 100).transact()
transfer_receipt = w3.cfx.wait_for_transaction_receipt(hash)
balance = contract.functions.balanceOf(random_account.address).call()
```

#### RPC support

Visit [JSON-RPC methods](https://developer.confluxnetwork.org/conflux-doc/docs/json_rpc/#json-rpc-methods) to check the full RPC list. Currently, most frequently used RPCs are supported.

``` python
w3.cfx.get_balance("cfx:aarc9abycue0hhzgyrr53m6cxedgccrmmyybjgh4xg", "latest_state")
# the returned value from RPC will be formatted
# 158972490234375000
```

#### Base32 Address Operation

You can use `from cfx_address import Base32Address` or simply use `w3.address` or `w3.cfx.address` to use the `Base32Address` class. This class provides several methods for you to convert or validate base32 addresses. See [cfx_address documentation](https://conflux-fans.github.io/cfx-address/cfx_address.html#module-cfx_address.address) for the full documentation.

```py
from cfx_address import Base32Address

address = Base32Address("0x1ecde7223747601823f7535d7968ba98b4881e09", network_id=1)
address
# 'cfxtest:aatp533cg7d0agbd87kz48nj1mpnkca8be1rz695j4'
[
    address.address_type,
    address.network_id,
    address.hex_address,
    address.verbose_address,
    address.abbr,
    address.mapped_evm_space_address,
    address.eth_checksum_address,
]
# ['user', 1, '0x1ecde7223747601823f7535d7968ba98b4881e09', 'CFXTEST:TYPE.USER:AATP533CG7D0AGBD87KZ48NJ1MPNKCA8BE1RZ695J4', 'cfxtest:aat...95j4', '0x349f086998cF4a0C5a00b853a0E93239D81A97f6', '0x1ECdE7223747601823f7535d7968Ba98b4881E09']
```

### APIs to support in the near future

- [ ] contract caller and contract events are not fully supported
- [ ] support for all RPCs


