Quickstart

Installation

To use helium-py, first install it using pip:

$ pip install helium-py

Example Transactions

Creating and submitting a payment transaction

A payment from an owned keypair initialized with a 12 word mnemonic to an address specified by its base58 representation. The transaction is serialized to binary and submitted to the blockchain API.

 1import logging
 2
 3from helium_py.crypto.keypair import Address, Keypair
 4from helium_py.transactions import Payment, PaymentV2
 5from helium_py.api import Accounts, PendingTransactions, Transactions
 6
 7logger = logging.getLogger(__name__)
 8
 9# Initialize an owned keypair from a 12 word mnemonic
10bob = Keypair.from_words(['one', 'two', ..., 'twelve'])
11
12# Initialize an address from a b58 string
13alice = Address.from_b58(b'148d8KTRcKA5JKP ekBcKFd4KfvprvFRpjGtivhtmRmnZ8MFYnP3')
14
15# get the speculative nonce for the keypair
16account = Accounts().account_for_address(bob.address.b58.decode())
17
18payment_transaction = PaymentV2(
19    payer=bob.address,
20    payments=[
21        Payment(
22            payee=alice,
23            amount=10,
24            memo=b'memo',
25        ),
26    ],
27    nonce=account['speculative_nonce'] + 1,
28)
29
30# an appropriate transaction fee is calculated at initialization
31logger.info(f'transaction fee is: {payment_transaction.calculated_fee}')
32
33# sign the payment txn with bob's keypair
34signed_payment_transaction = payment_transaction.sign(payer=bob)
35
36# submit the serialized txn to the Blockchain HTTP API
37pending_transactions_client = PendingTransactions()
38response_dict = pending_transactions_client.submit_transaction(signed_payment_transaction)
39
40# check on status of pending transaction
41pending_transactions_client.get_status(response_dict['hash'])
42
43# view finalized transaction information
44transaction_dict = Transactions().get_transaction(response_dict['hash'])

Sending an Account’s full balance

Sending the maximum amount from an account (leaving a 0 HNT balance) requires taking into account the transaction fee. All fees are denominated in Data Credits (DC), which is equal to $0.00001 USD, meaning 35,000 DC equals $0.35 USD. DC are obtained by burning HNT, permanently removing it from circulation. If you do not already have DC in your account, the appropriate amount of HNT will be burned to cover the fee.

The general formula is: amountToSend = balance - feeInHNT

The packages in helium-js provide utility functions to calculate the above:

 1import logging
 2
 3from helium_py.crypto.keypair import Address, Keypair
 4from helium_py.transactions import Payment, PaymentV2
 5from helium_py.api import Accounts, PendingTransactions, Transactions
 6from helium_py.currency import Balance, types
 7
 8logger = logging.getLogger(__name__)
 9
10# Initialize an owned keypair from a 12 word mnemonic
11bob = Keypair.from_words(['one', 'two', ..., 'twelve'])
12
13# Initialize an address from a b58 string
14alice = Address.from_b58(b'148d8KTRcKA5JKPekBcKFd4KfvprvFRpjGtivhtmRmnZ8MFYnP3')
15
16# get the speculative nonce for the keypair
17account = Accounts().account_for_address(bob.address.b58.decode())
18
19payment_transaction = PaymentV2(
20    payer=bob.address,
21    payments=[
22        Payment(
23            payee=alice,
24            amount=account['balance'],
25        ),
26    ],
27    nonce=account['speculative_nonce'] + 1,
28)
29
30# an appropriate transaction fee is calculated at initialization
31logger.info(f'transaction fee is: {payment_transaction.calculated_fee}')
32
33fee_in_dc = Balance(payment_transaction.calculated_fee, currency_type=types.DATA_CREDITS)
34fee_in_hnt = fee_in_dc.to_network_tokens()  # Oracle price can be provided manually or automatically injected
35amount_to_send = Balance(account['balance'], currency_type=types.NETWORK_TOKENS).minus(fee_in_hnt)
36
37payment_transaction_for_fee = PaymentV2(
38    payer=bob.address,
39    payments=[
40        Payment(
41            payee=alice,
42            amount=amount_to_send,
43        ),
44    ],
45    nonce=account['speculative_nonce'] + 1,
46)
47
48# sign the payment txn with bob's keypair
49signed_payment_transaction = payment_transaction_for_fee.sign(payer=bob)
50
51# submit the serialized txn to the Blockchain HTTP API
52pending_transactions_client = PendingTransactions()
53response_dict = pending_transactions_client.submit_transaction(signed_payment_transaction)
54
55# check on status of pending transaction
56pending_transactions_client.get_status(response_dict['hash'])
57
58# view finalized transaction information
59transaction_dict = Transactions().get_transaction(response_dict['hash'])