TxBuilder#

class defichain.transactions.builder.TxBuilder(address: str, account: Account, dataSource: Ocean | Node | None, feePerByte=1.0, replaceable: bool = False)#

This it the main class to build transactions!

The transaction will be built with the information provided and automatically signed.

All transactions are created for the given address. The account parameter must match the given address and contains the matching private key.

Through the given data source, all the necessary information is pulled from the blockchain which is required to create the transaction. The suggested data source is the ocean infrastructure. However, this can also be replaced by a defichain node connection. If no data source is specified, the appropriate inputs must be passed to the individual methods.

By default, a fee of one satoshi per byte is used.

All inputs of an address are always combined to one output.

>>> # Import ocean, wallet, network and txbuilder
>>> from defichain import Ocean
>>> from defichain import Wallet
>>> from defichain.networks import DefichainMainnet
>>> from defichain import TxBuilder
>>> # Specify ocean connection
>>> ocean =  Ocean(network="mainnet")
>>> # Create wallet and account
>>> mnemonic = "avocado key fan step egg engage winter upper attitude carry regret mixed utility body party trip valid oppose gas ensure deputy suspect blur trade"
>>> wallet = Wallet(DefichainMainnet)
>>> wallet.from_mnemonic(mnemonic)
>>> account = wallet.get_account(0)
>>> # Create TxBuilder
>>> builder = TxBuilder(account.get_p2wpkh(), account, ocean)
Parameters:
  • address (str) – (required) address for which the transaction is created

  • account (Account) – (required) account object belonging to the given address

  • dataSource (Ocean | Node | None) – (required) data source for creating the transaction

  • feePerByte (float) – (optional) approximate fee paid per byte

  • replaceable (bool) – (optional) allows transactions to be replacable by transactions with higher fee

send_tx(tx: Transaction | str, maxFeeRate: float = None) str#

Broadcasts the created transaction to the blockchain using the provided data source

Parameters:
  • tx (Transaction | str) – (required) transaction object or serialized transaction string

  • maxFeeRate (float) – (optional) maximum fee rate

Returns:

“hex” (str) - txid

test_tx(tx: Transaction | str, maxFeeRate: float = None) bool#

Tests if the transaction would be accepted by the blockchain using the data source

Parameters:
  • tx (Transaction | str) – (required) transaction object or serialized transaction string

  • maxFeeRate (float) – (optional) maximum fee rate

Returns:

bool

get_inputs_tx() Transaction#

Builds a transaction just with the inputs of the address. The outputs have to be manually specified.

Returns:

Transaction - just with inputs

get_address() str#

Returns the address specified in the builder object

Returns:

address (str)

get_account() Account#

Returns the account specified in the builder object

Returns:

Account

get_dataSource() RemoteDataOcean | RemoteDataNode | None#

Returns the data source specified in the builder object

Returns:

Remote Data Ocean | Remote Data Node | None

get_feePerByte() float#

Returns the fee per byte specified in the builder object

Returns:

float

get_replaceable() bool#

Returns if the builder object creates transactions that are replaceable

Returns:

bool