Collateral swap with flash loan

This recipe is swapping BAT collateral to USDC with flash loan on the following steps:

  1. Borrow USDC
  2. Deposit USDC
  3. Withdraw BAT
  4. Trade BAT to USDC in Kyber
  5. Payback USDC
await dsa.setInstance(237);

let withdrawAmount = 1000;
let withdrawAmtInWei = dsa.tokens.fromDecimal(withdrawAmount, "bat");

let slippage = 0.1; // 0.1% slippage.
let bat_address = dsa.tokens.info.bat.address
let usdc_address = dsa.tokens.info.usdc.address

let buyAmount = await dsa.kyber.getBuyAmount("USDC", "BAT", withdrawAmount, slippage);

let spells = dsa.Spell();

spells.add({
  connector: "instapool",
  method: "flashBorrow",
  args: [usdc_address, buyAmount.buyAmtRaw, 0, 0]
});

spells.add({
  connector: "compound",
  method: "deposit",
  args: [usdc_address, buyAmount.buyAmtRaw, 0, 0]
});

spells.add({
  connector: "compound",
  method: "withdraw",
  args: [bat_address, withdrawAmtInWei, 0, 0]
});

spells.add({
  connector: "kyber",
  method: "sell",
  args: [usdc_address, bat_address, withdrawAmtInWei, buyAmount.unitAmt, 0, 0]
});

spells.add({
  connector: "instapool",
  method: "flashPayback",
  args: [usdc_address, 0, 0]
});

dsa.cast(spells).then(console.log)
1 Like