diff --git a/src/batcher-fee-reduction/calculate.ts b/src/batcher-fee-reduction/calculate.ts index e0398e9..5307c1e 100644 --- a/src/batcher-fee-reduction/calculate.ts +++ b/src/batcher-fee-reduction/calculate.ts @@ -2,7 +2,7 @@ import { Assets, UTxO } from "lucid-cardano"; import { NetworkEnvironment } from "../types/network"; import { - FIXED_BATCHER_FEE, + BATCHER_FEE_CONFIG, getActiveBatcherFee, getReducedBatcherFee, } from "./configs.internal"; @@ -23,10 +23,11 @@ export function calculateBatcherFee({ reductionAssets: Assets; } { const reductionAssets: Assets = {}; + const standardFee = BATCHER_FEE_CONFIG[networkEnv][dexVersion].standardFee; const activeBatcherFeeConfig = getActiveBatcherFee(networkEnv, dexVersion); if (!activeBatcherFeeConfig) { return { - batcherFee: FIXED_BATCHER_FEE, + batcherFee: standardFee, reductionAssets, }; } @@ -40,19 +41,19 @@ export function calculateBatcherFee({ } } } - const eligibleReductionAssets: Assets = {}; for (const { asset } of activeBatcherFeeConfig.assets) { if (asset in totalAssets) { - eligibleReductionAssets[asset] = totalAssets[asset]; + reductionAssets[asset] = totalAssets[asset]; if (asset in orderAssets) { - eligibleReductionAssets[asset] -= orderAssets[asset]; + reductionAssets[asset] -= orderAssets[asset]; } } } return { batcherFee: getReducedBatcherFee( + standardFee, activeBatcherFeeConfig, - eligibleReductionAssets + reductionAssets ), reductionAssets: reductionAssets, }; diff --git a/src/batcher-fee-reduction/configs.internal.ts b/src/batcher-fee-reduction/configs.internal.ts index eaab8b0..d522948 100644 --- a/src/batcher-fee-reduction/configs.internal.ts +++ b/src/batcher-fee-reduction/configs.internal.ts @@ -33,17 +33,19 @@ export function getActiveBatcherFee( } export function getReducedBatcherFee( + standardFee: bigint, activeReductionConfig: BatcherFeeReductionConfig, reductionAssets: Assets ): bigint { - const totalReductionAmountRatio = new BigNumber(0); + let totalReductionAmountRatio = new BigNumber(0); const { assets, minFee } = activeReductionConfig; for (const { asset, maximumAmount } of assets) { if (asset in reductionAssets) { const reductionAmount = new BigNumber( reductionAssets[asset].toString() ).div(maximumAmount.toString()); - totalReductionAmountRatio.plus(reductionAmount); + totalReductionAmountRatio = + totalReductionAmountRatio.plus(reductionAmount); } } @@ -53,9 +55,9 @@ export function getReducedBatcherFee( ? new BigNumber(1) : totalReductionAmountRatio; - const maximumReduction = new BigNumber(FIXED_BATCHER_FEE.toString()) + const maximumReduction = new BigNumber(standardFee.toString()) .minus(minFee.toString()) - .div(FIXED_BATCHER_FEE.toString()) + .div(standardFee.toString()) .multipliedBy(100); // Apply the ratio to calculate batcher fee reduction @@ -63,10 +65,10 @@ export function getReducedBatcherFee( .multipliedBy(maximumReductionAmountRatio) .div(100); - // New batcher fee = (1 - reduction) * DEFAULT BATCHER FEE + // New batcher fee = (1 - reduction) * standardFee const finalFee = new BigNumber(1) .minus(totalReduction) - .multipliedBy(new BigNumber(FIXED_BATCHER_FEE.toString())) + .multipliedBy(new BigNumber(standardFee.toString())) .toFixed(0); return BigInt(finalFee); } @@ -159,12 +161,12 @@ export const BATCHER_FEE_CONFIG: Record< assets: [ { asset: - "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed72.4d494e", // MIN + "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed724d494e", // MIN maximumAmount: 10_000_000n, }, { asset: - "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d86.3bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP + "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d863bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP maximumAmount: 100_000_000n, }, ], @@ -176,17 +178,17 @@ export const BATCHER_FEE_CONFIG: Record< assets: [ { asset: - "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed72.4d494e", // MIN + "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed724d494e", // MIN maximumAmount: 10_000_000n, }, { asset: - "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d86.3bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP + "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d863bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP maximumAmount: 100_000_000n, }, { asset: - "d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0b.6c3ea488e6ff940bb6fb1b18fd605b5931d9fefde6440117015ba484cf321200", // ADA-MIN LP V2 + "d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0b6c3ea488e6ff940bb6fb1b18fd605b5931d9fefde6440117015ba484cf321200", // ADA-MIN LP V2 maximumAmount: 100_000_000n, }, ], @@ -203,13 +205,13 @@ export const BATCHER_FEE_CONFIG: Record< assets: [ { asset: - "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed72.4d494e", + "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed724d494e", // MIN maximumAmount: 10_000_000n, }, { asset: - "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d86.3bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", + "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d863bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP maximumAmount: 100_000_000n, }, @@ -222,17 +224,17 @@ export const BATCHER_FEE_CONFIG: Record< assets: [ { asset: - "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed72.4d494e", // MIN + "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed724d494e", // MIN maximumAmount: 10_000_000n, }, { asset: - "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d86.3bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP + "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d863bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP maximumAmount: 100_000_000n, }, { asset: - "d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0b.6c3ea488e6ff940bb6fb1b18fd605b5931d9fefde6440117015ba484cf321200", // ADA-MIN LP V2 + "d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0b6c3ea488e6ff940bb6fb1b18fd605b5931d9fefde6440117015ba484cf321200", // ADA-MIN LP V2 maximumAmount: 100_000_000n, }, ], @@ -249,12 +251,12 @@ export const BATCHER_FEE_CONFIG: Record< assets: [ { asset: - "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed72.4d494e", // MIN + "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed724d494e", // MIN maximumAmount: 10_000_000n, }, { asset: - "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d86.3bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP + "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d863bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP maximumAmount: 100_000_000n, }, ], @@ -266,17 +268,17 @@ export const BATCHER_FEE_CONFIG: Record< assets: [ { asset: - "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed72.4d494e", // MIN + "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed724d494e", // MIN maximumAmount: 10_000_000n, }, { asset: - "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d86.3bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP + "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d863bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP maximumAmount: 100_000_000n, }, { asset: - "d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0b.6c3ea488e6ff940bb6fb1b18fd605b5931d9fefde6440117015ba484cf321200", // ADA-MIN LP V2 + "d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0b6c3ea488e6ff940bb6fb1b18fd605b5931d9fefde6440117015ba484cf321200", // ADA-MIN LP V2 maximumAmount: 100_000_000n, }, ], @@ -295,12 +297,12 @@ export const BATCHER_FEE_CONFIG: Record< assets: [ { asset: - "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed72.4d494e", // MIN + "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed724d494e", // MIN maximumAmount: 10_000_000n, }, { asset: - "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d86.3bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP + "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d863bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP maximumAmount: 100_000_000n, }, ], @@ -317,12 +319,12 @@ export const BATCHER_FEE_CONFIG: Record< assets: [ { asset: - "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed72.4d494e", // MIN + "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed724d494e", // MIN maximumAmount: 10_000_000n, }, { asset: - "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d86.3bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP + "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d863bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP maximumAmount: 100_000_000n, }, ], @@ -339,12 +341,12 @@ export const BATCHER_FEE_CONFIG: Record< assets: [ { asset: - "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed72.4d494e", // MIN + "e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed724d494e", // MIN maximumAmount: 10_000_000n, }, { asset: - "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d86.3bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP + "e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d863bb0079303c57812462dec9de8fb867cef8fd3768de7f12c77f6f0dd80381d0d", // ADA-MIN LP maximumAmount: 100_000_000n, }, ], diff --git a/src/dex-v2.ts b/src/dex-v2.ts index 2890fe7..3ff10c1 100644 --- a/src/dex-v2.ts +++ b/src/dex-v2.ts @@ -912,7 +912,9 @@ export class DexV2 { msg: [metadata], limitOrders: limitOrderMessage, }); - lucidTx.payToAddress(sender, reductionAssets); + if (Object.keys(reductionAssets).length !== 0) { + lucidTx.payToAddress(sender, reductionAssets); + } if (composeTx) { lucidTx.compose(composeTx); } diff --git a/src/dex.ts b/src/dex.ts index 54512cf..95e8801 100644 --- a/src/dex.ts +++ b/src/dex.ts @@ -185,8 +185,10 @@ export class Dex { Data.to(OrderV1.Datum.toPlutusData(datum)), orderAssets ) - .payToAddress(sender, reductionAssets) .addSigner(sender); + if (Object.keys(reductionAssets).length !== 0) { + tx.payToAddress(sender, reductionAssets); + } if (isLimitOrder) { tx.attachMetadata(674, { msg: [MetadataMessage.SWAP_EXACT_IN_LIMIT_ORDER],