-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example of
ink_env::set_code_hash
.
- Loading branch information
Showing
5 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
examples/upgradeable-contracts/set-code-hash/new-increamenter/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
46 changes: 46 additions & 0 deletions
46
examples/upgradeable-contracts/set-code-hash/new-increamenter/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |