Making your first block on Cradle
In this section, we will go over the basics of creating a block on Cradle.
We will be creating a block that contains a simple transaction doing token transfer between two accounts. Although it is a simple example, the concepts are the same for more complex usage.
Proposing an empty block
Before sending an actual transaction, you can try proposing an empty block. This is done by simply clicking Propose Block button without any transaction. This will create a block with no transactions.
Options
- Interval refers to the simulation time in Cradle, not real time between blocks. For example, even if you set the interval to 5000 milliseconds, the block is generated immediately; however Cradle operates as if 5 seconds have passed.
- Proposer option allows you to choose who will be the proposer for the next block manually. Alternatively, you can leave it to Cradle to handle automatically by selecting the Automatic option.
This step is completely optional and exists purely as a demonstration. You can skip this step and go directly to the next section.
Depending on the network, it may take a few seconds for the first block to be created. This is due to the differences in the data required to propose a block; some networks require more data than others.
Creating a transaction
Now that we have a block, let's create a transaction. In this section, we will be creating a bare transaction without the help of a wallet or SDK. This is to illustrate the lifecycle of a transaction without any abstraction.
Sending transactions without the help of a wallet or SDK can be quite challenging. If you're not familiar with this, feel free to skip this section and refer to the examples in the next section.
Requirements for creating a transaction
Creating a transaction and posting it to Cradle is a two-step process.
- First, you need to create a transaction payload. This is a JSON-formatted payload containing the content of the transaction.
- Next, you need to encode the payload using the chain binary. This is to convert the JSON playload into a binary format that the chain can understand.
The encoding process is different for each chain, so you need to use the respective chain binary for encoding. For example, if you are using cosmoshub-4
, you need to use gaiad for encoding.
To create and encode a transaction for sending, you need the following:
- Transaction payload : JSON-formatted payload containing the content of the transaction.
- Chain binary : Respective chain binary for encoding the payload.
BankSend transaction
Below is an example payload of a transaction containing BankSend
message. BankSend
is one of the most common types of message that sends tokens between two accounts.
You can use this as a template to create your own transaction.
{
"body": {
"messages": [
{
"@type": "/cosmos.bank.v1beta1.MsgSend",
"from_address": "<sender address>",
"to_address": "<receiver address>",
"amount": [
{
"denom": "<denom>",
"amount": "<amount>"
}
]
}
],
"memo": "",
"timeout_height": "0",
"extension_options": [],
"non_critical_extension_options": []
},
"auth_info": {
"signer_infos": [],
"fee": {
"amount": [
{
"denom": "<denom>",
"amount": "<amount>"
}
],
"gas_limit": "200000",
"payer": "",
"granter": ""
}
},
"signatures": []
}
Notice that the signatures
field is empty. Usually, a transaction without a signature is regarded as invalid, as the signature is used to verify the identity of the sender.
However, Cradle bypasses verifying the signature, as well as public_key
of the transaction when accepting a transaction. This ultimately allows you to create a transaction whose sender_address
is from a wallet you do not own. This is useful when you want to create a transaction on behalf of a user.
Encode your transaction and send it to Cradle
Encode the payload using respective encoder, then send it to the Cradle session. You can send the transaction using the usual methods.
# Make sure to change `chain_binary`, `json_payload_file`, and `your_cradle_endpoint`
export ENCODED_TX=$(<chain_binary> tx encode <json_payload_file>);
curl <your_cradle_endpoint> -X POST \
-H "Content-Type: application/json" \
--data "{\"method\":\"broadcast_tx_sync\",\"params\":[\"$ENCODED_TX\"],\"id\":1}"
If sending the transaction is successful, you can see the output as below.
{"jsonrpc":"2.0","id":1,"result":{"code":0,"data":"","log":"","codespace":"","hash":"B4ECF4D37B656BC70716FC815CC4A4C77D2BF26D683B43CB008FC9D02C1D98E1"}}
Propose Block
If you sent the transaction successfully, you should see the transaction in the Mempool section. Next step is to propose the block. You can do so by clicking on "Propose Block" button in the Propose Block section.
The proposed block will include all the transactions in the mempool, and the next blockchain state will be computed based on the transactions.
The result of the transaction will be shown in the Transaction tab.
What's next
That's it! You have successfully created your first block on Cradle. You can now try creating more complex transactions, or try creating a block using an SDK.