How to do Flashloan using Instapool v2 Connector

Instapool v2

Instapool v2 connector works little different than original Instapool connector. In this post I’ll try to explain how it works with some code snippets.

Connector method

spells.add({
  connector: "instapool_v2",
  method: "flashBorrowAndCast",
  args: [token, amount, route, calldata]
});

I’ll explain how to do a collateral swap from WBTC to ETH on Aave

let spells = dsa.Spell()

let ethAddr = dsa.tokens.info.eth.address
let wbtcAddr = dsa.tokens.info.wbtc.address
let flashAmt = 10
let flashAmtInWei = dsa.tokens.fromDecimal(flashAmt, "wbtc")

let setId = 56654632
// Resolver function to get `uintAmt` value 
// https://docs.instadapp.io/resolvers/1inch/
let swapDetails = await dsa.oneInch.getBuyAmount(ethAddr, wbtcAddr, flashAmtInWei, 2)

// Resolver to get flashloan liquidity, more on this below
let flashloanDetails = await dsa.instapool_v2.getLiquidity(["wbtc"])
let liquidity = flashloanDetails.wbtc
let route
// Choose highest liquidity path
if (flashflashAmt < liquidity.dydx) {
  route = 0
} else if (flashAmt < liquidity.maker) {
  route = 1
} else if (flashAmt < liquidity.compound) {
  route = 2
} else if (flashAmt < liquidity.aave) {
  route = 3
} else {
  route = null
}

// Swap flashloaned WBTC to ETH
spells.add({
  connector: "oneInch",
  method: "sell",
  args: [ethAddr, wbtcAddr, flashAmtInWei, swapDetails.unitAmt, 0, setId]
})

// Deposit ETH received from previous step
spells.add({
  connector: "aave",
  method: "deposit",
  args: [ethAddr, 0, setId, 0]
});

// Withdraw all WBTC
spells.add({
  connector: "aave",
  method: "withdraw",
  args: [wbtcAddr, dsa.maxValue, 0, 0]
})

// Payback flashloan amount
spells.add({
  connector: "instapool_v2",
  method: "flashPayback",
  args: [wbtcAddr, flashAmtInWei, 0, 0]
})

// Now we have all steps need to be performed after getting the flashloan amount

let calldata = dsa.instapool_v2.encodeFlashCastData(spells)

let flashloanSpell = dsa.Spell()

// Flashloan spell
flashloanSpell.add({
  connector: 'instapool_v2',
  method: 'flashBorrowAndCast',
  args: [wbtcAddr, flashAmtInWei, route, calldata],
})

// Note: Make sure you have set DSA instance.
// If not, follow this to setup: https://docs.instadapp.io/setup/
await dsa.cast(flashloanSpell)

Getting liquidity data and choosing the route

You can use dsa.instapool_v2.getLiquidity function to get the liquidity data

// Get the available liquidity for eth, wbtc and dai
await dsa.instapool_v2.getLiquidity(["eth", "dai", "wbtc"])

Expected output

{
  "eth": {
    "dydx": 61270.384409892315, // Liquidity in whole amount
    "aave": 45798.06511446496,
    "maker": 0,
    "compound": 45798.06511446496
  },
  "dai": {
    "dydx": 2576554.065145491,
    "aave": 28024075.48124202,
    "maker": 24885659.17481212,
    "compound": 28002091.28995508
  },
  "wbtc": {
    "dydx": 0,
    "aave": 1424.6404116566296,
    "maker": 0,
    "compound": 1463.5163048677634
  }
}

You can choose the route with highest liquidity. Following are the respective routes:

dydx = 0
maker = 1
compound = 2
aave = 3

References

How to use getId & setId? - General - Instadapp Community

1 Like