Skip to content

Commit

Permalink
Added new swap contract integration and fixed fee utilities
Browse files Browse the repository at this point in the history
Also refactored fee resolver to make decision logic which fee to use based on asset correlation
  • Loading branch information
piotrwitek committed Oct 23, 2024
1 parent 6149dbe commit 78856d1
Show file tree
Hide file tree
Showing 66 changed files with 12,702 additions and 440 deletions.
3 changes: 3 additions & 0 deletions packages/deploy-configurations/configs/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Network } from '@deploy-configurations/types/network'
import { config as arbitrumConfig } from '../arbitrum.conf'
import { config as baseConfig } from '../base.conf'
import { config as goerliConfig } from '../goerli.conf'
import { config as hardhatConfig } from '../hardhat.conf'
import { config as localConfig } from '../local.conf'
import { config as mainnetConfig } from '../mainnet.conf'
import { config as optimismConfig } from '../optimism.conf'
Expand All @@ -24,6 +25,8 @@ export function getConfigByNetwork(network: Network): SystemConfig {
return localConfig
} else if (network === Network.TENDERLY) {
return tenderlyConfig
} else if (network === Network.HARDHAT) {
return hardhatConfig
} else {
throw new Error(`Unknown network ${network}`)
}
Expand Down
136 changes: 68 additions & 68 deletions packages/deploy-configurations/constants/operation-names.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { Protocol } from '@deploy-configurations/types/protocol'

export type AaveV3OperationNames =
| 'OpenAAVEV3Position_v2'
| 'CloseAAVEV3Position_v4'
| 'AdjustRiskUpAAVEV3Position_v2'
| 'AdjustRiskDownAAVEV3Position_v2'
| 'AAVEV3DepositBorrow_v2'
| 'AAVEV3OpenDepositBorrow_v2'
| 'AAVEV3Deposit'
| 'AAVEV3Borrow_v2'
| 'AAVEV3PaybackWithdraw_v2'
| 'MigrateAaveV3EOA_v2'
| 'OpenAAVEV3Position_v3'
| 'CloseAAVEV3Position_v5'
| 'AdjustRiskUpAAVEV3Position_v3'
| 'AdjustRiskDownAAVEV3Position_v3'
| 'AAVEV3DepositBorrow_v3'
| 'AAVEV3OpenDepositBorrow_v3'
| 'AAVEV3Deposit_v2'
| 'AAVEV3Borrow_v3'
| 'AAVEV3PaybackWithdraw_v3'
| 'MigrateAaveV3EOA_v3'

export type SparkOperationNames =
| 'SparkOpenPosition_v2'
| 'SparkClosePosition_v2'
| 'SparkAdjustRiskUp_v2'
| 'SparkAdjustRiskDown_v2'
| 'SparkDepositBorrow_v2'
| 'SparkOpenDepositBorrow_v2'
| 'SparkDeposit'
| 'SparkBorrow_v2'
| 'SparkPaybackWithdraw_v2'
| 'MigrateSparkEOA_v2'
| 'SparkOpenPosition_v3'
| 'SparkClosePosition_v3'
| 'SparkAdjustRiskUp_v3'
| 'SparkAdjustRiskDown_v3'
| 'SparkDepositBorrow_v3'
| 'SparkOpenDepositBorrow_v3'
| 'SparkDeposit_v2'
| 'SparkBorrow_v3'
| 'SparkPaybackWithdraw_v3'
| 'MigrateSparkEOA_v3'

export const OPERATION_NAMES = {
aave: {
Expand All @@ -38,66 +38,66 @@ export const OPERATION_NAMES = {
PAYBACK_WITHDRAW: 'AAVEPaybackWithdraw_2',
},
v3: {
OPEN_POSITION: 'OpenAAVEV3Position_v2',
CLOSE_POSITION: 'CloseAAVEV3Position_v4',
ADJUST_RISK_UP: 'AdjustRiskUpAAVEV3Position_v2',
ADJUST_RISK_DOWN: 'AdjustRiskDownAAVEV3Position_v2',
DEPOSIT_BORROW: 'AAVEV3DepositBorrow_v2',
OPEN_DEPOSIT_BORROW: 'AAVEV3OpenDepositBorrow_v2',
DEPOSIT: 'AAVEV3Deposit',
BORROW: 'AAVEV3Borrow_v2',
PAYBACK_WITHDRAW: 'AAVEV3PaybackWithdraw_v2',
MIGRATE_EOA: 'MigrateAaveV3EOA_v2',
OPEN_POSITION: 'OpenAAVEV3Position_v3',
CLOSE_POSITION: 'CloseAAVEV3Position_v5',
ADJUST_RISK_UP: 'AdjustRiskUpAAVEV3Position_v3',
ADJUST_RISK_DOWN: 'AdjustRiskDownAAVEV3Position_v3',
DEPOSIT_BORROW: 'AAVEV3DepositBorrow_v3',
OPEN_DEPOSIT_BORROW: 'AAVEV3OpenDepositBorrow_v3',
DEPOSIT: 'AAVEV3Deposit_v2',
BORROW: 'AAVEV3Borrow_v3',
PAYBACK_WITHDRAW: 'AAVEV3PaybackWithdraw_v3',
MIGRATE_EOA: 'MigrateAaveV3EOA_v3',
},
},
spark: {
OPEN_POSITION: 'SparkOpenPosition_v2',
CLOSE_POSITION: 'SparkClosePosition_v2',
ADJUST_RISK_UP: 'SparkAdjustRiskUp_v2',
ADJUST_RISK_DOWN: 'SparkAdjustRiskDown_v2',
DEPOSIT_BORROW: 'SparkDepositBorrow_v2',
OPEN_DEPOSIT_BORROW: 'SparkOpenDepositBorrow_v2',
DEPOSIT: 'SparkDeposit',
BORROW: 'SparkBorrow_v2',
PAYBACK_WITHDRAW: 'SparkPaybackWithdraw_v2',
MIGRATE_EOA: 'MigrateSparkEOA_v2',
OPEN_POSITION: 'SparkOpenPosition_v3',
CLOSE_POSITION: 'SparkClosePosition_v3',
ADJUST_RISK_UP: 'SparkAdjustRiskUp_v3',
ADJUST_RISK_DOWN: 'SparkAdjustRiskDown_v3',
DEPOSIT_BORROW: 'SparkDepositBorrow_v3',
OPEN_DEPOSIT_BORROW: 'SparkOpenDepositBorrow_v3',
DEPOSIT: 'SparkDeposit_v2',
BORROW: 'SparkBorrow_v3',
PAYBACK_WITHDRAW: 'SparkPaybackWithdraw_v3',
MIGRATE_EOA: 'MigrateSparkEOA_v3',
},
maker: {
OPEN_AND_DRAW: 'OpenAndDraw',
OPEN_DRAW_AND_CLOSE: 'OpenDrawAndClose',
INCREASE_MULTIPLE: 'IncreaseMultiple',
INCREASE_MULTIPLE_WITH_DAI_TOP_UP: 'IncreaseMultipleWithDaiTopup',
INCREASE_MULTIPLE_WITH_COLL_TOP_UP: 'IncreaseMultipleWithCollateralTopup',
INCREASE_MULTIPLE_WITH_DAI_AND_COLL_TOP_UP: 'IncreaseMultipleWithDaiAndCollTopup',
INCREASE_MULTIPLE_WITH_FLASHLOAN: 'IncreaseMultipleWithFlashloan',
OPEN_AND_DRAW: 'OpenAndDraw_v2',
OPEN_DRAW_AND_CLOSE: 'OpenDrawAndClose_v2',
INCREASE_MULTIPLE: 'IncreaseMultiple_v2',
INCREASE_MULTIPLE_WITH_DAI_TOP_UP: 'IncreaseMultipleWithDaiTopup_v2',
INCREASE_MULTIPLE_WITH_COLL_TOP_UP: 'IncreaseMultipleWithCollateralTopup_v2',
INCREASE_MULTIPLE_WITH_DAI_AND_COLL_TOP_UP: 'IncreaseMultipleWithDaiAndCollTopup_v2',
INCREASE_MULTIPLE_WITH_FLASHLOAN: 'IncreaseMultipleWithFlashloan_v2',
INCREASE_MULTIPLE_WITH_FLASHLOAN_AND_DAI_AND_COLL_TOP_UP:
'IncreaseMultipleWithFlashloanWithDaiAndCollTopup',
'IncreaseMultipleWithFlashloanWithDaiAndCollTopup_v2',
},
ajna: {
OPEN_MULTIPLY_POSITION: 'AjnaOpenMultiplyPosition_5',
ADJUST_RISK_UP: 'AjnaAdjustRiskUp_5',
ADJUST_RISK_DOWN: 'AjnaAdjustRiskDown_5',
DEPOSIT_BORROW: 'AjnaDepositBorrow_5',
PAYBACK_WITHDRAW: 'AjnaPaybackWithdraw_5',
CLOSE_POSITION_TO_QUOTE: 'AjnaCloseToQuotePosition_5',
CLOSE_POSITION_TO_COLLATERAL: 'AjnaCloseToCollateralPosition_5',
OPEN_MULTIPLY_POSITION: 'AjnaOpenMultiplyPosition_6',
ADJUST_RISK_UP: 'AjnaAdjustRiskUp_6',
ADJUST_RISK_DOWN: 'AjnaAdjustRiskDown_6',
DEPOSIT_BORROW: 'AjnaDepositBorrow_6',
PAYBACK_WITHDRAW: 'AjnaPaybackWithdraw_6',
CLOSE_POSITION_TO_QUOTE: 'AjnaCloseToQuotePosition_6',
CLOSE_POSITION_TO_COLLATERAL: 'AjnaCloseToCollateralPosition_6',
},
morphoblue: {
OPEN_POSITION: 'MorphoBlueOpenPosition',
CLOSE_POSITION: 'MorphoBlueClosePosition_2',
ADJUST_RISK_UP: 'MorphoBlueAdjustRiskUp',
ADJUST_RISK_DOWN: 'MorphoBlueAdjustRiskDown_2',
DEPOSIT_BORROW: 'MorphoBlueDepositBorrow',
OPEN_DEPOSIT_BORROW: 'MorphoBlueOpenDepositBorrow',
DEPOSIT: 'MorphoBlueDeposit',
BORROW: 'MorphoBlueBorrow',
PAYBACK_WITHDRAW: 'MorphoBluePaybackWithdraw_2',
CLAIM_REWARDS: 'MorphoBlueClaimRewards',
OPEN_POSITION: 'MorphoBlueOpenPosition_2',
CLOSE_POSITION: 'MorphoBlueClosePosition_3',
ADJUST_RISK_UP: 'MorphoBlueAdjustRiskUp_2',
ADJUST_RISK_DOWN: 'MorphoBlueAdjustRiskDown_3',
DEPOSIT_BORROW: 'MorphoBlueDepositBorrow_2',
OPEN_DEPOSIT_BORROW: 'MorphoBlueOpenDepositBorrow_2',
DEPOSIT: 'MorphoBlueDeposit_2',
BORROW: 'MorphoBlueBorrow_2',
PAYBACK_WITHDRAW: 'MorphoBluePaybackWithdraw_3',
CLAIM_REWARDS: 'MorphoBlueClaimRewards_2',
},
common: {
CUSTOM_OPERATION: 'CustomOperation',
ERC4626_DEPOSIT: 'ERC4626Deposit',
ERC4626_WITHDRAW: 'ERC4626Withdraw',
CUSTOM_OPERATION: 'CustomOperation_2',
ERC4626_DEPOSIT: 'ERC4626Deposit_2',
ERC4626_WITHDRAW: 'ERC4626Withdraw_2',
},
} as const

Expand Down
12 changes: 10 additions & 2 deletions packages/dma-common/utils/swap/calculate-fee.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { DEFAULT_FEE, FEE_BASE } from '@dma-common/constants'
import BigNumber from 'bignumber.js'

export function calculateFee(amountWei: BigNumber, fee: number = DEFAULT_FEE): BigNumber {
export function calculatePercentageFee(amountWei: BigNumber, fee: number = DEFAULT_FEE): BigNumber {
return amountWei
.times(fee)
.div(new BigNumber(fee).plus(new BigNumber(FEE_BASE)))
.abs()
.integerValue(BigNumber.ROUND_DOWN)
}

export function calculateFeeOnInputAmount(
export function calculateFixedFee(amountWei: BigNumber, fee: string): BigNumber {
return amountWei.minus(fee).integerValue(BigNumber.ROUND_DOWN)
}

export function calculatePercentageFeeOnInputAmount(
amountWei: BigNumber,
fee: number = DEFAULT_FEE,
): BigNumber {
return amountWei.times(fee).div(new BigNumber(FEE_BASE)).abs().integerValue(BigNumber.ROUND_UP)
}

export function calculateFixedFeeOnInputAmount(amountWei: BigNumber, fee: string): BigNumber {
return amountWei.plus(fee).integerValue(BigNumber.ROUND_UP)
}
7 changes: 6 additions & 1 deletion packages/dma-common/utils/swap/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export { calculateFee, calculateFeeOnInputAmount } from './calculate-fee'
export {
calculateFixedFee,
calculateFixedFeeOnInputAmount,
calculatePercentageFee,
calculatePercentageFeeOnInputAmount,
} from './calculate-fee'
6 changes: 6 additions & 0 deletions packages/dma-contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,9 @@ inclusion of the tools in the `hardhat.config.ts` file:
//import './tasks/verify-deployment'
//import './tasks/verify-operations'
```

## Other npm scripts

e.g. verify operation configs

`yarn operations:showremote aave.v3 --network mainnet`
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { asPercentageValue, exchangeToDAI, expect } from '@dma-common/test-utils
import { FakeRequestEnv, RuntimeConfig } from '@dma-common/types/common'
import { balanceOf } from '@dma-common/utils/balances'
import { amountFromWei, amountToWei } from '@dma-common/utils/common'
import { calculateFeeOnInputAmount } from '@dma-common/utils/swap'
import { calculatePercentageFeeOnInputAmount } from '@dma-common/utils/swap'
import { testBlockNumber } from '@dma-contracts/test/config'
import { restoreSnapshot, TestHelpers } from '@dma-contracts/utils'
import { SwapFeeType } from '@dma-library/types'
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('Swap | Unit', async () => {
describe('Asset for DAI', async () => {
const assetAmount = new BigNumber(10)
const assetAmountInWei = amountToWei(assetAmount)
const feeAmount = calculateFeeOnInputAmount(assetAmountInWei)
const feeAmount = calculatePercentageFeeOnInputAmount(assetAmountInWei)
const assetAmountInWeiWithFee = assetAmountInWei.plus(feeAmount)
let receiveAtLeastInWei: BigNumber
let data: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { asPercentageValue, exchangeFromDAI, expect } from '@dma-common/test-uti
import { FakeRequestEnv, RuntimeConfig } from '@dma-common/types/common'
import { balanceOf } from '@dma-common/utils/balances'
import { amountFromWei, amountToWei } from '@dma-common/utils/common'
import { calculateFeeOnInputAmount } from '@dma-common/utils/swap'
import { calculatePercentageFeeOnInputAmount } from '@dma-common/utils/swap'
import { testBlockNumber } from '@dma-contracts/test/config'
import { restoreSnapshot, TestHelpers } from '@dma-contracts/utils'
import { SwapFeeType } from '@dma-library/types'
Expand Down Expand Up @@ -36,7 +36,7 @@ describe('Swap | Unit', async () => {
let data: string

const amountInWei = amountToWei(1000)
const feeAmount = calculateFeeOnInputAmount(amountInWei)
const feeAmount = calculatePercentageFeeOnInputAmount(amountInWei)
const amountWithFeeInWei = amountInWei.plus(feeAmount)

before(async () => {
Expand Down Expand Up @@ -185,7 +185,7 @@ describe('Swap | Unit', async () => {
surplusAmount = new BigNumber(10)

moreThanTheTransferAmountWei = amountInWei.plus(amountToWei(surplusAmount))
moreThanTheTransferAmountWithFee = calculateFeeOnInputAmount(
moreThanTheTransferAmountWithFee = calculatePercentageFeeOnInputAmount(
moreThanTheTransferAmountWei,
).plus(moreThanTheTransferAmountWei)

Expand Down Expand Up @@ -237,7 +237,7 @@ describe('Swap | Unit', async () => {
),
)

const collectedFeeWei = calculateFeeOnInputAmount(moreThanTheTransferAmountWei)
const collectedFeeWei = calculatePercentageFeeOnInputAmount(moreThanTheTransferAmountWei)

expect.toBeEqual(
daiBalanceWei,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { asPercentageValue, exchangeFromDAI, expect } from '@dma-common/test-uti
import { FakeRequestEnv, RuntimeConfig } from '@dma-common/types/common'
import { balanceOf } from '@dma-common/utils/balances'
import { amountFromWei, amountToWei } from '@dma-common/utils/common'
import { calculateFeeOnInputAmount } from '@dma-common/utils/swap'
import { calculatePercentageFeeOnInputAmount } from '@dma-common/utils/swap'
import { testBlockNumber } from '@dma-contracts/test/config'
import { restoreSnapshot, TestHelpers } from '@dma-contracts/utils'
import { SwapFeeType } from '@dma-library/types'
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('Swap | Unit', async () => {

before(async () => {
amountInWei = amountToWei(1000)
amountWithFeeInWei = calculateFeeOnInputAmount(amountInWei).plus(amountInWei)
amountWithFeeInWei = calculatePercentageFeeOnInputAmount(amountInWei).plus(amountInWei)
})

beforeEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { asPercentageValue, expect, swapOneInchTokens } from '@dma-common/test-u
import { FakeRequestEnv, RuntimeConfig } from '@dma-common/types/common'
import { balanceOf } from '@dma-common/utils/balances'
import { amountFromWei, amountToWei } from '@dma-common/utils/common'
import { calculateFeeOnInputAmount } from '@dma-common/utils/swap'
import { calculatePercentageFeeOnInputAmount } from '@dma-common/utils/swap'
import { testBlockNumber } from '@dma-contracts/test/config'
import { restoreSnapshot, TestHelpers } from '@dma-contracts/utils'
import { SwapFeeType } from '@dma-library/types'
Expand Down Expand Up @@ -76,7 +76,7 @@ describe('Swap | Unit', async () => {
fromToken = WETH.address
toToken = WBTC.address

amountWithFeeInWei = calculateFeeOnInputAmount(amountInWei).plus(amountInWei)
amountWithFeeInWei = calculatePercentageFeeOnInputAmount(amountInWei).plus(amountInWei)

const response = await swapOneInchTokens(
fromToken,
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('Swap | Unit', async () => {
const feeWalletBalanceWeiAfter = await balanceOf(fromToken, feeBeneficiaryAddress, { config })
const feeWalletBalanceWeiChange = feeWalletBalanceWeiAfter.minus(feeWalletBalanceWeiBefore)

expect.toBeEqual(feeWalletBalanceWeiChange, calculateFeeOnInputAmount(amountInWei))
expect.toBeEqual(feeWalletBalanceWeiChange, calculatePercentageFeeOnInputAmount(amountInWei))
})

it('should not leave any fromToken in Swap contract', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { asPercentageValue, exchangeToDAI, expect } from '@dma-common/test-utils
import { FakeRequestEnv, RuntimeConfig } from '@dma-common/types/common'
import { balanceOf } from '@dma-common/utils/balances'
import { amountFromWei, amountToWei } from '@dma-common/utils/common'
import { calculateFee } from '@dma-common/utils/swap'
import { calculatePercentageFee } from '@dma-common/utils/swap'
import { testBlockNumber } from '@dma-contracts/test/config'
import { restoreSnapshot, TestHelpers } from '@dma-contracts/utils'
import { SwapFeeType } from '@dma-library/types'
Expand Down Expand Up @@ -75,7 +75,7 @@ describe('Swap | Unit', async () => {
6,
)

feeInUSDT = calculateFee(initialUSDTBalanceInWeiWithFee)
feeInUSDT = calculatePercentageFee(initialUSDTBalanceInWeiWithFee)

initialUSDTBalanceInWei = initialUSDTBalanceInWeiWithFee.minus(feeInUSDT)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { asPercentageValue, expect, swapOneInchTokens } from '@dma-common/test-u
import { FakeRequestEnv, RuntimeConfig } from '@dma-common/types/common'
import { balanceOf } from '@dma-common/utils/balances'
import { amountToWei } from '@dma-common/utils/common'
import { calculateFeeOnInputAmount } from '@dma-common/utils/swap'
import { calculatePercentageFeeOnInputAmount } from '@dma-common/utils/swap'
import { testBlockNumber } from '@dma-contracts/test/config'
import { restoreSnapshot, TestHelpers } from '@dma-contracts/utils'
import { SwapFeeType } from '@dma-library/types'
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('Swap | Unit', async () => {
it('should allow to use different tiers', async () => {
const amountInWei = amountToWei(10)
const fee = 50
const feeAmount = calculateFeeOnInputAmount(amountInWei, fee)
const feeAmount = calculatePercentageFeeOnInputAmount(amountInWei, fee)
const amountInWeiWithFee = amountInWei.plus(feeAmount)
await system.Swap.contract.connect(authorizedSigner).addFeeTier(fee)

Expand Down Expand Up @@ -190,7 +190,7 @@ describe('Swap | Unit', async () => {
it('should throw an error when fee tier does not exist', async () => {
const amountInWei = amountToWei(10)
const fee = 99
const feeAmount = calculateFeeOnInputAmount(amountInWei, fee)
const feeAmount = calculatePercentageFeeOnInputAmount(amountInWei, fee)
const amountInWeiWithFee = amountInWei.plus(feeAmount)

const response = await swapOneInchTokens(
Expand Down
4 changes: 2 additions & 2 deletions packages/dma-contracts/test/unit/swap/u-swap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { balanceOf } from '@dma-common/utils/balances'
import { amountToWei } from '@dma-common/utils/common'
import { createDeploy } from '@dma-common/utils/deploy'
import init from '@dma-common/utils/init'
import { calculateFee } from '@dma-common/utils/swap'
import { calculatePercentageFee } from '@dma-common/utils/swap'
import { swap, uniswapV3Swap, unoswap } from '@dma-contracts/test/fixtures'
import BigNumber from 'bignumber.js'
import { Contract } from 'ethers'
Expand Down Expand Up @@ -67,7 +67,7 @@ describe.skip('uSwap | Unit', () => {

describe('WETH to DAI, fee in WETH', () => {
const amountInWei = amountToWei(new BigNumber(10))
const fee = calculateFee(amountInWei, FEE)
const fee = calculatePercentageFee(amountInWei, FEE)
const depositAmountWithFeeWei = amountInWei.plus(fee)
const slippage = asPercentageValue(10, 100)

Expand Down
Loading

0 comments on commit 78856d1

Please sign in to comment.