Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5398ea3
fix: set collateral denomination and sufficiency in mock
rflechtner Nov 6, 2024
5613fd0
test: first mint test
rflechtner Nov 6, 2024
5ae7769
test: more mint_into tests
rflechtner Nov 7, 2024
0e2053c
test: burn_into tests
rflechtner Nov 7, 2024
96d8a87
test: rounding via repeated mints/burns
rflechtner Nov 12, 2024
725c15e
chore: fmt
rflechtner Nov 12, 2024
c1026bd
test: make sure you can't mint more than we can represent in the fixed
rflechtner Nov 13, 2024
a0af6fe
Merge branch 'pallet_bonded_coins' into rf-test-mint-and-burn
rflechtner Nov 14, 2024
9809cf9
test: fix most fixable tests
rflechtner Nov 18, 2024
2e1f495
refactor: use assert_eq_error_rate! instead of custom functions
rflechtner Nov 20, 2024
0472d52
fix: tests that can be fixed
rflechtner Nov 20, 2024
2b0745c
tests: allow configurable deviation from expected prices
rflechtner Nov 20, 2024
5ad4e15
test: edge case where we burn account's full balance when frozen
rflechtner Nov 21, 2024
8435ab5
test: minting and burning to from/to other accounts
rflechtner Nov 21, 2024
2bcc31d
chore: misc review suggestions
rflechtner Nov 21, 2024
976c253
test: mint where cost is 0
rflechtner Nov 21, 2024
1529674
fix error test case
Ad96el Nov 22, 2024
ba88a0c
Revert "fix error test case"
Ad96el Nov 25, 2024
77ac2a4
chore: use constant for collateral id in handles_asset_overflow
rflechtner Nov 25, 2024
38054de
chore: call fungibles trait methods on assets pallet directly
rflechtner Nov 25, 2024
41c70f5
chore: rename collateral_at_supply mocks function
rflechtner Nov 25, 2024
21b5333
fixup! chore: call fungibles trait methods on assets pallet directly
rflechtner Nov 25, 2024
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
1 change: 1 addition & 0 deletions pallets/pallet-bonded-coins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@ pub mod pallet {
.ok_or(ArithmeticError::Overflow)?;

let real_costs = normalized_costs
// TODO: can easily overflow, causing test burn_large_quantity to fail
.checked_mul(CurveParameterTypeOf::<T>::from_num(collateral_denomination))
.ok_or(ArithmeticError::Overflow)?
// should never fail
Expand Down
31 changes: 27 additions & 4 deletions pallets/pallet-bonded-coins/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use frame_support::{
Hashable,
};
use frame_system::{EnsureRoot, EnsureSigned};
use sp_core::U256;
use sp_runtime::{
traits::{BlakeTwo256, IdentifyAccount, IdentityLookup, Verify},
BoundedVec, BuildStorage, MultiSignature,
BoundedVec, BuildStorage, MultiSignature, Permill,
};
use substrate_fixed::{
traits::{FixedSigned, FixedUnsigned},
Expand Down Expand Up @@ -44,16 +45,33 @@ pub(crate) const DEFAULT_COLLATERAL_DENOMINATION: u8 = 10;
pub(crate) const DEFAULT_BONDED_DENOMINATION: u8 = 10;
pub(crate) const ONE_HUNDRED_KILT: u128 = 100_000_000_000_000_000;

pub(crate) const MAX_ERROR: Permill = Permill::from_perthousand(1);

// helper functions
pub fn assert_relative_eq(target: Float, expected: Float, epsilon: Float) {
pub fn assert_relative_eq<T: FixedSigned>(target: T, expected: T, epsilon: T) {
assert!(
(target - expected).abs() <= epsilon,
"Expected {:?} but got {:?}",
"Expected {:?} +/- {:?} but got {:?}",
expected,
epsilon,
target
);
}

pub(crate) fn mocks_curve_get_collateral_at_supply(supply: u128) -> u128 {
let supply_u256 = U256::from(supply);
let sup_squared = supply_u256 * supply_u256;
// curve is f(x) = 4x + 3, resulting in f'(x) = 2x^2 + 3x.
// The actual implementation operates on denomination-scaled amounts; we can
// replicate this behaviour based on smallest units by denomination-scaling 'n',
// the factor of x
let result =
U256::from(2) * sup_squared / U256::exp10(DEFAULT_BONDED_DENOMINATION.into()) + U256::from(3) * supply_u256;
result
.try_into()
.expect("expected collateral too large for balance type")
}

pub(crate) fn get_linear_bonding_curve<Float: FixedSigned>() -> Curve<Float> {
let m = Float::from_num(0);
let n = Float::from_num(2);
Expand Down Expand Up @@ -277,7 +295,7 @@ pub mod runtime {
.assimilate_storage(&mut storage)
.expect("assimilate should not fail");

let collateral_assets = self.collaterals.into_iter().map(|id| (id, ACCOUNT_99, false, 1));
let collateral_assets = self.collaterals.iter().map(|id| (*id, ACCOUNT_99, true, 1));

let all_assets: Vec<_> = self
.pools
Expand Down Expand Up @@ -308,6 +326,11 @@ pub mod runtime {
.map(|id| (*id, vec![], vec![], pool_details.denomination))
.collect::<Vec<(u32, Vec<u8>, Vec<u8>, u8)>>()
})
.chain(
self.collaterals
.into_iter()
.map(|id| (id, vec![], vec![], DEFAULT_COLLATERAL_DENOMINATION)),
)
.collect(),
}
.assimilate_storage(&mut storage)
Expand Down
Loading
Loading