Transactions#

This is the entry page to get information about building a raw transaction for defichain using python.

There are two ways to use this implementation:

  1. Transaction Builder: Abstracts the complexity of creating a transaction to a minimum (recommended for 99% of users)

  2. Advanced: You can create transaction on your own behave and stay very flexible while using the infrastructure of the library


Default Input Structure#

There will be some input structures throughout the implementation witch will be explained below:

Amounts#

The amount describes a combination of an token amount (int / float) and the corresponding token symbol: amount@token

>>> "1@DFI"  # one amount
>>> ["1@DFI", "1@BTC", "1@ETH"]  # multiple amounts

There is an helper class to make the creation of amounts evan easier:

class defichain.transactions.utils.BuildAmounts(amounts=None)

An easy to use class to quickly build amounts.

Parameters:

amounts (str or array str) – import already existing amounts: “amount@token” or [”amount1@t1”, “amount2@t2”]

Example:
>>> from defichain.node import BuildAmounts
>>>
>>> amounts = BuildAmounts()
>>> amounts.add("DFI", 1)
>>> amounts.add("BTC", 2)
>>> amounts.build()
['1@DFI', '2@BTC']
add(coin: str, amount: float) BuildAmounts

Add an amount to other amounts.

Parameters:
  • coin (str) – coin to use

  • amount (float) – amount of specified coin

Returns:

“BuildAmounts”

build() {}

Returns the amount

Returns:

Returns the amount. If it is a single amount, it will return a string. If there are multiple amounts it will return a list of strings.

Address Amounts#

The address is the key, the value are the amounts.

{
    "address": "amounts"
}

Example:

{
    "dcTKz5SQrqf4vGVsgra76ptXeNBcxPrenP": "1@DFI",
    "df1qzkf582h0sgfksj0yn0wxz0r9amyqfferm5wycs": ["2.0@BTC", "3.0@ETH"]
}

There is an helper class to make the creation of address amounts evan easier:

class defichain.transactions.utils.BuildAddressAmounts(address_amounts: {} = None)

An easy to use class to quickly build address amounts.

Parameters:

address_amounts (json string) – import an existing json address amount: {“address”: “amounts”}

Example:
>>> from defichain.node import BuildAddressAmounts
>>>
>>> address_amounts = BuildAddressAmounts()
>>> address_amounts.add("address1", "DFI", 1)
>>> address_amounts.add("address2", "BTC", 2)
>>> address_amounts.build()
{'address1': '1@DFI', 'address2': '2@BTC'}
add(address: str, coin: str, amount: float) BuildAddressAmounts

Add an amount to already existing address amounts.

Parameters:
  • address (str) – defichain address

  • coin (str) – the coin to use

  • amount (str) – the amount of the specified coin

Returns:

“BuildAddressAmounts”

build() {}

Returns the address amount.

Returns:

Returns the address amount