-
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 6 commits
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
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 |
---|---|---|
|
@@ -363,3 +363,58 @@ where | |
Ok(callee) | ||
}) | ||
} | ||
|
||
/// The result of a successful contract termination. | ||
#[derive(scale::Encode, scale::Decode)] | ||
pub struct ContractTerminationResult<AccountId, Balance> | ||
where | ||
AccountId: scale::Codec, | ||
Balance: scale::Codec, | ||
{ | ||
/// The beneficiary account who received the remaining value in the contract. | ||
pub beneficiary: AccountId, | ||
/// The value which was transferred to the `beneficiary`. | ||
pub transferred: Balance, | ||
cmichi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Tests if a contract terminates successfully after `self.env().terminate()` | ||
/// has been called. | ||
/// | ||
/// # Usage | ||
/// | ||
/// The macro is used like this: | ||
/// | ||
/// ```no_compile | ||
/// let should_terminate = move || your_contract.fn_which_should_terminate(); | ||
/// ink_env::assert_contract_termination!( | ||
/// should_terminate, | ||
/// expected_beneficiary, | ||
/// expected_value_transferred_to_beneficiary | ||
/// ); | ||
/// ``` | ||
/// | ||
/// See `examples/lock-until` for a complete usage example. | ||
#[cfg(feature = "std")] | ||
#[macro_export] | ||
macro_rules! assert_contract_termination { | ||
( | ||
$should_terminate:tt, | ||
$beneficiary:expr, | ||
$balance:expr | ||
) => { | ||
use std::panic; | ||
|
||
let value_any = panic::catch_unwind($should_terminate) | ||
.expect_err("contract did not terminate"); | ||
let encoded_input: &Vec<u8> = | ||
value_any.downcast_ref::<Vec<u8>>().expect("must work"); | ||
let info: ink_env::test::ContractTerminationResult<AccountId, Balance> = | ||
scale::Decode::decode(&mut &encoded_input[..]).expect("must work"); | ||
|
||
let expected_beneficiary: AccountId = $beneficiary; | ||
assert_eq!(info.beneficiary, expected_beneficiary); | ||
|
||
let expected_balance: Balance = $balance; | ||
assert_eq!(info.transferred, expected_balance); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very clever and creative solution! might be acceptable. however, needs further macro hygiene improvements since this is going to be exposed to the users. |
||
}; | ||
cmichi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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
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
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
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 = "lock_until" | ||
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 = "lock_until" | ||
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 = [] |
Oops, something went wrong.
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.
What the on-chain impl would do here is set a tombstone with a code hash and remove storage. AFAIU both is not easily achievable with our current off-chain env, hence I left it out here for the moment.
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.
Could you maybe provide a small dev. note comment explaining exactly this?