-
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
Conversation
|
||
let contract_id = self.account_id::<T>().expect("could not decode account id"); | ||
self.accounts.remove_account::<T>(contract_id); | ||
|
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?
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 comment
The 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.
Co-authored-by: Hero Bird <robin.freyler@gmail.com>
Codecov Report
@@ Coverage Diff @@
## master #554 +/- ##
==========================================
+ Coverage 67.94% 68.28% +0.33%
==========================================
Files 155 155
Lines 6811 6811
==========================================
+ Hits 4628 4651 +23
+ Misses 2183 2160 -23
Continue to review full report at Codecov.
|
pub fn terminate_me(&mut self) { | ||
self.env().terminate_contract(self.env().caller()); | ||
} |
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:
pub fn terminate_me(&mut self) { | |
self.env().terminate_contract(self.env().caller()); | |
} | |
pub fn terminate_me(&mut self) -> ! { | |
self.env().terminate_contract(self.env().caller()) | |
} |
We then run into this though:
error[E0277]: the trait bound `!: scale_info::TypeInfo` is not satisfied
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!
|
||
let contract_id = self.account_id::<T>().expect("could not decode account id"); | ||
self.accounts.remove_account::<T>(contract_id); | ||
|
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?
pub fn terminate_me(&mut self) { | ||
self.env().terminate_contract(self.env().caller()); | ||
} |
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!
examples/contract-terminate/lib.rs
Outdated
impl Default for JustTerminate { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} |
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.
Why? And why not flag it as #[ink(constructor)]
?
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.
Clippy made me do it!
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.
And what did clippy say exactly? 🙃
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.
It said https://www.youtube.com/watch?v=ZXsQAXx_ao0 🙃.
Okay, actually it was this one.
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.
LGTM
Thanks for the new example!
/// | ||
/// See `examples/contract-terminate` for a complete usage example. | ||
#[cfg(feature = "std")] | ||
pub fn assert_contract_termination<T, F>( |
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.
So much better as a function compared to a macro!
Closes #355 and #356.