Skip to content

Commit

Permalink
Add an example of ink_env::set_code_hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
willser committed Mar 27, 2022
1 parent da4fcb4 commit 0623907
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/upgradeable-contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ more information on proxy patterns.
* Executes any call that does not match a selector of itself with the code of another contract.
* The other contract does not need to be deployed on-chain.
* State is stored in the storage of the originally called contract.


## [`set-code`](https://github.com/paritytech/ink/tree/master/examples/upgradeable-contracts/set-code-hash)

* Update contract code by `set_code_hash`.
38 changes: 38 additions & 0 deletions examples/upgradeable-contracts/set-code-hash/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "incrementer"
version = "1.0.0"
edition = "2021"
authors = ["Will"]

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

[dependencies]
ink_primitives = { version = "3.0.0", path = "../../../crates/primitives", default-features = false }
ink_prelude = { version = "3.0.0", path = "../../../crates/prelude", default-features = false }
ink_metadata = { version = "3.0.0", path = "../../../crates/metadata", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.0.0", path = "../../../crates/env", default-features = false }
ink_storage = { version = "3.0.0", path = "../../../crates/storage", default-features = false }
ink_lang = { version = "3.0.0", path = "../../../crates/lang", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

[lib]
name = "incrementer"
path = "lib.rs"
crate-type = ["cdylib"]

[features]
default = ["std"]
std = [
"ink_primitives/std",
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_lang/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []


45 changes: 45 additions & 0 deletions examples/upgradeable-contracts/set-code-hash/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#![cfg_attr(not(feature = "std"), no_std)]

use ink_lang as ink;

#[ink::contract]
pub mod incrementer {

#[ink(storage)]
pub struct Incrementer {
count: u32,
}

impl Incrementer {
/// Creates a new counter smart contract initialized with the given base value.
#[ink(constructor)]
pub fn new(init_value: u32) -> Self {
Self { count: init_value }
}

/// Creates a new counter smart contract initialized to `0`.
#[ink(constructor)]
pub fn default() -> Self {
Self::new(0)
}

/// Splice `base` and `target` together.
#[ink(message)]
pub fn inc(&mut self) {
self.count += 1;
ink_env::debug_println!("count is {},use old code", self.count);
}

#[ink(message)]
pub fn get(&self) -> u32 {
self.count
}

/// Set new code to this contract.
#[ink(message)]
pub fn set_code(&mut self, code_hash: [u8; 32]) {
ink_env::set_code_hash(&code_hash).expect("Fail to set code.");
ink_env::debug_println!("set code_hash success");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "new-incrementer"
version = "0.1.0"
edition = "2021"
authors = ["Will"]

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

[dependencies]
ink_primitives = { version = "3.0.0", path = "../../../../crates/primitives", default-features = false }
ink_metadata = { version = "3.0.0", path = "../../../../crates/metadata", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.0.0", path = "../../../../crates/env", default-features = false, features = ["ink-debug"] }
ink_storage = { version = "3.0.0", path = "../../../../crates/storage", default-features = false }
ink_lang = { version = "3.0.0", path = "../../../../crates/lang", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

[lib]
name = "new_incrementer"
path = "lib.rs"
crate-type = ["cdylib"]

[features]
default = ["std"]
std = [
"ink_primitives/std",
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_lang/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![cfg_attr(not(feature = "std"), no_std)]

use ink_lang as ink;

#[ink::contract]
pub mod incrementer {

#[ink(storage)]
pub struct Incrementer {
count: u32,
}

impl Incrementer {
/// Creates a new counter smart contract initialized with the given base value.
#[ink(constructor)]
pub fn new(init_value: u32) -> Self {
Self { count: init_value }
}

/// Creates a new counter smart contract initialized to `0`.
#[ink(constructor)]
pub fn default() -> Self {
Self::new(0)
}

/// Splice `base` and `target` together.
#[ink(message)]
pub fn inc(&mut self) {
// Different step size with old contract.
self.count += 4;
ink_env::debug_println!("count is {},use new code", self.count);
}

#[ink(message)]
pub fn get(&self) -> u32 {
self.count
}

/// Set new code to this contract.
#[ink(message)]
pub fn set_code(&mut self, code_hash: [u8; 32]) {
ink_env::set_code_hash(&code_hash).expect("Fail to set code.");
ink_env::debug_println!("set code_hash success");
}
}
}

0 comments on commit 0623907

Please sign in to comment.