diff --git a/packages/addresses/package.json b/packages/addresses/package.json index a9efcad9b..27ae230a1 100644 --- a/packages/addresses/package.json +++ b/packages/addresses/package.json @@ -1,6 +1,6 @@ { "name": "@oasisdex/addresses", - "version": "0.0.38-alpha.2", + "version": "0.0.39", "typings": "lib/index.d.ts", "types": "lib/index.d.ts", "main": "lib/index.js", diff --git a/packages/deploy-configurations/configs/goerli.conf.ts b/packages/deploy-configurations/configs/goerli.conf.ts index bcca34e79..954413d4b 100644 --- a/packages/deploy-configurations/configs/goerli.conf.ts +++ b/packages/deploy-configurations/configs/goerli.conf.ts @@ -1064,7 +1064,7 @@ export const config: SystemConfig = { }, ERC20PoolFactory: { name: 'ERC20PoolFactory', - address: '0x0000000000000000000000000000000000000000', + address: '0x01Da8a85A5B525D476cA2b51e44fe7087fFafaFF', serviceRegistryName: SERVICE_REGISTRY_NAMES.ajna.ERC20_POOL_FACTORY, }, }, diff --git a/packages/deploy-configurations/configs/optimism.conf.ts b/packages/deploy-configurations/configs/optimism.conf.ts index 3a0853643..6c16c9f7d 100644 --- a/packages/deploy-configurations/configs/optimism.conf.ts +++ b/packages/deploy-configurations/configs/optimism.conf.ts @@ -262,7 +262,7 @@ export const config: SystemConfig = { }, MerkleRedeemer: { name: 'MerkleRedeemer', - address: ADDRESS_ZERO, + address: '0x34792F2481a3e2ADAD4d0000D8D202814090eFb5', }, DssCharter: { name: 'DssCharter', diff --git a/packages/dma-common/utils/swap/calculate-fee.ts b/packages/dma-common/utils/swap/calculate-fee.ts index 41ab06ab3..6b81e659d 100644 --- a/packages/dma-common/utils/swap/calculate-fee.ts +++ b/packages/dma-common/utils/swap/calculate-fee.ts @@ -4,7 +4,7 @@ import BigNumber from 'bignumber.js' export function calculateFee(amountWei: BigNumber, fee: number = DEFAULT_FEE): BigNumber { return amountWei .times(fee) - .div(new BigNumber(fee).plus(FEE_BASE)) + .div(new BigNumber(fee).plus(new BigNumber(FEE_BASE))) .abs() .integerValue(BigNumber.ROUND_DOWN) } diff --git a/packages/dma-library/package.json b/packages/dma-library/package.json index 71d8764ad..46f300e8b 100644 --- a/packages/dma-library/package.json +++ b/packages/dma-library/package.json @@ -1,6 +1,6 @@ { "name": "@oasisdex/dma-library", - "version": "0.4.6-alpha.2", + "version": "0.4.7", "typings": "lib/index.d.ts", "types": "lib/index.d.ts", "main": "lib/index.js", diff --git a/packages/dma-library/src/strategies/common/close-to-coll-swap-data.ts b/packages/dma-library/src/strategies/common/close-to-coll-swap-data.ts index 2777dbc16..c25dfd47f 100644 --- a/packages/dma-library/src/strategies/common/close-to-coll-swap-data.ts +++ b/packages/dma-library/src/strategies/common/close-to-coll-swap-data.ts @@ -102,20 +102,15 @@ export async function getSwapDataForCloseToCollateral({ // 4. Get Swap Data // This is the actual swap data that will be used in the transaction. + // We're inflating the needed collateral by the fee amount + // This is for when fees is collected on the other end of the swap const amountNeededToEnsureRemainingDebtIsRepaid = calculateNeededCollateralToPaybackDebt( debtPrice, debtTokenPrecision, colPrice, collateralTokenPrecision, outstandingDebt, - fee.div(FEE_BASE), - slippage, - ) - - const swapData = await getSwapData( - collateralToken.address, - debtToken.address, - amountNeededToEnsureRemainingDebtIsRepaid, + fee.div(new BigNumber(FEE_BASE).plus(fee)), slippage, ) @@ -129,6 +124,15 @@ export async function getSwapDataForCloseToCollateral({ ? calculateFee(amountNeededToEnsureRemainingDebtIsRepaid, fee.toNumber()) : ZERO + // 5. Get Swap Data + // The swap amount needs to be the collateral needed minus the preSwapFee + const swapData = await getSwapData( + collateralToken.address, + debtToken.address, + amountNeededToEnsureRemainingDebtIsRepaid.minus(preSwapFee), + slippage, + ) + return { swapData, collectFeeFrom, diff --git a/packages/dma-library/src/types/ajna/ajna-earn-position.ts b/packages/dma-library/src/types/ajna/ajna-earn-position.ts index bad4967a3..8b8dac982 100644 --- a/packages/dma-library/src/types/ajna/ajna-earn-position.ts +++ b/packages/dma-library/src/types/ajna/ajna-earn-position.ts @@ -53,8 +53,11 @@ export class AjnaEarnPosition implements IAjnaEarn { public quotePrice: BigNumber, public rewards: BigNumber, public netValue: BigNumber, - public pnl: BigNumber, - public totalEarnings: BigNumber, + public pnl: { + withFees: BigNumber + withoutFees: BigNumber + }, + public totalEarnings: { withFees: BigNumber; withoutFees: BigNumber }, ) { this.fundsLockedUntil = Date.now() + 5 * 60 * 60 * 1000 // MOCK funds locked until 5h from now this.price = priceIndex ? priceIndexToPrice(priceIndex) : ZERO diff --git a/packages/dma-library/src/views/ajna/index.ts b/packages/dma-library/src/views/ajna/index.ts index 7bf161338..6064f6623 100644 --- a/packages/dma-library/src/views/ajna/index.ts +++ b/packages/dma-library/src/views/ajna/index.ts @@ -18,9 +18,9 @@ interface EarnData { lps: BigNumber priceIndex: BigNumber | null nftID: string | null - cumulativeDeposit: BigNumber - cumulativeFees: BigNumber - cumulativeWithdraw: BigNumber + earnCumulativeFeesInQuoteToken: BigNumber + earnCumulativeQuoteTokenDeposit: BigNumber + earnCumulativeQuoteTokenWithdraw: BigNumber } export interface GetEarnData { @@ -79,7 +79,14 @@ export async function getEarnPosition( const rewardsManager = new ethers.Contract(rewardsManagerAddress, rewardsManagerAbi, provider) const [pool, earnData] = await Promise.all([getPoolData(poolAddress), getEarnData(proxyAddress)]) - const { cumulativeDeposit, cumulativeFees, cumulativeWithdraw, lps, nftID, priceIndex } = earnData + const { + lps, + nftID, + priceIndex, + earnCumulativeFeesInQuoteToken, + earnCumulativeQuoteTokenDeposit, + earnCumulativeQuoteTokenWithdraw, + } = earnData const quoteTokenAmount: BigNumber = lps.eq(ZERO) || priceIndex == null @@ -109,14 +116,28 @@ export async function getEarnPosition( : ZERO const netValue = quoteTokenAmount.times(quotePrice) - const pnl = cumulativeWithdraw - .plus(netValue) - .minus(cumulativeFees) - .minus(cumulativeDeposit) - .div(cumulativeDeposit) - const totalEarnings = netValue - .minus(cumulativeDeposit.minus(cumulativeWithdraw).plus(cumulativeFees)) - .div(quotePrice) + + const pnl = { + withFees: earnCumulativeQuoteTokenWithdraw + .plus(quoteTokenAmount) + .minus(earnCumulativeFeesInQuoteToken) + .minus(earnCumulativeQuoteTokenDeposit) + .div(earnCumulativeQuoteTokenDeposit), + withoutFees: earnCumulativeQuoteTokenWithdraw + .plus(quoteTokenAmount) + .minus(earnCumulativeQuoteTokenDeposit) + .div(earnCumulativeQuoteTokenDeposit), + } + const totalEarnings = { + withFees: quoteTokenAmount.minus( + earnCumulativeQuoteTokenDeposit + .minus(earnCumulativeQuoteTokenWithdraw) + .plus(earnCumulativeFeesInQuoteToken), + ), + withoutFees: quoteTokenAmount.minus( + earnCumulativeQuoteTokenDeposit.minus(earnCumulativeQuoteTokenWithdraw), + ), + } return new AjnaEarnPosition( pool,