You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/docs/content/docs/guides/aiken.mdx
+39-43Lines changed: 39 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,24 +4,23 @@ icon: "guides/aiken.png"
4
4
---
5
5
importLinkfrom"fumadocs-core/link";
6
6
7
-
Aiken is a functional programming language created for Cardano smart contract development. It prioritizes on-chain execution and offers a user-friendly approach for building secure and efficient smart contracts, making it a valuable choice for developers aiming to create robust on-chain applications.
7
+
Aiken is a functional programming language for Cardano smart contract development. It prioritizes on-chain execution and offers a user-friendly approach for building secure and efficient smart contracts.
8
8
9
-
In this tutorial, we will walk you through the process of writing a simple smart contract in Aiken, and create 2 transactions to lock and unlock assets on the Cardano blockchain.
10
-
11
-
You can also try the <Linkhref="https://aiken-template.meshjs.dev/">live demo</Link> and here are the codes on the <Linkhref="https://github.com/MeshJS/aiken-next-ts-template/tree/main">GitHub repository</Link>
9
+
This tutorial walks you through writing a smart contract in Aiken and creating two transactions to lock and unlock assets on the Cardano blockchain.
12
10
11
+
Try the <Linkhref="https://aiken-template.meshjs.dev/">live demo</Link>. View the code on the <Linkhref="https://github.com/MeshJS/aiken-next-ts-template/tree/main">GitHub repository</Link>.
13
12
14
13
## System setup
15
14
16
-
This section will guide you through the process of setting up your system compile Aiken smart contracts. You can skip this section if you have already set up your system or do not wish to compile the contract.
15
+
Set up your system to compile Aiken smart contracts. Skip this section if you have already set up your system or do not wish to compile the contract.
17
16
18
-
You can also check the installation instructions on the <Linkhref="https://aiken-lang.org/installation-instructions">Aiken website</Link> for more information.
17
+
Check the installation instructions on the <Linkhref="https://aiken-lang.org/installation-instructions">Aiken website</Link> for more information.
19
18
20
19
### Using aikup (on Linux & MacOS only) [!toc]
21
20
22
-
If you are using Linux or MacOS, you can use the utility tool to download and manage Aiken's pre-compiled executables.
21
+
Linux and MacOS users can use the utility tool to download and manage Aiken's pre-compiled executables.
23
22
24
-
You can install the Aiken CLI by running the following command in your terminal:
23
+
Install the Aiken CLI:
25
24
26
25
```tsx
27
26
$curl-sSfLhttps://install.aiken-lang.org | bash
@@ -30,57 +29,56 @@ $ aikup
30
29
31
30
### From sources (all platforms) [!toc]
32
31
33
-
Aiken is written in Rust, so you will need to install Rust and Cargo to compile the smart contract. You can install Rust by following the instructions on the <Linkhref="https://www.rust-lang.org/">Rust website</Link>.
32
+
Aiken is written in Rust. Install Rust and Cargo to compile the smart contract. Install Rust via the <Linkhref="https://www.rust-lang.org/">Rust website</Link>.
34
33
35
-
Next, you will need Cargo, the Rust package manager. You can install Cargo by following the instructions on the <Linkhref="https://doc.rust-lang.org/stable/book/ch01-01-installation.html">Cargo website</Link>.
34
+
Install Cargo, the Rust package manager, via the <Linkhref="https://doc.rust-lang.org/stable/book/ch01-01-installation.html">Cargo website</Link>.
36
35
37
-
You will know you have successfully installed Rust and Cargo when you can run the following commands in your terminal:
36
+
Verify installation:
38
37
39
38
```tsx
40
39
$rustc--version
41
40
$cargo--version
42
41
```
43
42
44
-
Next, you will need to install the Aiken CLI. You can install the Aiken CLI by running the following command in your terminal:
43
+
Install the Aiken CLI:
45
44
46
45
```tsx
47
46
$cargoinstallaiken
48
47
```
49
48
50
49
### Check your installation [!toc]
51
50
52
-
You will know you have successfully installed the Aiken CLI when you can run the following command in your terminal:
51
+
Verify the Aiken CLI installation:
53
52
54
53
```tsx
55
54
$aiken-V
56
55
```
57
56
58
-
If you face any issues, please check the installation instructions on the <Linkhref="https://aiken-lang.org/installation-instructions">Aiken website</Link> for more information.
59
-
57
+
If issues arise, check the <Linkhref="https://aiken-lang.org/installation-instructions">Aiken website</Link>.
60
58
61
59
## Writing a smart contract with Aiken
62
60
63
-
In this section, we will walk you through the process of writing a simple smart contract in Aiken. We will also create 2 transactions to lock and unlock assets on the Cardano blockchain.
61
+
Write a smart contract in Aiken and create two transactions to lock and unlock assets.
64
62
65
-
You can read more about this example on the <Linkhref="https://aiken-lang.org/example--hello-world/basics">Aiken website</Link>.
63
+
Read more about this example on the <Linkhref="https://aiken-lang.org/example--hello-world/basics">Aiken website</Link>.
66
64
67
65
### Create a new project [!toc]
68
66
69
-
First, we will create a new project. Please refer to <Linkhref="/guides/nextjs">this guide</Link> for more information on creating a new Next.js project.
67
+
Create a new project. Refer to <Linkhref="/guides/nextjs">this guide</Link> for creating a new Next.js project.
70
68
71
-
Next, we create a new Aiken project within this project folder:
69
+
Create a new Aiken project within this project folder:
72
70
73
71
```tsx
74
72
$aikenmeshjs/hello_world
75
73
$cdhello_world
76
74
$aikencheck
77
75
```
78
76
79
-
Remember to check your Aiken project by running `aiken check`after creating a new project and as you develop the contract.
77
+
Run `aiken check`to verify your project.
80
78
81
79
### Write the smart contract [!toc]
82
80
83
-
Let's create file for our validator,`validators/hello_world.ak`:
81
+
Create`validators/hello_world.ak`:
84
82
85
83
```tsx
86
84
useaiken/hash.{Blake2b_224, Hash}
@@ -109,32 +107,31 @@ validator {
109
107
}
110
108
```
111
109
112
-
This validator checks that the redeemer message is "Hello, World!" and that the transaction is signed by the owner of the datum. If both conditions are met, the validator returns `true`. Otherwise, it returns`false`.
110
+
This validator checks that the redeemer message is "Hello, World!" and that the transaction is signed by the datum owner. Returns `true` if both conditions are met; otherwise`false`.
113
111
114
-
Let's compile the smart contract with the Aiken CLI:
112
+
Compile the smart contract:
115
113
116
114
```tsx
117
115
$aikenbuild
118
116
```
119
117
120
-
This command will compile the smart contract and generate the `plutus.json` file in the root folder. This file is a <Linkhref="https://cips.cardano.org/cip/CIP-57">CIP-0057 Plutus blueprint</Link>, blueprint describes your on-chain contract and its binary interface.
121
-
118
+
This generates `plutus.json` in the root folder. This file is a <Linkhref="https://cips.cardano.org/cip/CIP-57">CIP-0057 Plutus blueprint</Link>, describing your on-chain contract and its binary interface.
122
119
123
120
## Creating locking transaction
124
121
125
122
### Preparing the frontend [!toc]
126
123
127
-
In this section, we will prepare the frontend for our smart contract. We will create a simple UI that allows users to lock and unlock assets on the Cardano blockchain.
124
+
Prepare the frontend to allow users to lock and unlock assets.
128
125
129
-
Firstly, we need to install the `cbor` package:
126
+
Install `cbor`:
130
127
131
128
```tsx
132
129
$npminstallcbor
133
130
```
134
131
135
-
Then, we create a folder, `data` and copy the `plutus.json` file into it.
132
+
Create a `data`folder and copy `plutus.json` into it.
136
133
137
-
Next, open `pages/index.tsx` and import the following packages:
Here, we are using the `plutus.json`file to create the script. We are also using the `resolvePlutusScriptAddress`function to resolve the script address.
165
+
Use `plutus.json` to create the script and `resolvePlutusScriptAddress` to resolve the address.
169
166
170
-
If you look at it closely, we are encoding with cbor the compiled code of the validator. This is because the validator is encoded in a flat format, which is not the format expected by the cardano-cli and `cardano serialization library`.
167
+
We encode the compiled validator code with cbor because the validator uses a flat format, unlike the format expected by cardano-cli and the serialization library.
171
168
172
169
### Locking assets [!toc]
173
170
174
-
We create the transactions to lock assets on the Cardano blockchain:
Here, we are creating a new transaction to lock assets on the Cardano blockchain. We are using the `resolvePaymentKeyHash` function to resolve the payment key hash of the wallet. We are also using the `sendLovelace` function to send lovelace to the script address.
197
-
198
-
As the contracts requires the owner's address in the datum field, we are creating a new datum with the owner's address. We are then using the `build` function to build the transaction, the `signTx` function to sign the transaction, and the `submitTx` function to submit the transaction to the Cardano blockchain.
193
+
This transaction locks assets. `resolvePaymentKeyHash` resolves the wallet's key hash. `sendLovelace` sends lovelace to the script address.
199
194
195
+
The contract requires the owner's address in the datum. We build, sign, and submit the transaction.
200
196
201
197
## Unlocking assets
202
198
203
-
Next, we create the transactions to unlock assets.
199
+
Create the transaction to unlock assets.
204
200
205
-
First, we create a useful function to retrieve the UTXO of the locked assets:
201
+
Retrieve the UTXO of the locked assets:
206
202
207
203
```tsx
208
204
asyncfunction _getAssetUtxo({ scriptAddress, asset, datum }) {
@@ -218,7 +214,7 @@ async function _getAssetUtxo({ scriptAddress, asset, datum }) {
218
214
}
219
215
```
220
216
221
-
And here are the codes to create the transactions to unlock assets:
Here, we are creating a new transaction to unlock assets on the Cardano blockchain. We are using the `resolvePlutusScriptAddress`function to resolve the script address. We are also using the `resolvePaymentKeyHash`function to resolve the payment key hash of the wallet.
253
+
This transaction unlocks assets. `resolvePlutusScriptAddress`resolves the script address. `resolvePaymentKeyHash`resolves the wallet's key hash.
258
254
259
-
As the contracts requires the owner's address in the datum field, we are creating a new datum with the owner's address. We are then using the `_getAssetUtxo`function to retrieve the UTXO of the locked assets. We are then using the `redeemValue`function to redeem the locked assets, the `sendValue`function to send the assets to the owner's address, and the `setRequiredSigners`function to set the required signers.
255
+
`_getAssetUtxo`retrieves the locked asset UTXO. `redeemValue`redeems the assets. `sendValue`sends assets to the owner. `setRequiredSigners`sets the required signers.
260
256
261
-
As the validator requires `Hello, World!` as the redeemer message, we are creating a new redeemer with the message `Hello, World!`. We are then using the `build` function to build the transaction, the `signTx` function to sign the transaction, and the `submitTx` function to submit the transaction to the Cardano blockchain.
257
+
The validator requires "Hello, World!" as the redeemer message. We build, sign, and submit the transaction.
262
258
263
-
You can check the full code on <Linkhref="https://github.com/MeshJS/aiken-next-ts-template/blob/main/pages/index.tsx">GitHub</Link>.
259
+
Check the full code on <Linkhref="https://github.com/MeshJS/aiken-next-ts-template/blob/main/pages/index.tsx">GitHub</Link>.
0 commit comments