Skip to content
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

Development #680

Merged
merged 4 commits into from
Aug 17, 2023
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
22 changes: 10 additions & 12 deletions docs/developers/tutorials/crowdfunding-p1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -89,33 +89,33 @@ 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"

```

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]
Expand Down Expand Up @@ -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:

Expand All @@ -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`):

Expand All @@ -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
Expand Down
12 changes: 8 additions & 4 deletions docs/developers/tutorials/crowdfunding-p2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
```

Expand Down Expand Up @@ -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);
}
```

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

#[view]
Expand Down
2 changes: 1 addition & 1 deletion testing/extract-tutorial-code/src/extract_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading