Sending Transactions
Perhaps the most important feature of the hmy
CLI is the ability to create and send signed transactions to the Harmony
blockchain.
Overview
Sending a transaction
Using the Binary:
Using the Shell Wrapper:
Example:
Checking the transaction hash
Check for finality of the transaction by using the transaction hash like so:
Using the Binary:
Using the Shell Wrapper:
Example:
Detail
ChainIDs
Let's first check what chain-ids are available for us to use, we can do that easily with:
Using the Binary:
Using the Shell Wrapper:
Example:
Notice that the output is pretty printed JSON
, most outputs of hmy
are JSON
encoded and hmy
defaults to showing it nicely indented. Sometimes though you might want to turn that off, you can do that for any command with the flag --no-pretty
.
By default, hmy
assumes the testnet
chain-id; override that with the --chain-id
flag
Our first transaction
We'll use the transfer
subcommand of hmy
to send a transaction.
Notice that simply invoking the transfer
subcommand gave us an error message about certain flags not being set. We'll need to provide legitimate values for these flags for our transaction to proceed successfully. Reading off the flags in the error message from left to right, the semantic meanings are as follows:
amount
: The quantity of Harmony One token to transfer from the senders to the receiverfrom
: The sender's one addressfrom-shard
: Shard from which sender's balance will be drawn fromto
: Receiver's ONE addressto-shard
: Shard in which receiver will receive the amount sent by the senderpassphrase:
your wallet passphrase, which is prompted when you hit enter (or you can use a txt file with password and add it: --passphrase file.txt)
A sharded blockchain is a new kind of blockchain architecture where the network is partitioned into sub-networks called shards. Sharding is one of the distinguishing features of Harmony and it is key to solving the traditional scalability problems encountered in other blockchain protocols.
Note: The same ONE address will have a different balance in each shard. Currently Harmony mainnet has four shards while testnet has three shards. Sending a transaction from one shard to another is called a "cross-shard transaction."
Thus, a correct usage of transfer
looks like:
Using the Binary:
Using the Shell Wrapper:
Example:
hmy
assumes that the private keys needed for signing the transaction on behalf of the sender (one1yc06ghr2p8xnl2380kpfayweguuhxdtupkhqzw
in this example) exist in the local keystore or in the hardware wallet if the --ledger
flag was used.
The sender's account must have enough of a balance on the from-shard
to send a transaction. In our example,one1yc06ghr2p8xnl2380kpfayweguuhxdtupkhqzw
must have an amount balance of at least 10 in shard 0.
Try out your transaction with the flag --dry-run
, this flag tells hmy
to create, cryptographically sign the transaction but not actually send it off. Sender's balances are checked and the output is a JSON dump of the signed transaction.
Signing and sending a transaction is very quick, about 2 seconds maximum. The actual sending of the transaction is done via an RPC
(Remote Procedure Call), you'll notice that we did not explicitly say where to send the transaction to. This is because the default destination of the RPC
call goes to http://localhost:9500
, the default HTTP
RPC
server running when you start a local harmony blockchain. For real world usage though, you'll want a different location. You can control that with the --node
flag (see the top of this page for an example).
Result of the transaction
Once an RPC
machine receives a transaction, it sends you back a transaction hash. This transaction hash is the key identifier used when querying the blockchain for transactions.
Simply having a transaction hash does NOT imply that the transaction was successfully accepted by the blockchain. A transaction is successfully accepted once it has been added to the blockchain. In the case of cross-shard transactions (when the from-shard, to-shard values are different), this means each shard has added the transaction to their blockchain.
We can pull down details of the finalized transaction with ./hmy blockchain transaction-receipt
as well:
Using the Binary:
Using the Shell Wrapper:
Example:
If the transaction has not finalized then the "result"
key in the JSON
output will have value of null
.
You should set the value of --node
to the same shard that sent the transaction, notice that the URL we used, https://api.s0.t.hmny.io
contained s0
, this means that this URL is targeting shard 0. For further information, see Querying the Blockchain.
You can tell hmy
to wait until transaction confirmation by providing a positive integer value to flag --wait-for-confirm
. For example, --wait-for-confirm=10
will try checking the receipt of the transaction for 10 seconds.
Last updated