-
Notifications
You must be signed in to change notification settings - Fork 86
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
FI: Add swapping events #1764
Merged
Merged
FI: Add swapping events #1764
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f40c31a
adding swap events in FI
lemunozm a4c781d
update runtimes
lemunozm c34db97
fix runtimes
lemunozm 22e94d9
fix clippy
lemunozm 941c19c
return always market ratio
lemunozm 44e962e
wire to one if both currencies are the same
lemunozm 4ccdf67
Merge branch 'main' into fi/swap-events
wischli c86da3c
remove ratio from FI events
lemunozm 6d4215f
minor rename
lemunozm 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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
use cfg_traits::{ | ||
investments::{ForeignInvestment, Investment, InvestmentCollector}, | ||
swaps::{SwapState, Swaps}, | ||
swaps::{Swap, SwapInfo, SwapStatus, Swaps}, | ||
StatusNotificationHook, | ||
}; | ||
use cfg_types::investments::CollectedAmount; | ||
|
@@ -12,8 +12,8 @@ use sp_std::marker::PhantomData; | |
|
||
use crate::{ | ||
entities::{InvestmentInfo, RedemptionInfo}, | ||
pallet::{Config, Error, ForeignInvestmentInfo, ForeignRedemptionInfo, Pallet}, | ||
pool_currency_of, Action, SwapId, | ||
pallet::{Config, Error, Event, ForeignInvestmentInfo, ForeignRedemptionInfo, Pallet}, | ||
pool_currency_of, Action, SwapId, SwapOf, | ||
}; | ||
|
||
impl<T: Config> ForeignInvestment<T::AccountId> for Pallet<T> { | ||
|
@@ -57,6 +57,8 @@ impl<T: Config> ForeignInvestment<T::AccountId> for Pallet<T> { | |
} | ||
} | ||
|
||
Pallet::<T>::deposit_apply_swap_events(who, swap_id, &swap, &status); | ||
|
||
Ok::<_, DispatchError>(msg) | ||
})?; | ||
|
||
|
@@ -108,6 +110,8 @@ impl<T: Config> ForeignInvestment<T::AccountId> for Pallet<T> { | |
} | ||
} | ||
|
||
Pallet::<T>::deposit_apply_swap_events(who, swap_id, &swap, &status); | ||
|
||
if info.is_completed(who, investment_id)? { | ||
*entry = None; | ||
} | ||
|
@@ -209,23 +213,64 @@ impl<T: Config> ForeignInvestment<T::AccountId> for Pallet<T> { | |
} | ||
} | ||
|
||
impl<T: Config> Pallet<T> { | ||
fn deposit_apply_swap_events( | ||
who: &T::AccountId, | ||
swap_id: SwapId<T>, | ||
swap: &SwapOf<T>, | ||
status: &SwapStatus<T::SwapBalance, T::SwapRatio>, | ||
) { | ||
if !status.swapped.is_zero() { | ||
Pallet::<T>::deposit_event(Event::SwapFullfilled { | ||
who: who.clone(), | ||
swap_id, | ||
remaining: Swap { | ||
amount_out: status.pending, | ||
..swap.clone() | ||
}, | ||
Comment on lines
+227
to
+230
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. Note that |
||
swapped_in: status.swapped, | ||
ratio: status.ratio, | ||
}); | ||
} | ||
|
||
if !status.pending.is_zero() { | ||
Pallet::<T>::deposit_event(Event::SwapCreated { | ||
who: who.clone(), | ||
swap_id, | ||
swap: Swap { | ||
amount_out: status.pending, | ||
..swap.clone() | ||
}, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
pub struct FulfilledSwapHook<T>(PhantomData<T>); | ||
impl<T: Config> StatusNotificationHook for FulfilledSwapHook<T> { | ||
type Error = DispatchError; | ||
type Id = (T::AccountId, SwapId<T>); | ||
type Status = SwapState<T::SwapBalance, T::SwapBalance, T::CurrencyId>; | ||
type Status = SwapInfo<T::SwapBalance, T::SwapBalance, T::CurrencyId, T::SwapRatio>; | ||
|
||
fn notify_status_change( | ||
(who, (investment_id, action)): Self::Id, | ||
swap_state: Self::Status, | ||
swap_info: Self::Status, | ||
) -> DispatchResult { | ||
let pool_currency = pool_currency_of::<T>(investment_id)?; | ||
let swapped_amount_in = swap_state.swapped_in; | ||
let swapped_amount_out = swap_state.swapped_out; | ||
let pending_amount = swap_state.remaining.amount_out; | ||
let swapped_amount_in = swap_info.swapped_in; | ||
let swapped_amount_out = swap_info.swapped_out; | ||
let pending_amount = swap_info.remaining.amount_out; | ||
|
||
Pallet::<T>::deposit_event(Event::SwapFullfilled { | ||
who: who.clone(), | ||
swap_id: (investment_id, action), | ||
remaining: swap_info.remaining.clone(), | ||
swapped_in: swap_info.swapped_in, | ||
ratio: swap_info.ratio, | ||
}); | ||
|
||
match action { | ||
Action::Investment => match pool_currency == swap_state.remaining.currency_in { | ||
Action::Investment => match pool_currency == swap_info.remaining.currency_in { | ||
true => SwapDone::<T>::for_increase_investment( | ||
&who, | ||
investment_id, | ||
|
@@ -308,7 +353,9 @@ impl<T: Config> StatusNotificationHook for CollectedRedemptionHook<T> { | |
|
||
if let Some(swap) = swap { | ||
let swap_id = (investment_id, Action::Redemption); | ||
let status = T::Swaps::apply_swap(&who, swap_id, swap)?; | ||
let status = T::Swaps::apply_swap(&who, swap_id, swap.clone())?; | ||
|
||
Pallet::<T>::deposit_apply_swap_events(&who, swap_id, &swap, &status); | ||
|
||
if !status.swapped.is_zero() { | ||
SwapDone::<T>::for_redemption( | ||
|
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
Oops, something went wrong.
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.
The
OrderInfo
returned byTokenSwaps::get_order_details
contains aratio
field of typeOrderRatio<Ratio>
. Maybe that suffices such that we don't need to introduce an additional ratio field?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.
OrderRatio
is the ratio you want to configure: the way the order behaves. Theratio
here is the ratio value itself. It differs, i.e., whenOrderRatio::Market
, we do not know the ratio value.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! Makes sense to keep as is then.