API#
Models#
Account#
- djwebdapp.models.account_setup(sender, instance, **kwargs)[source]#
Initial account setup, pre_save signal.
If the
Account
is created with asecret_key
and without anyaddress
and theBlockchain.provider
supports it, then it will fetch theaddress
automatically.If the
Account
is created without anaddress
nor asecret_key
then it is considered that you are creating a new wallet and thedjwebdapp.provider.Provider.generate_secret_key()
method will generate a py:attr:~Account.secret_key.If the
balance
is not set at all, then it will be set with therefresh_balance()
method.
- class djwebdapp.models.Account(*args, **kwargs)[source]#
A blockchain account.
- name#
Optionnal name for this account.
- description#
Optionnal description for this account.
- created_at#
Automatic datetime of the creation of this account in the database.
- updated_at#
Automatic datetime of the last update of this account in the database.
- address#
Account address on the blockchain.
- blockchain#
Foreign key to the
Blockchain
model this account is related to.
- balance#
Decimal balance of this account, updated by the
refresh_balance()
method. with the refresh_balances management command. Uses
- owner#
Foreign key to your User model which owns this account, if any.
- secret_key#
djfernet encrypted private key of this account.
This is used to sign transactions. The secret key must be set with the
set_secret_key()
method.A secret key is generated automatically by the
account_setup()
function if the account is not created with aaddress
.
- counter#
Counter of transactions sent from this account.
- last_level#
Last block level when a transaction was sent from this account.
- index#
Boolean to indicate wether the indexer should index all transactions or not.
- exception DoesNotExist#
- exception MultipleObjectsReturned#
- property provider#
Return a Provider instance for this wallet.
The
provider_cls
returns the class that will be used.
- refresh_balance(commit=True)[source]#
Refresh the balance of this account.
Using
get_balance()
, it will updatebalance
field and save the model.Then, return the
balance
.- Parameters:
commit – Needs to be True for this method to actually save the model.
Blockchain#
- class djwebdapp.models.Blockchain(*args, **kwargs)[source]#
Class to represent a blockchain.
- name#
Name of the blockchain.
- provider_class#
Provider class to use for this blockchain.
- is_active#
Boolean to indicate wether this blockchain is active or not.
- description#
Optionnal description for this blockchain.
- unit#
Unit name of the blockchain, ie. eth, xtz…
- unit_micro#
Unit name of the smallest unit of the blockchain, ie. wei, mutez…
- index_level#
Highest indexed level.
- min_level#
Lowest indexed level
- min_confirmations#
Number of confirmation blocks from confirm to done states.
- exception DoesNotExist#
- exception MultipleObjectsReturned#
- property provider#
Return a fresh instance of the provider class bound to self.
- property provider_cls#
Return the imported provider class.
Node#
- class djwebdapp.models.Node(*args, **kwargs)[source]#
Blockchain node that we can use to query.
- blockchain#
Foreign key to the
Blockchain
model this node is related to.
- name#
Node name, generated from endpoint if empty.
- endpoint#
Node endpoint to query.
- is_active#
Boolean to indicate wether this node is active or not.
- priority#
Nodes with the highest priority will be used first.
- exception DoesNotExist#
- exception MultipleObjectsReturned#
Transaction#
- djwebdapp.models.dependency_graph(sender, instance, **kwargs)[source]#
If the transaction is a contract call, then add the contract as dependency.
- class djwebdapp.models.Transaction(*args, **kwargs)[source]#
Transaction superclass, base for all blockchain-specific classes.
- normalizer_class#
Name of the
Normalizer
subclass to call to normalize blockchain transactions for contracts subclassing this model.
- id#
UUID primary key.
- created_at#
Datetime when the transaction was created.
- updated_at#
Datetime when the transaction was last updated.
- name#
Optional free label name.
- description#
Optional description text.
- blockchain#
Foreign key to the
Blockchain
model this transaction is related to.
- level#
Level of the blockchain when the transaction was send.
- hash#
Transaction hash.
- counter#
Transaction counter. Used to avoid replay attack. This counter is filled by the blockchain provider.
- nonce#
Transaction nonce. Used to order transactions in the same block.
- gasprice#
Gas price used to send the transaction.
- gas#
Number of gas used to send the transaction.
- last_fail#
If the transaction failed, this field contains the datetime of the last failure.
- max_fails#
Number of failures to retry before aborting transaction.
- has_code#
If the transaction is a contract creation, and the transaction contains the deployed contract code, this field is set to True.
- metadata#
This field contains the metadata of the transaction.
- normalized#
Enabled when transaction is normalized. Set True by the
normalize()
method.
- state#
Status of the transaction. By default, the transaction is in the deploy state. When the transaction is indexed, the state is set to done. The state is updated by the
state_set()
method.
- error#
If the transaction failed, this field contains the error message.
- history#
History of the state of the transaction associated with the datetime of the state change.
- amount#
Amount send by the transaction if there is.
- args#
Arguments, appliable to deployments or method calls.
- address#
Contract address, appliable to method calls.
- function#
Function name, if this is a method call.
- kind#
Kind of transaction. Can be contract, function or transfer.
- index#
Boolean to indicate wether the indexer should index all transactions or not.
- contract#
Smart contract related to this transaction, appliable to method call transactions.
- caller#
Transaction that called this one, if any.
- target_contract#
In your own subclasses for function calls, this should be an FK to the contract subclass, then
save()
will provisioncontract
from the parent model of the target contract.
- entrypoint#
In your own subclasses for function calls, this should be the name of the function that corresponds to the subclass, for example, FA12TezosMint.entrypoint is set to the “mint” string. Then,
save()
can provisionfunction
from that.
- contract_name#
In your own subclasses for contract transactions, this should be the name of the file that contains smart contract code, without extension. The file must be in the sub-directory contracts of the django app of the model.
- exception DoesNotExist#
- exception MultipleObjectsReturned#
- dependency_add(transaction)[source]#
Add a transaction that must be deployed before this one.
- Parameters:
transaction – The transaction that must be deployed before this one.
- deploy()[source]#
Deploy the transaction.
Wrapper around the
djwebdapp.provider.Provider.deploy()
method to deploy this trasnsaction.In case of exception, it will log the exception, set
last_fail
, and count the number of deploy attempts inhistory
.If the deploy attempts count is above the
max_fails
field, then setstate
to aborted, otherwise, set it to retry.
- normalize()[source]#
Method invoked when normalizing a transaction.
By default, this relies on
normalizer_class
- save(*args, **kwargs)[source]#
Save the transaction in the database.
Sets
kind
based on the attributes of this transaction andblockchain
from thesender
automatically.Also, provision
function
andcontract
fromentrypoint
andtarget_contract
.
TransactionManager#
- class djwebdapp.models.TransactionManager(*args, **kwargs)[source]#
Manager for the
Transaction
model.This Model Manager overrides get_or_create() and update_or_create() to handle parent foreign keys dynamically.
- property parent_fk_column#
Return the name of the foreign key to the parent.
Dependency#
- class djwebdapp.models.Dependency(*args, **kwargs)[source]#
A dependency between two transactions.
When a transaction is deployed, it is checked if it has a dependency. If it does, the transaction is not deployed, but the dependency is checked instead. If the dependency is deployed, the transaction is deployed. Otherwise, the transaction is not.
- dependent#
The
Transaction
that depends on thedependency
one.
- dependency#
The
Transaction
that is required to be deployed before thedependent
one.
- graph#
The
Transaction
that this graph was created for. This is used for performance reasons, to not have to load the full dependency table for every transaction.
- created_at#
The time the dependency was created.
- updated_at#
The time the dependency was updated.
- exception DoesNotExist#
- exception MultipleObjectsReturned#
Provider#
- class djwebdapp.provider.Provider(blockchain=None, wallet=None)[source]#
Base Provider class, encapsulates business logic with blockchains.
Instanciating a Provider requires either a blockchain or a wallet argument.
Warning
Do not use this class directly, it is meant to be sub-classed for each blockchain type.
- Parameters:
blockchain –
Blockchain
object to instanciate the Provider with, in which case the Python client for the blockchain will use its default account.wallet –
Account
object to bind the provider with, in which case thesecret_key
must be set in the Account.
Note
You don’t need to pass both wallet and blockchain arguments, the provider will instanciate with the blockchain attribute of the wallet argument if any.
- blockchain#
Blockchain
this provider was instanciated for.
- transaction_class#
Transaction
subclass this provider works with. For example,TezosTransaction
if it’s a tezos provider, or aEthereumTransaction
if it’s an ethereum provider. This is supposed to be set as a class attribute by the provider subclass.
- exclude_states#
States to exclude when searching for transactions to deploy.
- property client#
Cached result of
get_client()
- deploy(transaction)[source]#
Deploy a given
Transaction
object.
- download(target: str)[source]#
Download a contract history from the configured indexer.
This will use an indexer such as etherscan or tzkt to download the history from a web-API rather than by indexing the blockchain which could take a really long while for large contracts.
- Parameters:
target – String address of the contract to download history for.
- get_balance(address=None)[source]#
Query the blockchain and return the balance of an address.
- address#
Address to get the balance of, use the current client address by default.
- get_client(wallet=None)[source]#
Return the Python client that provider encapsulates.
- Parameters:
wallet –
Account
object to use, note that it must have asecret_key
.
- property head#
Return the current block number.
Raises NotImplemented in base Provider class.
- index()[source]#
Index the blockchain.
Return if
reorg()
returns True.Iterate over each level from the last indexed level in the
djwebdapp.models.Blockchain.index_level
column (or 0) up to the current head level. Callindex_level()
for each level.
- index_init()[source]#
Query the database for transactions hashes and contract addresses to index in
index()
Provisions
- index_level(level: int)[source]#
Index a given block level.
Left to implement in new provider subclasses.
- normalize()[source]#
Run normalize() on all un-normalized transactions.
Call
normalize()
on each transaction that has not been normalized.Internal transactions are normalized after their caller is normalized.
- reorg()[source]#
Handle reorg if necessary.
Compare the head level with the last indexed level, if it’s superior then consider a reorg happened on the blockchain.
In this case, empty the level, hash, and address of every
Transaction
in DB which has a level greater than or equal to the current head level, and set their state to deleted.
- spool()[source]#
Deploy the next transaction of any kind and return it.
It checks for the next transaction with the following logic:
spool_transfers()
: is there any new transfer to deploy?spool_contracts()
: is there any new contract to deploy from an account with balance?
- spool_calls()[source]#
Return the calls to deploy in
spool()
.Return a QuerySet of
transaction_class
objects of thisblockchain
which:are of
kind
function
have no
hash
are related to a contract which does have an
address
which
state
is not inexclude_states
,which sender
Account
have not deployed to the blockchain during this level according tohead
anddjwebdapp.models.Account.last_level
which sender has balance above 0
ordered by
created_at
ascending order, so that it gets the oldest first.
- spool_contracts()[source]#
Return the contracts to deploy, used by
spool()
.Return a QuerySet of
transaction_class
objects of thisblockchain
which:are of
kind
contract
have no
hash
have code according to
has_code
, so that we can actually deploy ithave no
address
which
state
is not inexclude_states
,which sender
Account
have not deployed to the blockchain during this level according tohead
anddjwebdapp.models.Account.last_level
which sender has balance above 0
ordered by
created_at
ascending order, so that it gets the oldest first.
- spool_transfers()[source]#
Return the transfers to deploy, used by
spool()
.Return a QuerySet of
transaction_class
objects of thisblockchain
which:are of
kind
transfer
have no
hash
which
state
is not inexclude_states
,which sender
Account
have not deployed to the blockchain during this level according tohead
anddjwebdapp.models.Account.last_level
which sender has balance above 0
ordered by
created_at
ascending order, so that it gets the oldest first.
Normalizer#
Normalizer classes are optionnal helpers to easy contract indexation.
Exceptions#
- exception djwebdapp.exceptions.BaseException[source]#
Base exception class for all exceptions of this package.