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

✨ feat(withdraw spark ops): spark add missing operations #21

Merged
Merged
2 changes: 2 additions & 0 deletions packages/deploy-configurations/constants/operation-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const OPERATION_NAMES = {
DEPOSIT: 'SparkDeposit',
BORROW: 'SparkBorrow',
PAYBACK_WITHDRAW: 'SparkPaybackWithdraw',
WITHDRAW: 'SparkWithdraw',
WITHDRAW_TO_DEBT: 'SparkWithdrawToDebt',
},
maker: {
OPEN_AND_DRAW: 'OpenAndDraw',
Expand Down
2 changes: 2 additions & 0 deletions packages/deploy-configurations/operation-definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export { getSparkDepositOperationDefinition } from './spark/borrow'
export { getSparkDepositBorrowOperationDefinition } from './spark/borrow'
export { getSparkOpenDepositBorrowOperationDefinition } from './spark/borrow'
export { getSparkPaybackWithdrawOperationDefinition } from './spark/borrow'
export { getSparkWithdrawOperationDefinition } from './spark/borrow/withdraw'
export { getSparkWithdrawToDebtOperationDefinition } from './spark/borrow/withdraw-to-debt'
export { getSparkOpenOperationDefinition } from './spark/multiply'
export { getSparkCloseAndExitOperationDefinition } from './spark/multiply'
export { getSparkCloseAndRemainOperationDefinition } from './spark/multiply'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { loadContractNames, OPERATION_NAMES } from '@deploy-configurations/constants'
import { Network } from '@deploy-configurations/types/network'
import { getActionHash } from '@deploy-configurations/utils/action-hash'

export function getSparkWithdrawToDebtOperationDefinition(network: Network) {
const SERVICE_REGISTRY_NAMES = loadContractNames(network)

return {
name: OPERATION_NAMES.spark.WITHDRAW_TO_DEBT,
actions: [
{
hash: getActionHash(SERVICE_REGISTRY_NAMES.spark.WITHDRAW),
optional: false,
},
{
hash: getActionHash(SERVICE_REGISTRY_NAMES.common.COLLECT_FEE),
optional: false,
},
{
hash: getActionHash(SERVICE_REGISTRY_NAMES.common.SWAP),
optional: false,
},
{
hash: getActionHash(SERVICE_REGISTRY_NAMES.common.UNWRAP_ETH),
optional: false,
},
{
hash: getActionHash(SERVICE_REGISTRY_NAMES.common.RETURN_FUNDS),
optional: false,
},
],
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

import { loadContractNames, OPERATION_NAMES } from '@deploy-configurations/constants'
import { Network } from '@deploy-configurations/types/network'
import { getActionHash } from '@deploy-configurations/utils/action-hash'

export function getSparkWithdrawOperationDefinition(network: Network) {
const SERVICE_REGISTRY_NAMES = loadContractNames(network)

return {
name: OPERATION_NAMES.spark.WITHDRAW,
actions: [
{
hash: getActionHash(SERVICE_REGISTRY_NAMES.spark.WITHDRAW),
optional: false,
},
{
hash: getActionHash(SERVICE_REGISTRY_NAMES.common.COLLECT_FEE),
optional: false,
},
{
hash: getActionHash(SERVICE_REGISTRY_NAMES.common.UNWRAP_ETH),
optional: false,
},
{
hash: getActionHash(SERVICE_REGISTRY_NAMES.common.RETURN_FUNDS),
optional: false,
},
],
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import { IOperation } from '@dma-library/types'
import BigNumber from 'bignumber.js'

type WithdrawArgs = {
/**
* Send withdrawal amount with no decimal precision applied
* EG 1.02 USDC should be sent as 1.02e6 which is 1020000
*/
withdrawAmount: BigNumber
collateralTokenAddress: string
collateralIsEth: boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { getSparkWithdrawToDebtOperationDefinition } from '@deploy-configurations/operation-definitions'
import { Network } from '@deploy-configurations/types/network'
import { MAX_UINT, ZERO } from '@dma-common/constants'
import { actions } from '@dma-library/actions'
import { AaveLikeStrategyAddresses } from '@dma-library/operations/aave-like'
import { IOperation } from '@dma-library/types'
import BigNumber from 'bignumber.js'

type WithdrawToDebtArgs = {
withdrawAmount: BigNumber
receiveAtLeast: BigNumber
swapData: string
collateralTokenAddress: string
debtTokenAddress: string
debtIsEth: boolean
proxy: string
addresses: AaveLikeStrategyAddresses
network: Network
}

export type SparkWithdrawToDebtOperation = (args: WithdrawToDebtArgs) => Promise<IOperation>

export const withdrawToDebt: SparkWithdrawToDebtOperation = async args => {
const { network } = args

const withdrawCollateralFromSpark = actions.spark.withdraw(args.network, {
asset: args.collateralTokenAddress,
amount: args.withdrawAmount,
to: args.proxy,
})

const collectFeeAfterWithdraw = actions.common.collectFee(
args.network,
{
asset: args.collateralTokenAddress,
},
[1],
)

const swapCollateralTokensForDebtTokens = actions.common.swap(network, {
fromAsset: args.collateralTokenAddress,
toAsset: args.debtTokenAddress,
amount: args.withdrawAmount,
receiveAtLeast: args.receiveAtLeast,
fee: ZERO.toNumber(),
withData: args.swapData,
// Not relevant here
collectFeeInFromToken: true,
})

const unwrapEth = actions.common.unwrapEth(network, {
amount: new BigNumber(MAX_UINT),
})

const returnFunds = actions.common.returnFunds(network, {
asset: args.debtIsEth ? args.addresses.tokens.ETH : args.debtTokenAddress,
})

const calls = [
withdrawCollateralFromSpark,
collectFeeAfterWithdraw,
swapCollateralTokensForDebtTokens,
unwrapEth,
returnFunds,
]

return {
calls: calls,
operationName: getSparkWithdrawToDebtOperationDefinition(args.network).name,
}
}
51 changes: 51 additions & 0 deletions packages/dma-library/src/operations/spark/borrow/withdraw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { getSparkWithdrawOperationDefinition } from '@deploy-configurations/operation-definitions'
import { Network } from '@deploy-configurations/types/network'
import { MAX_UINT } from '@dma-common/constants'
import { actions } from '@dma-library/actions'
import { AaveLikeStrategyAddresses } from '@dma-library/operations/aave-like'
import { IOperation } from '@dma-library/types'
import BigNumber from 'bignumber.js'

type WithdrawArgs = {
withdrawAmount: BigNumber
collateralTokenAddress: string
collateralIsEth: boolean
proxy: string
addresses: AaveLikeStrategyAddresses
network: Network
}

export type SparkWithdrawOperation = (args: WithdrawArgs) => Promise<IOperation>

export const withdraw: SparkWithdrawOperation = async args => {
const { network } = args

const withdrawCollateralFromSpark = actions.spark.withdraw(args.network, {
asset: args.collateralTokenAddress,
amount: args.withdrawAmount,
to: args.proxy,
})

const collectFeeAfterWithdraw = actions.common.collectFee(
args.network,
{
asset: args.collateralTokenAddress,
},
[1],
)

const unwrapEth = actions.common.unwrapEth(network, {
amount: new BigNumber(MAX_UINT),
})

const returnFunds = actions.common.returnFunds(network, {
asset: args.collateralIsEth ? args.addresses.tokens.ETH : args.collateralTokenAddress,
})

const calls = [withdrawCollateralFromSpark, collectFeeAfterWithdraw, unwrapEth, returnFunds]

return {
calls: calls,
operationName: getSparkWithdrawOperationDefinition(args.network).name,
}
}
12 changes: 12 additions & 0 deletions packages/dma-library/src/operations/spark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import {
paybackWithdraw as sparkPaybackWithdraw,
SparkPaybackWithdrawOperation,
} from './borrow/payback-withdraw'
import {
withdraw as sparkWithdraw,
SparkWithdrawOperation,
} from './borrow/withdraw'
import {
withdrawToDebt as sparkWithdrawToDebt,
SparkWithdrawToDebtOperation,
} from './borrow/withdraw-to-debt'
import {
adjustRiskDown as sparkAdjustRiskDown,
SparkAdjustDownOperation,
Expand All @@ -29,6 +37,8 @@ const borrow = {
depositBorrow: sparkDepositBorrow,
openDepositBorrow: sparkOpenDepositBorrow,
paybackWithdraw: sparkPaybackWithdraw,
withdraw: sparkWithdraw,
withdrawToDebt: sparkWithdrawToDebt,
}
const multiply = {
open: sparkOpen,
Expand All @@ -43,6 +53,8 @@ export type SparkBorrowOperations = {
depositBorrow: SparkDepositBorrowOperation
openDepositBorrow: SparkOpenDepositBorrowOperation
paybackWithdraw: SparkPaybackWithdrawOperation
withdraw: SparkWithdrawOperation
withdrawToDebt: SparkWithdrawToDebtOperation
}

export type SparkMultiplyOperations = {
Expand Down
Loading