Skip to content

Commit

Permalink
Merge branch 'master' into migrations_cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mrq1911 authored Aug 21, 2023
2 parents e65a3f5 + d251d15 commit a71c527
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 66 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "runtime-integration-tests"
version = "1.9.1"
version = "1.9.2"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
25 changes: 14 additions & 11 deletions integration-tests/src/bonds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn issue_bonds_should_work_when_issued_for_native_asset() {
let bond_asset_details = AssetRegistry::assets(bond_id).unwrap();

assert_eq!(bond_asset_details.asset_type, pallet_asset_registry::AssetType::Bond);
assert!(bond_asset_details.name.is_empty());
assert_eq!(bond_asset_details.name.into_inner(), Bonds::bond_name(HDX, maturity));
assert_eq!(bond_asset_details.existential_deposit, NativeExistentialDeposit::get());

assert_balance!(&ALICE.into(), HDX, ALICE_INITIAL_NATIVE_BALANCE - amount);
Expand All @@ -44,7 +44,7 @@ fn issue_bonds_should_work_when_issued_for_native_asset() {
}

#[test]
fn issue_bonds_should_work_when_issued_for_shared_asset() {
fn issue_bonds_should_work_when_issued_for_share_asset() {
Hydra::execute_with(|| {
// Arrange
let amount = 100 * UNITS;
Expand All @@ -55,44 +55,47 @@ fn issue_bonds_should_work_when_issued_for_shared_asset() {

let bounded_name: BoundedVec<u8, <Runtime as pallet_asset_registry::Config>::StringLimit> =
"SHARED".as_bytes().to_vec().try_into().unwrap();
let shared_asset_id = AssetRegistry::register_asset(
let share_asset_id = AssetRegistry::register_asset(
bounded_name,
pallet_asset_registry::AssetType::PoolShare(HDX, DOT),
1_000,
None,
None,
)
.unwrap();
assert_ok!(Currencies::deposit(shared_asset_id, &ALICE.into(), amount,));
assert_ok!(Currencies::deposit(share_asset_id, &ALICE.into(), amount,));

// Act
let bond_id = AssetRegistry::next_asset_id().unwrap();
assert_ok!(Bonds::issue(
RuntimeOrigin::signed(ALICE.into()),
shared_asset_id,
share_asset_id,
amount,
maturity
));

// Assert
assert_eq!(Bonds::bond(bond_id).unwrap(), (shared_asset_id, maturity));
assert_eq!(Bonds::bond(bond_id).unwrap(), (share_asset_id, maturity));

let bond_asset_details = AssetRegistry::assets(bond_id).unwrap();

assert_eq!(bond_asset_details.asset_type, pallet_asset_registry::AssetType::Bond);
assert!(bond_asset_details.name.is_empty());
assert_eq!(
bond_asset_details.name.into_inner(),
Bonds::bond_name(share_asset_id, maturity)
);
assert_eq!(bond_asset_details.existential_deposit, 1_000);

assert_balance!(&ALICE.into(), shared_asset_id, 0);
assert_balance!(&ALICE.into(), share_asset_id, 0);
assert_balance!(&ALICE.into(), bond_id, amount_without_fee);

assert_balance!(
&<Runtime as pallet_bonds::Config>::FeeReceiver::get(),
shared_asset_id,
share_asset_id,
fee
);

assert_balance!(&Bonds::pallet_account_id(), shared_asset_id, amount_without_fee);
assert_balance!(&Bonds::pallet_account_id(), share_asset_id, amount_without_fee);
});
}

Expand All @@ -118,7 +121,7 @@ fn issue_bonds_should_not_work_when_issued_for_bond_asset() {
amount,
maturity
),
pallet_bonds::Error::<hydradx_runtime::Runtime>::DisallowedAseet
pallet_bonds::Error::<hydradx_runtime::Runtime>::DisallowedAsset
);
});
}
2 changes: 1 addition & 1 deletion pallets/bonds/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-bonds"
version = "1.0.0"
version = "2.0.0"
authors = ['GalacticCouncil']
edition = "2021"
license = "Apache-2.0"
Expand Down
17 changes: 8 additions & 9 deletions pallets/bonds/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ use frame_support::{assert_ok, traits::EnsureOrigin};
use frame_system::RawOrigin;

use orml_traits::MultiCurrency;
use primitives::constants::time::unix_time::MONTH;
use primitives::{constants::time::unix_time::MONTH, AssetId, Balance};

pub const NOW: Moment = 1689844300000; // unix time in milliseconds
pub const ONE: u128 = 1_000_000_000_000;
pub const HDX: u32 = 0;
pub const ONE: Balance = 1_000_000_000_000;
pub const HDX: AssetId = 0;

benchmarks! {
where_clause {
where
T: Config,
T: pallet_timestamp::Config,
T::AssetId: From<u32> + Into<u32>,
T::Balance: From<u32> + From<u128>,
T::Moment: From<u64>
}
Expand All @@ -48,11 +47,11 @@ benchmarks! {
let amount: T::Balance = (200 * ONE).into();
let maturity = NOW + MONTH;

T::Currency::deposit(HDX.into(), &issuer, amount)?;
T::Currency::deposit(HDX, &issuer, amount)?;

}: _(RawOrigin::Signed(issuer), HDX.into(), (100 * ONE).into(), maturity)
}: _(RawOrigin::Signed(issuer), HDX, (100 * ONE).into(), maturity)
verify {
assert!(BondIds::<T>::get::<(T::AssetId, Moment)>((HDX.into(), maturity)).is_some());
assert!(BondIds::<T>::get::<(AssetId, Moment)>((HDX, maturity)).is_some());
}

redeem {
Expand All @@ -61,11 +60,11 @@ benchmarks! {
let origin = T::IssueOrigin::try_successful_origin().unwrap();
let issuer = T::IssueOrigin::ensure_origin(origin).unwrap();
let amount: T::Balance = (200 * ONE).into();
T::Currency::deposit(HDX.into(), &issuer, amount)?;
T::Currency::deposit(HDX, &issuer, amount)?;

let maturity = NOW + MONTH;

assert_ok!(crate::Pallet::<T>::issue(RawOrigin::Signed(issuer.clone()).into(), HDX.into(), amount, maturity));
assert_ok!(crate::Pallet::<T>::issue(RawOrigin::Signed(issuer.clone()).into(), HDX, amount, maturity));

let fee = <T as Config>::ProtocolFee::get().mul_ceil(amount);
let amount_without_fee: T::Balance = amount.checked_sub(&fee).unwrap();
Expand Down
62 changes: 28 additions & 34 deletions pallets/bonds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ use frame_support::{
PalletId,
};
use frame_system::{ensure_signed, pallet_prelude::OriginFor};
use scale_info::TypeInfo;
use sp_core::MaxEncodedLen;
use sp_std::vec::Vec;

use hydradx_traits::{AssetKind, CreateRegistry, Registry};
use orml_traits::{GetByKey, MultiCurrency};
use primitives::Moment;
use primitives::{AssetId, Moment};

#[cfg(test)]
mod tests;
Expand All @@ -74,7 +73,6 @@ pub use weights::WeightInfo;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use codec::HasCompact;
use frame_support::pallet_prelude::*;

#[pallet::pallet]
Expand All @@ -86,16 +84,6 @@ pub mod pallet {
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// Identifier for the class of asset.
type AssetId: Member
+ Parameter
+ Default
+ Copy
+ HasCompact
+ MaybeSerializeDeserialize
+ MaxEncodedLen
+ TypeInfo;

/// Balance type.
type Balance: Parameter
+ Member
Expand All @@ -110,14 +98,14 @@ pub mod pallet {
+ From<u128>;

/// Multi currency mechanism.
type Currency: MultiCurrency<Self::AccountId, CurrencyId = Self::AssetId, Balance = Self::Balance>;
type Currency: MultiCurrency<Self::AccountId, CurrencyId = AssetId, Balance = Self::Balance>;

/// Asset Registry mechanism - used to register bonds in the asset registry.
type AssetRegistry: Registry<Self::AssetId, Vec<u8>, Self::Balance, DispatchError>
+ CreateRegistry<Self::AssetId, Self::Balance, Error = DispatchError>;
type AssetRegistry: Registry<AssetId, Vec<u8>, Self::Balance, DispatchError>
+ CreateRegistry<AssetId, Self::Balance, Error = DispatchError>;

/// Provider for existential deposits of assets.
type ExistentialDeposits: GetByKey<Self::AssetId, Self::Balance>;
type ExistentialDeposits: GetByKey<AssetId, Self::Balance>;

/// Provider for the current timestamp.
type TimestampProvider: Time<Moment = Moment>;
Expand Down Expand Up @@ -148,35 +136,35 @@ pub mod pallet {
/// Registered bond ids.
/// Maps (underlying asset ID, maturity) -> bond ID
#[pallet::getter(fn bond_id)]
pub(super) type BondIds<T: Config> = StorageMap<_, Blake2_128Concat, (T::AssetId, Moment), T::AssetId>;
pub(super) type BondIds<T: Config> = StorageMap<_, Blake2_128Concat, (AssetId, Moment), AssetId>;

#[pallet::storage]
/// Registered bonds.
/// Maps bond ID -> (underlying asset ID, maturity)
#[pallet::getter(fn bond)]
pub(super) type Bonds<T: Config> = StorageMap<_, Blake2_128Concat, T::AssetId, (T::AssetId, Moment)>;
pub(super) type Bonds<T: Config> = StorageMap<_, Blake2_128Concat, AssetId, (AssetId, Moment)>;

#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
/// A bond asset was registered
TokenCreated {
issuer: T::AccountId,
asset_id: T::AssetId,
bond_id: T::AssetId,
asset_id: AssetId,
bond_id: AssetId,
maturity: Moment,
},
/// New bond were issued
Issued {
issuer: T::AccountId,
bond_id: T::AssetId,
bond_id: AssetId,
amount: T::Balance,
fee: T::Balance,
},
/// Bonds were redeemed
Redeemed {
who: T::AccountId,
bond_id: T::AssetId,
bond_id: AssetId,
amount: T::Balance,
},
}
Expand All @@ -191,7 +179,7 @@ pub mod pallet {
/// Maturity not long enough
InvalidMaturity,
/// Asset type not allowed for underlying asset
DisallowedAseet,
DisallowedAsset,
}

#[pallet::call]
Expand Down Expand Up @@ -219,17 +207,12 @@ pub mod pallet {
///
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::issue())]
pub fn issue(
origin: OriginFor<T>,
asset_id: T::AssetId,
amount: T::Balance,
maturity: Moment,
) -> DispatchResult {
pub fn issue(origin: OriginFor<T>, asset_id: AssetId, amount: T::Balance, maturity: Moment) -> DispatchResult {
let who = T::IssueOrigin::ensure_origin(origin)?;

ensure!(
T::AssetTypeWhitelist::contains(&T::AssetRegistry::retrieve_asset_type(asset_id)?),
Error::<T>::DisallowedAseet
Error::<T>::DisallowedAsset
);

let fee = T::ProtocolFee::get().mul_ceil(amount);
Expand All @@ -244,8 +227,8 @@ pub mod pallet {

let ed = T::ExistentialDeposits::get(&asset_id);

let bond_id = <T::AssetRegistry as CreateRegistry<T::AssetId, T::Balance>>::create_asset(
&[],
let bond_id = <T::AssetRegistry as CreateRegistry<AssetId, T::Balance>>::create_asset(
&Self::bond_name(asset_id, maturity),
AssetKind::Bond,
ed,
)?;
Expand Down Expand Up @@ -292,7 +275,7 @@ pub mod pallet {
///
#[pallet::call_index(1)]
#[pallet::weight(<T as Config>::WeightInfo::redeem())]
pub fn redeem(origin: OriginFor<T>, bond_id: T::AssetId, amount: T::Balance) -> DispatchResult {
pub fn redeem(origin: OriginFor<T>, bond_id: AssetId, amount: T::Balance) -> DispatchResult {
let who = ensure_signed(origin)?;

let (underlying_asset_id, maturity) = Self::bond(bond_id).ok_or(Error::<T>::NotRegistered)?;
Expand Down Expand Up @@ -320,4 +303,15 @@ impl<T: Config> Pallet<T> {
pub fn pallet_account_id() -> T::AccountId {
T::PalletId::get().into_account_truncating()
}

/// Return bond token name
pub fn bond_name(asset_id: AssetId, when: Moment) -> Vec<u8> {
let mut buf: Vec<u8> = Vec::new();

buf.extend_from_slice(&asset_id.to_le_bytes());
buf.extend_from_slice(b".");
buf.extend_from_slice(&when.to_le_bytes());

buf
}
}
3 changes: 2 additions & 1 deletion pallets/bonds/src/tests/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ fn reissuance_of_bonds_should_work_again_when_all_bonds_were_redeemed() {
#[test]
fn issue_bonds_should_fail_when_asset_is_blacklisted() {
let bond_id: AssetId = 10;

ExtBuilder::default()
.with_registered_asset(bond_id, NATIVE_EXISTENTIAL_DEPOSIT, AssetKind::Bond)
.build()
Expand All @@ -462,7 +463,7 @@ fn issue_bonds_should_fail_when_asset_is_blacklisted() {
// Act & Assert
assert_noop!(
Bonds::issue(RuntimeOrigin::signed(ALICE), bond_id, amount, maturity),
Error::<Test>::DisallowedAseet
Error::<Test>::DisallowedAsset
);
});
}
Expand Down
7 changes: 1 addition & 6 deletions pallets/bonds/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type Block = frame_system::mocking::MockBlock<Test>;

pub type AccountId = u64;
pub type Balance = u128;
pub type AssetId = u32;

pub const HDX: AssetId = 0;
pub const DAI: AssetId = 1;
Expand Down Expand Up @@ -106,7 +105,6 @@ impl Contains<AssetKind> for AssetTypeWhitelist {

impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type AssetId = AssetId;
type Balance = Balance;
type Currency = Tokens;
type AssetRegistry = DummyRegistry<Test>;
Expand Down Expand Up @@ -174,10 +172,7 @@ impl pallet_timestamp::Config for Test {

pub struct DummyRegistry<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> CreateRegistry<AssetId, Balance> for DummyRegistry<T>
where
T::AssetId: Into<AssetId> + From<u32>,
{
impl<T: Config> CreateRegistry<AssetId, Balance> for DummyRegistry<T> {
type Error = DispatchError;

fn create_asset(_name: &[u8], _kind: AssetKind, existential_deposit: Balance) -> Result<AssetId, DispatchError> {
Expand Down
1 change: 0 additions & 1 deletion runtime/hydradx/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,6 @@ impl Contains<AssetKind> for AssetTypeWhitelist {

impl pallet_bonds::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AssetId = AssetId;
type Balance = Balance;
type Currency = Currencies;
type AssetRegistry = AssetRegistry;
Expand Down

0 comments on commit a71c527

Please sign in to comment.