-
Notifications
You must be signed in to change notification settings - Fork 36
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
Add hello world tutorial #1255
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7520e0f
Add hello world tutorial
02932b5
Address review comments
94aa460
Fix build
3183cf0
Addressed review comments
a44b641
Fix lint
3c1b548
Address last comment
4d23a2c
Merge branch 'main' into helloWorldTutorial
it09 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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. | ||
|
||
dragosgd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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(), | ||
dragosgd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
|
||
#. 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. |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.