Skip to content

Commit

Permalink
fix batcher fee reduction (#41)
Browse files Browse the repository at this point in the history
* fix batcher fee reduction

* fix calculateBatcherFee

* fix reductionAssets
  • Loading branch information
m1n999999 authored Sep 18, 2024
1 parent 00b270d commit 0733029
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 35 deletions.
13 changes: 7 additions & 6 deletions src/batcher-fee-reduction/calculate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
};
}
Expand All @@ -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,
};
Expand Down
56 changes: 29 additions & 27 deletions src/batcher-fee-reduction/configs.internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -53,20 +55,20 @@ 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
const totalReduction = new BigNumber(maximumReduction)
.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);
}
Expand Down Expand Up @@ -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,
},
],
Expand All @@ -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,
},
],
Expand All @@ -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,
},
Expand All @@ -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,
},
],
Expand All @@ -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,
},
],
Expand All @@ -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,
},
],
Expand All @@ -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,
},
],
Expand All @@ -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,
},
],
Expand All @@ -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,
},
],
Expand Down
4 changes: 3 additions & 1 deletion src/dex-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/dex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit 0733029

Please sign in to comment.