Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add max withdraw and max deposit validations #652

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.41",
"version": "0.6.42",
"typings": "lib/index.d.ts",
"types": "lib/index.d.ts",
"main": "lib/index.js",
Expand Down
17 changes: 8 additions & 9 deletions packages/dma-library/src/strategies/common/erc4626/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Erc4626DepositPayload,
Erc4626DepositStrategy,
} from '../../../types/common'
import { validateMaxDeposit } from './validation/validate-max-deposit'

export const deposit: Erc4626DepositStrategy = async (args, dependencies) => {
const addresses = { tokens: { ...ADDRESSES[dependencies.network][SystemKeys.COMMON] } }
Expand Down Expand Up @@ -67,18 +68,16 @@ export const deposit: Erc4626DepositStrategy = async (args, dependencies) => {
},
dependencies.network,
)

const targetPosition = position.deposit(
new BigNumber(
ethers.utils
.formatUnits(swapData.minToTokenAmount.toString(), args.depositTokenPrecision)
.toString(),
),
const depositAmount = new BigNumber(
ethers.utils
.formatUnits(swapData.minToTokenAmount.toString(), args.depositTokenPrecision)
.toString(),
)
const targetPosition = position.deposit(depositAmount)

const warnings = []

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

return {
simulation: {
Expand Down Expand Up @@ -119,7 +118,7 @@ export const deposit: Erc4626DepositStrategy = async (args, dependencies) => {

const warnings = []

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

return {
simulation: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { formatCryptoBalance } from '@dma-common/utils/common'
import { Erc4626Position } from '@dma-library/types'
import { Erc4626StrategyError } from '@dma-library/types/common/erc4626-validation'
import { BigNumber } from 'bignumber.js'

export function validateMaxDeposit(
depositAmount: BigNumber,
position: Erc4626Position,
): Erc4626StrategyError[] {
const maxDeposit = position.maxDeposit

if (depositAmount.gt(maxDeposit)) {
return [
{
name: 'deposit-more-than-possible',
data: {
amount: formatCryptoBalance(maxDeposit),
},
},
]
}

return []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { formatCryptoBalance } from '@dma-common/utils/common'
import { Erc4626Position } from '@dma-library/types'
import { Erc4626StrategyError } from '@dma-library/types/common/erc4626-validation'
import { BigNumber } from 'bignumber.js'

export function validateMaxWithdraw(
withdrawAmount: BigNumber,
position: Erc4626Position,
): Erc4626StrategyError[] {
const maxWithdraw = position.maxWithdrawal

if (withdrawAmount.gt(maxWithdraw)) {
return [
{
name: 'withdraw-more-than-available',
data: {
amount: formatCryptoBalance(maxWithdraw),
},
},
]
}

return []
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Erc4626WithdrawPayload,
Erc4626WithdrawStrategy,
} from '../../../types/common'
import { validateMaxWithdraw } from './validation/validate-max-withdraw'

export const withdraw: Erc4626WithdrawStrategy = async (args, dependencies) => {
const addresses = { tokens: { ...ADDRESSES[dependencies.network][SystemKeys.COMMON] } }
Expand Down Expand Up @@ -83,7 +84,7 @@ export const withdraw: Erc4626WithdrawStrategy = async (args, dependencies) => {

const warnings = []

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

return {
simulation: {
Expand Down Expand Up @@ -125,7 +126,7 @@ export const withdraw: Erc4626WithdrawStrategy = async (args, dependencies) => {

const warnings = []

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

return {
simulation: {
Expand Down
4 changes: 3 additions & 1 deletion packages/dma-library/src/types/ajna/ajna-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Strategy } from '@dma-library/types'
import { AjnaError, AjnaNotice, AjnaSuccess, AjnaWarning } from '@dma-library/types/ajna'

import { Erc4626StrategyError } from '../common/erc4626-validation'

export type SummerStrategy<Position> = Strategy<Position> & {
simulation: Strategy<Position>['simulation'] & {
errors: AjnaError[]
errors: AjnaError[] | Erc4626StrategyError[]
warnings: AjnaWarning[]
notices: AjnaNotice[]
successes: AjnaSuccess[]
Expand Down
15 changes: 15 additions & 0 deletions packages/dma-library/src/types/common/erc4626-validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type Erc4626StrategyError = Erc4626MaxWithdrawalError | Erc4626MaxDepositError

export type Erc4626MaxWithdrawalError = {
name: 'withdraw-more-than-available'
data: {
amount: string
}
}

export type Erc4626MaxDepositError = {
name: 'deposit-more-than-possible'
data: {
amount: string
}
}
5 changes: 5 additions & 0 deletions packages/dma-library/src/types/common/erc4626-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export interface IErc4626Position extends SupplyPosition {
}
tvl: BigNumber
maxWithdrawal: BigNumber
maxDeposit: BigNumber
allocations?: {
token: string
supply: BigNumber
Expand Down Expand Up @@ -197,6 +198,7 @@ export class Erc4626Position implements IErc4626Position {
},
public totalEarnings: { withFees: BigNumber; withoutFees: BigNumber },
public maxWithdrawal: BigNumber,
public maxDeposit: BigNumber,
public tvl: BigNumber,
public allocations?: {
token: string
Expand Down Expand Up @@ -284,6 +286,7 @@ export class Erc4626Position implements IErc4626Position {
this.pnl,
this.totalEarnings,
this.maxWithdrawal.plus(quoteTokenAmount),
this.maxDeposit.minus(quoteTokenAmount),
this.tvl,
this.allocations,
this.rewards,
Expand All @@ -304,6 +307,7 @@ export class Erc4626Position implements IErc4626Position {
this.pnl,
this.totalEarnings,
this.maxWithdrawal.minus(quoteTokenAmount),
this.maxDeposit.plus(quoteTokenAmount),
this.tvl,
this.allocations,
this.rewards,
Expand All @@ -324,6 +328,7 @@ export class Erc4626Position implements IErc4626Position {
this.pnl,
this.totalEarnings,
ZERO,
this.maxDeposit,
this.tvl,
this.allocations,
this.rewards,
Expand Down
11 changes: 8 additions & 3 deletions packages/dma-library/src/views/common/erc4626.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ export async function getErc4626Position(

const positionParameters = subgraphResponse.positions[0]
const vaultParametersFromSubgraph = subgraphResponse.vaults[0]
const [balance, maxWithdraw] = await Promise.all([
const [balance, maxWithdraw, maxDeposit] = await Promise.all([
vaultContractInstance.balanceOf(proxyAddress),
vaultContractInstance.maxWithdraw(proxyAddress),
vaultContractInstance.maxDeposit(proxyAddress),
])
const assets = await vaultContractInstance.convertToAssets(balance)
const quoteTokenAmount = new BigNumber(ethers.utils.formatUnits(assets, precision).toString())
const maxWithdrawal = new BigNumber(ethers.utils.formatUnits(maxWithdraw, precision).toString())
const maxWithdrawalAmount = new BigNumber(
ethers.utils.formatUnits(maxWithdraw, precision).toString(),
)
const maxDepositAmount = new BigNumber(ethers.utils.formatUnits(maxDeposit, precision).toString())

const vault = {
address: vaultAddress,
Expand Down Expand Up @@ -120,7 +124,8 @@ export async function getErc4626Position(
netValue,
pnl,
totalEarnings,
maxWithdrawal,
maxWithdrawalAmount,
maxDepositAmount,
tvl,
allocations,
rewards,
Expand Down
Loading