Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions lib/lb_v2/test_suits.ak
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use aiken/transaction/value.{PolicyId, Value, ada_asset_name, ada_policy_id}
use lb_v2/order_validation.{apply_collecting_orders}
use lb_v2/types.{Asset, FactoryDatum, OrderDatum, PenaltyConfig, UsingSeller}
use lb_v2/utils.{
calculate_penalty, make_wrapper_redeemer, order_auth_an, order_commission,
calculate_penalty, default_denominator, make_wrapper_redeemer, order_auth_an, order_commission,
order_minimum_ada, sort_two_consecutive_factory_datum,
}
use lb_v2/validation.{build_default_collected_order_value}
Expand Down Expand Up @@ -157,17 +157,17 @@ fn calculate_penalty_params() -> Fuzzer<(Int, Int, Int)> {
}

test test_calculate_penalty(total_amount via calculate_penalty_params()) {
let (total_input_amount, total_output_amount, percent) = total_amount
let (total_input_amount, total_output_amount, penalty_numerator) = total_amount
let penalty_amount =
calculate_penalty(
penalty_config: Some(PenaltyConfig { penalty_start_time: 200, percent }),
penalty_config: Some(PenaltyConfig { penalty_start_time: 200, penalty_numerator }),
end_valid_time_range: 200,
total_input_amount: total_input_amount,
total_output_amount: total_output_amount,
)
let expect_pen_amount =
if total_input_amount > total_output_amount {
( total_input_amount - total_output_amount ) * percent / 100
( total_input_amount - total_output_amount ) * penalty_numerator / default_denominator
} else {
0
}
Expand Down
16 changes: 8 additions & 8 deletions lib/lb_v2/treasury_validation.ak
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use lb_v2/types.{
}
use lb_v2/utils.{
amm_authen_policy_id, amm_factory_auth_asset_name, amm_pool_auth_asset_name,
create_pool_comission, default_burn_liquidity, max_base_fee_numerator,
create_pool_comission, default_burn_liquidity, default_denominator, default_users_lp_asset_amount_numerator, max_base_fee_numerator,
max_penalty, min_base_fee_numerator, min_pool_allocation, one_month_ms,
treasury_auth_an, treasury_minimum_ada, two_day_ms,
}
Expand Down Expand Up @@ -91,7 +91,7 @@ pub fn validate_creating_treasury_out(
RInlineDatum { hash: datum_hash } -> dict.has_key(datums, datum_hash)
},
pool_allocation >= min_pool_allocation,
pool_allocation <= 100,
pool_allocation <= default_denominator,
when minimum_order_raise is {
Some(min_order) -> min_order > 0
_ -> True
Expand All @@ -112,14 +112,14 @@ pub fn validate_creating_treasury_out(
reserve_raise == 0,
total_liquidity == 0,
when penalty_config is {
Some(PenaltyConfig { penalty_start_time, percent }) -> and {
Some(PenaltyConfig { penalty_start_time, penalty_numerator }) -> and {
penalty_start_time > start_time,
penalty_start_time < end_time,
// Business requires a maximum penalty period of 2 final days
penalty_start_time >= end_time - two_day_ms,
percent > 0,
penalty_numerator > 0,
// Business requires maximum penalty rate is 25 percent
percent <= max_penalty,
penalty_numerator <= max_penalty,
}
_ -> True
},
Expand Down Expand Up @@ -302,8 +302,8 @@ pub fn calculate_allocation(
asset_b: Asset,
amm_authen_policy_id: PolicyId,
) -> (Int, Int, Int, Value, AssetName) {
let datum_reserve_a = lbe_reserve_a * pool_allocation / 100
let datum_reserve_b = lbe_reserve_b * pool_allocation / 100
let datum_reserve_a = lbe_reserve_a * pool_allocation / default_denominator
let datum_reserve_b = lbe_reserve_b * pool_allocation / default_denominator

let Asset(asset_a_pid, asset_a_tn) = asset_a
let Asset(asset_b_pid, asset_b_tn) = asset_b
Expand All @@ -317,7 +317,7 @@ pub fn calculate_allocation(
)
let total_lbe_lp = pool_total_liquidity - default_burn_liquidity
let expected_receiver_lp_tokens =
total_lbe_lp * ( pool_allocation - 50 ) / pool_allocation
total_lbe_lp * ( pool_allocation - default_users_lp_asset_amount_numerator ) / pool_allocation
let treasury_lp_amount = total_lbe_lp - expected_receiver_lp_tokens

let expected_receiver_value =
Expand Down
2 changes: 1 addition & 1 deletion lib/lb_v2/types.ak
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub type FactoryRedeemer {

pub type PenaltyConfig {
penalty_start_time: Int,
percent: Int,
penalty_numerator: Int,
}

pub type ReceiverDatum {
Expand Down
14 changes: 9 additions & 5 deletions lib/lb_v2/utils.ak
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ pub const minimum_order_collected = 30

pub const minimum_order_redeemed = 30

// The minimum percentage of total funds will go to pool
pub const min_pool_allocation = 70
pub const default_denominator = 10000

pub const default_users_lp_asset_amount_numerator = 5000

// The minimum ratio of total funds will go to pool
pub const min_pool_allocation = 7000

// 2 days in milliseconds = 2 * 24 * 60 * 60 * 1000
// The business requires a maximum penalty period of two final days.
Expand All @@ -42,7 +46,7 @@ pub const two_day_ms = 172800000
pub const one_month_ms = 2592000000

// Maximum penalty rate based on business
pub const max_penalty = 25
pub const max_penalty = 2500

// the ADA amount store in Treasury UTxO
pub const treasury_minimum_ada = 5_000_000
Expand Down Expand Up @@ -142,7 +146,7 @@ pub fn calculate_penalty(
) -> Int {
when penalty_config is {
None -> 0
Some(PenaltyConfig { penalty_start_time, percent }) ->
Some(PenaltyConfig { penalty_start_time, penalty_numerator }) ->
if end_valid_time_range < penalty_start_time {
// not in penalty time
0
Expand All @@ -151,7 +155,7 @@ pub fn calculate_penalty(
// withdrawal amount of this Tx
let withdrawal_amount = total_input_amount - total_output_amount
// calculate penalty
withdrawal_amount * percent / 100
withdrawal_amount * penalty_numerator / default_denominator
} else {
0
}
Expand Down
18 changes: 9 additions & 9 deletions plutus.json

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions plutus.ts

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as T from "@minswap/translucent";

import invariant from "@minswap/tiny-invariant";
import {
DEFAULT_DENOMINATOR,
DISCOVERY_MAX_RANGE,
MAX_COLLECT_SELLERS,
PENALTY_MAX_PERCENT,
Expand Down Expand Up @@ -291,7 +292,7 @@ export class Api {
}
invariant(reserveBase > 0, "Reserve Base > 0");
if (penaltyConfig) {
let { penaltyStartTime, percent } = penaltyConfig;
let { penaltyStartTime, penaltyNumerator } = penaltyConfig;
invariant(
penaltyStartTime > BigInt(startTime),
"Penalty Start Time > Start Time",
Expand All @@ -304,9 +305,9 @@ export class Api {
penaltyStartTime >= BigInt(endTime - PENALTY_MAX_RANGE),
"Maximum penalty period of 2 final days",
);
invariant(percent > 0, "Penalty Percent > 0");
invariant(penaltyNumerator > 0, "Penalty Percent > 0");
invariant(
percent <= PENALTY_MAX_PERCENT,
penaltyNumerator <= PENALTY_MAX_PERCENT,
`Penalty Percent <= ${PENALTY_MAX_PERCENT}`,
);
}
Expand Down Expand Up @@ -856,7 +857,10 @@ export class Api {
if (inAmount > outAmount) {
// Calculate withdrawal amount and penalty
const withdrawalAmount = inAmount - outAmount;
return (withdrawalAmount * penaltyConfig.percent) / 100n;
return (
(withdrawalAmount * penaltyConfig.penaltyNumerator) /
DEFAULT_DENOMINATOR
);
}
return 0n;
}
Expand Down
17 changes: 13 additions & 4 deletions src/build-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import {
LBE_MESSAGE_COLLECT_ORDERS,
LBE_MESSAGE_COLLECT_MANAGER,
LBE_MESSAGE_COUNTING_SELLERS,
DEFAULT_USERS_LP_ASSET_AMOUNT_NUMERATOR,
DEFAULT_DENOMINATOR,
} from "./constants";
import {
collectValidators,
Expand Down Expand Up @@ -163,7 +165,10 @@ export type BuildCollectOrdersOptions = {
validTo: UnixTime;
};

export type PenaltyConfig = { penaltyStartTime: bigint; percent: bigint };
export type PenaltyConfig = {
penaltyStartTime: bigint;
penaltyNumerator: bigint;
};

export type BuildUpdateLBEOptions = {
treasuryInput: LbeUTxO;
Expand Down Expand Up @@ -793,8 +798,10 @@ export class WarehouseBuilder {
reserveA = totalReserveRaise;
reserveB = treasuryInDatum.reserveBase;
}
const poolReserveA = (reserveA * treasuryInDatum.poolAllocation) / 100n;
const poolReserveB = (reserveB * treasuryInDatum.poolAllocation) / 100n;
const poolReserveA =
(reserveA * treasuryInDatum.poolAllocation) / DEFAULT_DENOMINATOR;
const poolReserveB =
(reserveB * treasuryInDatum.poolAllocation) / DEFAULT_DENOMINATOR;
const totalLiquidity = calculateInitialLiquidity(
poolReserveA,
poolReserveB,
Expand All @@ -817,7 +824,9 @@ export class WarehouseBuilder {

const totalLbeLPs = totalLiquidity - LP_COLATERAL;
const receiverLP =
(totalLbeLPs * (treasuryInDatum.poolAllocation - 50n)) /
(totalLbeLPs *
(treasuryInDatum.poolAllocation -
DEFAULT_USERS_LP_ASSET_AMOUNT_NUMERATOR)) /
treasuryInDatum.poolAllocation;
const treasuryOutDatum: TreasuryDatum = {
...treasuryInDatum,
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/hihi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let main = async () => {
"addr_test1qr03hndgkqdw4jclnvps6ud43xvuhms7rurjq87yfgzc575pm6dyr7fz24xwkh6k0ldufe2rqhgkwcppx5fzjrx5j2rs2rt9qc",
receiver:
"addr_test1qr03hndgkqdw4jclnvps6ud43xvuhms7rurjq87yfgzc575pm6dyr7fz24xwkh6k0ldufe2rqhgkwcppx5fzjrx5j2rs2rt9qc",
poolAllocation: 100n,
poolAllocation: 10000n,
revocable: true,
poolBaseFee: 30n,
};
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/run-smoke-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const happyCase = async (options: { batcher: WarehouseBatcher; api: Api }) => {
endTime: things.endTime,
owner: things.address,
receiver: things.address,
poolAllocation: 100n,
poolAllocation: 10000n,
revocable: true,
poolBaseFee: 30n,
};
Expand Down
6 changes: 4 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ export const TREASURY_MIN_ADA = 5_000_000n;
export const MANAGER_MIN_ADA = 2_500_000n;
export const SELLER_MIN_ADA = 2_500_000n;
export const ORDER_MIN_ADA = 2_500_000n;
export const MAX_PENALTY_RATE = 25n;
export const MAX_PENALTY_RATE = 2500n;
export const MINIMUM_SELLER_COLLECTED = 20n;
export const MINIMUM_ORDER_COLLECTED = 30n;
export const MINIMUM_ORDER_REDEEMED = 30n;

// LBE Constraint
export const DISCOVERY_MAX_RANGE = 30 * 24 * 60 * 60 * 1000; // 30 days
export const POOL_ALLOCATION_MIN = 70n;
export const POOL_ALLOCATION_MIN = 7000n;
export const POOL_BASE_FEE_MIN = 5n;
export const POOL_BASE_FEE_MAX = 2000n;
export const PENALTY_MAX_RANGE = 2 * 24 * 60 * 60 * 1000;
export const PENALTY_MAX_PERCENT = 70;
export const DEFAULT_DENOMINATOR = 10000n;
export const DEFAULT_USERS_LP_ASSET_AMOUNT_NUMERATOR = 5000n;

// Batching
export const MAX_COLLECT_SELLERS = 20;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/collect-sellers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async function genTestWarehouse() {
...defaultTreasuryDatum,
penaltyConfig: {
penaltyStartTime: defaultTreasuryDatum.endTime - 5000n,
percent: 25n,
penaltyNumerator: 2500n,
},
};
const treasuryUTxO = {
Expand Down
12 changes: 6 additions & 6 deletions src/tests/create-treasury.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ beforeEach(async () => {
const owner = await t.wallet.address();
let options: BuildCreateTreasuryOptions = {
sellerAmount: DEFAULT_SELLER_AMOUNT,
factoryUtxo,
factoryUtxo: factoryUtxo,
sellerOwner: owner,
treasuryDatum: defaultTreasuryDatum,
validFrom: t.utils.slotToUnixTime(emulator.slot),
Expand Down Expand Up @@ -79,7 +79,7 @@ test("create-treasury | PASS | Penalty Config", async () => {
let penaltyConfig = {
penaltyStartTime:
W.defaultTreasuryDatum.endTime - BigInt(2 * 24 * 60 * 60 * 1000),
percent: MAX_PENALTY_RATE,
penaltyNumerator: MAX_PENALTY_RATE,
};
assertValidator(remixTreasuryDatum({ penaltyConfig }), "");
});
Expand Down Expand Up @@ -383,7 +383,7 @@ test("create-treasury | FAIL | Treasury Output Datum incorrect! | X | penaltyCon
let penaltyConfig = {
penaltyStartTime:
W.defaultTreasuryDatum.startTime + BigInt(24 * 60 * 60 * 1000),
percent: -10n,
penaltyNumerator: -10n,
};
assertValidatorFail(remixTreasuryDatum({ penaltyConfig }));
});
Expand All @@ -392,7 +392,7 @@ test("create-treasury | FAIL | Treasury Output Datum incorrect! | X | penaltySta
// penalty rate exceeds MAX_PENALTY_RATE
let penaltyConfig = {
penaltyStartTime: 0n,
percent: MAX_PENALTY_RATE,
penaltyNumerator: MAX_PENALTY_RATE,
};
assertValidatorFail(remixTreasuryDatum({ penaltyConfig }));
});
Expand All @@ -401,7 +401,7 @@ test("create-treasury | FAIL | Treasury Output Datum incorrect! | X | penaltySta
// penalty rate exceeds MAX_PENALTY_RATE
let penaltyConfig = {
penaltyStartTime: W.defaultTreasuryDatum.endTime,
percent: MAX_PENALTY_RATE,
penaltyNumerator: MAX_PENALTY_RATE,
};
assertValidatorFail(remixTreasuryDatum({ penaltyConfig }));
});
Expand All @@ -411,7 +411,7 @@ test("create-treasury | FAIL | Treasury Output Datum incorrect! | X | penaltySta
let penaltyConfig = {
penaltyStartTime:
W.defaultTreasuryDatum.endTime - BigInt(3 * 24 * 60 * 60 * 1000),
percent: MAX_PENALTY_RATE,
penaltyNumerator: MAX_PENALTY_RATE,
};
assertValidatorFail(remixTreasuryDatum({ penaltyConfig }));
});
Expand Down
8 changes: 6 additions & 2 deletions src/tests/example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
type BuildUsingSellerOptions,
type WarehouseBuilderOptions,
} from "../build-tx";
import { MINIMUM_ORDER_REDEEMED, MINIMUM_SELLER_COLLECTED } from "../constants";
import {
DEFAULT_DENOMINATOR,
MINIMUM_ORDER_REDEEMED,
MINIMUM_SELLER_COLLECTED,
} from "../constants";
import {
collectMinswapValidators,
collectValidators,
Expand Down Expand Up @@ -193,7 +197,7 @@ test("example flow", async () => {
owner: address2PlutusAddress(ACCOUNT_0.address),
receiver: address2PlutusAddress(ACCOUNT_0.address),
receiverDatum: { RInlineDatum: { hash: extraDatumHash } },
poolAllocation: 100n,
poolAllocation: 10000n,
minimumRaise: null,
maximumRaise: null,
reserveBase: 69000000000000n,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/using-seller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function genTestWarehouse() {
...defaultTreasuryDatum,
penaltyConfig: {
penaltyStartTime: defaultTreasuryDatum.endTime - 5000n,
percent: 25n,
penaltyNumerator: 2500n,
},
};
const treasuryUTxO = {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/warehouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const genWarehouse = async (maxTxSize?: number) => {
owner: address2PlutusAddress(ACCOUNT_0.address),
receiver: address2PlutusAddress(ACCOUNT_0.address),
receiverDatum: "RNoDatum",
poolAllocation: 100n,
poolAllocation: 10000n,
minimumRaise: null,
maximumRaise: null,
reserveBase: 69000000000000n,
Expand Down