-
Notifications
You must be signed in to change notification settings - Fork 450
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 example which uses ext_transfer + ext_terminate #554
Merged
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
987cbe5
[chores] Fix typo: invokation ➜ invocation
cmichi 9fdb2c9
[chores] Fix typo: timstamp ➜ timestamp
cmichi cdfba99
[chores] Fix typo: ininitialized ➜ initialized
cmichi d2878b1
[env] Implement terminate_contract in off-chain env
cmichi 0295a97
[examples] Add lock-until
cmichi 09b7211
[env] Implement proper off-chain testing for ext_terminate
cmichi bfbc9ee
Apply suggestions from code review
a16c8b1
[env] Fix Environment type
cmichi fea6a03
[env] Derive Balance/AccountId from Environment
cmichi e641b8f
[env] Fix types in macro
cmichi d15bd4d
[examples] Remove lock-until
cmichi 9778815
[examples] Add contract-terminate
cmichi a4e81e2
[examples] Add contract-transfer
cmichi ed21715
[examples] Make clippy happy
cmichi efa6e90
[examples] Fix example name
cmichi 1778b48
[examples] Remove Default impls
cmichi 96b1f91
[examples] Move macro to contract-terminate/test_utils
cmichi 688faab
[env] Migrate macro to fn which gets Environment type param
cmichi d228696
[env] Add explanatory dev comment
cmichi 6c0cc5f
[examples] Allow clippy::new_without_default
cmichi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Ignore build artifacts from the local tests sub-crate. | ||
/target/ | ||
|
||
# Ignore backup files creates by cargo fmt. | ||
**/*.rs.bk | ||
|
||
# Remove Cargo.lock when creating an executable, leave it for libraries | ||
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
Cargo.lock |
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,36 @@ | ||
[package] | ||
name = "contract_terminate" | ||
version = "3.0.0-rc1" | ||
authors = ["Parity Technologies <admin@parity.io>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
ink_primitives = { version = "3.0.0-rc1", path = "../../crates/primitives", default-features = false } | ||
ink_metadata = { version = "3.0.0-rc1", path = "../../crates/metadata", default-features = false, features = ["derive"], optional = true } | ||
ink_env = { version = "3.0.0-rc1", path = "../../crates/env", default-features = false } | ||
ink_storage = { version = "3.0.0-rc1", path = "../../crates/storage", default-features = false } | ||
ink_lang = { version = "3.0.0-rc1", path = "../../crates/lang", default-features = false } | ||
|
||
scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] } | ||
scale-info = { version = "0.4", default-features = false, features = ["derive"], optional = true } | ||
|
||
|
||
[lib] | ||
name = "contract_terminate" | ||
path = "lib.rs" | ||
crate-type = ["cdylib"] | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"ink_primitives/std", | ||
"ink_metadata", | ||
"ink_metadata/std", | ||
"ink_env/std", | ||
"ink_storage/std", | ||
"ink_lang/std", | ||
"scale/std", | ||
"scale-info", | ||
"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,96 @@ | ||
// Copyright 2018-2020 Parity Technologies (UK) Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! A smart contract which demonstrates behavior of the `self.env().terminate()` | ||
//! function. It terminates itself once `terminate_me()` is called. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use ink_lang as ink; | ||
|
||
#[ink::contract] | ||
pub mod just_terminates { | ||
/// No storage is needed for this simple contract. | ||
#[ink(storage)] | ||
pub struct JustTerminate {} | ||
|
||
impl JustTerminate { | ||
/// Creates a new instance of this contract. | ||
#[ink(constructor)] | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
/// Terminates with the caller as beneficiary. | ||
#[ink(message)] | ||
pub fn terminate_me(&mut self) { | ||
self.env().terminate_contract(self.env().caller()); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use ink_env::{ | ||
call, | ||
test, | ||
}; | ||
use ink_lang as ink; | ||
|
||
#[ink::test] | ||
fn terminating_works() { | ||
// given | ||
let accounts = default_accounts(); | ||
let contract_id = ink_env::test::get_current_contract_account_id::< | ||
ink_env::DefaultEnvironment, | ||
>() | ||
.expect("Cannot get contract id"); | ||
set_sender(accounts.alice); | ||
set_balance(contract_id, 100); | ||
let mut contract = JustTerminate::new(); | ||
|
||
// when | ||
let should_terminate = move || contract.terminate_me(); | ||
|
||
// then | ||
ink_env::assert_contract_termination!(should_terminate, accounts.alice, 100); | ||
cmichi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
fn default_accounts( | ||
) -> ink_env::test::DefaultAccounts<ink_env::DefaultEnvironment> { | ||
ink_env::test::default_accounts::<ink_env::DefaultEnvironment>() | ||
.expect("Off-chain environment should have been initialized already") | ||
} | ||
|
||
fn set_sender(sender: AccountId) { | ||
let callee = ink_env::account_id::<ink_env::DefaultEnvironment>() | ||
.unwrap_or([0x0; 32].into()); | ||
test::push_execution_context::<Environment>( | ||
sender, | ||
callee, | ||
1000000, | ||
1000000, | ||
test::CallData::new(call::Selector::new([0x00; 4])), // dummy | ||
); | ||
} | ||
|
||
fn set_balance(account_id: AccountId, balance: Balance) { | ||
ink_env::test::set_account_balance::<ink_env::DefaultEnvironment>( | ||
account_id, balance, | ||
) | ||
.expect("Cannot set account balance"); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The correct version of this would be:
We then run into this though:
Wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the explanation!