Skip to content

Commit

Permalink
Merge pull request #321 from sora-xor/merge-develop-to-master
Browse files Browse the repository at this point in the history
Merge develop to master
  • Loading branch information
vovac12 authored Mar 9, 2023
2 parents faabc97 + dc71949 commit c66fdd6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ cargo build --release --features private-net
access running network via polkadot.js/apps (select Development -> Local Node, e.g. [link](https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944#/explorer))
#### On macOS
macOS is shipped with the BSD version of `getopt`. If the script behaves incorrectly and does not parse arguments,
make sure you have installed and set as active the GNU versions of `awk` and `getopt`.
make sure you have installed and set as active the GNU versions of `awk` (or `gawk`) and `getopt`.


### Run benchmarks to generate weights
Expand Down
91 changes: 62 additions & 29 deletions pallets/liquidity-proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1784,53 +1784,84 @@ pub mod pallet {
receivers,
} = swap_batch_info;

let filter = LiquiditySourceFilter::with_mode(
dex_id,
filter_mode.clone(),
selected_source_types.clone(),
);

if Self::is_forbidden_filter(
&input_asset_id,
&asset_id,
&selected_source_types,
&filter_mode,
) {
fail!(Error::<T>::ForbiddenFilter);
}

// extrinsic fails if there are duplicate output asset ids
if !unique_asset_ids.insert(asset_id.clone()) {
Err(Error::<T>::AggregationError)?
}

if receivers.len() == 0 {
Err(Error::<T>::InvalidReceiversInfo)?
}

let out_amount = receivers.iter().map(|recv| recv.target_amount).sum();
Self::inner_exchange(
dex_id,
&who,
&who,
&input_asset_id,
&asset_id,
SwapAmount::WithDesiredOutput {
desired_amount_out: out_amount,
max_amount_in: max_input_amount,
},
filter.clone(),
)?;
let (executed_input_amount, remainder_per_receiver): (Balance, Balance) =
if asset_id != input_asset_id {
let filter = LiquiditySourceFilter::with_mode(
dex_id,
filter_mode.clone(),
selected_source_types.clone(),
);

if Self::is_forbidden_filter(
&input_asset_id,
&asset_id,
&selected_source_types,
&filter_mode,
) {
fail!(Error::<T>::ForbiddenFilter);
}

let (
SwapOutcome {
amount: executed_input_amount,
..
},
_,
) = Self::inner_exchange(
dex_id,
&who,
&who,
&input_asset_id,
&asset_id,
SwapAmount::WithDesiredOutput {
desired_amount_out: out_amount,
max_amount_in: max_input_amount,
},
filter.clone(),
)?;

let caller_output_asset_balance =
assets::Pallet::<T>::total_balance(&asset_id, &who)?;
let remainder_per_receiver: Balance =
if caller_output_asset_balance < out_amount {
let remainder =
out_amount.saturating_sub(caller_output_asset_balance);
remainder / (receivers.len() as u128)
+ remainder % (receivers.len() as u128)
} else {
0
};
(executed_input_amount, remainder_per_receiver)
} else {
(out_amount, 0)
};

max_input_amount = max_input_amount
.checked_sub(out_amount)
.checked_sub(executed_input_amount)
.ok_or(Error::<T>::SlippageNotTolerated)?;

let mut unique_batch_receivers: BTreeSet<T::AccountId> = BTreeSet::new();
fallible_iterator::convert(receivers.into_iter().map(|val| Ok(val))).for_each(
|receiver| {
// extrinsic fails if there are duplicate account ids in the same output asset id
if !unique_batch_receivers.insert(receiver.account_id.clone()) {
Err(Error::<T>::AggregationError)?
}
assets::Pallet::<T>::transfer_from(
&asset_id,
&who,
&receiver.account_id,
receiver.target_amount,
receiver.target_amount - remainder_per_receiver,
)
},
)
Expand Down Expand Up @@ -1944,5 +1975,7 @@ pub mod pallet {
UnableToDisableLiquiditySource,
/// Liquidity source is already disabled
LiquiditySourceAlreadyDisabled,
// Information about swap batch receivers is invalid
InvalidReceiversInfo,
}
}
4 changes: 2 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("sora-substrate"),
impl_name: create_runtime_str!("sora-substrate"),
authoring_version: 1,
spec_version: 48,
spec_version: 49,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 48,
transaction_version: 49,
state_version: 0,
};

Expand Down

0 comments on commit c66fdd6

Please sign in to comment.