Skip to content

Commit

Permalink
chore: update fee calcs
Browse files Browse the repository at this point in the history
  • Loading branch information
halaprix committed Mar 21, 2024
1 parent 183fa8f commit 706dc1e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/dma-library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oasisdex/dma-library",
"version": "0.6.43-erc4626-swap",
"version": "0.6.43-erc4626-swap.2",
"typings": "lib/index.d.ts",
"types": "lib/index.d.ts",
"main": "lib/index.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/dma-library/src/strategies/common/erc4626/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const deposit: Erc4626DepositStrategy = async (args, dependencies) => {
const isSwapping = args.depositTokenAddress.toLowerCase() !== args.pullTokenAddress.toLowerCase()

if (isSwapping) {
const { swapData, collectFeeFrom, fee } = await getSwapData(args, dependencies)
const { swapData, collectFeeFrom, fee, tokenFee } = await getSwapData(args, dependencies)
const operation = await operations.erc4626Operations.deposit(
{
vault: args.vault,
Expand Down Expand Up @@ -85,7 +85,7 @@ export const deposit: Erc4626DepositStrategy = async (args, dependencies) => {
fromTokenAmount: amountToWei(args.amount, args.pullTokenPrecision),
toTokenAmount: swapData.toTokenAmount,
minToTokenAmount: swapData.minToTokenAmount,
tokenFee: fee,
tokenFee: tokenFee,
collectFeeFrom: collectFeeFrom,
exchangeCalldata: swapData.exchangeCalldata,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const withdraw: Erc4626WithdrawStrategy = async (args, dependencies) => {
const isClose = args.amount.isGreaterThan(position.quoteTokenAmount)

if (isSwapping) {
const { swapData, collectFeeFrom, fee } = await getSwapData(
const { swapData, collectFeeFrom, fee, tokenFee } = await getSwapData(
{ ...args, amount: isClose ? position.quoteTokenAmount : args.amount },
dependencies,
)
Expand Down Expand Up @@ -93,7 +93,7 @@ export const withdraw: Erc4626WithdrawStrategy = async (args, dependencies) => {
toTokenAmount: swapData.toTokenAmount,
minToTokenAmount: swapData.minToTokenAmount,
exchangeCalldata: swapData.exchangeCalldata,
tokenFee: fee,
tokenFee: tokenFee,
collectFeeFrom: collectFeeFrom,
}
return {
Expand Down
11 changes: 9 additions & 2 deletions packages/dma-library/src/strategies/common/generic-swap-data.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Address } from '@deploy-configurations/types/address'
import { FEE_ESTIMATE_INFLATOR } from '@dma-common/constants'
import { calculateFee } from '@dma-common/utils/swap'
import { GetSwapData } from '@dma-library/types/common'
import * as SwapUtils from '@dma-library/utils/swap'
import BigNumber from 'bignumber.js'

import { ZERO } from '../../../../dma-common/constants/numbers'
import { ONE, ZERO } from '../../../../dma-common/constants/numbers'

interface GetGenericSwapDataArgs {
fromToken: {
Expand Down Expand Up @@ -40,6 +41,7 @@ export async function getGenericSwapData({

const preSwapFee =
collectFeeFrom === 'sourceToken' ? calculateFee(swapAmountBeforeFees, fee.toNumber()) : ZERO

const swapAmountAfterFees = swapAmountBeforeFees
.minus(preSwapFee)
.integerValue(BigNumber.ROUND_DOWN)
Expand All @@ -50,6 +52,11 @@ export async function getGenericSwapData({
swapAmountAfterFees,
slippage,
)
const postSwapFee =
collectFeeFrom === 'targetToken' ? calculateFee(swapData.toTokenAmount, fee.toNumber()) : ZERO

return { swapData, collectFeeFrom, fee: fee.toString() }
const tokenFee = preSwapFee.plus(
postSwapFee.times(ONE.plus(FEE_ESTIMATE_INFLATOR)).integerValue(BigNumber.ROUND_DOWN),
)
return { swapData, collectFeeFrom, fee: fee.toString(), tokenFee }
}

0 comments on commit 706dc1e

Please sign in to comment.