-
Notifications
You must be signed in to change notification settings - Fork 300
feat(tokens): OnDeposit, OnTransfer, OnSlash hooks #815
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
Conversation
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.
need more tests
tokens/src/lib.rs
Outdated
@@ -890,6 +901,7 @@ impl<T: Config> Pallet<T> { | |||
amount: T::Balance, | |||
existence_requirement: ExistenceRequirement, | |||
) -> DispatchResult { | |||
T::OnTransfer::on_transfer(currency_id, from, to, amount)?; |
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.
move this after the early return. on the case no event is emitted, no hook should be called.
} | ||
|
||
/// Hook to run before transferring from an account to another. | ||
pub trait OnTransfer<AccountId, CurrencyId, 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.
Do you intent to allow OnTransfer to be able to cancel the transfer?
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.
Ideally I wanted all of these hooks to be able to fail, in consequence cancelling the transfer / deposit / slash. The trait bound on slash()
prevents such an implementation unfortunately, but the other two (on_deposit and on_transfer) are allowed to return a Result
so that's what they do.
Codecov Report
@@ Coverage Diff @@
## master #815 +/- ##
==========================================
+ Coverage 78.05% 78.24% +0.19%
==========================================
Files 102 102
Lines 10041 10129 +88
==========================================
+ Hits 7837 7925 +88
Misses 2204 2204
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
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
Adds configurable hooks to the Tokens pallet, for functions which emit
Event::Deposited
,Event::Transfer
,Event::Slashed
. A hook call is not added toslash_reserved_named(...)
, because that function callsslash_reserved(...)
so the hook is run there.These hooks enable tokenizing "positions" which require lazy operations to be executed each time they are modified. An example would be tokenizing one's nomination / stake. For Alice to send her nominated position (the right to claim the nominated tokens) to Bob, her nomination rewards have to first be collected. In this example, the transfer hook would collect the sender's rewards before the actual transfer occurs.