Manage Witnet transactions
Use the Witnet SDK to build, sign, send and await finalization of Witnet transactions.
Last updated
Was this helpful?
Use the Witnet SDK to build, sign, send and await finalization of Witnet transactions.
Last updated
Was this helpful?
The code examples below assume that the environment is properly set up as described in . You can easily adapt the examples to pass the URL of the Wit/RPC provider to use, and/or the wallet master key (and the password, if encrypted).
Transactions that need to be signed by one or more externally owned addresses:
Transactions that get automatically generated and signed by network validators:
Transaction factories in the Witnet SDK can be attached to either a Wallet, an Account or a Signer object. This will determine the variety of different signers that may end up signing one single transaction.
Value transfers are determined by a list of recipients, and a amount of $WIT coins to be transferred to each one of them. The sum of all input UTXO must cover the total transfer value and the network fees.
The output UTXO change, if any, will be owned by the changePkh
address of the ledger object (i.e. Wallet, Account or Signer) attached to the transaction factory .
Stake deposits are determined by the amount of $WIT coins to be staked, a validator address and an authorization code, where:
The stake value must be equal or greater than 10,000 $WIT.
The authorization code must have been previously generated and signed by the specified validator.
The withdrawer address embedded within the authorization code will be the only one allowed to withdraw part of, or whole, the stake (plus eventual benefits).
The output UTXO change, if any, will be owned by the changePkh
address of the ledger object (i.e. Wallet, Account or Signer) attached to the transaction factory.
Stake withdrawals can only be performed from addresses having previously delegated stake into validators (or delegatees). Withdrawers have right to withdraw not only the staked amount, but also any stake yield produced by delegatees since the stake delegations took place, minus some initially arranged commission.
Any amount can be withdrawn from the current stake hold on some specific withdrawer - validator stake entry, as long as the remaining stake keeps being equal or greater to 10,000 $WIT, or the full stake is withdrawn.
An oracle query is defined as a combination of a Radon Request and some Service Level Agreement parameters.
Radon Requests are the "smart data contracts" of the Witnet blockchain. They formally describe how to retrieve data or real-world facts from one or multiple sources on the Internet, and how to aggregate and transform that data before delivering a query result. They can also be used as an entropy source for randomness.
fees?
The reward for the validator that eventually gets the Data Request Transaction (or DRT) included in the blockchain.
It also determines the reward that the validator including all involved Commit transactions, the one including all Reveal transactions, and every proven-to-be earnest member of the witnessing committee will all get, individually.
If no fees are specified, a value will be self-estimated based on current network congestion, with the expectancy of getting the DRT mined in less than 5 minutes.
witnesses?
The number of witnesses requested to solve the oracle query.
This number provides redundancy against validators that unexpectedly (or willingly) disconnect from the network, or reveal diverging results.
It can be set to any number between 1 and , although a value of 12 should be a great balance on most cases as to guarantee getting a fully secure and reliable result.
It defaults to 12 if not specified. On the Witnet testnet, it defaults to the minimum between 12 and the actual number of active validators (which could eventually turn below 12).
maxResultSize?
Limits the size of the expected result.
It can be set up to 2048 bytes, although it only needs to be specified if the Radon Request is known to produce data of variable size (e.g. strings, byte buffers, arrays or maps). Defaults to 32, if required but not specified.
The output UTXO in a DRT, if any, would stand for returning the change of the transaction (i.e. sum of input UTXOs value minus fees minus value), and would be owned by the signer of the DRT (i.e. the data requester). This is so as to avoid exploitation of the fee market for DRTs, by using dummy DRTs as if they were VTTs.
Different transaction factories can get attached to the same ledger object, in which case they would all share the same UTXO and transaction caches.
The UTXO selection strategy that is followed when building a new transaction can be changed at anytime by updating the strategy
property on the factory's ledger to any of the following values:
"big-first"
Biggest UTXOs are selected first.
"random"
UTXOs are selected randomly.
"slim-fit"
The smallest of all UTXOs that individually covers the transaction fees and value value is selected. If none fulfills this condition, UTXOs get then selected by following the "big-first"
strategy.
"small-first"
Smallest UTXOs are selected first.
As shown on examples above on a case-by-case basis, building a transaction from a transaction factory is as simple as calling to the signTransaction
method and passing some required or optional transaction parameters, depending on the transaction type being signed.
A TypeError will be thrown if passing invalid transaction parameters, or not passing the required ones depending to the transaction type. An Error will be thrown instead if the attached ledger objects had not enough available UTXOs as to cover for transaction expenses, or if some previously signed transaction is in the process of being transmitted to the Witnet network.
One optional parameter common to all transaction types is the fees. A specific amount of $WIT coins can of course be specified, or a Transaction Priority value instead. Depending on the specified priority, the Witnet SDK will seamlessly evaluate the network congestion and estimate the actual fees to pay with the expectation of getting the transaction included in a block within a limited time range:
"opulent"
Less than 60 seconds.
"high"
Less than 5 minutes.
"medium"
Less than 15 minutes.
"low"
Less than 1 hour.
"stinky"
Less than 6 hours.
A previously signed transaction can be sent over the network by just calling the sendTransaction
on a transaction factory object with no extra parameters. Calling this method with no parameters fails if the previously signed transaction had already been sent.
A TransmissionError will be thrown if the transmission fails for whatever reason. The transmission could then be either retried, or just discarded by passing new transaction parameters.
Transaction parameters can also be passed when calling to sendTransaction
, in which case any previously signed and not yet sent transaction would get discarded and its input UTXOs recovered. A new transaction would then get built, signed and immediately sent to the network.
Passing a transaction hash to the confirmTransaction
method on on a ledger object , provides a way to get asynchronously reported every time a change in the status of some previously sent transaction is detected. Additionally, the following parameters can be optionally specified:
A TimeoutError will be thrown If the waiting for inclusion times out.
The input UTXOs won't be automatically pushed back into the UTXOs cache. In these cases, you can either force to reload the whole UTXO dataset when signing the next transaction, or resume the wait for inclusion by again calling to confirmTransaction
.
A MempoolError will be thrown if a not yet included transaction gets removed from the Witnet mempool.
This can happen if a transaction relies on some input UTXO that gets eventually consumed by other transaction paying higher network fees (e.g. created from the same wallet but different application, for instance). It may rarely happen as well with UTs referring stake entries that get incidentally slashed before those UTs get included in a block.
The wait for inclusion cannot be resumed on transactions removed from the Witnet mempool. The local UTXOs caches that belong to the signers of the failing transaction will be automatically reloaded, though.
Once the required number of confirmations is reached, the output UTXOs owned by any addresses controlled by the signing ledger will be automatically added to their respective UTXOs cache.
Calls to signTransaction
, sendTransaction
and confirmTransaction
methods, as well as calls to Transaction Callback handlers, provide a Transaction Receipt object that contains metadata describing a Witnet-compliant transaction and its current network status. Some of the receipt fields are common to all transaction types, while others are specific to certain transaction types:
authorization?
STs
Authorization code signed by some validator, authorizing some specific withdrawer to eventually delegate stake into such validator.
blockEpoch?
All
Witnet epoch at which the transaction got included in a block.
blockHash?
All
Hash of the block that included the transaction on chain.
blockMiner?
All
The validator address that included the transaction on chain.
change?
All but UTs
Difference between total inputs, minus the transaction's fees and value. Measured in $WIT coins.
confirmations?
All
Number of blocks after the transaction got included in a block.
droHash?
DRTs
The hash of combining both the radHash
and rest of SLA parameters of the DRT. Different DRTs can share the same droHash
.
error?
All
Network error produced upon latest transmission or status polling attempt. It gets cleared out out when a re-transmission or the latest polling attempt get solved successfully.
fees
All
Amount of $WIT coins that the validator including the transaction on chain will get.
from
All
The list of signing addresses that pay for the transaction.
hash
All
The unique hash of the signed transaction.
outputLock?
VTTs, UTs
Timestamp since when value transfer outputs will be available for expenditure.
radHash?
DRTs
The unique hash of the Radon Request included in the DRT. DRTs referring the same Radon Request will share the same radHash
.
recipients?
VTTs
Array of recipient addresses and amount of $WIT coins that each one of them will get.
status
All
Last known Transaction Status.
timestamp
All
The Unix timestamp of the last time a change was locally detected in the transaction status.
type
All
The name of the transaction type.
tx
All
Protobuf object containing a Witnet-compliant object, including inputs, outputs and signatures.
validator?
STs, UTs
The validator address.
value
All
The total amount of $WIT coins to be received by either the specified or implicit recipients (on all transaction types but DRTs), or the set of validators involved in solving an oracle query (on DRTs).
weight
All
The transaction's weight units.
withdrawer?
STs, UTs
The withdrawer address.
witnesses?
DRTs
The number of witnessing validators expected to contribute to the resolution of an oracle query.
Transactions created out of calls to signTransaction
and sendTransaction
methods will be internally cached in memory. The status of cached transactions can vary over time to any of the following values:
"signed"
The transaction has been signed but not yet attempted to be sent.
"pending"
The transaction has been signed and is being transmitted to the network.
"relayed"
The transaction has been successfully transmitted to the Witnet network.
"removed"
The transaction has been removed from the Witnet mempool without ever being included in a block.
"mined"
The transaction has just been included within a Witnet block, with no confirmations just yet.
"confirmed"
The transaction has kept being included within the same Witnet block (no chain rollbacks detected) for some specified number of epochs.
"finalized"
The transaction has been included within a Witnet block, and double-checked by the end of next superblock.
This needs to be done just once per transaction type, or in the case of once per Radon Request wanted to be used. As shown on examples above, must be attached upon construction to some given .
confirmations
Number of blocks to wait for after inclusion, as to consider the transaction to be finalized. Defaults to 0 if no otherwise specified, meaning the wait ends as soon as the transaction gets included in a block.
timeoutSecs
An overall timeout is settled as to abort the waiting if it takes too long. Defaults to 10 minutes if no otherwise specified.
onCheckpoint
Handler function to be called upon each new Witnet epoch (i.e. every 20 seconds), once the transaction gets presumably included in a block, as long as the number of required confirmations is not reached. An updated transaction receipt is reported on each call.
onStatusChange
Handler function to be called every time a change on the transaction status is detected, or some network error is produced while polling for status updates. An updated transaction receipt is reported on each call.