Skip to content

Commit

Permalink
feat: 🎨 Amm parameters adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignazio Bovo committed Oct 18, 2023
1 parent 3066093 commit bf4b872
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion chain-metadata.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion query-node/chain-metadata/2002.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion runtime-modules/content/src/benchmarks/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ benchmarks! {
channel_id,
curator_member_id
)?;
let slope = 10_000_000u32.into();
let slope = Permill::from_perthousand(1);
let intercept = 100u32.into();
let params = AmmParams{ slope, intercept };
// No pausable feature prevents this
Expand Down
2 changes: 1 addition & 1 deletion runtime-modules/content/src/benchmarks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ fn call_activate_amm<T: Config>(
channel_id: T::ChannelId,
) {
let params = AmmParams {
slope: 10_000_000u32.into(),
slope: Permill::from_perthousand(1),
intercept: 100u32.into(),
};
Pallet::<T>::activate_amm(RawOrigin::Signed(sender).into(), actor, channel_id, params).unwrap()
Expand Down
2 changes: 1 addition & 1 deletion runtime-modules/content/src/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2659,7 +2659,7 @@ impl ActivateAmmFixture {
actor: ContentActor::Member(DEFAULT_MEMBER_ID),
channel_id: ChannelId::one(),
params: AmmParamsOf::<Test> {
slope: 10_000_000u32.into(),
slope: Permill::from_perthousand(1),
intercept: Zero::zero(),
},
}
Expand Down
6 changes: 3 additions & 3 deletions runtime-modules/project-token/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const DEFAULT_SPLIT_PARTICIPATION: u32 =
DEFAULT_SPLIT_PAYOUT * (DEFAULT_TOKEN_ISSUANCE / DEFAULT_SPLIT_ALLOCATION);

// Amm
const DEFAULT_AMM_AMOUNT: u32 = 1;
const DEFAULT_AMM_JOY_AMOUNT: u32 = 5_000_100; // (a = 10_000_000) * amount^2 /2 + (b = 100) * amount, amount = 1
const DEFAULT_AMM_AMOUNT: u32 = 1000;
const DEFAULT_AMM_JOY_AMOUNT: u32 = 100500; // (a = 1 perthousand) * amount^2 /2 + (b = 100) * amount, amount = 1000

// Patronage
const DEFAULT_PATRONAGE: YearlyRate = YearlyRate(Permill::from_percent(15));
Expand Down Expand Up @@ -161,7 +161,7 @@ fn init_token_sale<T: Config>(token_id: T::TokenId) -> Result<TokenSaleId, Dispa

fn activate_amm<T: Config>(token_id: T::TokenId, member_id: T::MemberId) -> DispatchResult {
let params = AmmParams {
slope: 10_000_000u32.into(),
slope: Permill::from_perthousand(1),
intercept: 100u32.into(),
};
Token::<T>::activate_amm(token_id, member_id, params)
Expand Down
2 changes: 1 addition & 1 deletion runtime-modules/project-token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ decl_storage! { generate_storage_info
pub MaxYearlyPatronageRate get(fn max_yearly_patronage_rate) config(): YearlyRate = YearlyRate(Permill::from_percent(15));

/// Minimum slope parameters allowed for AMM curve
pub MinAmmSlopeParameter get(fn min_amm_slope_parameter) config(): TokenBalanceOf<T> = TokenBalanceOf::<T>::from(1_000_000_u32);
pub MinAmmSlopeParameter get(fn min_amm_slope_parameter) config(): Permill = Permill::from_rational(1_u32, 1_000_000u32);
}

add_extra_genesis {
Expand Down
2 changes: 1 addition & 1 deletion runtime-modules/project-token/src/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ impl ActivateAmmFixture {
Self { member_id, ..self }
}

pub fn with_linear_function_params(self, a: Balance, b: Balance) -> Self {
pub fn with_linear_function_params(self, a: Permill, b: Balance) -> Self {
let params = AmmParams {
slope: a,
intercept: b,
Expand Down
8 changes: 4 additions & 4 deletions runtime-modules/project-token/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ pub struct GenesisConfigBuilder {
pub(crate) bond_tx_fees: Permill,
pub(crate) unbond_tx_fees: Permill,
pub(crate) max_yearly_patronage_rate: YearlyRate,
pub(crate) min_amm_slope_parameter: Balance,
pub(crate) min_amm_slope_parameter: Permill,
}

/// test externalities + initial balances allocation
Expand Down Expand Up @@ -662,7 +662,7 @@ pub const DEFAULT_SPLIT_PARTICIPATION: u128 = DEFAULT_SPLIT_REVENUE / 100;
// ------ Bonding Curve Constants ------------
pub const DEFAULT_AMM_BUY_AMOUNT: u128 = 1000;
pub const DEFAULT_AMM_SELL_AMOUNT: u128 = 100;
pub const AMM_CURVE_SLOPE: u128 = 10_000_000;
pub const AMM_CURVE_SLOPE: Permill = Permill::from_perthousand(1);
pub const AMM_CURVE_INTERCEPT: u128 = 1000;
pub const DEFAULT_AMM_BUY_FEES: Permill = Permill::from_percent(1);
pub const DEFAULT_AMM_SELL_FEES: Permill = Permill::from_percent(10);
Expand Down Expand Up @@ -748,11 +748,11 @@ pub(crate) fn amm_function_values(
let sq_coeff = AMM_CURVE_SLOPE / 2;
let res = match bond_operation {
AmmOperation::Buy => {
sq_coeff * ((supply + amount) * (supply + amount) - supply2)
sq_coeff.mul_floor((supply + amount) * (supply + amount) - supply2)
+ AMM_CURVE_INTERCEPT * amount
}
AmmOperation::Sell => {
sq_coeff * (supply2 - (supply - amount) * (supply - amount))
sq_coeff.mul_floor(supply2 - (supply - amount) * (supply - amount))
+ AMM_CURVE_INTERCEPT * amount
}
};
Expand Down
2 changes: 1 addition & 1 deletion runtime-modules/project-token/src/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl GenesisConfigBuilder {
bond_tx_fees: DEFAULT_AMM_BUY_FEES,
unbond_tx_fees: DEFAULT_AMM_SELL_FEES,
max_yearly_patronage_rate: DEFAULT_MAX_YEARLY_PATRONAGE_RATE.into(),
min_amm_slope_parameter: 10u32.into(),
min_amm_slope_parameter: AMM_CURVE_SLOPE.into(),
}
}

Expand Down
8 changes: 4 additions & 4 deletions runtime-modules/project-token/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ impl<
#[derive(Default, Encode, Decode, TypeInfo, Clone, Debug, Eq, PartialEq, MaxEncodedLen)]
pub struct AmmParams<Balance> {
/// Slope parameter : a
pub slope: Balance,
pub slope: Permill,

/// Intercept : b
pub intercept: Balance,
Expand All @@ -598,7 +598,7 @@ pub struct AmmParams<Balance> {
#[derive(Default, Encode, Decode, TypeInfo, Clone, Debug, Eq, PartialEq, MaxEncodedLen)]
pub struct AmmCurve<Balance> {
/// Slope parameter : a
pub slope: Balance,
pub slope: Permill,

/// Intercept : b
pub intercept: Balance,
Expand Down Expand Up @@ -637,12 +637,12 @@ impl<Balance: TokenBalanceTrait> AmmCurve<Balance> {
let amount_sq = amount
.checked_mul(&amount)
.ok_or(Error::<T>::ArithmeticError)?;
let first_term = self.slope.saturating_mul(amount_sq).div(2u32.into());
let first_term = self.slope.mul_floor(amount_sq).div(2u32.into());
let second_term = self.intercept.saturating_mul(amount);
let mixed = amount
.checked_mul(&self.provided_supply)
.ok_or(Error::<T>::ArithmeticError)?;
let third_term = self.slope.saturating_mul(mixed);
let third_term = self.slope.mul_floor(mixed);
let res = match bond_operation {
AmmOperation::Buy => first_term
.checked_add(&second_term)
Expand Down
2 changes: 1 addition & 1 deletion types/src/augment/augment-api-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ declare module '@polkadot/api-base/types/storage' {
/**
* Minimum slope parameters allowed for AMM curve
**/
minAmmSlopeParameter: AugmentedQuery<ApiType, () => Observable<u128>, []>;
minAmmSlopeParameter: AugmentedQuery<ApiType, () => Observable<Permill>, []>;
/**
* Minimum revenue split duration constraint
**/
Expand Down
4 changes: 2 additions & 2 deletions types/src/augment/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ export default {
* Lookup201: pallet_project_token::types::AmmCurve<Balance>
**/
PalletProjectTokenAmmCurve: {
slope: 'u128',
slope: 'Permill',
intercept: 'u128',
providedSupply: 'u128'
},
Expand Down Expand Up @@ -3298,7 +3298,7 @@ export default {
* Lookup396: pallet_project_token::types::AmmParams<Balance>
**/
PalletProjectTokenAmmParams: {
slope: 'u128',
slope: 'Permill',
intercept: 'u128'
},
/**
Expand Down
4 changes: 2 additions & 2 deletions types/src/augment/types-lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,7 @@ declare module '@polkadot/types/lookup' {

/** @name PalletProjectTokenAmmCurve (201) */
export interface PalletProjectTokenAmmCurve extends Struct {
readonly slope: u128;
readonly slope: Permill;
readonly intercept: u128;
readonly providedSupply: u128;
}
Expand Down Expand Up @@ -3811,7 +3811,7 @@ declare module '@polkadot/types/lookup' {

/** @name PalletProjectTokenAmmParams (396) */
export interface PalletProjectTokenAmmParams extends Struct {
readonly slope: u128;
readonly slope: Permill;
readonly intercept: u128;
}

Expand Down

0 comments on commit bf4b872

Please sign in to comment.