Skip to content

Add hello world tutorial #1255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions source/mainnet/tutorials/hello-world/hello-world.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
.. _hello-world:

====================================
Creating your first Concordium dApp
====================================

This tutorial guides you through creating and deploying your first smart contract on Concordium. You'll create a simple **Hello World** smart contract and deploy it on the testnet.

Before you start
================

Ensure you have set up your development environment by following this :ref:`tutorial <setup-env>`.

Creating your first smart contract
==================================

#. Initialize a new smart contract project:

.. code-block:: console

$ cargo concordium init

#. Select the **default** template when prompted, then choose an appropiate project name. The ``init`` command has now created a template smart contract and a corresponding local repository.

Write the contract
--------------------

Modify ``src/lib.rs`` to create a simple message-storing contract:

#. Modify the existing state structure, the new state defines the **message** variable:

.. code-block:: rust

/// The state of the smart contract.
#[derive(Serialize, SchemaType)]
pub struct State {
message: String,
}

#. Modify the **init** method which initialises the **message** variable:

.. code-block:: rust

#[init(contract = "hello_world")]
fn init(_ctx: &InitContext, _state_builder: &mut StateBuilder) -> InitResult<State> {
Ok(State {
message: "Hello World!".to_string(),
})
}

#. Modify the **receive** method by specifying **String** as the parameter type, changing it's name to be more suggestive and assigning the **input** string to the **state**:

.. code-block:: rust

#[receive(
contract = "hello_world",
name = "set_message",
parameter = "String",
error = "Error",
mutable
)]
fn set_message(ctx: &ReceiveContext, host: &mut Host<State>) -> Result<(), Error> {
let new_message: String = ctx.parameter_cursor().get()?;
host.state_mut().message = new_message;
Ok(())
}

#. Modify the **view** function by specifying the **String** return value type and the **message** variable of the **state**

.. code-block:: rust

#[receive(contract = "hello_world", name = "view", return_value = "String")]
fn view(_ctx: &ReceiveContext, host: &Host<State>) -> ReceiveResult<String> {
Ok(host.state().message.clone())
}

#. Build the contract:

.. code-block:: console

$ cargo concordium build

After the command runs successfully, a file called **module.wasm.v1** will be generated in the **concordium-out** folder

Deploying your contract
=======================

We recommend deploying on the **testnet**, which can be done by following these steps:

#. :ref:`Set up a wallet <setup-wallets-lp>`
#. Request **testnet CCD** using the **Request** option in the wallet
#. **Access** the `Smart Contract Deploy Tool <https://sctools.mainnet.concordium.software/>`_ in order to deploy the generated module
#. **Click** on the button to connect to the **Browser Wallet**
#. **Upload** the module generated in the previous section in the Step 1 section of the dApp then **click** the button to **deploy**
#. Choose **Derive from step 1** in **Step 2**, then **click** the button to **initialise** the smart contract, a **Smart Contract Index** will be shown
#. **Input** the index in the next step, then select the **Derive From Smart Contract Index** option
#. Choose **view** as the **Entry Point Name**, then **click** the **Read Smart Contract** button, **"Hello World"** will be displayed

Congratulations! You've now created and deployed your first smart contract on the Concordium blockchain.
1 change: 1 addition & 0 deletions source/mainnet/tutorials/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Additional Resources
:caption: Smart Contract Tutorials

setup-env
Hello World <./hello-world/hello-world>
Counter <./counter/counter-contract>
PiggyBank <./piggy-bank/index>
wCCD <./wCCD/index>
Expand Down