-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #516 from input-output-hk/olgahryniuk-patch-66
Create a new folder for transaction tutorials
- Loading branch information
Showing
20 changed files
with
1,798 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
content/06-development-guidelines/07-transaction-tutorials.mdx
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,6 @@ | ||
--- | ||
title: Transaction tutorials | ||
metaTitle: Transaction tutorials | ||
--- | ||
|
||
<!-- Heading no content --> |
14 changes: 14 additions & 0 deletions
14
content/06-development-guidelines/07-transaction-tutorials/01-introduction.mdx
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,14 @@ | ||
--- | ||
title: What is a transaction? | ||
metaTitle: What is a transaction? | ||
--- | ||
|
||
A transaction is an event created, signed, and sent by a user to modify the ledger's state. It is commonly used to transfer ada or other tokens from one user to another. Additionally, it can serve various purposes, such as token creation, delegation registration to a stake pool, or interaction with smart contracts, among others. The process to modify the ledger through a transaction is as follows: | ||
|
||
- **Creating the transaction**. A transaction includes a set of data that specifies how you intend to modify the ledger. The fundamental components of a transaction include UTXOs, from which the funds are sourced, and destination addresses to which you want to send funds along with the desired amount of tokens. There are various tools available to assist in creating transactions, such as wallets and `cardano-cli`. | ||
|
||
- **Signing the transaction**. A user who owns the funds to be spent must provide authorization for the transaction through a signature. If the funds are held in a smart contract address, the authorization is carried out by executing the smart contract itself. Various tools are available to assist in signing transactions, including wallets and `cardano-cli`. | ||
|
||
- **Submitting the transaction**. For a transaction to be reflected in the ledger, the user must submit it. These transactions are received by stake pools, validated, and then added to the ledger within a block. | ||
|
||
Each transaction includes an identifier known as `TxId`, and after completing these three steps, you can view transaction's content in the Cardano Explorer, accessible at https://explorer.cardano.org, for example. |
66 changes: 66 additions & 0 deletions
66
...t/06-development-guidelines/07-transaction-tutorials/02-minting-transaction.mdx
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,66 @@ | ||
--- | ||
title: Minting transactions | ||
metaTitle: Minting transactions | ||
--- | ||
|
||
The ability to create and manage custom tokens is one of the distinguishing features of the Cardano blockchain. Minting refers to the process of 'producing new tokens, either as part of an initial coin offering (ICO) or ongoing token issuance'. Minting transactions enable users to generate or burn tokens according to predefined rules. This tutorial explores the details of minting transactions on Cardano. | ||
|
||
Consider a scenario where Bob wants to give Alice a token, but only Bob has the minting authority for that token. To achieve this, Bob needs to create a script that defines the minting and burning rules. There are two approaches to accomplish this: using Plutus policy scripts or simple multi-signature scripts. In this case, the tutorial suggests using a simple multi-signature script since the rule is straightforward: only Bob can mint the token. | ||
|
||
Before you begin, ensure that you have all the necessary components ready. First, you need to generate new keys for the script itself. The process for obtaining script keys is as follows: | ||
|
||
``` | ||
$ cardano-cli address key-gen \ | ||
--verification-key-file transactions-tutorial-policy/policy.vkey \ | ||
--signing-key-file transactions-tutorial-policy/policy.skey | ||
``` | ||
|
||
Before creating the script, you need to get the key hash of the new keys: | ||
|
||
``` | ||
$ cardano-cli address key-hash \ | ||
--payment-verification-key-file transactions-tutorial-policy/policy.vkey | ||
8ebd5f9c84fc25ae4506c7d0b687b2f7e82fe3f891036833e7f25c9b | ||
``` | ||
|
||
You can now create the script by writing the following code into `transactions-tutorial-policy/policy.script`: | ||
|
||
``` | ||
{ | ||
“keyHash”: “8ebd5f9c84fc25ae4506c7d0b687b2f7e82fe3f891036833e7f25c9b”, | ||
“type”: “sig” | ||
} | ||
``` | ||
|
||
You can get the `PolicyId` with the following command: | ||
|
||
``` | ||
$ cardano-cli transaction policyid \ | ||
--script-file transactions-tutorial-policy/policy.script | ||
C38b0924e32e677f7787f0a0247b177588ec135db927688d8a63310a | ||
``` | ||
|
||
It is also helpful to store it in an environment variable: | ||
|
||
``` | ||
export POLICY_ID=C38b0924e32e677f7787f0a0247b177588ec135db927688d8a63310a | ||
``` | ||
|
||
Now, you need to create a token name, which has to be in hexadecimal (54657374546F6B656E): | ||
|
||
``` | ||
export TOKEN_NAME=54657374546F6B656E | ||
``` | ||
|
||
Build the transaction: | ||
|
||
``` | ||
cardano-cli transaction build \ | ||
--tx-in f947f84f1156995afd695247a8dc8a508fd40d371ce0afb801029769a0104874#0 \ | ||
--mint "1 $(echo $POLICY_ID).$(echo $TOKEN_NAME)" \ | ||
--testnet-magic 1 \ | ||
--change-address $(cat bob.addr) \ | ||
--mint-script-file ../cardano/transactions-tutorial-policy/policy.script \ | ||
--out-file minting.tx \ | ||
--tx-out "$(cat alice.addr)+1047330+1 $(echo $POLICY_ID).$(echo $TOKEN_NAME)" | ||
``` |
116 changes: 116 additions & 0 deletions
116
...ent/06-development-guidelines/07-transaction-tutorials/03-stake-transaction.mdx
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,116 @@ | ||
--- | ||
title: Staking transactions | ||
metaTitle: Staking transactions | ||
--- | ||
|
||
Staking is a fundamental feature of Cardano, allowing you to earn rewards by delegating ada to stake pools. This tutorial shows how to delegate your ada to a stake pool, withdraw rewards, or stop delegating your ada if you decide to switch to another stake pool. | ||
|
||
To delegate ada, first, you need to generate a stake key pair. This involves creating a `stake.vkey` and `stake.skey`, in addition to your regular payment key pair (`payment.vkey` and `payment.skey`), and obtaining a new address called `stake.addr`, similar to your `payment.addr`. | ||
|
||
Assuming you already have your `payment.vkey` and `payment.skey`, which you can easily generate using the `cardano-cli address key-gen` command, proceed by creating the `stake.vkey` and `stake.skey`: | ||
|
||
``` | ||
cardano-cli stake-address key-gen \ | ||
--verification-key-file stake.vkey \ | ||
--signing-key-file stake.skey | ||
``` | ||
|
||
Now you have your payment key pair and stake key pair. The next step is to build the `payment.addr` and `stake.addr` files: | ||
|
||
``` | ||
# payment.addr | ||
cardano-cli address build \ | ||
--payment-verification-key-file payment.vkey \ | ||
--stake-verification-key-file stake.vkey \ | ||
--out-file payment.addr | ||
``` | ||
|
||
Note that the example includes the `--stake-verification-key-file` argument. This command is used to create an address that includes its staking part. Addresses without a staking part are primarily used for sending and receiving ada, as well as interacting with DApps. Some examples may omit this argument, but it's crucial for delegation and participating in the staking process. | ||
|
||
``` | ||
# stake.addr | ||
cardano-cli stake-address build \ | ||
--stake-verification-key-file stake.vkey \ | ||
--out-file stake.addr | ||
``` | ||
|
||
This command uses only the stake verification file. | ||
|
||
Before proceeding with delegation, it's essential to create two new certificates: a registration certificate and a delegation certificate. These files are required to register the stake key on the blockchain and indicate your intention to delegate ada. | ||
|
||
This command generates a certificate, which indicates the intention to register the stake key: | ||
|
||
``` | ||
# registration.cert | ||
cardano-cli stake-address registration-certificate \ | ||
--stake-verification-key-file stake.vkey \ | ||
--out-file reg.cert | ||
``` | ||
|
||
Create a delegation certificate: | ||
|
||
``` | ||
# delegate.cert | ||
cardano-cli stake-address delegation-certificate \ | ||
--stake-verification-key-file stake.vkey \ | ||
--stake-pool-id pool18pn6p9ef58u4ga3wagp44qhzm8f6zncl57g6qgh0pk3yytwz54h \ | ||
--out-file delegation.cert | ||
``` | ||
|
||
At this stage of the tutorial, you should have already selected a stake pool. The example features ADACT (PoolId: pool18pn6p9ef58u4ga3wagp44qhzm8f6zncl57g6qgh0pk3yytwz54h). You can list all pools using the command `cardano-cli query stake-pools` or visit https://preview.cardanoscan.io/pools to select the pool that best suits your needs. Please note that the example uses the `preview` testnet. If you're using another testnet or mainnet, make sure that the pool ID is on the correct network. | ||
|
||
Now that you have the `reg.cert` and `delegation.cert` files, you can proceed to the next step. You need to send a transaction containing the certificates. There are two options: sending them individually or sending them together. The example shows how to send them both at the same time. | ||
|
||
Start by querying the funds of `payment.addr`: | ||
|
||
``` | ||
cardano-cli query utxo --address $(cat payment.addr) | ||
TxHash TxIx Amount | ||
-------------------------------------------------------------------------------------- | ||
142c46bb93b9c80140a6302e4a8a360e6f46f55aaf001c825ca790bb23572754 0 10000000000 lovelace + TxOutDatumNone | ||
``` | ||
|
||
If there are no funds in the address, you can request them from the [faucet](https://docs.cardano.org/cardano-testnet/tools/faucet/). Make sure you select the right testnet. | ||
|
||
It's time to build, sign, and submit your transaction. | ||
|
||
The command below introduces two new arguments of transaction creation: `--witness-override` and `certificate-file`. The first one specifies that the transaction will require two signatures. Therefore, transaction fees will be higher. `certificate-file` adds your new certificates to the transaction: | ||
|
||
``` | ||
cardano-cli transaction build \ | ||
--witness-override 2 \ | ||
--tx-in 142c46bb93b9c80140a6302e4a8a360e6f46f55aaf001c825ca790bb23572754#0 \ | ||
--certificate-file reg.cert \ | ||
--certificate-file delegation.cert \ | ||
--change-address $(cat payment.addr) \ | ||
--out-file delegateTx.raw | ||
``` | ||
|
||
In the signing phase, you need to add two signatures: `payment.skey` and `stake.skey`: | ||
|
||
``` | ||
cardano-cli transaction sign \ | ||
--tx-body-file delegateTx.raw \ | ||
--signing-key-file payment.skey \ | ||
--signing-key-file stake.skey \ | ||
--out-file delegateTx.signed | ||
cardano-cli transaction submit \ | ||
--tx-file delegateTx.signed | ||
Transaction successfully submitted. | ||
``` | ||
|
||
Once the transaction is submitted and processed, you have successfully delegated your ada. Now, let's take a look at your delegation and rewards: | ||
|
||
``` | ||
cardano-cli query stake-address-info --address $(cat stake.addr) | ||
[ | ||
{ | ||
"address": "stake_test1uq954t492tmusk2dy9z505g3cz3sfpnh0swsqjmzk47rasqyn8uqp", | ||
"delegation": "pool18pn6p9ef58u4ga3wagp44qhzm8f6zncl57g6qgh0pk3yytwz54h", | ||
"rewardAccountBalance": 0 | ||
} | ||
] | ||
``` | ||
|
||
After a few days, you will receive rewards, and the next part of the tutorial demonstrates how to withdraw them. |
59 changes: 59 additions & 0 deletions
59
.../06-development-guidelines/07-transaction-tutorials/04-withdraw-transaction.mdx
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,59 @@ | ||
--- | ||
title: Withdrawing transactions | ||
metaTitle: Withdrawing transactions | ||
--- | ||
|
||
This part of the tutorial will demonstrate how easy it is to claim rewards using the `cardano-cli`. First, you need to ensure that you already have rewards available in your stake address. Check it with the following command: | ||
|
||
``` | ||
$ cardano-cli query stake-address-info --address $(cat stake.addr) | ||
[ | ||
{ | ||
"address": "stake_test1uq954t492tmusk2dy9z505g3cz3sfpnh0swsqjmzk47rasqyn8uqp", | ||
"delegation": "pool18pn6p9ef58u4ga3wagp44qhzm8f6zncl57g6qgh0pk3yytwz54h", | ||
"rewardAccountBalance": 29385845 | ||
} | ||
] | ||
``` | ||
|
||
In this case, you have ~29 ada. You also need some of your own ada to pay the fees, so you need to query your payment address: | ||
|
||
``` | ||
$ cardano-cli query utxo --address $(cat payment.addr) | ||
TxHash TxIx Amount | ||
-------------------------------------------------------------------------------------- | ||
afb33e353a9880b7cbd9e5eb2cbffa024d1b3b938ee2c739e53dd187094e8f0d 0 10000000 lovelace + TxOutDatumInline ReferenceTxInsScriptsInlineDatumsInBabbageEra (HashableScriptData "\216y\159\CAN*\255" (ScriptDataConstructor 0 [ScriptDataNumber 42])) | ||
afb33e353a9880b7cbd9e5eb2cbffa024d1b3b938ee2c739e53dd187094e8f0d 1 9987657206 lovelace + TxOutDatumNone | ||
``` | ||
|
||
Use the second UTXO with TxIx=1. You're ready to create your withdrawal transaction: | ||
|
||
``` | ||
cardano-cli transaction build \ | ||
--tx-in afb33e353a9880b7cbd9e5eb2cbffa024d1b3b938ee2c739e53dd187094e8f0d#1 \ | ||
--withdrawal $(cat stake.addr)+29385845 \ | ||
--change-address $(cat payment.addr) \ | ||
--out-file withdrawal-tx.raw | ||
``` | ||
|
||
Note that this transaction will take funds from the stake address, so it will need to be signed with the `stake.skey` file: | ||
|
||
``` | ||
cardano-cli transaction sign \ | ||
--tx-file withdrawal-tx.raw \ | ||
--signing-key-file payment.skey \ | ||
--signing-key-file stake.skey \ | ||
--out-file withdrawal-tx.signed | ||
cardano-cli transaction submit --tx-file withdrawal-tx.signed | ||
``` | ||
|
||
Finally, verify that you have received your rewards in your payment address: | ||
|
||
``` | ||
$ cardano-cli query utxo --address $(cat payment.addr) | ||
TxHash TxIx Amount | ||
-------------------------------------------------------------------------------------- | ||
2b1bfc342c1f5531df4cfa220eac79574142c7263d97885d2ad8588ca1a7e22b 0 10016871698 lovelace + TxOutDatumNone | ||
afb33e353a9880b7cbd9e5eb2cbffa024d1b3b938ee2c739e53dd187094e8f0d 0 10000000 lovelace + TxOutDatumInline ReferenceTxInsScriptsInlineDatumsInBabbageEra (HashableScriptData "\216y\159\CAN*\255" (ScriptDataConstructor 0 [ScriptDataNumber 42])) | ||
``` |
64 changes: 64 additions & 0 deletions
64
...6-development-guidelines/07-transaction-tutorials/05-redelegate-transaction.mdx
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,64 @@ | ||
--- | ||
title: Redelegation | ||
metaTitle: Redelegation | ||
--- | ||
|
||
This tutorial will show how to redelegate your stake to another stake pool. There can be multiple reasons to redelegate your stake, but the most common ones are: | ||
|
||
- Original stake pool is no longer available | ||
- You've discovered a more convenient option | ||
- You have your own stake pool and you want to delegate your stake to it. | ||
|
||
To do that, you need to create a new certificate that you are sending in a transaction, as in the first part of the tutorial: | ||
|
||
``` | ||
cardano-cli stake-address delegation-certificate \ | ||
--stake-verification-key-file stake.vkey \ | ||
--stake-pool-id pool1vzqtn3mtfvvuy8ghksy34gs9g97tszj5f8mr3sn7asy5vk577ec \ | ||
--out-file new-delegation.cert | ||
``` | ||
|
||
Now the delegation certificate contains the `--stake-pool-id` of the new stake pool. It means that you're ready to build your transaction, selecting the UTXO from which you will pay the fee: | ||
|
||
``` | ||
$ cardano-cli query utxo --address $(cat payment.addr) | ||
TxHash TxIx Amount | ||
-------------------------------------------------------------------------------------- | ||
2b1bfc342c1f5531df4cfa220eac79574142c7263d97885d2ad8588ca1a7e22b 0 10016871698 lovelace + TxOutDatumNone | ||
afb33e353a9880b7cbd9e5eb2cbffa024d1b3b938ee2c739e53dd187094e8f0d 0 10000000 lovelace + TxOutDatumInline ReferenceTxInsScriptsInlineDatumsInBabbageEra (HashableScriptData "\216y\159\CAN*\255" (ScriptDataConstructor 0 [ScriptDataNumber 42])) | ||
``` | ||
|
||
Build: | ||
|
||
``` | ||
cardano-cli transaction build \ | ||
--witness-override 2 \ | ||
--tx-in 2b1bfc342c1f5531df4cfa220eac79574142c7263d97885d2ad8588ca1a7e22b#0 \ | ||
--change-address $(cat payment.addr) \ | ||
--certificate new-delegation.cert \ | ||
--out-file new-delegation.tx | ||
Sign: | ||
cardano-cli transaction sign \ | ||
--tx-file new-delegation.tx \ | ||
--signing-key-file payment.skey \ | ||
--signing-key-file stake.skey \ | ||
--out-file new-delegation.signed | ||
cardano-cli transaction submit --tx-file new-delegation.signed | ||
``` | ||
|
||
If you check your stake address details, you'll see that you have delegated your stake to a new stake pool and you have zero rewards for now: | ||
|
||
``` | ||
$ cardano-cli query stake-address-info --address $(cat stake.addr) | ||
[ | ||
{ | ||
"address": "stake_test1uq954t492tmusk2dy9z505g3cz3sfpnh0swsqjmzk47rasqyn8uqp", | ||
"delegation": "pool1vzqtn3mtfvvuy8ghksy34gs9g97tszj5f8mr3sn7asy5vk577ec", | ||
"rewardAccountBalance": 0 | ||
} | ||
] | ||
``` | ||
|
Oops, something went wrong.