10 minute quickstart with Kadena
Welcome to the world of Kadena, a powerful blockchain platform that combines scalability with simplicity. In this guide, we'll walk you through the essential steps to kickstart your journey with Kadena. Whether you're a seasoned blockchain developer or a newcomer to the space, you'll find the process intuitive and efficient.
Here we run Devnet and deploy a "Hello World" smart-contract on Kadena blockchain in 10 minutes
Start fat-container kadena/devnet
-
Create docker volume
docker volume create kadena_devnet
docker volume create kadena_devnet
-
start kadena-devnet fat-container
docker run -it -p 8080:8080 -v kadena_devnet:/data --name devnet kadena/devnet# restart withdocker start devnet
docker run -it -p 8080:8080 -v kadena_devnet:/data --name devnet kadena/devnet# restart withdocker start devnet
Monitor the blockchain
In the fat-container we expose an explorer that connects to the devnet
Here you can see the blocks that are mined, and the transactions that are executed
In Kadena a block is mined every 30 seconds. However, to optimize development workflow, the devnet mines a block in 5 seconds.
Chainweaver wallet
- Use Chainweaver
- Download and install from https://github.com/kadena-io/chainweaver/releases
- Or, use the web version: https://chainweaver.kadena.network
- Launch Chainweaver and create your mnemonic key
Add devnet to Chainweaver
- Click "Settings" tab in the bottom left
- Select "Network"
- Fill in the network name: "Devnet"
- Open the network you created "> Devnet"
- Add a node: "127.0.0.1:8080", the red dot on the right, should become green now.
Create keys to sign transactions
- Go to "Keys" on the left and click "+ Generate" on the top-right. This is your first key-pair.
- To show the balance of this account, click "Add k: Account".
- Go back to the "Accounts" tab on the left. Notice that the "Balance (KDA)" says "Does not exist".
In Kadena, keys and accounts do not represent the same thing. An account needs to be created before it can be used.
Fund your account
Before we can create an account, you need to have KDA to pay for the gas-fees (transaction fee).
We can gain KDA by funding it from a pre-installed "devnet" account called "sender00".
In this process, we’ll submit a transaction that creates an account based on the
"keys" and "predicate" that you supply. The combination of keys
+ predicate
makes a keyset
, which is used to guard
your account.
Prerequisite: you need to have node installed on your machine:
NodeJS
(personal recommendation to install with n
)
-
Clone the repository. It has scripts to make your life easier
git clone https://github.com/kadena-community/getting-started.git# orgit clone git@github.com:kadena-community/getting-started.git# and install dependencies (you need to have nodejs preinstalled)cd getting-startednpm install
git clone https://github.com/kadena-community/getting-started.git# orgit clone git@github.com:kadena-community/getting-started.git# and install dependencies (you need to have nodejs preinstalled)cd getting-startednpm install
-
Send money from "sender00" to your account. Copy your account name from the "Accounts" tab and fill it in the command
npm run start -- fund --keys "<your-key>" --predicate "keys-all"
npm run start -- fund --keys "<your-key>" --predicate "keys-all"
-
Open the Block Explorer http://localhost:8080/explorer/ to monitor the transaction
-
In Chainweaver, click "Refresh" to update the account balances
Deploy a contract
We're going to deploy the hello world
smart-contract using your newly created
account. This is what the smart contract looks like:
(namespace 'free)(module hello-world GOVERNANCE (defcap GOVERNANCE () true) (defun say-hello(name:string) (format "Hello, {}! ~ from: ${publicKey}" [name]) ))
(namespace 'free)(module hello-world GOVERNANCE (defcap GOVERNANCE () true) (defun say-hello(name:string) (format "Hello, {}! ~ from: ${publicKey}" [name]) ))
We've created the commands necessary to deploy this smart contract. You can just run the following command. Make sure to have Chainweaver open, as it's required to sign with your private key.
Important: the web version of Chainweaver does not have an automated signing flow. Use the Chainweaver Web command. Follow the instructions in the terminal.
Once you run the command you'll see a modal open in Chainweaver. This modal shows a few things:
- Transaction metadata like who pays for the transaction, etc.
- The "code" that's executed when the transaction
- The "data" that's available for the functions that are executed (in this case nothing)
- The "signers" needed to sign for the transaction
In this example we have an "unscoped signer" as for deploying a smart contract, there are no "capabilities" that scope what your signature can be used for (read more about this in our Step-By-Step Guide to Writing Smart Contracts)
npm run start -- deploy --keys "<your-key>" --predicate "keys-all" # chainweaver web: follow the instructions in the terminalnpm run start -- deploy --keys "<your-key>" --predicate "keys-all" --sign-manually
npm run start -- deploy --keys "<your-key>" --predicate "keys-all" # chainweaver web: follow the instructions in the terminalnpm run start -- deploy --keys "<your-key>" --predicate "keys-all" --sign-manually
In the terminal you'll see a "requestkey". Copy this and search for it to the block explorer. You'll see a transaction with a "Result" that looks like:
Loaded module free.hello-world, hash PMQzeVszU0Zo9WJzjGmzndylVKb5tSjAukS7J0EMpLQ
Now you can interact with the smart contract
Interacting with a smart contract
- Open Chainweaver
- Navigate to the "Contracts" on the left
- Open the "Module Explorer"-tab on the right
- Search in "Deployed Contracts" for the
hello-world
contract - Click the "View" button to show details of the smart contract
If everything went correctly, you should be able to see the smart contract and its details.
Now on the left side you can write a command. You can write any command and execute it on the blockchain. Each command requires at least someone to pay the transaction fee, often called "gas".
You can also write a command that does not require transaction fee as it doesn't write, but only read from the database of the blockchain.
This is a read command that you can execute that uses the contract that you just deployed. Write it in the left side of your Chainweaver inside "Contracts" page.
(free.hello-world.say-hello "Albert")
(free.hello-world.say-hello "Albert")
- Click the "deploy" button on the top right.
- Click next twice
- You'll get an error "A 'Gas Payer' has not been selected for this transaction. Are you sure this is correct?", but you can ignore that since you're executing a read-only command.
- Scroll to the bottom. Here you'll see the "Raw Response"
"Hello, Albert!"
Writing to the blockchain
Actually, you've already written to the blockchain. When you deployed your smart contract, you executed a write. But that's not a write from within the smart contract, but a "native" one.
To write on the blockchain via a smart contract, we need to make a few adjustments to our smart contract.
We've created an updated smart contract for you that you can use for that.
Check it out here.
Run the following command to redeploy the hello-world
smart contract:
npm run start -- deploy \ --keys "your-key" \ --predicate keys-all \ --file ./src/pact/hello-world.pact # chainweaver web: follow the instructions in the terminalnpm run start -- deploy \ --keys "your-key" \ --predicate keys-all \ --file ./src/pact/hello-world.pact \ --sign-manually
npm run start -- deploy \ --keys "your-key" \ --predicate keys-all \ --file ./src/pact/hello-world.pact # chainweaver web: follow the instructions in the terminalnpm run start -- deploy \ --keys "your-key" \ --predicate keys-all \ --file ./src/pact/hello-world.pact \ --sign-manually
You'll walk through the same process as before in Chainweaver.
Yes you read that correctly. You can redeploy and update a smart contract!
This is one of the many cool features of Pact and Kadena, however, you cannot change the schema (yet)
We've created a schema that'll show the structure of what you post on the
blockchain. This is the "value"-side of a "key-value-pair". Each value is
assigned to a key that you pass when you execute a (write <table> <key> { })
.
Anything between {}
must comply with the schema.
The schema is very simple, and looks like this:
(defschema hello-world-schema @doc "The schema for hello world" text:string)
(defschema hello-world-schema @doc "The schema for hello world" text:string)
We also created a function that allows you to write to the schema:
(defun write-hello(name:string) (write hello-world-table name { "text": (say-hello name) }))
(defun write-hello(name:string) (write hello-world-table name { "text": (say-hello name) }))
Executing a write function
-
Go to the "contracts" page in Chainweaver
-
Write the following snippet
(free.hello-world.write-hello "Albert")
(free.hello-world.write-hello "Albert")
-
Click "Deploy"
-
Select Chain ID "0"
-
In "Transaction Sender" select "Account" that corresponds with the account that you funded. It should be
k:<public-key>
-
Other settings should be correctly filled in as default
-
Click "next"
-
In "Unrestricted Signing Keys" select the
public-key
of your account -
Click "next"
-
You'll see a Notice:
A 'Gas Payer' has not been selected for this transaction. Are you sure this is correct?
You can safely ignore this, as the gas-payment will be done by the unrestricted signer
-
Click "submit". Now that you have written to the table, we can read from the table
Read from the table
Execute the following function, and deploy to read the keys from the table.
Note: this function costs a lot as this is not something you'd usually do in a regular transaction.
- Paste the snippet in the editor
(map (read free.hello-world.hello-world-table) (keys free.hello-world.hello-world-table))
(map (read free.hello-world.hello-world-table) (keys free.hello-world.hello-world-table))
- Click "deploy"
- Change the Chain ID to 0 (as we only deployed the contract on Chain 0)
- Change the gas limit to
99999
- Click next to the end
- In "Raw Response" you should be able to see [{"text": "Hello, Albert!"}]
Further reading
Get started with the basics of Pact by reading the Welcome to Pact docs.
A very good and complete tutorial on learning pact, with real world scenario's, is the Real World Pact series of Thomas Honeyman