From 183fa8ff620c31eafe94122bd8c78f22da0c74d4 Mon Sep 17 00:00:00 2001 From: halaprix Date: Thu, 21 Mar 2024 16:50:32 +0100 Subject: [PATCH 1/3] chore: add swap to erc4626 simulations --- packages/dma-library/package.json | 2 +- .../src/strategies/common/erc4626/deposit.ts | 13 ++++++++++++- .../src/strategies/common/erc4626/withdraw.ts | 12 +++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/dma-library/package.json b/packages/dma-library/package.json index 64b70ac49..cb80b3523 100644 --- a/packages/dma-library/package.json +++ b/packages/dma-library/package.json @@ -1,6 +1,6 @@ { "name": "@oasisdex/dma-library", - "version": "0.6.43", + "version": "0.6.43-erc4626-swap", "typings": "lib/index.d.ts", "types": "lib/index.d.ts", "main": "lib/index.js", diff --git a/packages/dma-library/src/strategies/common/erc4626/deposit.ts b/packages/dma-library/src/strategies/common/erc4626/deposit.ts index b066edf66..52408a2be 100644 --- a/packages/dma-library/src/strategies/common/erc4626/deposit.ts +++ b/packages/dma-library/src/strategies/common/erc4626/deposit.ts @@ -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: fee, + collectFeeFrom: collectFeeFrom, + exchangeCalldata: swapData.exchangeCalldata, + } return { simulation: { - swaps: [], + swaps: [swap], errors, warnings, notices: [], @@ -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), diff --git a/packages/dma-library/src/strategies/common/erc4626/withdraw.ts b/packages/dma-library/src/strategies/common/erc4626/withdraw.ts index f58080953..dfce673e9 100644 --- a/packages/dma-library/src/strategies/common/erc4626/withdraw.ts +++ b/packages/dma-library/src/strategies/common/erc4626/withdraw.ts @@ -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: fee, + collectFeeFrom: collectFeeFrom, + } return { simulation: { - swaps: [], + swaps: [swap], errors, warnings, notices: [], From 706dc1e543080a7b53fa3b156a4c7994b7ca0d9b Mon Sep 17 00:00:00 2001 From: halaprix Date: Thu, 21 Mar 2024 17:11:42 +0100 Subject: [PATCH 2/3] chore: update fee calcs --- packages/dma-library/package.json | 2 +- .../src/strategies/common/erc4626/deposit.ts | 4 ++-- .../src/strategies/common/erc4626/withdraw.ts | 4 ++-- .../src/strategies/common/generic-swap-data.ts | 11 +++++++++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/dma-library/package.json b/packages/dma-library/package.json index cb80b3523..0dff71945 100644 --- a/packages/dma-library/package.json +++ b/packages/dma-library/package.json @@ -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", diff --git a/packages/dma-library/src/strategies/common/erc4626/deposit.ts b/packages/dma-library/src/strategies/common/erc4626/deposit.ts index 52408a2be..a333a506b 100644 --- a/packages/dma-library/src/strategies/common/erc4626/deposit.ts +++ b/packages/dma-library/src/strategies/common/erc4626/deposit.ts @@ -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, @@ -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, } diff --git a/packages/dma-library/src/strategies/common/erc4626/withdraw.ts b/packages/dma-library/src/strategies/common/erc4626/withdraw.ts index dfce673e9..847af0936 100644 --- a/packages/dma-library/src/strategies/common/erc4626/withdraw.ts +++ b/packages/dma-library/src/strategies/common/erc4626/withdraw.ts @@ -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, ) @@ -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 { diff --git a/packages/dma-library/src/strategies/common/generic-swap-data.ts b/packages/dma-library/src/strategies/common/generic-swap-data.ts index d1d1f0687..25e8f8b16 100644 --- a/packages/dma-library/src/strategies/common/generic-swap-data.ts +++ b/packages/dma-library/src/strategies/common/generic-swap-data.ts @@ -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: { @@ -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) @@ -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 } } From 257e3d6b1275a19f328afb6f2efbb9a36788da34 Mon Sep 17 00:00:00 2001 From: halaprix Date: Thu, 21 Mar 2024 17:14:01 +0100 Subject: [PATCH 3/3] chore: bump package --- packages/dma-library/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dma-library/package.json b/packages/dma-library/package.json index 0dff71945..b468eb860 100644 --- a/packages/dma-library/package.json +++ b/packages/dma-library/package.json @@ -1,6 +1,6 @@ { "name": "@oasisdex/dma-library", - "version": "0.6.43-erc4626-swap.2", + "version": "0.6.44", "typings": "lib/index.d.ts", "types": "lib/index.d.ts", "main": "lib/index.js",