Skip to content

Commit

Permalink
Merge pull request #657 from OasisDEX:kk/sc-14965/fx-add-swap-to-simu…
Browse files Browse the repository at this point in the history
…lation

chore: add swap to erc4626 simulations
  • Loading branch information
halaprix authored Mar 21, 2024
2 parents 93d5fb5 + 257e3d6 commit fdf41c8
Show file tree
Hide file tree
Showing 4 changed files with 35 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",
"version": "0.6.44",
"typings": "lib/index.d.ts",
"types": "lib/index.d.ts",
"main": "lib/index.js",
Expand Down
15 changes: 13 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 @@ -79,9 +79,19 @@ export const deposit: Erc4626DepositStrategy = async (args, dependencies) => {

const errors = [...validateMaxDeposit(depositAmount, position)]

const swap = {
fromTokenAddress: args.pullTokenAddress,
toTokenAddress: args.depositTokenAddress,
fromTokenAmount: amountToWei(args.amount, args.pullTokenPrecision),
toTokenAmount: swapData.toTokenAmount,
minToTokenAmount: swapData.minToTokenAmount,
tokenFee: tokenFee,
collectFeeFrom: collectFeeFrom,
exchangeCalldata: swapData.exchangeCalldata,
}
return {
simulation: {
swaps: [],
swaps: [swap],
errors,
warnings,
notices: [],
Expand Down Expand Up @@ -161,6 +171,7 @@ async function getSwapData(args: Erc4626DepositPayload, dependencies: Erc4626Com
slippage: args.slippage,
swapAmountBeforeFees: swapAmountBeforeFees,
getSwapData: dependencies.getSwapData,
// TODO: use fee resolver with low correlated fee
__feeOverride: isCorrelatedPosition(args.pullTokenSymbol, args.depositTokenSymbol)
? new BigNumber(2)
: new BigNumber(20),
Expand Down
14 changes: 12 additions & 2 deletions packages/dma-library/src/strategies/common/erc4626/withdraw.ts
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 @@ -86,9 +86,19 @@ export const withdraw: Erc4626WithdrawStrategy = async (args, dependencies) => {

const errors = [...validateMaxWithdraw(args.amount, position)]

const swap = {
fromTokenAddress: args.withdrawTokenAddress,
toTokenAddress: args.returnTokenAddress,
fromTokenAmount: amountToWei(args.amount, args.withdrawTokenPrecision),
toTokenAmount: swapData.toTokenAmount,
minToTokenAmount: swapData.minToTokenAmount,
exchangeCalldata: swapData.exchangeCalldata,
tokenFee: tokenFee,
collectFeeFrom: collectFeeFrom,
}
return {
simulation: {
swaps: [],
swaps: [swap],
errors,
warnings,
notices: [],
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 fdf41c8

Please sign in to comment.