Skip to content

Commit c898e14

Browse files
committed
Revise the section smart contracts
1 parent 7f32a1e commit c898e14

File tree

10 files changed

+82
-62
lines changed

10 files changed

+82
-62
lines changed

docs/for_developers/smart_contracts/getting_started.md

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ tags:
99
Before we dive deep into the structure of a contract and the SDK's semantics, we require some background information about the semantics enforced by ParallelChain Mainnet. The full capabilities of ParallelChain Mainnet continue to grow with active developers
1010
and a growing community. Let's see how smart contracts are called and processed by a node in the form of transactions.
1111

12-
First, the ParallelChain Client (`pchain_client`) will submit a transaction when making a smart contract call to the validating node using a serialization crate [pchain-types](https://github.com/parallelchain-io/pchain-types-rust). The node's RPC service and mempool will process the list of transactions and check for validity and correctness.
12+
First, the ParallelChain Client (`pchain_client`) will submit a transaction when making a smart contract call to the validating node using a serialization crate [pchain-types](https://crates.io/crates/pchain-types). The node's RPC service and mempool will process the list of transactions and check for validity and correctness.
1313

14-
Transactions that fail the validation check may not be included in a block. The transactions are then packed in a block and sent to the execution engine in the node to execute the transactions.
14+
[Transactions](../../fundamentals/transactions.md#how-transaction-works) that fail the validation check may not be included in a [block](../../fundamentals/blocks.md#how-block-works). The transactions are then packed in a block and sent to the execution engine in the node to execute the transactions.
1515

16-
The executor will call `wasmer` (Web Assembly Engine) which provides an isolated context to perform the execution. This enables the smart contract code to read the current state of the blockchain and interact with it. However, the execution results are temporarily stored and subject to further checks before the block may be committed or rolled back on error.
16+
The executor will call [wasmer](https://crates.io/crates/wasmer) (Web Assembly Engine) which provides an isolated context to perform the execution. This enables the smart contract code to read the current state of the blockchain and interact with it. However, the execution results are temporarily stored and subject to further checks before the block may be committed or rolled back on error.
1717

18-
`wasmer` also computes the gas fees through a metering module, which is responsible for limiting the execution to the amount of gas paid. Each transaction returns a result in the form of receipts along with logs. The receipts (with or without log), transactions and block itself will contain its hash (or Merkle proof) and will be all included in the same block.
18+
`wasmer` also computes the [gas](../../fundamentals/gas.md) fees through a metering module, which is responsible for limiting the execution to the amount of gas paid. Each transaction returns a result in the form of receipts along with logs. The receipts (with or without log), transactions and block itself will contain its hash (or Merkle proof) and will be all included in the same block.
1919

2020

2121
## Smart Contract Development Kit
@@ -36,20 +36,52 @@ For more information on Rust's crate system, see [Rust Book Chapter 7: Packages
3636
Please specify the dependency in `Cargo.toml` for using SDK by fetching from [crates.io](https://crates.io/) or repository in Github.
3737

3838
=== "Crates.io"
39-
```rust
39+
```toml
40+
[lib]
41+
crate-type = ["cdylib"]
42+
4043
[dependencies]
4144
pchain-sdk = "0.4"
4245
```
4346

4447
=== "Github"
45-
```rust
48+
```toml
49+
[lib]
50+
crate-type = ["cdylib"]
51+
4652
[dependencies]
4753
pchain-sdk = { git = "https://github.com/parallelchain-io/pchain-sdk" }
4854
```
4955

50-
## Building the Contract with pchain_compile
56+
### Implement Smart Contract
57+
58+
Let's start with a sample smart contract for illustration in this guide. The smart contract code is written in the file `lib.rs`.
59+
60+
```rust
61+
use pchain_sdk::{call, contract, contract_methods};
62+
63+
#[contract]
64+
struct MyContract { }
65+
66+
#[contract_methods]
67+
impl MyContract {
68+
69+
#[call]
70+
fn hello_from(name :String) -> u32 {
71+
pchain_sdk::log(
72+
"topic: Hello From".as_bytes(),
73+
format!("Hello, Contract. From: {}", name).as_bytes()
74+
);
75+
name.len() as u32
76+
}
77+
}
78+
```
79+
80+
The details about the macros (e.g. `contract`, `contract_methods`, `call`) used in the code can be found in later sections [Contract Methods](./advanced/contract_methods.md) and [Contract Storage](./advanced/contract_storage.md). Let's skip knowing them first, and continue reading about building and deploying the contract in following sections.
5181

52-
**pchain_compile** is a CLI build tool for smart contract developers to build their source code to WASM binaries for deployment on
82+
## Building The Contract With `pchain_compile`
83+
84+
[pchain_compile](https://github.com/parallelchain-io/pchain-compile) is a CLI build tool for smart contract developers to build their source code to WASM binaries for deployment on
5385
ParallelChain Mainnet.
5486

5587
The WASM binary generated from a generic cargo build process has the following disadvantages:
@@ -63,10 +95,10 @@ The WASM binary generated from a generic cargo build process has the following d
6395

6496
- Executing the build process in a docker environment with pre-defined toolchain versioning.
6597

66-
- Optimizing and compressing the size of the WASM binary with the help of `wasm-snip` and `wasm-opt` packages.
98+
- Optimizing and compressing the size of the WASM binary with the help of [wasm-snip](https://crates.io/crates/wasm-snip) and [wasm-opt](https://crates.io/crates/wasm-opt) packages.
6799

68100

69-
### Downloading **pchain_compile**
101+
### Download **pchain_compile**
70102

71103
`pchain_compile` supports Linux, macOS and Windows. Depending on the operating system, they can be downloaded from Assets of ParallelChain Lab's Git Hub [release page](https://github.com/parallelchain-io/pchain-compile/releases).
72104

@@ -80,7 +112,7 @@ cargo install pchain_compile
80112

81113

82114

83-
### Building WASM Binary
115+
### Build WASM Binary
84116

85117
In order to build a `WASM` binary of the smart contract, run the following command:
86118

@@ -103,7 +135,7 @@ A `.wasm` file will be generated in the current directory.
103135

104136
## Deploying a Smart Contract
105137

106-
You can deploy the contract using the pchain_client command line tool. You should make sure to obtain your latest account nonce before submitting the transaction.
138+
You can deploy the contract using the pchain_client command line tool. You should make sure to [know your latest account nonce](../../for_users/pchain_client_cli/query.md#check-account-related-information) before submitting the transaction.
107139

108140
=== "Linux / macOS"
109141
```bash
@@ -130,7 +162,7 @@ You can deploy the contract using the pchain_client command line tool. You shoul
130162

131163
You can follow the instruction in [Create Transaction](../../for_users/pchain_client_cli/transaction.md) about submiting a transaction through `pchain-client`.
132164

133-
## Checking Contract in State
165+
## Checking Contract In State
134166

135167
To verify that the smart contract is deployed correctly, you can run the command `query` with the flag `contract`. It queries the state of the blockchain and saves the wasm code as `code.wasm` in the current directory.
136168
If you want to store the contract file in your preferred location, you need to provide the flag `destination` to specify the path with your preferred file extension:
@@ -146,20 +178,15 @@ If you want to store the contract file in your preferred location, you need to p
146178

147179
## Calling Contract
148180

149-
Suppose you have already deployed a contract that contains a `call` method as follows:
181+
Suppose you have already deployed a contract that contains a `call` method named `hello_from` as mentioned in previous section [Implement Smart Contract](#implement-smart-contract):
150182

183+
>
151184
```rust
152-
#[call]
153-
fn hello_from(name: String) -> u32 {
154-
pchain_sdk::log(
155-
"topic: Hello From".as_bytes(),
156-
format!("Hello, Contract. From: {}", name).as_bytes()
157-
);
158-
name.len() as u32
159-
}
185+
#[call]
186+
fn hello_from(name :String) -> u32
160187
```
161188

162-
To invoke this contract in the blockchain by using `pchain_client`, you can prepare a JSON file that contains a list of arguments and matches with the `call` method. For example, this `call` method takes a string argument. Then, the content of the JSON file should be as follows:
189+
To invoke this contract by using `pchain_client`, you can prepare a JSON file that contains a list of arguments and matches with the `call` method. For example, this `call` method takes a string argument. Then, the content of the JSON file should be as follows:
163190

164191
```json
165192
{
@@ -169,9 +196,9 @@ To invoke this contract in the blockchain by using `pchain_client`, you can prep
169196
}
170197
```
171198

172-
This JSON file will be used in the subcommand `call` as mentioned in the subsequent sections on this page.
199+
This JSON file will be used in creating a transaction with [Call Command](../../fundamentals/transactions.md#account-commands).
173200

174-
### Submitting Transaction with Call Command
201+
### Submit Transaction With Call Command
175202

176203
To call a smart contract, submit a transaction with your account nonce and contract address using the `pchain_client`.
177204

@@ -204,6 +231,10 @@ Here is the command to call a contract:
204231
--amount <AMOUNT_TO_CONTRACT>
205232
```
206233

234+
The command argument `method` is the name of the method with macro `#[call]` in the code. Take the contract in [Implement Smart Contract](#implement-smart-contract) as example, "hello_from" should be set as the value of the command argument `method`.
235+
236+
The command argument `arguments` is the JSON file that contains the method arguments to the method being called in the contract. The way to create this JSON file is described in previous section [Calling Contract](#calling-contract).
237+
207238
The gas limit required for the transaction depends on the complexity of the smart contract. For safety reasons, you can always set a higher gas limit. You can also test contract calls on testnet to reassure.
208239

209240
You can follow the instruction in [Create Transaction](../../for_users/pchain_client_cli/transaction.md) about submiting a transaction through `pchain-client`.
@@ -221,7 +252,7 @@ To query the resulting receipt of the transaction,
221252

222253
The commands stored in `transaction` and command receipts in `receipt` are following the same order. That means you can always find the corresponding transaction from a command receipt.
223254

224-
### Parsing Call Result
255+
### Parse Call Result
225256

226257
To parse the response from the contract method, represented in the field named `return value` , which is in `CallResult` format, you can use the `parse call-result` command in ParallelChain Client.
227258

docs/for_developers/smart_contracts/installing_sdk.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ tags:
77

88
## Installing Rust
99

10-
To install the required toolkits, the standard method is by `rustup`, which maintains dependencies and is a version manager for `cargo` and `rustc` (the rust compiler). Detailed installation instructions can be found on the [official website](https://www.rust-lang.org/tools/install)
10+
To install the required toolkits, the standard method is by `rustup`, which maintains dependencies and is a version manager for `cargo` and `rustc` (the rust compiler). Detailed installation instructions can be found on the [official website](https://www.rust-lang.org/tools/install).
1111

1212
## Setting up a Development Environment
1313

1414
A recommended Integrated Development Environment (IDE) is [VSCode](https://code.visualstudio.com/), which allows the addition of the plugin [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer) to type-check all of your code on save. This feature allows you to locate errors in the code easily, and the error messages displayed in the IDE are the same as when executing the `cargo build` command. However, any other editor that is comfortable for you can be used. However, any other editor that is comfortable for you can be used.
1515

1616
## Installing Wasm Toolchain (Optional)
1717

18-
ParallelChain Mainnet also provides a smart contract compilation tool called `pchain-compile` to compile your Rust smart contract into WASM bytecode. While not necessary, the Installation of Wasm Toolchain can help check if your code is compilable before using the time-consuming process in the use of `pchain-compile`.
18+
ParallelChain Mainnet also provides a smart contract compilation tool called [pchain-compile](getting_started.md#building-the-contract-with-pchain_compile) to compile your Rust smart contract into WASM bytecode. While not necessary, the Installation of Wasm Toolchain can help check if your code is compilable before using the time-consuming process in the use of `pchain-compile`.
1919

2020
The `wasm32-unknown-unknown` toolchain is required for smart contracts to compile as WASM. To add the wasm toolchain in Rust, type the command below:
2121
```bash

0 commit comments

Comments
 (0)