Skip to content

Commit

Permalink
add stable-asset (#1238)
Browse files Browse the repository at this point in the history
* add stable-asset

* revert orml

* move submodues

* weights

* Update runtime/mandala/src/lib.rs

Co-authored-by: Xiliang Chen <[email protected]>

* Update runtime/mandala/src/lib.rs

Co-authored-by: Xiliang Chen <[email protected]>

* clippy

* fix vec

Co-authored-by: Xiliang Chen <[email protected]>
  • Loading branch information
ukby1234 and xlc authored Aug 26, 2021
1 parent e2198c2 commit 1113b04
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "predeploy-contracts"]
path = predeploy-contracts
url = https://github.com/AcalaNetwork/predeploy-contracts.git
[submodule "stable-asset"]
path = ecosystem-modules/stable-asset
url = https://github.com/nutsfinance/stable-asset.git
16 changes: 16 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ members = [
"ecosystem-modules/chainsafe",
"ecosystem-modules/starport",
"ecosystem-modules/compound-cash",
"ecosystem-modules/stable-asset/lib/stable-asset",
]

resolver = "2"
Expand Down
1 change: 1 addition & 0 deletions ecosystem-modules/stable-asset
Submodule stable-asset added at fc2101
1 change: 1 addition & 0 deletions runtime/mandala/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ module-session-manager = { path = "../../modules/session-manager", default-featu
module-staking-pool = { path = "../../modules/staking-pool", default-features = false }
module-staking-pool-rpc-runtime-api = { path = "../../modules/staking-pool/rpc/runtime-api", default-features = false }
module-polkadot-bridge = { path = "../../modules/polkadot-bridge", default-features = false }
nutsfinance-stable-asset = { version = "0.1.0", default-features = false, path = "../../ecosystem-modules/stable-asset/lib/stable-asset", package = "nutsfinance-stable-asset" }
primitives = { package = "acala-primitives", path = "../../primitives", default-features = false }
runtime-common = { path = "../common", default-features = false }

Expand Down
1 change: 1 addition & 0 deletions runtime/mandala/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub mod homa;
pub mod honzon;
pub mod incentives;
pub mod nominees_election;
pub mod nutsfinance_stable_asset;
pub mod prices;
pub mod session_manager;
pub mod transaction_pause;
Expand Down
167 changes: 167 additions & 0 deletions runtime/mandala/src/benchmarking/nutsfinance_stable_asset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// This file is part of Acala.

// Copyright (C) 2020-2021 Acala Foundation.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{AccountId, Runtime, StableAsset};

use super::utils::set_balance_fungibles;
use frame_benchmarking::{account, whitelisted_caller};
use frame_system::RawOrigin;
use orml_benchmarking::runtime_benchmarks;
use primitives::currency::{AUSD, DOT, LDOT};
use sp_std::prelude::*;

const SEED: u32 = 0;

runtime_benchmarks! {
{ Runtime, nutsfinance_stable_asset }

create_pool {
let pool_asset = DOT;
let assets = vec![LDOT, AUSD];
let precisions = vec![1u128, 1u128];
let mint_fee = 10000000u128;
let swap_fee = 20000000u128;
let redeem_fee = 50000000u128;
let intial_a = 100u128;
let fee_recipient: AccountId = account("fee", 0, SEED);
}: _(RawOrigin::Root, pool_asset, assets, precisions, mint_fee, swap_fee, redeem_fee, intial_a, fee_recipient)

modify_a {
let pool_asset = DOT;
let assets = vec![LDOT, AUSD];
let precisions = vec![1u128, 1u128];
let mint_fee = 10000000u128;
let swap_fee = 20000000u128;
let redeem_fee = 50000000u128;
let intial_a = 100u128;
let fee_recipient: AccountId = account("fee", 0, SEED);
let _ = StableAsset::create_pool(RawOrigin::Root.into(), pool_asset, assets, precisions, mint_fee, swap_fee, redeem_fee, intial_a, fee_recipient.clone());
let pool_id = StableAsset::pool_count() - 1;
}: _(RawOrigin::Root, pool_id, 1000u128, 2629112370)

mint {
let tester: AccountId = whitelisted_caller();
let pool_asset = DOT;
let assets = vec![LDOT, AUSD];
let precisions = vec![1u128, 1u128];
let mint_fee = 10000000u128;
let swap_fee = 20000000u128;
let redeem_fee = 50000000u128;
let intial_a = 100u128;
let fee_recipient: AccountId = account("fee", 0, SEED);
let _ = StableAsset::create_pool(RawOrigin::Root.into(), pool_asset, assets, precisions, mint_fee, swap_fee, redeem_fee, intial_a, fee_recipient.clone());
let pool_id = StableAsset::pool_count() - 1;
set_balance_fungibles(LDOT, &tester, 100000000000u128);
set_balance_fungibles(AUSD, &tester, 100000000000u128);
}: _(RawOrigin::Signed(tester), pool_id, vec![10000000000u128, 20000000000u128], 0u128)

swap {
let tester: AccountId = whitelisted_caller();
let pool_asset = DOT;
let assets = vec![LDOT, AUSD];
let precisions = vec![1u128, 1u128];
let mint_fee = 10000000u128;
let swap_fee = 20000000u128;
let redeem_fee = 50000000u128;
let intial_a = 100u128;
let fee_recipient: AccountId = account("fee", 0, SEED);
let _ = StableAsset::create_pool(RawOrigin::Root.into(), pool_asset, assets, precisions, mint_fee, swap_fee, redeem_fee, intial_a, fee_recipient.clone());
let pool_id = StableAsset::pool_count() - 1;
set_balance_fungibles(LDOT, &tester, 100000000000u128);
set_balance_fungibles(AUSD, &tester, 100000000000u128);
let _ = StableAsset::mint(RawOrigin::Signed(tester.clone()).into(), pool_id, vec![10000000000u128, 20000000000u128], 0u128);
}: _(RawOrigin::Signed(tester), pool_id, 0, 1, 5000000u128, 0u128)

collect_fee {
let tester: AccountId = whitelisted_caller();
let pool_asset = DOT;
let assets = vec![LDOT, AUSD];
let precisions = vec![1u128, 1u128];
let mint_fee = 10000000u128;
let swap_fee = 20000000u128;
let redeem_fee = 50000000u128;
let intial_a = 100u128;
let fee_recipient: AccountId = account("fee", 0, SEED);
let _ = StableAsset::create_pool(RawOrigin::Root.into(), pool_asset, assets, precisions, mint_fee, swap_fee, redeem_fee, intial_a, fee_recipient.clone());
let pool_id = StableAsset::pool_count() - 1;
set_balance_fungibles(LDOT, &tester, 100000000000u128);
set_balance_fungibles(AUSD, &tester, 100000000000u128);
let _ = StableAsset::mint(RawOrigin::Signed(tester.clone()).into(), pool_id, vec![10000000000u128, 20000000000u128], 0u128);
let _ = StableAsset::swap(RawOrigin::Signed(tester.clone()).into(), pool_id, 0, 1, 5000000u128, 0u128);
}: _(RawOrigin::Root, pool_id)

redeem_proportion {
let tester: AccountId = whitelisted_caller();
let pool_asset = DOT;
let assets = vec![LDOT, AUSD];
let precisions = vec![1u128, 1u128];
let mint_fee = 10000000u128;
let swap_fee = 20000000u128;
let redeem_fee = 50000000u128;
let intial_a = 100u128;
let fee_recipient: AccountId = account("fee", 0, SEED);
let _ = StableAsset::create_pool(RawOrigin::Root.into(), pool_asset, assets, precisions, mint_fee, swap_fee, redeem_fee, intial_a, fee_recipient.clone());
let pool_id = StableAsset::pool_count() - 1;
set_balance_fungibles(LDOT, &tester, 100000000000u128);
set_balance_fungibles(AUSD, &tester, 100000000000u128);
let _ = StableAsset::mint(RawOrigin::Signed(tester.clone()).into(), pool_id, vec![10000000000u128, 20000000000u128], 0u128);
}: _(RawOrigin::Signed(tester), pool_id, 100000000u128, vec![0u128, 0u128])

redeem_single {
let tester: AccountId = whitelisted_caller();
let pool_asset = DOT;
let assets = vec![LDOT, AUSD];
let precisions = vec![1u128, 1u128];
let mint_fee = 10000000u128;
let swap_fee = 20000000u128;
let redeem_fee = 50000000u128;
let intial_a = 100u128;
let fee_recipient: AccountId = account("fee", 0, SEED);
let _ = StableAsset::create_pool(RawOrigin::Root.into(), pool_asset, assets, precisions, mint_fee, swap_fee, redeem_fee, intial_a, fee_recipient.clone());
let pool_id = StableAsset::pool_count() - 1;
set_balance_fungibles(LDOT, &tester, 100000000000u128);
set_balance_fungibles(AUSD, &tester, 100000000000u128);
let _ = StableAsset::mint(RawOrigin::Signed(tester.clone()).into(), pool_id, vec![10000000000u128, 20000000000u128], 0u128);
}: _(RawOrigin::Signed(tester), pool_id, 100000000u128, 0u32, 0u128)

redeem_multi {
let tester: AccountId = whitelisted_caller();
let pool_asset = DOT;
let assets = vec![LDOT, AUSD];
let precisions = vec![1u128, 1u128];
let mint_fee = 10000000u128;
let swap_fee = 20000000u128;
let redeem_fee = 50000000u128;
let intial_a = 100u128;
let fee_recipient: AccountId = account("fee", 0, SEED);
let _ = StableAsset::create_pool(RawOrigin::Root.into(), pool_asset, assets, precisions, mint_fee, swap_fee, redeem_fee, intial_a, fee_recipient.clone());
let pool_id = StableAsset::pool_count() - 1;
set_balance_fungibles(LDOT, &tester, 100000000000u128);
set_balance_fungibles(AUSD, &tester, 100000000000u128);
let _ = StableAsset::mint(RawOrigin::Signed(tester.clone()).into(), pool_id, vec![10000000000u128, 20000000000u128], 0u128);
}: _(RawOrigin::Signed(tester), pool_id, vec![500000u128, 500000u128], 1100000000000000000u128)
}

#[cfg(test)]
mod tests {
use super::*;
use crate::benchmarking::utils::tests::new_test_ext;
use orml_benchmarking::impl_benchmark_test_suite;

impl_benchmark_test_suite!(new_test_ext(),);
}
5 changes: 5 additions & 0 deletions runtime/mandala/src/benchmarking/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
};

use frame_benchmarking::account;
use frame_support::traits::tokens::fungibles;
use frame_support::{assert_ok, traits::Contains};
use frame_system::RawOrigin;
use orml_traits::MultiCurrencyExtended;
Expand Down Expand Up @@ -55,6 +56,10 @@ pub fn feed_price(prices: Vec<(CurrencyId, Price)>) -> DispatchResult {
Ok(())
}

pub fn set_balance_fungibles(currency_id: CurrencyId, who: &AccountId, balance: Balance) {
assert_ok!(<orml_tokens::Pallet<Runtime> as fungibles::Mutate<AccountId>>::mint_into(currency_id, who, balance));
}

#[cfg(test)]
pub mod tests {
pub fn new_test_ext() -> sp_io::TestExternalities {
Expand Down
28 changes: 28 additions & 0 deletions runtime/mandala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ pub use runtime_common::{
AUSD, DOT, LDOT, RENBTC,
};

/// Import the stable_asset pallet.
pub use nutsfinance_stable_asset;

mod authority;
mod benchmarking;
pub mod constants;
Expand Down Expand Up @@ -173,6 +176,7 @@ parameter_types! {
pub UnreleasedNativeVaultAccountId: AccountId = PalletId(*b"aca/urls").into_account();
// Ecosystem modules
pub const StarportPalletId: PalletId = PalletId(*b"aca/stpt");
pub const StableAssetPalletId: PalletId = PalletId(*b"nuts/sta");
}

pub fn get_all_module_accounts() -> Vec<AccountId> {
Expand All @@ -190,6 +194,7 @@ pub fn get_all_module_accounts() -> Vec<AccountId> {
StarportPalletId::get().into_account(),
ZeroAccountId::get(),
UnreleasedNativeVaultAccountId::get(),
StableAssetPalletId::get().into_account(),
]
}

Expand Down Expand Up @@ -1845,6 +1850,25 @@ impl orml_xcm::Config for Runtime {
type SovereignOrigin = EnsureRootOrHalfGeneralCouncil;
}

parameter_types! {
pub const Precision: u128 = 1000000000000000000u128; // 18 decimals
pub const FeePrecision: u128 = 10000000000u128; // 10 decimals
}

impl nutsfinance_stable_asset::Config for Runtime {
type Event = Event;
type AssetId = CurrencyId;
type Balance = Balance;
type Assets = Tokens;
type PalletId = StableAssetPalletId;

type AtLeast64BitUnsigned = u128;
type Precision = Precision;
type FeePrecision = FeePrecision;
type WeightInfo = weights::nutsfinance_stable_asset::WeightInfo<Runtime>;
type ListingOrigin = EnsureRootOrHalfGeneralCouncil;
}

impl cumulus_pallet_aura_ext::Config for Runtime {}

/// The address format for describing accounts.
Expand Down Expand Up @@ -1998,6 +2022,9 @@ construct_runtime! {
AuraExt: cumulus_pallet_aura_ext::{Pallet, Storage, Config} = 194,
SessionManager: module_session_manager::{Pallet, Call, Storage, Event<T>, Config<T>} = 195,

// Stable asset
StableAsset: nutsfinance_stable_asset::{Pallet, Call, Storage, Event<T>} = 200,

// Dev
Sudo: pallet_sudo::{Pallet, Call, Config<T>, Storage, Event<T>} = 255,
}
Expand Down Expand Up @@ -2352,6 +2379,7 @@ impl_runtime_apis! {
orml_add_benchmark!(params, batches, orml_oracle, benchmarking::oracle);

orml_add_benchmark!(params, batches, ecosystem_chainsafe, benchmarking::chainsafe_transfer);
orml_add_benchmark!(params, batches, nutsfinance_stable_asset, benchmarking::nutsfinance_stable_asset);

if batches.is_empty() { return Err("Benchmark not found for this module.".into()) }
Ok(batches)
Expand Down
1 change: 1 addition & 0 deletions runtime/mandala/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ pub mod orml_tokens;
pub mod orml_vesting;

pub mod ecosystem_chainsafe;
pub mod nutsfinance_stable_asset;
Loading

0 comments on commit 1113b04

Please sign in to comment.