Skip to content

Commit

Permalink
Merge pull request #905 from gregdhill/refactor/dex
Browse files Browse the repository at this point in the history
Refactor/dex
  • Loading branch information
sander2 authored Feb 2, 2023
2 parents 6b32be2 + 54e4b44 commit 7e4757a
Show file tree
Hide file tree
Showing 30 changed files with 820 additions and 1,854 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 2 additions & 14 deletions crates/dex-general/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,13 @@ frame-system = { git = "https://github.com/paritytech/substrate", default-featur
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.31" }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
pallet-balances = { git = 'https://github.com/paritytech/substrate', default-features = false, branch = 'polkadot-v0.9.31' }

# Polkadot
xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.31", default-features = false }
xcm-executor = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.31", default-features = false }

# Cumulus
cumulus-primitives-core = { git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.31" }

# Orml dependencies
orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc39cfddefb10ef0de23655e2c3dcdab66a19404", default-features = false }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc39cfddefb10ef0de23655e2c3dcdab66a19404", default-features = false }

[dev-dependencies]
pallet-balances = { git = 'https://github.com/paritytech/substrate', default-features = false, branch = 'polkadot-v0.9.31' }
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.31" }
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.31" }

Expand All @@ -47,19 +40,14 @@ std = [
"sp-core/std",
"frame-support/std",
"frame-system/std",
"frame-benchmarking/std",
"sp-runtime/std",
"sp-std/std",
"sp-arithmetic/std",
"pallet-balances/std",

"xcm/std",
"xcm-executor/std",
"cumulus-primitives-core/std",

"orml-traits/std",
"orml-tokens/std",
]

runtime-benchmarks = [
"frame-benchmarking",
]
Expand Down
3 changes: 0 additions & 3 deletions crates/dex-general/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ sp_api::decl_runtime_apis! {
AssetBalance: Codec,
AssetId: Codec
{

fn get_balance(asset_id: AssetId, owner: AccountId) -> AssetBalance;

fn get_pair_by_asset_id(
asset_0: AssetId,
asset_1: AssetId
Expand Down
17 changes: 0 additions & 17 deletions crates/dex-general/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ pub use dex_general_rpc_runtime_api::DexGeneralApi as DexGeneralRuntimeApi;

#[rpc(client, server)]
pub trait DexGeneralApi<BlockHash, AccountId, AssetId> {
#[method(name = "dexGeneral_getBalance")]
fn get_balance(&self, asset_id: AssetId, account: AccountId, at: Option<BlockHash>) -> RpcResult<NumberOrHex>;

#[method(name = "dexGeneral_getPairByAssetId")]
fn get_pair_by_asset_id(
&self,
Expand Down Expand Up @@ -144,20 +141,6 @@ where
.map_err(runtime_error_into_rpc_err)
}

fn get_balance(
&self,
asset_id: AssetId,
account: AccountId,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));

api.get_balance(&at, asset_id, account)
.map(|asset_balance| asset_balance.into())
.map_err(runtime_error_into_rpc_err)
}

fn get_pair_by_asset_id(
&self,
asset_0: AssetId,
Expand Down
65 changes: 26 additions & 39 deletions crates/dex-general/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,13 @@ use crate::Pallet as DexGeneral;
use frame_benchmarking::{benchmarks, whitelisted_caller};
use frame_support::assert_ok;
use frame_system::RawOrigin;
use sp_runtime::SaturatedConversion;

const UNIT: u128 = 1_000_000_000_000;

const ASSET_0: AssetId = AssetId {
chain_id: 2001,
asset_type: 2,
asset_index: 515,
};

const ASSET_1: AssetId = AssetId {
chain_id: 2001,
asset_type: 2,
asset_index: 516,
};

const ASSET_2: AssetId = AssetId {
chain_id: 2001,
asset_type: 2,
asset_index: 518,
};
const ASSET_0: u32 = 0;
const ASSET_1: u32 = 1;
const ASSET_2: u32 = 2;

pub fn lookup_of_account<T: Config>(
who: T::AccountId,
Expand All @@ -48,7 +35,7 @@ fn run_to_block<T: Config>(n: u32) {

benchmarks! {

where_clause { where T::AssetId: From<AssetId>}
where_clause { where T::AssetId: From<u32> }

set_fee_receiver{
let caller: T::AccountId = whitelisted_caller();
Expand All @@ -61,21 +48,21 @@ benchmarks! {
create_pair {
let caller: T::AccountId = whitelisted_caller();

assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT));

} : _(RawOrigin::Root, ASSET_0.into(), ASSET_1.into())

bootstrap_create {
let reward:Vec<T::AssetId> = vec![ASSET_0.into()];
let reward: Vec<T::AssetId> = vec![ASSET_0.into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)];
}: _(RawOrigin::Root, ASSET_0.into(), ASSET_1.into(), 1000, 1000, 1000_000_000, 1000_000_000, 100u128.saturated_into(), reward, reward_amounts)

bootstrap_contribute{
let caller: T::AccountId = whitelisted_caller();

assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT));

let reward:Vec<T::AssetId> = vec![ASSET_0.into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)];
Expand All @@ -97,8 +84,8 @@ benchmarks! {
bootstrap_claim{
let caller: T::AccountId = whitelisted_caller();

assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT));

let reward:Vec<T::AssetId> = vec![ASSET_0.into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)];
Expand Down Expand Up @@ -138,8 +125,8 @@ benchmarks! {
bootstrap_end{
let caller: T::AccountId = whitelisted_caller();

assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT));

let reward:Vec<T::AssetId> = vec![ASSET_0.into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)];
Expand Down Expand Up @@ -193,8 +180,8 @@ benchmarks! {
bootstrap_refund{
let caller: T::AccountId = whitelisted_caller();

assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT));

let reward:Vec<T::AssetId> = vec![ASSET_0.into()];
let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)];
Expand Down Expand Up @@ -225,8 +212,8 @@ benchmarks! {

add_liquidity{
let caller: T::AccountId = whitelisted_caller();
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT));

assert_ok!(DexGeneral::<T>::create_pair((RawOrigin::Root).into(), ASSET_0.into(), ASSET_1.into()));

Expand All @@ -236,8 +223,8 @@ benchmarks! {

remove_liquidity{
let caller: T::AccountId = whitelisted_caller();
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT));

assert_ok!(DexGeneral::<T>::create_pair((RawOrigin::Root).into(), ASSET_0.into(), ASSET_1.into()));

Expand All @@ -257,9 +244,9 @@ benchmarks! {

swap_exact_assets_for_assets{
let caller: T::AccountId = whitelisted_caller();
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_2.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_2.into(), &caller, 1000 * UNIT));

assert_ok!(DexGeneral::<T>::create_pair((RawOrigin::Root).into(), ASSET_0.into(), ASSET_1.into()));
assert_ok!(DexGeneral::<T>::create_pair((RawOrigin::Root).into(), ASSET_1.into(), ASSET_2.into()));
Expand Down Expand Up @@ -290,9 +277,9 @@ benchmarks! {

swap_assets_for_exact_assets{
let caller: T::AccountId = whitelisted_caller();
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiAssetsHandler::deposit(ASSET_2.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT));
assert_ok!(<T as Config>::MultiCurrency::deposit(ASSET_2.into(), &caller, 1000 * UNIT));

assert_ok!(DexGeneral::<T>::create_pair((RawOrigin::Root).into(), ASSET_1.into(), ASSET_2.into()));
assert_ok!(DexGeneral::<T>::create_pair((RawOrigin::Root).into(), ASSET_0.into(), ASSET_1.into()));
Expand Down
Loading

0 comments on commit 7e4757a

Please sign in to comment.