Skip to content

Commit

Permalink
fix: flashloan step as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
robercano committed Jun 3, 2024
1 parent d8f846f commit 5d683fd
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 31 deletions.
5 changes: 5 additions & 0 deletions sdk/sdk-common/src/common/implementation/TokenAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ export class TokenAmount implements ITokenAmount {
return new BigNumber(this.amount)
}

/** @see ITokenAmount.isZero */
isZero(): boolean {
return this.toBN().isZero()
}

/** PRIVATE */
private _validateSameToken(tokenAmount: ITokenAmount): void {
if (tokenAmount.token.symbol !== this.token.symbol) {
Expand Down
7 changes: 7 additions & 0 deletions sdk/sdk-common/src/common/interfaces/ITokenAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ export interface ITokenAmount extends ITokenAmountData, IPrintable {
* This is, it includes all the decimals of the token and can be passed to a solidity contract
*/
toBaseUnit(): string

/**
* @name isZero
* @description Checks if the amount is zero
* @returns true if the amount is zero or false otherwise
*/
isZero(): boolean
}

/**
Expand Down
67 changes: 41 additions & 26 deletions sdk/sdk-e2e/tests/refinanceAaveV3SparkAnyPair.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ jest.setTimeout(300000)

/** TEST CONFIG */
const config = {
SDKAPiUrl: 'https://zmjmtfsocb.execute-api.us-east-1.amazonaws.com/api/sdk',
TenderlyForkUrl: 'https://virtual.mainnet.rpc.tenderly.co/5eea57de-3dc2-4cae-b7ed-24b16b0cbde0',
DPMAddress: '0x551eb8395093fde4b9eef017c93593a3c7a75138',
walletAddress: '0xbEf4befb4F230F43905313077e3824d7386E09F8',
collateralTokenSymbol: CommonTokenSymbols.WETH,
collateralAmount: '0.0198',
debtTokenSymbol: CommonTokenSymbols.DAI,
debtAmount: '34',
SDKAPiUrl: 'https://72dytt1e93.execute-api.us-east-1.amazonaws.com/api/sdk',
TenderlyForkUrl: 'https://virtual.mainnet.rpc.tenderly.co/28f27b3b-bafb-4902-a1a0-53668b179117',
DPMAddress: '0xb2f1349068c1cb6a596a22a3531b8062778c9da4',
walletAddress: '0xDDc68f9dE415ba2fE2FD84bc62Be2d2CFF1098dA',
source: {
collateralTokenSymbol: CommonTokenSymbols.wstETH,
collateralAmount: '10',
debtTokenSymbol: CommonTokenSymbols.USDC,
debtAmount: '0',
},
target: {
collateralTokenSymbol: CommonTokenSymbols.WETH,
debtTokenSymbol: CommonTokenSymbols.DAI,
},
sendTransactionEnabled: true,
}

Expand Down Expand Up @@ -79,15 +85,25 @@ describe.skip('Refinance AaveV3 Spark | SDK', () => {
})

// Tokens
const debtToken: Maybe<IToken> = await chain.tokens.getTokenBySymbol({
symbol: config.debtTokenSymbol,
const sourceDebtToken: Maybe<IToken> = await chain.tokens.getTokenBySymbol({
symbol: config.source.debtTokenSymbol,
})
assert(debtToken, `${config.debtTokenSymbol} not found`)
assert(sourceDebtToken, `${config.source.debtTokenSymbol} not found`)

const collateralToken: Maybe<IToken> = await chain.tokens.getTokenBySymbol({
symbol: config.collateralTokenSymbol,
const sourceCollateralToken: Maybe<IToken> = await chain.tokens.getTokenBySymbol({
symbol: config.source.collateralTokenSymbol,
})
assert(collateralToken, `${config.collateralTokenSymbol} not found`)
assert(sourceCollateralToken, `${config.source.collateralTokenSymbol} not found`)

const targetDebtToken: Maybe<IToken> = await chain.tokens.getTokenBySymbol({
symbol: config.target.debtTokenSymbol,
})
assert(targetDebtToken, `${config.target.debtTokenSymbol} not found`)

const targetCollateralToken: Maybe<IToken> = await chain.tokens.getTokenBySymbol({
symbol: config.target.collateralTokenSymbol,
})
assert(targetCollateralToken, `${config.target.collateralTokenSymbol} not found`)

const aaveV3 = await chain.protocols.getProtocol({ name: ProtocolName.AaveV3 })
assert(aaveV3, 'AaveV3 protocol not found')
Expand All @@ -98,8 +114,8 @@ describe.skip('Refinance AaveV3 Spark | SDK', () => {

const aaveV3PoolId = AaveV3LendingPoolId.createFrom({
protocol: aaveV3,
collateralToken: collateralToken,
debtToken: debtToken,
collateralToken: sourceCollateralToken,
debtToken: sourceDebtToken,
emodeType: EmodeType.None,
})

Expand All @@ -113,18 +129,18 @@ describe.skip('Refinance AaveV3 Spark | SDK', () => {
}

// Source position
const morphoPosition = AaveV3Position.createFrom({
const aaveV3Position = AaveV3Position.createFrom({
type: PositionType.Multiply,
id: AaveV3PositionId.createFrom({
id: 'AaveV3Position',
}),
debtAmount: TokenAmount.createFrom({
token: debtToken,
amount: config.debtAmount,
token: sourceDebtToken,
amount: config.source.debtAmount,
}),
collateralAmount: TokenAmount.createFrom({
token: collateralToken,
amount: config.collateralAmount,
token: sourceCollateralToken,
amount: config.source.collateralAmount,
}),
pool: aaveV3Pool,
})
Expand All @@ -141,8 +157,8 @@ describe.skip('Refinance AaveV3 Spark | SDK', () => {

const poolId = SparkLendingPoolId.createFrom({
protocol: spark,
collateralToken: collateralToken,
debtToken: debtToken,
collateralToken: targetCollateralToken,
debtToken: targetDebtToken,
emodeType: EmodeType.None,
})

Expand All @@ -161,7 +177,7 @@ describe.skip('Refinance AaveV3 Spark | SDK', () => {
}

const refinanceParameters = RefinanceParameters.createFrom({
sourcePosition: morphoPosition,
sourcePosition: aaveV3Position,
targetPool: sparkPool,
slippage: Percentage.createFrom({ value: 0.2 }),
})
Expand All @@ -171,7 +187,7 @@ describe.skip('Refinance AaveV3 Spark | SDK', () => {

expect(refinanceSimulation).toBeDefined()

expect(refinanceSimulation.sourcePosition?.id).toEqual(morphoPosition.id)
expect(refinanceSimulation.sourcePosition?.id).toEqual(aaveV3Position.id)
expect(refinanceSimulation.targetPosition.pool.id).toEqual(sparkPool.id)

const refinanceOrder: Maybe<Order> = await user.newOrder({
Expand All @@ -181,7 +197,6 @@ describe.skip('Refinance AaveV3 Spark | SDK', () => {

assert(refinanceOrder, 'Order not found')

console.log('Order:', JSON.stringify(refinanceOrder, null, 2))
if (config.sendTransactionEnabled) {
// Send transaction
console.log('Sending transaction...')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export async function estimateSwapFromAmount(params: {
return receiveAtLeast
}

if (receiveAtLeast.isZero()) {
return TokenAmount.createFrom({
token: params.fromToken,
amount: '0',
})
}

const spotPrice = (
await params.oracleManager.getSpotPrice({
baseToken: receiveAtLeast.token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ export async function refinanceLendingToLending(
const simulator = Simulator.create(refinanceLendingToLendingAnyPairStrategy)

const isCollateralSwapSkipped = targetPool.collateralToken.equals(sourcePool.collateralToken)
const isDebtSwapSkipped = targetPool.debtToken.equals(sourcePool.debtToken)
const isDebtAmountZero = position.debtAmount.toBaseUnit() === '0'
const isDebtTokenSame = targetPool.debtToken.equals(sourcePool.debtToken)
const isDebtSwapSkipped = isDebtTokenSame || isDebtAmountZero

const maxDebtAmount = TokenAmount.createFrom({
token: position.debtAmount.token,
Expand Down Expand Up @@ -112,7 +113,7 @@ export async function refinanceLendingToLending(
depositAmount: isCollateralSwapSkipped
? position.collateralAmount
: ctx.getReference(['SwapCollateralFromSourcePosition', 'received']),
borrowAmount: isDebtSwapSkipped
borrowAmount: isDebtTokenSame
? flashloanAmount
: await estimateSwapFromAmount({
receiveAtLeast: flashloanAmount,
Expand Down Expand Up @@ -172,7 +173,7 @@ export async function refinanceLendingToLending(
},
}),
{
skip: isDebtAmountZero || isDebtSwapSkipped,
skip: isDebtSwapSkipped,
type: SimulationSteps.ReturnFunds,
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const refinanceLendingToLendingAnyPairStrategy = makeStrategy([
{
name: 'Flashloan',
step: SimulationSteps.Flashloan,
optional: false,
optional: true,
},
{
name: 'PaybackWithdrawFromSourcePosition',
Expand Down Expand Up @@ -35,7 +35,7 @@ export const refinanceLendingToLendingAnyPairStrategy = makeStrategy([
{
name: 'RepayFlashloan',
step: SimulationSteps.RepayFlashloan,
optional: false,
optional: true,
},
{
name: 'ReturnFunds',
Expand Down

0 comments on commit 5d683fd

Please sign in to comment.