This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: high level API to access transaction properties (#338)
* implement parser for tx data * add easy access to nested tx properties via get(), meta() and ga_meta() methods closes #272
- Loading branch information
Showing
6 changed files
with
316 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
======= | ||
Amounts | ||
======= | ||
|
||
The Aeternity blockchain tokens are an ERC-20 compatible token, | ||
where the unit is the Aetto (1e-18). | ||
|
||
The ``utils`` submodule provides functions to manipulate the | ||
amounts. | ||
|
||
|
||
Format amount to a human readable format | ||
======================================== | ||
|
||
The following snippets show how to format | ||
an amount from ``aetto`` to a human readable format. | ||
|
||
:: | ||
# import the utils submodule | ||
from aeternity import utils | ||
|
||
amount = 1000000000000000000 | ||
human_value = utils.format_amount(amount) | ||
print(human_value) # prints 1AE | ||
|
||
|
||
Parse amounts | ||
============= | ||
|
||
This snippets shows how to parse amounts | ||
expressed in a human friendly, float or scientific notation. | ||
|
||
:: | ||
# import the utils submodule | ||
from aeternity import utils | ||
|
||
amount = utils.amount_to_aettos(1.2) | ||
print(amount) # prints 1200000000000000000 | ||
|
||
amount = utils.amount_to_aettos("10AE") | ||
print(amount) # prints 10000000000000000000 | ||
|
||
amount = utils.amount_to_aettos(1e1) | ||
print(amount) # prints 10000000000000000000 | ||
|
||
amount = utils.amount_to_aettos(1) | ||
print(amount) # prints 1 | ||
|
||
|
||
.. Important:: | ||
when using amount_to_aettos function, the maximum value as imput is 1e9, | ||
that will result in an aetto value of 1000000000000000000000000000 (1e27). | ||
Bigger values wil return in an error. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ you quickly accomplish common tasks. | |
|
||
cli_accounts | ||
validate_contract_bytecode | ||
amounts | ||
|
||
.. seealso:: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,87 @@ | ||
============ | ||
======== | ||
TxObject | ||
============ | ||
======== | ||
|
||
``TxObject`` is one of the central entity of the Python SDK, | ||
and it represent a transaction object. | ||
|
||
.. autoclass:: aeternity.transactions.TxObject | ||
:members: | ||
:members: get, meta, ga_meta | ||
|
||
The fields of a ``TxObject`` are | ||
|
||
- ``hash``: the transaction hash | ||
- ``data``: the transaction data, it varies for every transaction type | ||
- ``metadata``: it contains additional data that may be relevant in the transaction context | ||
- ``tx``: the rlp + base64 check encoded string of the transaction that is used to broadcast a transaction | ||
|
||
Since a transaction can be a nested structured, the ``TxObject`` is nested as well: | ||
considering a simple spend transaction, the actual structure of the transaction is: | ||
|
||
:: | ||
|
||
SignedTx( | ||
tag: - signed transaction type (11) | ||
version - transaction version, 1 in this case | ||
[signature] - the list of signatures for the signed transaction | ||
tx: SpendTx( - the inner spend transaction | ||
tag - spend transaction type (12) | ||
version - spend transaction version, 1 in this case | ||
sender_id - spend sender | ||
recipient_id - spend recipient | ||
amount - amount being transferred | ||
fee - fee for the miner | ||
ttl - the mempool time to live | ||
nonce - sender account nonce | ||
payload - arbitrary payload | ||
) | ||
) | ||
|
||
This means that to access, for example, the spend transaction ``recipient_id`` from a ``TxObject``, | ||
the code would be: | ||
|
||
:: | ||
|
||
tx_object = node_cli.spend(sender, recipient, "100AE") | ||
# access the recipient_id | ||
tx_object.data.tx.data.recipient_id | ||
|
||
unless the transaction has been posted from a generalized account, in which case there | ||
are 4 levels of nesting: | ||
|
||
:: | ||
|
||
tx_object = node_cli.spend(sender, recipient, "100AE") | ||
# access the recipient_id for a GA generated transaction | ||
tx_object.data.tx.data.tx.data.tx.data.recipient_id | ||
|
||
This is of course somewhat awkward, and therefore the ``TxObject`` provides the ``get(NAME)``, ``meta(NAME)``, ``ga_meta(NAME)`` functions. | ||
|
||
The functions are used to access the values of the properties without worrying about the structure of the transaction, | ||
so the example above will become: | ||
|
||
:: | ||
|
||
tx_object = node_cli.spend(sender, recipient, "100AE") | ||
# access the recipient_id for any spend transaction | ||
tx_object.get("recipient_id") | ||
|
||
Metadata | ||
-------- | ||
Metadatas are special informations that are not part of the transaction itself but my be generated | ||
ad a additional output while creating or parsing a transaction, in particular metadata fields are: | ||
|
||
- ``min_fee`` minimum fee for a transaction, this value is always calculaed and can be used to | ||
evaluate the actual fee used for the transaction. | ||
- ``contract_id`` the id of a contract, only present when deploying a new contract (starts with prefix ``ct_``). | ||
- ``salt`` the random generated salt to prepare the ``commitment_id`` of a name pre-claim transaction. The | ||
salt must be used then to prepare a claim transaction. | ||
|
||
TxObject data fields | ||
-------------------- | ||
Here is the complete list of transactions and available fields: | ||
|
||
|
||
.. literalinclude:: ../../aeternity/transactions.py | ||
:lines: 65-389 | ||
:dedent: 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.