RPC Error: gas required exceeds allowance or always failing transaction

I am trying to execute the list of spells to short DAI, just as you show in the documentation.

Everything is working up to the very last line: dsa.cast(spells).then(console.log)

… after which I then get this error in my Google Chrome console (also note that I am using MetaMask):

Screen Shot 2020-07-02 at 18.04.31

I have enough funds for both DAI and USDC in my DSA, and I also have enough ETH (about $20 given that for now I’m just playing around with this) in my MetaMask wallet to be able to pay any fees (unless for some reason the fees to execute all these spells are huge).

I have searched as you usually would but could not find an explanation or reason for the error in this particular context. Any ideas? Thank you in advance! :)))

Hi @jancz,

Could you paste your code as well so that we can have a look for you?

Thank you for your quick response!

I used EXACTLY what is in the documentation because I just wanted to try it out. And I pasted it into the console in Chrome whilst I am on my Smart Account Dashboard.

Either ways, if it helps, here it is:

if (window.ethereum) {
  window.web3 = new Web3(window.ethereum)
} else if (window.web3) {
  window.web3 = new Web3(window.web3.currentProvider)
} else {
  window.web3 = new Web3(customProvider)

const dsa = new DSA(web3);


let borrowAmount = 20; // 20 DAI
let borrowAmtInWei = dsa.tokens.fromDecimal(borrowAmount, "dai"); // borrow flash loan and swap via Oasis

let slippage = 2; // 2% slippage.
let dai_address = dsa.tokens.info.dai.address
let usdc_address = dsa.tokens.info.usdc.address

let buyAmount = await dsa.oasis.getBuyAmount("USDC", "DAI", borrowAmount, slippage);

let spells = dsa.Spell();

  connector: "instapool",
  method: "flashBorrow",
  args: [dai_address, borrowAmtInWei, 0, 0]

  connector: "oasis",
  method: "sell",
  args: [usdc_address, dai_address, borrowAmtInWei, buyAmount.unitAmt, 0, 0]

  connector: "maker",
  method: "open",
  args: ["USDC-A"]

  connector: "maker",
  method: "deposit",
  args: [0, -1, 0, 0] // deposit all USDC

  connector: "maker",
  method: "borrow",
  args: [0, borrowAmtInWei, 0, 0]

  connector: "instapool",
  method: "flashPayback",
  args: [dai_address, 0, 0]


Thank you once again for your help :))

Flash loan is a tricky one. Please refer to the answer here:

@leckylao amazing, thank you for letting me know, I understand now :slight_smile:

So essentially the transaction gets reverted back because the flash loan cannot be repaid in the transaction because the difference between DAI and USDC is not big enough to profit on.

Thank you again!

@jancz Also, make sure to update the SDK to the recent version to use the flash loan.

@sowmayjn will do, thank you very much for letting me know! :))

1 Like

@leckylao @sowmayjn

Hey guys, sorry to be a pain, I have another issue. I figured everything out from the first issue. The issue I have now is that when I execute the code, as shown earlier in this thread, which is taken directly from your documentation, what ends up happening is that rather than executing all the transactions until the end, it does not finish how I expected.

What happened in the end was that both DAI and USDC were present inside the MakerDAO vault and the loan wasn’t actually repaid, and I was just left with more USDC than DAI compared to before the transaction. I then repaid it manually on the dashboard. Maybe this is supposed to happen and I am just not aware of it? You can check the mechanics of the transaction here: https://etherscan.io/tx/0x7242f4a3ce894f87373c13736d2a37a5a9a2ee680bbd287bec9ab4a881669089

Sorry again for the confusion and let me know if there is anything else I can do to elaborate on my question. Thank you in advance :))

Hi @jancz,

Not sure what you would like to do originally but basically the TX shows you borrowed DAI from flash loan, traded Dai to USDC. Created Maker vault and and deposited USDC then borrowed Dai to repay flash loan.

You can get more details here. https://defiexplore.com/address/0xc18ad1db0c13844b80e023a4710376e359e045b1