-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor Batcher Fee reduction (#43)
* refactor Batcher Fee reduction * update docs * env for testnet --------- Co-authored-by: Mingggggg <[email protected]> Co-authored-by: Ha Quang Minh <[email protected]>
- Loading branch information
1 parent
425261a
commit 0df20dd
Showing
8 changed files
with
662 additions
and
425 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,114 @@ | ||
import BigNumber from "bignumber.js"; | ||
import { Assets, UTxO } from "lucid-cardano"; | ||
|
||
import { NetworkEnvironment } from "../types/network"; | ||
import { | ||
BATCHER_FEE_CONFIG, | ||
getActiveBatcherFee, | ||
getReducedBatcherFee, | ||
BatcherFeeConfig, | ||
BatcherFeeReductionConfig, | ||
DexVersion, | ||
} from "./configs.internal"; | ||
import { DexVersion } from "./types.internal"; | ||
|
||
export function calculateBatcherFee({ | ||
utxos, | ||
orderAssets, | ||
dexVersion, | ||
networkEnv, | ||
}: { | ||
dexVersion: DexVersion; | ||
utxos: UTxO[]; | ||
orderAssets: Assets; | ||
networkEnv: NetworkEnvironment; | ||
}): { | ||
batcherFee: bigint; | ||
reductionAssets: Assets; | ||
} { | ||
const reductionAssets: Assets = {}; | ||
const standardFee = BATCHER_FEE_CONFIG[networkEnv][dexVersion].standardFee; | ||
const activeBatcherFeeConfig = getActiveBatcherFee(networkEnv, dexVersion); | ||
if (!activeBatcherFeeConfig) { | ||
return { | ||
batcherFee: standardFee, | ||
reductionAssets, | ||
}; | ||
} | ||
const totalAssets: Assets = {}; | ||
for (const u of utxos) { | ||
for (const [asset, amount] of Object.entries(u.assets)) { | ||
if (asset in totalAssets) { | ||
totalAssets[asset] += amount; | ||
} else { | ||
totalAssets[asset] = amount; | ||
|
||
export namespace BatcherFee { | ||
export function getFinalFee({ | ||
config, | ||
reductionAssets, | ||
}: { | ||
config: BatcherFeeReductionConfig; | ||
reductionAssets: Assets; | ||
}): bigint { | ||
if (Object.keys(reductionAssets).length === 0) { | ||
return config.maxFee; | ||
} | ||
const { assets, minFee } = config; | ||
// Calculate total amount ratio through each reduction asset | ||
// The ratio is: current amount in wallet / maximum amount | ||
let totalReductionAmountRatio = new BigNumber(0); | ||
for (const { asset, maximumAmount } of assets) { | ||
if (asset in reductionAssets) { | ||
const reductionAmount = new BigNumber(reductionAssets[asset].toString()) | ||
.div(maximumAmount.toString()); | ||
totalReductionAmountRatio = | ||
totalReductionAmountRatio.plus(reductionAmount); | ||
} | ||
} | ||
|
||
// Maximum ratio is 1 | ||
const maximumReductionAmountRatio = totalReductionAmountRatio.isGreaterThanOrEqualTo(new BigNumber(1)) | ||
? new BigNumber(1) | ||
: totalReductionAmountRatio; | ||
|
||
const maximumReduction = new BigNumber(config.maxFee.toString()) | ||
.minus(minFee.toString()) | ||
.div(config.maxFee.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 | ||
const finalFee = new BigNumber(1) | ||
.minus(totalReduction) | ||
.multipliedBy(new BigNumber(config.maxFee.toString())) | ||
.toFixed(0); | ||
|
||
return BigInt(finalFee); | ||
} | ||
for (const { asset } of activeBatcherFeeConfig.assets) { | ||
if (asset in totalAssets) { | ||
reductionAssets[asset] = totalAssets[asset]; | ||
if (asset in orderAssets) { | ||
reductionAssets[asset] -= orderAssets[asset]; | ||
|
||
export function finalizeFee({ | ||
networkEnv, | ||
currentDate, | ||
dexVersion, | ||
utxos, | ||
orderAssets | ||
}: { | ||
networkEnv: NetworkEnvironment; | ||
currentDate: Date, | ||
dexVersion: DexVersion; | ||
utxos: UTxO[]; | ||
orderAssets: Assets; | ||
}): { | ||
batcherFee: bigint; | ||
reductionAssets: Assets; | ||
} { | ||
const defaultFee = BatcherFeeConfig.CONFIG[networkEnv][dexVersion].defaultFee; | ||
const activeReductionConfig = BatcherFeeConfig.getActiveConfig({ | ||
networkEnv, | ||
dexVersion, | ||
currentDate | ||
}); | ||
|
||
if (activeReductionConfig === undefined) { | ||
return { | ||
batcherFee: defaultFee, | ||
reductionAssets: {} | ||
} | ||
} | ||
|
||
const reductionAssets: Assets = {}; | ||
const totalAssets: Assets = {}; | ||
for (const u of utxos) { | ||
for (const [asset, amount] of Object.entries(u.assets)) { | ||
if (asset in totalAssets) { | ||
totalAssets[asset] += amount; | ||
} else { | ||
totalAssets[asset] = amount; | ||
} | ||
} | ||
} | ||
for (const { asset } of activeReductionConfig.assets) { | ||
if (asset in totalAssets) { | ||
reductionAssets[asset] = totalAssets[asset]; | ||
if (asset in orderAssets) { | ||
reductionAssets[asset] -= orderAssets[asset]; | ||
} | ||
} | ||
} | ||
return { | ||
batcherFee: getFinalFee({ | ||
config: activeReductionConfig, | ||
reductionAssets: reductionAssets | ||
}), | ||
reductionAssets: reductionAssets, | ||
}; | ||
} | ||
return { | ||
batcherFee: getReducedBatcherFee( | ||
standardFee, | ||
activeBatcherFeeConfig, | ||
reductionAssets | ||
), | ||
reductionAssets: reductionAssets, | ||
}; | ||
} | ||
} |
Oops, something went wrong.