From 7250a8d9fe55ce5bfcc1240768cc9503f4b57f44 Mon Sep 17 00:00:00 2001 From: danielailie Date: Thu, 17 Aug 2023 12:05:35 +0300 Subject: [PATCH 1/3] Update crowdfunding tutorial --- docs/developers/tutorials/crowdfunding-p1.md | 22 +++++++++----------- docs/developers/tutorials/crowdfunding-p2.md | 12 +++++++---- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/docs/developers/tutorials/crowdfunding-p1.md b/docs/developers/tutorials/crowdfunding-p1.md index 4416f3bf5..199a4554e 100644 --- a/docs/developers/tutorials/crowdfunding-p1.md +++ b/docs/developers/tutorials/crowdfunding-p1.md @@ -72,13 +72,13 @@ The source code of each smart contract requires its own folder. You'll need to c ```bash mkdir -p ~/MultiversX/SmartContracts cd ~/MultiversX/SmartContracts -mxpy contract new crowdfunding --template adder +mxpy contract new crowdfunding --template empty code crowdfunding ``` You may choose any location you want for your smart contract. The above is just an example. Either way, now that you are in the folder dedicated to the smart contract, we can begin. -Straight away you get a project that works - `mxpy` created your project out of a template. These templates are contracts written and tested by MultiversX, which can be used by anybody as starting points. The `adder` template is pretty much the simplest contract you can imagine. +Straight away you get a project that works - `mxpy` created your project out of a template. These templates are contracts written and tested by MultiversX, which can be used by anybody as starting points. The last line also opens the new project in a new VS Code instance. @@ -89,18 +89,18 @@ Open `Cargo.toml` in the text editor of your choice, and add the following conte ```toml,file=Cargo.toml [package] name = "crowdfunding" -version = "0.0.1" +version = "0.0.0" authors = [ "you",] edition = "2018" [lib] -path = "src/crowdfunding_main.rs" +path = "src/crowdfunding.rs" [dependencies.multiversx-sc] -version = "0.39.0" +version = "0.43.0" [dev-dependencies.multiversx-sc-scenario] -version = "0.39.0" +version = "0.43.0" ``` @@ -108,14 +108,14 @@ Let's see what this means: - The package is unsurprisingly named `crowdfunding`, and has the version `0.0.1`. You can set any version you like, just make sure it has 3 numbers separated by dots. It's a requirement. - This package has dependencies. It will require other packages. Since you're writing a Rust smart contract for the MultiversX Network, you'll need 3 special and very helpful packages, developed by MultiversX. -- The file `src/crowdfunding_main.rs` will contain the source code of the smart contract, and that is what the `[lib]` section is declaring. You can name this file anything you want. The default Rust naming is `lib.rs`, but it can be easier organizing your code when the main code files bear the names of the contracts. +- The file `src/crowdfunding.rs` will contain the source code of the smart contract, and that is what the `[lib]` section is declaring. You can name this file anything you want. The default Rust naming is `lib.rs`, but it can be easier organizing your code when the main code files bear the names of the contracts. - The resulting binary will be named `crowdfunding` (actually, `crowdfunding.wasm`, but the compiler will add the `.wasm` part), based on the crate name. [comment]: # (mx-context-auto) ## **Step 2: write the code** -With the structure in place, you can now write the code and build it. Open `src/lib.rs` , remove the existing `Adder` code and insert the following: +With the structure in place, you can now write the code and build it. Open `src/crowdfunding.rs` , remove the existing `Empty` code and insert the following: ```rust,file=hello-world.rs #![no_std] @@ -170,7 +170,7 @@ The `init` method of the Crowdfunding smart contract is currently empty. We'll a ## **Step 3: the build** -After creating the file `src/crowdfunding_main.rs` with the content described in [the previous step](/developers/tutorials/crowdfunding-p1#step-2-the-code), you can issue the first build command. Make sure you save the file first. +After creating the file `src/crowdfunding.rs` with the content described in [the previous step](/developers/tutorials/crowdfunding-p1#step-2-the-code), you can issue the first build command. Make sure you save the file first. Now go back to the terminal, make sure the current folder is the one containing the Crowdfunding smart contract (use `pwd` for that), then issue the build command: @@ -184,9 +184,7 @@ When the command completes, a new folder will appear: `output`. This folder now The following can be safely deleted, as they are not important for this contract: -- the `snippets.sh` file - the `tests` folder -- the `interaction` folder The structure of your folder should be like this (output printed by the command `tree -L 3`): @@ -203,7 +201,7 @@ The structure of your folder should be like this (output printed by the command ├── scenarios │ └── crowdfunding.scen.json ├── src -│ └── crowdfunding_main.rs +│ └── crowdfunding.rs └── wasm ├── Cargo.toml └── src diff --git a/docs/developers/tutorials/crowdfunding-p2.md b/docs/developers/tutorials/crowdfunding-p2.md index a42b91a3e..a639f6448 100644 --- a/docs/developers/tutorials/crowdfunding-p2.md +++ b/docs/developers/tutorials/crowdfunding-p2.md @@ -137,7 +137,7 @@ It is not enough to receive the funds, the contract also needs to keep track of fn fund(&self) { let payment = self.call_value().egld_value(); let caller = self.blockchain().get_caller(); - self.deposit(&caller).update(|deposit| *deposit += payment); + self.deposit(&caller).update(|deposit| *deposit += payment.clone_value()); } ``` @@ -252,11 +252,11 @@ It doesn't make sense to fund after the deadline has passed, so fund transaction fn fund(&self) { let payment = self.call_value().egld_value(); - let current_time = self.blockchain().get_block_timstamp(); + let current_time = self.blockchain().get_block_timestamp(); require!(current_time < self.deadline().get(), "cannot fund after deadline"); let caller = self.blockchain().get_caller(); - self.deposit(&caller).update(|deposit| *deposit += payment); + self.deposit(&caller).update(|deposit| *deposit += payment.clone_value()); } ``` @@ -336,7 +336,11 @@ pub enum Status { ``` Make sure to add it outside the contract trait. +Don't forget to add the import for the derive types. This can be place on top off the file next to the other import. +```rust +multiversx_sc::derive_imports!(); +``` The `#[derive]` keyword in Rust allows you to automatically implement certain traits for your type. `TopEncode` and `TopDecode` mean that objects of this type are serializable, which means they can be interpreted from/to a string of bytes. `TypeAbi` is needed to export the type when you want to interact with the already deployed contract. This is out of scope of this tutorial though. @@ -502,7 +506,7 @@ pub trait Crowdfunding { ); let caller = self.blockchain().get_caller(); - self.deposit(&caller).update(|deposit| *deposit += payment); + self.deposit(&caller).update(|deposit| *deposit += payment.clone_value()); } #[view] From 74834e0f21e87ab8dc28d2ee210b3cd7392b3eb1 Mon Sep 17 00:00:00 2001 From: danielailie Date: Thu, 17 Aug 2023 12:20:43 +0300 Subject: [PATCH 2/3] Code review follow up --- docs/developers/tutorials/crowdfunding-p2.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/developers/tutorials/crowdfunding-p2.md b/docs/developers/tutorials/crowdfunding-p2.md index a639f6448..309098408 100644 --- a/docs/developers/tutorials/crowdfunding-p2.md +++ b/docs/developers/tutorials/crowdfunding-p2.md @@ -137,7 +137,7 @@ It is not enough to receive the funds, the contract also needs to keep track of fn fund(&self) { let payment = self.call_value().egld_value(); let caller = self.blockchain().get_caller(); - self.deposit(&caller).update(|deposit| *deposit += payment.clone_value()); + self.deposit(&caller).update(|deposit| *deposit += &*payment); } ``` @@ -256,7 +256,7 @@ It doesn't make sense to fund after the deadline has passed, so fund transaction require!(current_time < self.deadline().get(), "cannot fund after deadline"); let caller = self.blockchain().get_caller(); - self.deposit(&caller).update(|deposit| *deposit += payment.clone_value()); + self.deposit(&caller).update(|deposit| *deposit += &*payment); } ``` @@ -506,7 +506,7 @@ pub trait Crowdfunding { ); let caller = self.blockchain().get_caller(); - self.deposit(&caller).update(|deposit| *deposit += payment.clone_value()); + self.deposit(&caller).update(|deposit| *deposit += &*payment); } #[view] From a4694ccb3c718d4907e034616059c59eef183f1a Mon Sep 17 00:00:00 2001 From: danielailie Date: Thu, 17 Aug 2023 13:15:57 +0300 Subject: [PATCH 3/3] Update rust tests --- testing/extract-tutorial-code/src/extract_code.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/extract-tutorial-code/src/extract_code.rs b/testing/extract-tutorial-code/src/extract_code.rs index 2fe1e2a05..df98e4941 100644 --- a/testing/extract-tutorial-code/src/extract_code.rs +++ b/testing/extract-tutorial-code/src/extract_code.rs @@ -34,7 +34,7 @@ fn main() { match filename.as_str() { "Cargo.toml" => write_code_block("../crowdfunding-esdt/Cargo.toml", code_block), "final.rs" => { - write_code_block("../crowdfunding-esdt/src/crowdfunding_main.rs", code_block) + write_code_block("../crowdfunding-esdt/src/crowdfunding.rs", code_block) } "crowdfunding-init.scen.json" => write_code_block( "../crowdfunding-esdt/scenarios/crowdfunding-init.scen.json",