From 7520e0f7d93fbb04aea156377df942660836f6c4 Mon Sep 17 00:00:00 2001 From: Dragos-Gabriel Danciulescu Date: Fri, 24 Jan 2025 16:15:05 +0200 Subject: [PATCH 1/6] Add hello world tutorial --- .../tutorials/hello-world/hello-world.rst | 99 +++++++++++++++++++ source/mainnet/tutorials/index.rst | 1 + 2 files changed, 100 insertions(+) create mode 100644 source/mainnet/tutorials/hello-world/hello-world.rst diff --git a/source/mainnet/tutorials/hello-world/hello-world.rst b/source/mainnet/tutorials/hello-world/hello-world.rst new file mode 100644 index 0000000000..35fb7d4551 --- /dev/null +++ b/source/mainnet/tutorials/hello-world/hello-world.rst @@ -0,0 +1,99 @@ +.. _hello-world: + +==================================== +Creating your first Concordium dApp +==================================== + +This tutorial guides you through creating and deploying your first smart contract on the Concordium blockchain. 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 `. + +Creating your first smart contract +================================== + +Create a new project +-------------------- + +#. Create a new project directory: + + .. code-block:: console + + $ mkdir hello_world && cd hello_world + +#. Initialize a new smart contract project: + + .. code-block:: console + + $ cargo concordium init + +#. Select the **default** template when prompted. + +Writing the contract +-------------------- + +Modify ``src/lib.rs`` to create a simple message-storing contract: + +#. Define the state structure: + + .. code-block:: rust + + /// The state of the smart contract. + #[derive(Serialize, SchemaType)] + pub struct State { + message: String, + } + +#. Create the initialization function: + + .. code-block:: rust + + #[init(contract = "hello_world")] + fn init(_ctx: &InitContext, _state_builder: &mut StateBuilder) -> InitResult { + Ok(State { + message: "Hello World!".to_string(), + }) + } + +#. Add the message update function: + + .. code-block:: rust + + #[receive( + contract = "hello_world", + name = "set_message", + parameter = "String", + error = "Error", + mutable + )] + fn set_message(ctx: &ReceiveContext, host: &mut Host) -> Result<(), Error> { + let new_message: String = ctx.parameter_cursor().get()?; + host.state_mut().message = new_message; + Ok(()) + } + +#. Add the view function: + + .. code-block:: rust + + #[receive(contract = "hello_world", name = "view", return_value = "String")] + fn view(_ctx: &ReceiveContext, host: &Host) -> ReceiveResult { + Ok(host.state().message.clone()) + } + +#. Build the project: + + .. code-block:: console + + $ cargo concordium build --out hello_world.wasm.v1 + +Deploying your contract +======================= + +#. :ref:`Set up a wallet ` +#. Request testnet CCD using the **Request** option +#. Deploy your contract using the `Smart Contract Deploy Tool `_ or read the :ref:`deploy a smart contract module guide ` and use the :ref:`concoridum client ` to deploy +#. Use the ``view`` function to verify your deployed contract + +Congratulations! You've created and deployed your first smart contract on the Concordium blockchain. diff --git a/source/mainnet/tutorials/index.rst b/source/mainnet/tutorials/index.rst index 41cceee8d1..0da46daa2f 100644 --- a/source/mainnet/tutorials/index.rst +++ b/source/mainnet/tutorials/index.rst @@ -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> From 02932b5cbf0567b58159241f2d1edc2b83efa863 Mon Sep 17 00:00:00 2001 From: Dragos-Gabriel Danciulescu Date: Thu, 30 Jan 2025 18:25:19 +0200 Subject: [PATCH 2/6] Address review comments --- .../tutorials/hello-world/hello-world.rst | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/source/mainnet/tutorials/hello-world/hello-world.rst b/source/mainnet/tutorials/hello-world/hello-world.rst index 35fb7d4551..aaede09bae 100644 --- a/source/mainnet/tutorials/hello-world/hello-world.rst +++ b/source/mainnet/tutorials/hello-world/hello-world.rst @@ -1,41 +1,34 @@ +.. include:: ../../../variables.rst .. _hello-world: ==================================== Creating your first Concordium dApp ==================================== -This tutorial guides you through creating and deploying your first smart contract on the Concordium blockchain. You'll create a simple "Hello World" smart contract and deploy it on the testnet. +This tutorial guides you through creating and deploying your first smart contract 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 `. Creating your first smart contract ================================== -Create a new project --------------------- - -#. Create a new project directory: - - .. code-block:: console - - $ mkdir hello_world && cd hello_world - #. Initialize a new smart contract project: .. code-block:: console $ cargo concordium init -#. Select the **default** template when prompted. +#. Select the **default** template when prompted, then choose an appropiate project name. -Writing the contract +Write the contract -------------------- Modify ``src/lib.rs`` to create a simple message-storing contract: -#. Define the state structure: +#. Create the state structure which defines the **message** variable: .. code-block:: rust @@ -45,7 +38,7 @@ Modify ``src/lib.rs`` to create a simple message-storing contract: message: String, } -#. Create the initialization function: +#. Modify the **init** method which initialises the **message** variable: .. code-block:: rust @@ -56,7 +49,7 @@ Modify ``src/lib.rs`` to create a simple message-storing contract: }) } -#. Add the message update function: +#. 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 @@ -73,7 +66,7 @@ Modify ``src/lib.rs`` to create a simple message-storing contract: Ok(()) } -#. Add the view function: +#. Modify the **view** function by specifying the **String** return value type and the **message** variable of the **state** .. code-block:: rust @@ -82,18 +75,26 @@ Modify ``src/lib.rs`` to create a simple message-storing contract: Ok(host.state().message.clone()) } -#. Build the project: +#. Build the contract: .. code-block:: console - $ cargo concordium build --out hello_world.wasm.v1 + $ 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 ======================= -#. :ref:`Set up a wallet ` -#. Request testnet CCD using the **Request** option -#. Deploy your contract using the `Smart Contract Deploy Tool `_ or read the :ref:`deploy a smart contract module guide ` and use the :ref:`concoridum client ` to deploy -#. Use the ``view`` function to verify your deployed contract +We recommend deploying on the **testnet**, which can be done by following these steps: -Congratulations! You've created and deployed your first smart contract on the Concordium blockchain. +#. :ref:`Set up a wallet ` +#. Request **testnet CCD** using the **Request** option in the wallet +#. **Access** the `Smart Contract Deploy Tool `_ 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. From 94aa46047f74a3b12395d3e533f8b6df72c8004a Mon Sep 17 00:00:00 2001 From: Dragos-Gabriel Danciulescu Date: Thu, 30 Jan 2025 18:32:14 +0200 Subject: [PATCH 3/6] Fix build --- source/mainnet/tutorials/hello-world/hello-world.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/source/mainnet/tutorials/hello-world/hello-world.rst b/source/mainnet/tutorials/hello-world/hello-world.rst index aaede09bae..098aa40f74 100644 --- a/source/mainnet/tutorials/hello-world/hello-world.rst +++ b/source/mainnet/tutorials/hello-world/hello-world.rst @@ -1,4 +1,3 @@ -.. include:: ../../../variables.rst .. _hello-world: ==================================== From 3183cf0e57084ca6bc7fe94fb6a51adf5a45bc93 Mon Sep 17 00:00:00 2001 From: Dragos-Gabriel Danciulescu Date: Tue, 4 Mar 2025 13:28:24 +0200 Subject: [PATCH 4/6] Addressed review comments --- source/mainnet/tutorials/hello-world/hello-world.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/mainnet/tutorials/hello-world/hello-world.rst b/source/mainnet/tutorials/hello-world/hello-world.rst index 098aa40f74..343e0df2a1 100644 --- a/source/mainnet/tutorials/hello-world/hello-world.rst +++ b/source/mainnet/tutorials/hello-world/hello-world.rst @@ -4,7 +4,7 @@ Creating your first Concordium dApp ==================================== -This tutorial guides you through creating and deploying your first smart contract Concordium. You'll create a simple **Hello World** smart contract and deploy it on the testnet. +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 ================ @@ -20,14 +20,14 @@ Creating your first smart contract $ cargo concordium init -#. Select the **default** template when prompted, then choose an appropiate project name. +#. Select the **default** template when prompted, then choose an appropiate project name. The ``init`` command has now created a template smart contract. Write the contract -------------------- Modify ``src/lib.rs`` to create a simple message-storing contract: -#. Create the state structure which defines the **message** variable: +#. Modify the existing state structure, the new state defines the **message** variable: .. code-block:: rust From a44b641c78c149e4a09d6014532373a51b4d892a Mon Sep 17 00:00:00 2001 From: Dragos-Gabriel Danciulescu Date: Tue, 4 Mar 2025 13:29:55 +0200 Subject: [PATCH 5/6] Fix lint --- source/mainnet/tutorials/hello-world/hello-world.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/mainnet/tutorials/hello-world/hello-world.rst b/source/mainnet/tutorials/hello-world/hello-world.rst index 343e0df2a1..5855dbb326 100644 --- a/source/mainnet/tutorials/hello-world/hello-world.rst +++ b/source/mainnet/tutorials/hello-world/hello-world.rst @@ -20,7 +20,7 @@ Creating your first smart contract $ 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. +#. Select the **default** template when prompted, then choose an appropiate project name. The ``init`` command has now created a template smart contract. Write the contract -------------------- From 3c1b548a0ed42892307425ab17a01f071a8f2d1a Mon Sep 17 00:00:00 2001 From: Dragos-Gabriel Danciulescu Date: Wed, 5 Mar 2025 18:44:43 +0200 Subject: [PATCH 6/6] Address last comment --- source/mainnet/tutorials/hello-world/hello-world.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/mainnet/tutorials/hello-world/hello-world.rst b/source/mainnet/tutorials/hello-world/hello-world.rst index 5855dbb326..1e564868c7 100644 --- a/source/mainnet/tutorials/hello-world/hello-world.rst +++ b/source/mainnet/tutorials/hello-world/hello-world.rst @@ -20,7 +20,7 @@ Creating your first smart contract $ 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. +#. 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 --------------------