Skip to content

Commit

Permalink
Add assets in question to swap events (#391)
Browse files Browse the repository at this point in the history
* Add asset in question to swap events

* Cargo fmt

* Adjust tests to use new events

* Satisfy Clippy
  • Loading branch information
sea212 authored Nov 29, 2021
1 parent b3f65bd commit 27eb7c4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 17 deletions.
4 changes: 4 additions & 0 deletions misc/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,13 @@
"weights": "Option<BTreeMap<Asset, u128>>"
},
"PoolAssetEvent": {
"asset": "Asset",
"bound": "Balance",
"cpep": "CommonPoolEventParams<AccountId>",
"transferred": "Balance"
},
"PoolAssetsEvent": {
"assets": "Vec<Asset>",
"bounds": "Vec<Balance>",
"cpep": "CommonPoolEventParams<AccountId>",
"transferred": "Vec<Balance>"
Expand Down Expand Up @@ -235,6 +237,8 @@
"asset_amount_in": "Balance",
"asset_amount_out": "Balance",
"asset_bound": "Balance",
"asset_in": "Asset",
"asset_out": "Asset",
"cpep": "CommonPoolEventParams<AccountId>",
"max_price": "Balance"
},
Expand Down
10 changes: 7 additions & 3 deletions zrml/swaps/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub struct CommonPoolEventParams<AI> {
parity_scale_codec::Decode,
parity_scale_codec::Encode,
)]
pub struct PoolAssetsEvent<AI, B> {
pub struct PoolAssetsEvent<AI, AS, B> {
pub assets: Vec<AS>,
pub bounds: Vec<B>,
pub cpep: CommonPoolEventParams<AI>,
pub transferred: Vec<B>,
Expand All @@ -45,7 +46,8 @@ pub struct PoolAssetsEvent<AI, B> {
parity_scale_codec::Decode,
parity_scale_codec::Encode,
)]
pub struct PoolAssetEvent<AI, B> {
pub struct PoolAssetEvent<AI, AS, B> {
pub asset: AS,
pub bound: B,
pub cpep: CommonPoolEventParams<AI>,
pub transferred: B,
Expand All @@ -62,10 +64,12 @@ pub struct PoolAssetEvent<AI, B> {
parity_scale_codec::Decode,
parity_scale_codec::Encode,
)]
pub struct SwapEvent<AI, B> {
pub struct SwapEvent<AI, AS, B> {
pub asset_amount_in: B,
pub asset_amount_out: B,
pub asset_bound: B,
pub asset_in: AS,
pub asset_out: AS,
pub cpep: CommonPoolEventParams<AI>,
pub max_price: B,
}
66 changes: 56 additions & 10 deletions zrml/swaps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ mod pallet {
}

Self::deposit_event(Event::<T>::PoolExitSubsidy(PoolAssetEvent {
asset: base_asset,
bound: upper_bound,
cpep: CommonPoolEventParams { pool_id, who },
transferred,
Expand Down Expand Up @@ -408,6 +409,7 @@ mod pallet {
});

Self::deposit_event(Event::<T>::PoolJoinSubsidy(PoolAssetEvent {
asset: base_asset,
bound: amount,
cpep: CommonPoolEventParams { pool_id, who },
transferred: amount,
Expand Down Expand Up @@ -731,35 +733,79 @@ mod pallet {
Pool<BalanceOf<T>, T::MarketId>,
),
/// Someone has exited a pool. \[PoolAssetsEvent\]
PoolExit(PoolAssetsEvent<<T as frame_system::Config>::AccountId, BalanceOf<T>>),
PoolExit(
PoolAssetsEvent<
<T as frame_system::Config>::AccountId,
Asset<T::MarketId>,
BalanceOf<T>,
>,
),
/// Someone has (partially) exited a pool by removing subsidy. \[PoolAssetEvent, amount\]
PoolExitSubsidy(PoolAssetEvent<<T as frame_system::Config>::AccountId, BalanceOf<T>>),
PoolExitSubsidy(
PoolAssetEvent<
<T as frame_system::Config>::AccountId,
Asset<T::MarketId>,
BalanceOf<T>,
>,
),
/// Exits a pool given an exact amount of an asset. \[PoolAssetEvent\]
PoolExitWithExactAssetAmount(
PoolAssetEvent<<T as frame_system::Config>::AccountId, BalanceOf<T>>,
PoolAssetEvent<
<T as frame_system::Config>::AccountId,
Asset<T::MarketId>,
BalanceOf<T>,
>,
),
/// Exits a pool given an exact pool's amount. \[PoolAssetEvent\]
PoolExitWithExactPoolAmount(
PoolAssetEvent<<T as frame_system::Config>::AccountId, BalanceOf<T>>,
PoolAssetEvent<
<T as frame_system::Config>::AccountId,
Asset<T::MarketId>,
BalanceOf<T>,
>,
),
/// Someone has joined a pool. \[PoolAssetsEvent\]
PoolJoin(PoolAssetsEvent<<T as frame_system::Config>::AccountId, BalanceOf<T>>),
PoolJoin(
PoolAssetsEvent<
<T as frame_system::Config>::AccountId,
Asset<T::MarketId>,
BalanceOf<T>,
>,
),
/// Someone has joined a pool by providing subsidy. \[PoolAssetEvent, amount\]
PoolJoinSubsidy(PoolAssetEvent<<T as frame_system::Config>::AccountId, BalanceOf<T>>),
PoolJoinSubsidy(
PoolAssetEvent<
<T as frame_system::Config>::AccountId,
Asset<T::MarketId>,
BalanceOf<T>,
>,
),
/// Joins a pool given an exact amount of an asset. \[PoolAssetEvent\]
PoolJoinWithExactAssetAmount(
PoolAssetEvent<<T as frame_system::Config>::AccountId, BalanceOf<T>>,
PoolAssetEvent<
<T as frame_system::Config>::AccountId,
Asset<T::MarketId>,
BalanceOf<T>,
>,
),
/// Joins a pool given an exact pool's amount. \[PoolAssetEvent\]
PoolJoinWithExactPoolAmount(
PoolAssetEvent<<T as frame_system::Config>::AccountId, BalanceOf<T>>,
PoolAssetEvent<
<T as frame_system::Config>::AccountId,
Asset<T::MarketId>,
BalanceOf<T>,
>,
),
/// Total subsidy collected for a pool. \[pool_id, subsidy\]
SubsidyCollected(PoolId, BalanceOf<T>),
/// An exact amount of an asset is entering the pool. \[SwapEvent\]
SwapExactAmountIn(SwapEvent<<T as frame_system::Config>::AccountId, BalanceOf<T>>),
SwapExactAmountIn(
SwapEvent<<T as frame_system::Config>::AccountId, Asset<T::MarketId>, BalanceOf<T>>,
),
/// An exact amount of an asset is leaving the pool. \[SwapEvent\]
SwapExactAmountOut(SwapEvent<<T as frame_system::Config>::AccountId, BalanceOf<T>>),
SwapExactAmountOut(
SwapEvent<<T as frame_system::Config>::AccountId, Asset<T::MarketId>, BalanceOf<T>>,
),
}

#[pallet::pallet]
Expand Down
10 changes: 10 additions & 0 deletions zrml/swaps/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ fn pool_exit_decreases_correct_pool_parameters() {
assert_ok!(Swaps::pool_exit(alice_signed(), 0, _1, vec!(_1, _1, _1, _1),));

assert!(event_exists(crate::Event::PoolExit(PoolAssetsEvent {
assets: vec![ASSET_A, ASSET_B, ASSET_C, ASSET_D],
bounds: vec!(_1, _1, _1, _1),
cpep: CommonPoolEventParams { pool_id: 0, who: 0 },
transferred: vec!(_1 + 1, _1 + 1, _1 + 1, _1 + 1),
Expand Down Expand Up @@ -624,6 +625,7 @@ fn pool_exit_with_exact_pool_amount_exchanges_correct_values() {
_4
));
assert!(event_exists(crate::Event::PoolExitWithExactPoolAmount(PoolAssetEvent {
asset: ASSET_A,
bound: _4,
cpep: CommonPoolEventParams { pool_id: 0, who: 0 },
transferred: _5 - 335,
Expand All @@ -648,6 +650,7 @@ fn pool_exit_with_exact_asset_amount_exchanges_correct_values() {
_1
));
assert!(event_exists(crate::Event::PoolExitWithExactAssetAmount(PoolAssetEvent {
asset: ASSET_A,
bound: _1,
cpep: CommonPoolEventParams { pool_id: 0, who: 0 },
transferred: asset_after_join - 1000,
Expand All @@ -670,6 +673,7 @@ fn pool_join_increases_correct_pool_parameters() {

assert_ok!(Swaps::pool_join(alice_signed(), 0, _5, vec!(_25, _25, _25, _25),));
assert!(event_exists(crate::Event::PoolJoin(PoolAssetsEvent {
assets: vec![ASSET_A, ASSET_B, ASSET_C, ASSET_D],
bounds: vec!(_25, _25, _25, _25),
cpep: CommonPoolEventParams { pool_id: 0, who: 0 },
transferred: vec!(_5, _5, _5, _5),
Expand Down Expand Up @@ -718,6 +722,7 @@ fn pool_join_with_exact_asset_amount_exchanges_correct_values() {
0
));
assert!(event_exists(crate::Event::PoolJoinWithExactAssetAmount(PoolAssetEvent {
asset: ASSET_A,
bound: 0,
cpep: CommonPoolEventParams { pool_id: 0, who: 0 },
transferred: alice_sent
Expand Down Expand Up @@ -748,6 +753,7 @@ fn pool_join_with_exact_pool_amount_exchanges_correct_values() {
));
let asset_amount = alice_initial - Currencies::free_balance(ASSET_A, &ALICE);
assert!(event_exists(crate::Event::PoolJoinWithExactPoolAmount(PoolAssetEvent {
asset: ASSET_A,
bound: _5,
cpep: CommonPoolEventParams { pool_id: 0, who: 0 },
transferred: asset_amount,
Expand Down Expand Up @@ -862,6 +868,8 @@ fn swap_exact_amount_in_exchanges_correct_values_with_cpmm() {
asset_amount_in: _1,
asset_amount_out: 9900990100,
asset_bound: _1 / 2,
asset_in: ASSET_A,
asset_out: ASSET_B,
cpep: CommonPoolEventParams { pool_id: 0, who: 0 },
max_price: _2,
})));
Expand Down Expand Up @@ -929,6 +937,8 @@ fn swap_exact_amount_out_exchanges_correct_values_with_cpmm() {
asset_amount_in: 10101010100,
asset_amount_out: _1,
asset_bound: _2,
asset_in: ASSET_A,
asset_out: ASSET_B,
cpep: CommonPoolEventParams { pool_id: 0, who: 0 },
max_price: _3,
})));
Expand Down
13 changes: 9 additions & 4 deletions zrml/swaps/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) fn pool_exit_with_exact_amount<F1, F2, F3, F4, T>(
where
F1: FnMut(BalanceOf<T>, BalanceOf<T>) -> Result<BalanceOf<T>, DispatchError>,
F2: FnMut(BalanceOf<T>) -> DispatchResult,
F3: FnMut(PoolAssetEvent<T::AccountId, BalanceOf<T>>),
F3: FnMut(PoolAssetEvent<T::AccountId, Asset<T::MarketId>, BalanceOf<T>>),
F4: FnMut(BalanceOf<T>, BalanceOf<T>) -> Result<BalanceOf<T>, DispatchError>,
T: Config,
{
Expand All @@ -45,6 +45,7 @@ where
T::Shares::transfer(p.asset, &pool_account, &p.who, asset_amount)?;

(p.event)(PoolAssetEvent {
asset: p.asset,
bound: p.bound,
cpep: CommonPoolEventParams { pool_id: p.pool_id, who: p.who },
transferred: asset_amount,
Expand All @@ -59,7 +60,7 @@ pub(crate) fn pool_join_with_exact_amount<F1, F2, F3, T>(
) -> DispatchResult
where
F1: FnMut(BalanceOf<T>, BalanceOf<T>) -> Result<BalanceOf<T>, DispatchError>,
F2: FnMut(PoolAssetEvent<T::AccountId, BalanceOf<T>>),
F2: FnMut(PoolAssetEvent<T::AccountId, Asset<T::MarketId>, BalanceOf<T>>),
F3: FnMut(BalanceOf<T>, BalanceOf<T>) -> Result<BalanceOf<T>, DispatchError>,
T: Config,
{
Expand All @@ -79,6 +80,7 @@ where
T::Shares::transfer(p.asset, &p.who, &pool_account_id, asset_amount)?;

(p.event)(PoolAssetEvent {
asset: p.asset,
bound: p.bound,
cpep: CommonPoolEventParams { pool_id: p.pool_id, who: p.who },
transferred: asset_amount,
Expand All @@ -90,7 +92,7 @@ where
// Common code for `pool_join` and `pool_exit` methods.
pub(crate) fn pool<F1, F2, F3, T>(mut p: PoolParams<'_, F1, F2, F3, T>) -> DispatchResult
where
F1: FnMut(PoolAssetsEvent<T::AccountId, BalanceOf<T>>),
F1: FnMut(PoolAssetsEvent<T::AccountId, Asset<T::MarketId>, BalanceOf<T>>),
F2: FnMut(BalanceOf<T>, BalanceOf<T>, Asset<T::MarketId>) -> DispatchResult,
F3: FnMut(Asset<T::MarketId>) -> DispatchResult,
T: Config,
Expand All @@ -117,6 +119,7 @@ where
(p.transfer_pool)(pool_shares_id)?;

(p.event)(PoolAssetsEvent {
assets: p.pool.assets.clone(),
bounds: p.asset_bounds,
cpep: CommonPoolEventParams { pool_id: p.pool_id, who: p.who },
transferred,
Expand All @@ -131,7 +134,7 @@ pub(crate) fn swap_exact_amount<F1, F2, T>(
) -> DispatchResult
where
F1: FnMut() -> Result<[BalanceOf<T>; 2], DispatchError>,
F2: FnMut(SwapEvent<T::AccountId, BalanceOf<T>>),
F2: FnMut(SwapEvent<T::AccountId, Asset<T::MarketId>, BalanceOf<T>>),
T: crate::Config,
{
Pallet::<T>::check_if_pool_is_active(p.pool)?;
Expand Down Expand Up @@ -202,6 +205,8 @@ where
asset_amount_in,
asset_amount_out,
asset_bound: p.asset_bound,
asset_in: p.asset_in,
asset_out: p.asset_out,
cpep: CommonPoolEventParams { pool_id: p.pool_id, who: p.who },
max_price: p.max_price,
});
Expand Down

0 comments on commit 27eb7c4

Please sign in to comment.