Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #33 from Setheum-Labs/standard-0.5.1-dev
Browse files Browse the repository at this point in the history
Update Build on Stp258Standard v0.5.1-dev
  • Loading branch information
balqaasem authored Mar 23, 2021
2 parents 32c6ae7 + 8bee9c1 commit 4a7f8f4
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 109 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
authors = ['Setheum Labs<https://github.com/Setheum-Labs>']
description = 'Setheum Elastic Reserve Protocol (SERP) Market Pallet'
description = 'Provide `Stp258Standard` implementation using `pallet-balances` and `stp258-tokens'
edition = '2018'
homepage = 'https://setheum.xyz'
license = 'Apache-2.0 License'
name = 'stp258-currencies'
repository = 'https://github.com/Setheum-Labs/Setheum/'
version = '0.5.0-dev'
version = '0.5.1-dev'

[dependencies]
serde = { version = "1.0.111", optional = true }
Expand All @@ -18,15 +18,15 @@ sp-std = { version = "3.0.0", default-features = false }
frame-support = { version = "3.0.0", default-features = false }
frame-system = { version = "3.0.0", default-features = false }

stp258-traits = { version = '0.5.0-dev', git = "https://github.com/Setheum-Labs/stp258-traits", branch = "alpha" }
stp258-traits = { version = '0.5.0-dev', git = "https://github.com/Setheum-Labs/stp258-traits", branch = "pretty-messy" }
orml-utilities = { version = "0.4.0", default-features = false }

funty = { version = "=1.1.0", default-features = false } # https://github.com/bitvecto-rs/bitvec/issues/105
funty = { version = "1.1.0", default-features = false } # https://github.com/bitvecto-rs/bitvec/issues/105

[dev-dependencies]
sp-core = "3.0.0"
pallet-balances = "3.0.0"
stp258-tokens = { version = "0.5.0-dev", git = "https://github.com/Setheum-Labs/stp258-tokens"}
stp258-tokens = { version = "0.5.1-dev", git = "https://github.com/Setheum-Labs/stp258-tokens"}

[features]
default = ["std"]
Expand Down
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ pub mod module {
pub(crate) type AmountOf<T> =
<<T as Config>::Stp258Currency as Stp258CurrencyExtended<<T as frame_system::Config>::AccountId>>::Amount;

#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);

#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
Expand All @@ -72,12 +69,6 @@ pub mod module {
#[pallet::constant]
type GetStp258NativeId: Get<CurrencyIdOf<Self>>;


/// The balance of an account.
#[pallet::constant]
type GetBaseUnit: Get<u64>;


/// Weight information for extrinsics in this module.
type WeightInfo: WeightInfo;
}
Expand All @@ -103,13 +94,14 @@ pub mod module {
Withdrawn(CurrencyIdOf<T>, T::AccountId, BalanceOf<T>),
}

#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);

#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}

#[pallet::call]
impl<T: Config> Pallet<T> {

/// Transfer some balance to another account under `currency_id`.
///
/// The dispatch origin for this call must be `Signed` by the
Expand Down Expand Up @@ -167,6 +159,14 @@ impl<T: Config> Stp258Currency<T::AccountId> for Pallet<T> {
type CurrencyId = CurrencyIdOf<T>;
type Balance = BalanceOf<T>;

fn base_unit(currency_id: Self::CurrencyId) -> Self::Balance {
if currency_id == T::GetStp258NativeId::get() {
T::Stp258Native::minimum_balance()
} else {
T::Stp258Currency::base_unit(currency_id)
}
}

fn minimum_balance(currency_id: Self::CurrencyId) -> Self::Balance {
if currency_id == T::GetStp258NativeId::get() {
T::Stp258Native::minimum_balance()
Expand Down
80 changes: 59 additions & 21 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{AccountIdConversion, IdentityLookup},
AccountId32, ModuleId,
AccountId32, ModuleId, Perbill,
};

use crate as stp258_currencies;
use crate as stp258_standard;

parameter_types! {
pub const BlockHashCount: u64 = 250;
Expand Down Expand Up @@ -67,36 +67,64 @@ parameter_type_with_key! {
};
}

parameter_type_with_key! {
pub GetBaseUnit: |currency_id: CurrencyId| -> Balance {
match currency_id {
&SETT => 10_000,
&JUSD => 1_000,
_ => 0,
}
};
}

const SERP_QUOTE_MULTIPLE: Balance = 2;
const SINGLE_UNIT: Balance = 1;
const SERPER_RATIO: Perbill = Perbill::from_percent(25);
const SETT_PAY_RATIO: Perbill = Perbill::from_percent(75);

parameter_types! {
pub DustAccount: AccountId = ModuleId(*b"dsss/dst").into_account();
}

parameter_types! {
pub const GetSerperAcc: AccountId = SERPER;
pub const GetSerpQuoteMultiple: Balance = SERP_QUOTE_MULTIPLE;
pub const GetSettPayAcc: AccountId = SETTPAY;
pub const GetSingleUnit: Balance = SINGLE_UNIT;
pub const GetSerperRatio: Perbill = SERPER_RATIO;
pub const GetSettPayRatio: Perbill = SETT_PAY_RATIO;
}

impl stp258_tokens::Config for Runtime {
type Event = Event;
type Balance = Balance;
type Amount = i64;
type CurrencyId = CurrencyId;
type WeightInfo = ();
type ExistentialDeposits = ExistentialDeposits;
type GetBaseUnit = GetBaseUnit;
type GetSerpQuoteMultiple = GetSerpQuoteMultiple;
type GetSerperAcc = GetSerperAcc;
type GetSettPayAcc = GetSettPayAcc;
type GetSerperRatio = GetSerperRatio;
type GetSettPayRatio = GetSettPayRatio;
type GetSingleUnit = GetSingleUnit;
type OnDust = stp258_tokens::TransferDust<Runtime, DustAccount>;
}

pub const STP258_NATIVE_ID: CurrencyId = 1;
pub const STP258_TOKEN_ID: CurrencyId = 2;

const STP258_BASE_UNIT: u64 = 1000;
pub const DNAR: CurrencyId = 1;
pub const SETT: CurrencyId = 2;
pub const JUSD: CurrencyId = 3;

parameter_types! {
pub const GetStp258NativeId: CurrencyId = STP258_NATIVE_ID;
pub const GetBaseUnit: u64 = STP258_BASE_UNIT;
pub const GetStp258NativeId: CurrencyId = DNAR;
}

impl Config for Runtime {
type Event = Event;
type Stp258Currency = Stp258Tokens;
type Stp258Native = AdaptedStp258Asset;
type GetStp258NativeId = GetStp258NativeId;
type GetBaseUnit = GetBaseUnit;
type WeightInfo = ();
}
pub type Stp258Native = Stp258NativeOf<Runtime>;
Expand All @@ -112,15 +140,17 @@ construct_runtime!(
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Module, Call, Storage, Config, Event<T>},
Stp258Currencies: stp258_currencies::{Module, Call, Event<T>},
Stp258Standard: stp258_standard::{Module, Call, Event<T>},
Stp258Tokens: stp258_tokens::{Module, Storage, Event<T>, Config<T>},
PalletBalances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>},
}
);

pub const ALICE: AccountId = AccountId32::new([1u8; 32]);
pub const BOB: AccountId = AccountId32::new([2u8; 32]);
pub const EVA: AccountId = AccountId32::new([5u8; 32]);

pub const ALICE: AccountId = AccountId32::new([0u8; 32]);
pub const BOB: AccountId = AccountId32::new([1u8; 32]);
pub const SERPER: AccountId = AccountId32::new([3u8; 32]);
pub const SETTPAY: AccountId = AccountId32::new([4u8; 32]);
pub const ID_1: LockIdentifier = *b"1 ";

pub struct ExtBuilder {
Expand All @@ -141,13 +171,21 @@ impl ExtBuilder {
self
}

pub fn one_hundred_for_alice_n_bob(self) -> Self {
pub fn one_hundred_for_alice_n_bob_n_serper_n_settpay(self) -> Self {
self.balances(vec![
(ALICE, STP258_NATIVE_ID, 100),
(BOB, STP258_NATIVE_ID, 100),
(ALICE, STP258_TOKEN_ID, 100 * STP258_BASE_UNIT),
(BOB, STP258_TOKEN_ID, 100 * STP258_BASE_UNIT),
])
(ALICE, DNAR, 100),
(BOB, DNAR, 100),
(SERPER, DNAR, 100),
(SETTPAY, DNAR, 100),
(ALICE, SETT, 100 * 10_000),
(BOB, SETT, 100 * 10_000),
(SERPER, SETT, 100 * 10_000),
(SETTPAY, SETT, 100 * 10_000),
(ALICE, JUSD, 100 * 1_000),
(BOB, JUSD, 100 * 1_000),
(SERPER, JUSD, 100 * 1_000),
(SETTPAY, JUSD, 100 * 1_000),
])
}

pub fn build(self) -> sp_io::TestExternalities {
Expand All @@ -160,7 +198,7 @@ impl ExtBuilder {
.endowed_accounts
.clone()
.into_iter()
.filter(|(_, currency_id, _)| *currency_id == STP258_NATIVE_ID)
.filter(|(_, currency_id, _)| *currency_id == DNAR)
.map(|(account_id, _, initial_balance)| (account_id, initial_balance))
.collect::<Vec<_>>(),
}
Expand All @@ -171,7 +209,7 @@ impl ExtBuilder {
endowed_accounts: self
.endowed_accounts
.into_iter()
.filter(|(_, currency_id, _)| *currency_id != STP258_NATIVE_ID)
.filter(|(_, currency_id, _)| *currency_id != DNAR)
.collect::<Vec<_>>(),
}
.assimilate_storage(&mut t)
Expand Down
Loading

0 comments on commit 4a7f8f4

Please sign in to comment.