Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bonds #645

Merged
merged 35 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
151cf3a
create pallet template
Roznovjak Jul 18, 2023
de0ccc0
draft
Roznovjak Jul 19, 2023
e6a173a
add tests
Roznovjak Jul 20, 2023
fddd427
formatting
Roznovjak Jul 20, 2023
bc29fba
satisfy clippy
Roznovjak Jul 20, 2023
2a6f8e6
add configurable origins and unlock extrinsic
Roznovjak Jul 20, 2023
ce81ddc
formatting
Roznovjak Jul 20, 2023
b903074
additional tests
Roznovjak Jul 20, 2023
1ae60c4
update docs
Roznovjak Jul 21, 2023
88291ab
fix tests
Roznovjak Jul 21, 2023
70e51f6
refactoring, benchmarking, updated mock, runtime integration
Roznovjak Jul 24, 2023
b27f531
add integration tests
Roznovjak Jul 25, 2023
260a15b
polishing
Roznovjak Jul 26, 2023
a5ef03e
formatting
Roznovjak Jul 26, 2023
8a3166e
satisfy clippy
Roznovjak Jul 26, 2023
8b49580
Merge branch 'master' into bonds
Roznovjak Jul 28, 2023
cc854bb
bump versions
Roznovjak Jul 28, 2023
6106d0f
fixes in the impl
Roznovjak Aug 1, 2023
d855bfc
Merge branch 'master' into bonds
Roznovjak Aug 1, 2023
4f41cee
bump version
Roznovjak Aug 2, 2023
a08c0bc
Merge branch 'master' into bonds
Roznovjak Aug 2, 2023
1458b3b
add IssueOrigin back to config
Roznovjak Aug 2, 2023
b6a83fc
add readme file
Roznovjak Aug 2, 2023
1214513
use same amounts in the tests
Roznovjak Aug 2, 2023
5032c1a
changes based on comments
Roznovjak Aug 4, 2023
cde8757
bump versions
Roznovjak Aug 4, 2023
05fdaab
update docs
Roznovjak Aug 7, 2023
0fe4fdd
fix integration test
Roznovjak Aug 7, 2023
6fc6514
add missing test
Roznovjak Aug 8, 2023
e1d58cc
use assert_eq in integration tests
Roznovjak Aug 8, 2023
ea45b34
resolve comments
Roznovjak Aug 8, 2023
49b9108
resolve comments
Roznovjak Aug 8, 2023
823fb0d
Merge branch 'master' into bonds
Roznovjak Aug 11, 2023
c482a3e
bump version
Roznovjak Aug 11, 2023
3a2507f
add benchmarking weights
Roznovjak Aug 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 42 additions & 15 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ members = [
'pallets/dynamic-fees',
'pallets/duster',
'pallets/otc',
'pallets/bonds',
'math',
]

Expand Down Expand Up @@ -64,6 +65,7 @@ pallet-transaction-multi-payment = { path = "pallets/transaction-multi-payment",
pallet-transaction-pause = { path = "pallets/transaction-pause", default-features = false }
pallet-xcm-rate-limiter = { path = "pallets/xcm-rate-limiter", default-features = false }
warehouse-liquidity-mining = { package = "pallet-liquidity-mining", path = "pallets/liquidity-mining", default-features = false }
pallet-bonds = { path = "pallets/bonds", default-features = false}

hydra-dx-build-script-utils = { path = "utils/build-script-utils", default-features = false }
scraper = { path = "scraper", default-features = false }
Expand Down
3 changes: 2 additions & 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.8.2"
version = "1.8.3"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand All @@ -18,6 +18,7 @@ hydradx-adapters = { workspace = true }
pallet-omnipool = { workspace = true }
pallet-circuit-breaker = { workspace = true }
pallet-omnipool-liquidity-mining = { workspace = true }
pallet-bonds = { workspace = true }

# Warehouse dependencies
pallet-asset-registry = { workspace = true }
Expand Down
124 changes: 124 additions & 0 deletions integration-tests/src/bonds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#![cfg(test)]

use crate::assert_balance;
use crate::polkadot_test_net::*;

use frame_support::{assert_noop, assert_ok};
use orml_traits::MultiCurrency;
use sp_runtime::BoundedVec;
use xcm_emulator::TestExt;

use hydradx_runtime::{AssetRegistry, Bonds, Currencies, Runtime, RuntimeOrigin};
use primitives::constants::time::unix_time::MONTH;

#[test]
fn issue_bonds_should_work_when_issued_for_native_asset() {
Hydra::execute_with(|| {
// Arrange
let amount = 100 * UNITS;
let fee = <Runtime as pallet_bonds::Config>::ProtocolFee::get().mul_ceil(amount);
let amount_without_fee: Balance = amount.checked_sub(fee).unwrap();

let maturity = NOW + MONTH;

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

// Assert
assert_eq!(Bonds::bonds(bond_id).unwrap(), (HDX, maturity));

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

assert!(bond_asset_details.asset_type == pallet_asset_registry::AssetType::Bond);
Roznovjak marked this conversation as resolved.
Show resolved Hide resolved
assert!(bond_asset_details.name.is_empty());
assert_eq!(bond_asset_details.existential_deposit, NativeExistentialDeposit::get());

assert_balance!(&ALICE.into(), HDX, ALICE_INITIAL_NATIVE_BALANCE - amount);
assert_balance!(&ALICE.into(), bond_id, amount_without_fee);

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

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

#[test]
fn issue_bonds_should_work_when_issued_for_shared_asset() {
Hydra::execute_with(|| {
// Arrange
let amount = 100 * UNITS;
let fee = <Runtime as pallet_bonds::Config>::ProtocolFee::get().mul_ceil(amount);
let amount_without_fee: Balance = amount.checked_sub(fee).unwrap();

let maturity = NOW + MONTH;

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(
bounded_name,
pallet_asset_registry::AssetType::PoolShare(HDX, DOT),
1_000,
None,
None,
)
.unwrap();
assert_ok!(Currencies::deposit(shared_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,
amount,
maturity
));

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

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

assert!(bond_asset_details.asset_type == pallet_asset_registry::AssetType::Bond);
Roznovjak marked this conversation as resolved.
Show resolved Hide resolved
assert!(bond_asset_details.name.is_empty());
assert_eq!(bond_asset_details.existential_deposit, 1_000);

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

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

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

#[test]
fn issue_bonds_should_not_work_when_issued_for_bond_asset() {
Hydra::execute_with(|| {
// Arrange
let amount = 100 * UNITS;
let maturity = NOW + MONTH;

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

// Act & Assert
assert_noop!(
Bonds::issue(
RuntimeOrigin::signed(ALICE.into()),
underlying_asset_id,
amount,
maturity
),
pallet_bonds::Error::<hydradx_runtime::Runtime>::InvalidAssetType
);
});
}
1 change: 1 addition & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod bonds;
mod call_filter;
mod circuit_breaker;
mod cross_chain_transfer;
Expand Down
9 changes: 7 additions & 2 deletions integration-tests/src/polkadot_test_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use frame_support::{
};
pub use hydradx_runtime::{AccountId, NativeExistentialDeposit, Treasury, VestingPalletId};
use pallet_transaction_multi_payment::Price;
pub use primitives::{constants::chain::CORE_ASSET_ID, AssetId, Balance};
pub use primitives::{constants::chain::CORE_ASSET_ID, AssetId, Balance, Moment};

use cumulus_primitives_core::ParaId;
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
Expand Down Expand Up @@ -51,6 +51,8 @@ pub const ETH: AssetId = 4;
pub const BTC: AssetId = 5;
pub const ACA: AssetId = 6;

pub const NOW: Moment = 1689844300000; // unix time in milliseconds

decl_test_relay_chain! {
pub struct PolkadotRelay {
Runtime = polkadot_runtime::Runtime,
Expand Down Expand Up @@ -186,7 +188,7 @@ pub fn polkadot_ext() -> sp_io::TestExternalities {

pub fn hydra_ext() -> sp_io::TestExternalities {
use frame_support::traits::OnInitialize;
use hydradx_runtime::{MultiTransactionPayment, Runtime, System};
use hydradx_runtime::{MultiTransactionPayment, Runtime, System, Timestamp};

let stable_amount = 50_000 * UNITS * 1_000_000;
let native_amount = 936_329_588_000_000_000;
Expand Down Expand Up @@ -222,6 +224,8 @@ pub fn hydra_ext() -> sp_io::TestExternalities {
(b"ETH".to_vec(), 1_000u128, Some(ETH)),
(b"BTC".to_vec(), 1_000u128, Some(BTC)),
(b"ACA".to_vec(), 1_000u128, Some(ACA)),
// workaround for next_asset_id() to return correct values
(b"DUMMY".to_vec(), 1_000u128, None),
],
native_asset_name: b"HDX".to_vec(),
native_existential_deposit: existential_deposit,
Expand Down Expand Up @@ -294,6 +298,7 @@ pub fn hydra_ext() -> sp_io::TestExternalities {
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
System::set_block_number(1);
Timestamp::set_timestamp(NOW);
// Make sure the prices are up-to-date.
MultiTransactionPayment::on_initialize(1);
});
Expand Down
2 changes: 1 addition & 1 deletion pallets/asset-registry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-asset-registry"
version = "2.2.4"
version = "2.3.0"
description = "Pallet for asset registry management"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
Loading
Loading