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

Add deploy script projects to templates #324

Merged
merged 23 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
21 changes: 21 additions & 0 deletions templates/default/deploy-scripts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
edition = "2021"
name = "deploy_scripts"
version = "1.0.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0"
chrono = "0.4.26"
hex = "0.4"
reqwest = {version = "0.11", features = ["json"]}
serde = "1.0"
serde_json = "1.0"
sha2 = "0.10"
thiserror = "1.0"
tokio = {version = "1.18", features = ["rt", "macros"]}
clap = { version = "4", features = ["derive", "env"]}
concordium-rust-sdk="2.4"
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
itertools = "0.11.0"

112 changes: 112 additions & 0 deletions templates/default/deploy-scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Deploy, Initialize, and Update Script Template

This project has boilerplate code to write deployment, initialization, and update scripts for Concordium smart contract protocols.

# Purpose

Automatic scripts are useful to speed up the development and testing of your protocol on the chain.
In addition, scripts help to set up identical protocols on different chains easily. E.g. you can deploy your protocol to testnet or mainnet by just specifying a corresponding node connection when running the script.
DOBEN marked this conversation as resolved.
Show resolved Hide resolved

# Setup

Option 1:

```
cargo concordium init
```

Option 2 (alternative command):

```
cargo generate --git https://github.com/Concordium/concordium-rust-smart-contracts.git
```

Any of the two commands will work and will give you several templates to choose from.

- Choose the `templates/default` and answer the questions to complete the setup process (answer `default` for the two questions if you want to run the below example).

At the end, you will have a Rust project setup with this boilerplate code included.
DOBEN marked this conversation as resolved.
Show resolved Hide resolved

# Running The Script

Build and run the script from the deploy-scripts folder using
```
cargo run
```

The following options are necessary when running the script

```
--node <CONCORDIUM_URL>
V2 API of the concordium node. [default: http://node.testnet.concordium.com:20000]
--account <CONCORDIUM_ACCOUNT>
Location path and file name of the Concordium account key file (e.g. ./myPath/4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export).
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
--modules <MODULES>
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
Location paths and names of Concordium smart contract modules. Use this flag several times \
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
if you have several smart contract modules to be deployed (e.g. --modules ./myPath/default.wasm.v1 --modules ./default2.wasm.v1).
DOBEN marked this conversation as resolved.
Show resolved Hide resolved
```

The `account` parameter should be a Concordium wallet account either exported from the
Browser wallet or the mobile wallets, or in the format emitted by the
genesis tool.

Example:
```
cargo run -- --node http://node.testnet.concordium.com:20000 --account ./myPath/4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export --modules ./myPath/default.wasm.v1 --modules ./default2.wasm.v1
```

# Functionalities

The boilerplate code has support for the following functionalities:

Read functions:
- `estimate_energy`: To estimate the energy needed to execute one of the three write functions below.
- `module_exists`: To check if a module has already been deployed on the chain.
- `get_nonce`: To get the current nonce of the provided wallet account.

Write functions:
- `deploy_wasm_module`: To deploy a new smart contract module on the chain.
- `init_contract`: To initialize a smart contract instance on the chain.
- `update_contract`: To update a smart contract instance on the chain.

Event parsing helper functions:
- `parse_deploy_module_event`: To parse the chain events after deploying a module.
- `parse_contract_init_event`: To parse the chain events after initialization of a smart contract instance.
- `parse_contract_update_event`: To parse the chain events after updating a smart contract instance.

The `main.rs` file has a section (marked with `// Write your own deployment/initialization script below. An example is given here.`) that you should replace with your custom logic. You can write your script using `deploy`, `initialize`, and contract `update` transactions.

# Running the Example

The `main.rs` file has a section (marked with `// Write your own deployment/initialization script below. An example is given here.`) that provides an example that you can run.

ATTENTION: You have to have created a smart contract with the name `default` to run the given example. This can be done by answering `default` to the two questions when creating the project via `cargo-generate`.
DOBEN marked this conversation as resolved.
Show resolved Hide resolved

Navigate into the root folder and compile the `default` smart contract with the command:
```
cargo concordium build --out ./deploy-scripts/default.wasm.v1
```

Navigate into the deploy-scripts folder and run the example with the `default` smart contract (replace your wallet account in the below command):

```
cargo run -- --node http://node.testnet.concordium.com:20000 --account ./4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export --modules ./default.wasm.v1
```

The output should be:

```
Deploying module....
Module with reference 15c936d9f60dc99c543282a8f16823d2ec5c6faae689772ae06a9b2de45a39d0 already exists on the chain.

Initializing contract....
Sent tx: 09ecaa6a66e4fe2a756dd9ad8c91f5fc2099a6dd30ebd4532cb8c5aad1bab440
Transaction finalized, tx_hash=09ecaa6a66e4fe2a756dd9ad8c91f5fc2099a6dd30ebd4532cb8c5aad1bab440 contract=(6941, 0)

Estimating energy....
Contract invoke success: estimated_energy=731

Updating contract....
Sent tx: c61b40a09e422835c70b07369bc5f4bba8292499be80cd735af21941c9798dd2
Transaction finalized, tx_hash=c61b40a09e422835c70b07369bc5f4bba8292499be80cd735af21941c9798dd2
```
Loading
Loading