Fusion API


Web3.py can be installed (preferably in a virtualenv) using pip as follows:

$ pip install cpc-fusion

Note that if you pip is referring to the one in python2, please use the following command instead:

$ pip3 install cpc-fusion

Note that CPC fusion is tested on Ubuntu 18.04 and python 3.6.7. A lower version of Ubuntu and python may incur an error or unsuccessful installation.

Installation from source can be done from the root of the project with the following command.

$ pip install .

Using Fusion

To use the web3 library, you are required to initialize the Web3 class.

Before connecting, you must set up a local chain or sync with our Mainnet

  1. To start a local chain, use the following commands

    $ cd ./examples/cpchain
    $ sudo ./cpchain-all.sh

    Note that starting a local chain may fails. You may try several times until success.

  2. To sync with Alpha Mainnet, use the following command

    $ build/bin/cpchain run --rpcapi personal,eth,cpc,admission,net,web3,db,txpool,miner --rpcaddr --runmode=testnet

    WARNING: The current master version is not compatible with Alpha Mainnet. Interested users can refer to commit 7d29a2b to sync with Alpha Mainnet. After cloning from github repository, you can checkout the commit 7d29a2b by following command:

    $ git checkout 7d29a2b
    $ sudo make all

    Then use the commands above to connect to Alpha Mainnet.

Use the auto module to guess at common node connection options.

>>> from cpc_fusion import Web3
>>> cf = Web3(Web3.HTTPProvider(''))
>>> cf.cpc.blockNumber


If you get the result UnhandledRequest: No providers responded to the RPC request, then you are not connected to a node.

API Reference


class cpc_fusion.cpc.Cpc

The cpc_fusion.cpc object exposes the following properties and methods to interact with the RPC APIs under the cpc_ namespace.

Often, when a property or method returns a map from keys to values, it also returns an AttributeDict which acts like a dict that you can access the keys as attributes but cannot modify its fields. For example, you can find the latest block number in these two ways:

>>> block = cpc_fusion.cpc.getBlock('latest')
  'hash': '0xe8ad537a261e6fff80d551d8d087ee0f2202da9b09b64d172a5f45e818eb472a',
  'number': 4022281,
  # ... etc ...

>>> block['number']
>>> block.number

>>> block.number = 4022282
Traceback # ... etc ...
TypeError: This data is immutable -- create a copy instead of modifying

The following methods are available on the cpc_fusion.cpc namespace.


  • Delegates to eth_getBlockTransactionCountByNumber or eth_getBlockTransactionCountByHash RPC Methods

Returns the number of transactions in the block specified by block_identifier. Delegates to eth_getBlockTransactionCountByNumber if block_identifier is an integer or one of the predefined block parameters 'latest', 'earliest', 'pending', otherwise delegates to eth_getBlockTransactionCountByHash.

>>> cpc_fusion.cpc.getBlockTransactionCount(46147)
>>> cpc_fusion.cpc.getBlockTransactionCount('0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd')  # block 46147
  • Delegates to eth_getTransactionByHAsh RPC Method

Returns the transaction specified by transaction_hash

>>> cpc_fusion.cpc.getTransaction('0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060')
    'blockHash': '0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd',
    'blockNumber': 46147,
    'from': '0xa1e4380a3b1f749673e270229993ee55f35663b4',
    'gas': 21000,
    'gasPrice': 50000000000000,
    'hash': '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060',
    'input': '0x',
    'nonce': 0,
    'to': '0x5df9b87991262f6ba471f09758cde1c0fc1de734',
    'transactionIndex': 0,
    'value': 31337,
Cpc.getTransactionFromBlock(block_identifier, transaction_index)


This method is obsolete and replaced by Cpc.getTransactionByBlock

Cpc.getTransactionByBlock(block_identifier, transaction_index)
  • Delegates to eth_getTransactionByBlockNumberAndIndex or eth_getTransactionByBlockHashAndIndex RPC Methods

Returns the transaction at the index specified by transaction_index from the block specified by block_identifier. Delegates to eth_getTransactionByBlockNumberAndIndex if block_identifier is an integer or one of the predefined block parameters 'latest', 'earliest', 'pending', otherwise delegates to eth_getTransactionByBlockHashAndIndex.

>>> cpc_fusion.cpc.getTransactionFromBlock(46147, 0)
    'blockHash': '0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd',
    'blockNumber': 46147,
    'from': '0xa1e4380a3b1f749673e270229993ee55f35663b4',
    'gas': 21000,
    'gasPrice': 50000000000000,
    'hash': '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060',
    'input': '0x',
    'nonce': 0,
    'to': '0x5df9b87991262f6ba471f09758cde1c0fc1de734',
    'transactionIndex': 0,
    'value': 31337,
>>> cpc_fusion.cpc.getTransactionFromBlock('0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd', 0)
    'blockHash': '0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd',
    'blockNumber': 46147,
    'from': '0xa1e4380a3b1f749673e270229993ee55f35663b4',
    'gas': 21000,
    'gasPrice': 50000000000000,
    'hash': '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060',
    'input': '0x',
    'nonce': 0,
    'to': '0x5df9b87991262f6ba471f09758cde1c0fc1de734',
    'transactionIndex': 0,
    'value': 31337,
Cpc.waitForTransactionReceipt(transaction_hash, timeout=120)

Waits for the transaction specified by transaction_hash to be included in a block, then returns its transaction receipt.

Optionally, specify a timeout in seconds. If timeout elapses before the transaction is added to a block, then waitForTransactionReceipt() raises a cpc_fusion.exceptions.TimeExhausted exception.

>>> cpc_fusion.cpc.waitForTransactionReceipt('0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060')
# If transaction is not yet in a block, time passes, while the thread sleeps...
# ...
# Then when the transaction is added to a block, its receipt is returned:
    'blockHash': '0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd',
    'blockNumber': 46147,
    'contractAddress': None,
    'cumulativeGasUsed': 21000,
    'from': '0xa1e4380a3b1f749673e270229993ee55f35663b4',
    'gasUsed': 21000,
    'logs': [],
    'root': '96a8e009d2b88b1483e6941e6812e32263b05683fac202abc622a3e31aed1957',
    'to': '0x5df9b87991262f6ba471f09758cde1c0fc1de734',
    'transactionHash': '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060',
    'transactionIndex': 0,
  • Delegates to eth_getTransactionReceipt RPC Method

Returns the transaction receipt specified by transaction_hash. If the transaction has not yet been mined returns None

>>> cpc_fusion.cpc.getTransactionReceipt('0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060')  # not yet mined
# wait for it to be mined....
>>> cpc_fusion.cpc.getTransactionReceipt('0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060')
    'blockHash': '0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd',
    'blockNumber': 46147,
    'contractAddress': None,
    'cumulativeGasUsed': 21000,
    'from': '0xa1e4380a3b1f749673e270229993ee55f35663b4',
    'gasUsed': 21000,
    'logs': [],
    'root': '96a8e009d2b88b1483e6941e6812e32263b05683fac202abc622a3e31aed1957',
    'to': '0x5df9b87991262f6ba471f09758cde1c0fc1de734',
    'transactionHash': '0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060',
    'transactionIndex': 0,
Cpc.getTransactionCount(account, block_identifier=cpc_fusion.cpc.defaultBlock)
  • Delegates to eth_getTransactionCount RPC Method

Returns the number of transactions that have been sent from account as of the block specified by block_identifier.

account may be a hex address or an ENS name

>>> cpc_fusion.cpc.getTransactionCount('0xd3cda913deb6f67967b99d67acdfa1712c293601')
  • Delegates to eth_sendTransaction RPC Method

Signs and sends the given transaction

The transaction parameter should be a dictionary with the following fields.

  • from: bytes or text, hex address or ENS name - (optional, default: cpc_fusion.cpc.defaultAccount) The address the transaction is send from.
  • to: bytes or text, hex address or ENS name - (optional when creating new contract) The address the transaction is directed to.
  • gas: integer - (optional, default: 90000) Integer of the gas provided for the transaction execution. It will return unused gas.
  • gasPrice: integer - (optional, default: To-Be-Determined) Integer of the gasPrice used for each paid gas
  • value: integer - (optional) Integer of the value send with this transaction
  • data: bytes or text - The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see Ethereum Contract ABI.
  • nonce: integer - (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.

If the transaction specifies a data value but does not specify gas then the gas value will be populated using the estimateGas() function with an additional buffer of 100000 gas up to the gasLimit of the latest block. In the event that the value returned by estimateGas() method is greater than the gasLimit a ValueError will be raised.

>>> cpc_fusion.cpc.sendTransaction({'to': '0xd3cda913deb6f67967b99d67acdfa1712c293601', 'from': cpc_fusion.cpc.coinbase, 'value': 12345})
  • Delegates to eth_sendRawTransaction RPC Method

Sends a signed and serialized transaction. Returns the transaction hash.

>>> signed_txn = cf.cpc.account.signTransaction(dict(
>>> cf.cpc.sendRawTransaction(signed_txn.rawTransaction)


  • Delegates to eth_blockNumber RPC Method

Returns the number of the most recent block

>>> cpc_fusion.cpc.blockNumber
Cpc.getBlock(block_identifier=cpc.defaultBlock, full_transactions=False)
  • Delegates to eth_getBlockByNumber or eth_getBlockByHash RPC Methods

Returns the block specified by block_identifier. Delegates to eth_getBlockByNumber if block_identifier is an integer or one of the predefined block parameters 'latest', 'earliest', 'pending', otherwise delegates to eth_getBlockByHash.

If full_transactions is True then the 'transactions' key will contain full transactions objects. Otherwise it will be an array of transaction hashes.

>>> cf.cpc.getBlock(100)
AttributeDict({'difficulty': 2,
 'extraData': HexBytes('0xdd85302e302e31876370636861696e88676f312e31302e34856c696e75780000'),
 'gasLimit': 4712388,
 'gasUsed': 0,
 'hash': HexBytes('0xf52f56f96b704e02cf67f797ccbf33a42a095cc00aa9c2be6530ee02b9d34ad7'),
 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'),
 'miner': '0xc05302AceBd0730E3A18A058d7d1CB1204C4a092',
 'number': 100,
 'parentHash': HexBytes('0x7ff55edcbd638510900c0b4a5d5c6cbe66f8f1ff61f66a30f12bd4e1d263d89a'),
 'receiptsRoot': HexBytes('0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421'),
 'size': 934,
 'stateRoot': HexBytes('0x7761096fe7777161e4e66463e17df6a2b555054c369418295703a9ea93008bca'),
 'timestamp': 1543488712,
 'totalDifficulty': 201,
 'transactions': [],
 'transactionsRoot': HexBytes('0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421')})


Cpc.getBalance(account, block_identifier=cpc.defaultBlock)
  • Delegates to eth_getBalance RPC Method

Returns the balance of the given account at the block specified by block_identifier.

account may be a hex address or an ENS name

>>> cpc_fusion.cpc.getBalance('0xd3cda913deb6f67967b99d67acdfa1712c293601')
cpc_fusion.cpc.newAccount(self, password)
  • Delegates to personal_newAccount RPC Method

Generates a new account in the node’s keychain encrypted with the given passphrase. Returns the address of the created account.

>>> cf.personal.newAccount('1')
cpc_fusion.cpc.lockAccount(self, account)
  • Delegates to personal_lockAccount RPC Method

Locks the given account.

>>> cf.personal.lockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601')
cpc_fusion.cpc.unlockAccount(self, account, passphrase, duration=None)
  • Delegates to personal_unlockAccount RPC Method

Unlocks the given account for duration seconds. If duration is None then the account will remain unlocked indefinitely. Returns boolean as to whether the account was successfully unlocked.

>>> cf.personal.unlockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601', 'wrong-passphrase')
>>> cf.personal.unlockAccount('0xd3cda913deb6f67967b99d67acdfa1712c293601', 'the-passphrase')
cpc_fusion.cpc.sendTransaction(self, transaction, passphrase)
  • Delegates to personal_sendTransaction RPC Method

Sends the transaction.


Cpc.contract(address=None, contract_name=None, ContractFactoryClass=Contract, **contract_factory_kwargs)

If address is provided, then this method will return an instance of the contract defined by abi. The address may be a hex string, or an ENS name like 'mycontract.cpc'.

from cpc_fusion import Web3

cf = Web3(...)

contract = cf.cpc.contract(address='0x000000000000000000000000000000000000dead', abi=...)

# alternatively:
contract = cf.cpc.contract(address='mycontract.cpc', abi=...)


If you use an ENS name to initialize a contract, the contract will be looked up by name on each use. If the name could ever change maliciously, first ens_get_address, and then create the contract with the hex address.

If address is not provided, the newly created contract class will be returned. That class will then be initialized by supplying the address.

from cpc_fusion import Web3

cf = Web3(...)

Contract = cf.cpc.contract(abi=...)

# later, initialize contracts with the same metadata at different addresses:
contract1 = Contract(address='0x000000000000000000000000000000000000dead')
contract2 = Contract(address='mycontract.cpc')

contract_name will be used as the name of the contract class. If it is None then the name of the ContractFactoryClass will be used.

ContractFactoryClass will be used as the base Contract class.

The following arguments are accepted for contract class creation.

  • abi
  • asm
  • ast
  • bytecode
  • bytecode_runtime
  • clone_bin
  • dev_doc
  • interface
  • metadata
  • opcodes
  • src_map
  • src_map_runtime
  • user_doc

See ./contracts for more information about how to use contracts.


Modifies the default contract factory from Contract to contractFactoryClass. Future calls to Cpc.contract() will then default to contractFactoryClass.

An example of an alternative Contract Factory is ConciseContract.


  • Delegates to eth_getRNodes RPC Method

Returns the array of address that become rnode as of the candidate of committee.

>>> cf.cpc.getRNodes
[{'Address': '0xe94b7b6c5a0e526a4d97f9768ad6097bde25c62a','Rpt': 16,'Status': 0},{'Address': '0xe94b7b6c5a0e526a4d97f9768ad6097bde25c62a','Rpt': 16,'Status': 1},]

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from cpc_fusion import Web3
from cpc_fusion.middleware import geth_poa_middleware

cf = Web3(Web3.HTTPProvider(''))
# inject the poa compatibility middleware to the innermost layer
cf.middleware_stack.inject(geth_poa_middleware, layer=0)

  • Delegates to eth_getCurrentTerm RPC Method

Returns CurrentTerm.

>>> cf.cpc.getCurrentTerm
  • Delegates to eth_getCurrentRound RPC Method

Returns CurrentView.

>>> cf.cpc.getCurrentView
  • Delegates to eth_getBlockGenerationInfo RPC Method

Returns Committee Slice.

>>> cf.cpc.getBlockGenerationInfo
[{'View': 2, 'Term': 250, 'Proposer': '0x3a18598184ef84198db90c28fdfdfdf56544f747', 'BlockNumber': 3000, 'TermLen': 4}, {'View': 2, 'Term': 250, 'Proposer': '0xc05302acebd0730e3a18a058d7d1cb1204c4a092', 'BlockNumber': 3001, 'TermLen': 4}, {'View': 2, 'Term': 250, 'Proposer': '0xc05302acebd0730e3a18a058d7d1cb1204c4a092', 'BlockNumber': 3002, 'TermLen': 4}, {'View': 2, 'Term': 250, 'Proposer': '0xc05302acebd0730e3a18a058d7d1cb1204c4a092', 'BlockNumber': 3003, 'TermLen': 4}, {'View': 2, 'Term': 250, 'Proposer': '0x3a18598184ef84198db90c28fdfdfdf56544f747', 'BlockNumber': 3004, 'TermLen': 4}, {'View': 2, 'Term': 250, 'Proposer': '0x3a18598184ef84198db90c28fdfdfdf56544f747', 'BlockNumber': 3005, 'TermLen': 4}, {'View': 2, 'Term': 250, 'Proposer': '0x3a18598184ef84198db90c28fdfdfdf56544f747', 'BlockNumber': 3006, 'TermLen': 4}, {'View': 2, 'Term': 250, 'Proposer': '0x6e31e5b68a98dcd17264bd1ba547d0b3e874da1e', 'BlockNumber': 3007, 'TermLen': 4}]