1

I am testing smart contract interaction with Gains.trade on hardhat mainnet fork. However i always got this error Error: VM Exception while processing transaction: reverted with reason string 'Dai/insufficient-allowance'. I have approve the contract but still got the error.

async function main() {

await helpers.impersonateAccount(mimic);
const impersonate = await ethers.getSigner(mimic);


console.log(
    "Vitalik account before transaction",
    ethers.formatEther(await ethers.provider.getBalance(impersonate))
  );

const gns = new ethers.Contract(gainsAddress, gainsTrading, provider)
const dai = new ethers.Contract(DaiAddress, DaiABI, provider)

const price = await getGainsPrice();

// const convPrice = parseInt(ethers.parseUnits(price.toString(), 8));


const convPrice = price / (10 ** 8);
const tp = convPrice + (0.01 * convPrice * (15 / 5));
console.log(`tp: ${tp}`)
const tpCov = tp * (10 ** 10)

const contractPrice =  convPrice * (10 ** 10);
// const floor = Math.floor(contractPrice);


console.log(`contract price: ${contractPrice}`);
console.log(`tp conversion: ${tpCov}`)


const tradeTuple = {
    'trader': mimic,
    'pairIndex': 0,
    'index': 0,  //tradeIndex
    'initialPosToken': 0,
    'positionSizeDai': ethers.parseUnits('2000', 18).toString(),  // collateral in 1e18
    'openPrice': BigInt(contractPrice),
    'buy': true,
    'leverage': 5,  //leverage adjustable by slider on frontend
    'tp': BigInt(tpCov),
    'sl': 0
}

console.log(contractPrice)


const daiTransaction = await dai.connect(impersonate).approve(
    gainsAddress,
    ethers.parseUnits('2000', 18).toString()
)
const daiReceipt = await daiTransaction.wait()
console.log(daiReceipt.blockNumber)

const trade = await gns.connect(impersonate).openTrade(
    tradeTuple, 0, 0, '12492725505', '0x0000000000000000000000000000000000000000'
)
const tradeReceipt = await trade.wait()
console.log(tradeReceipt.blockNumber)

}

2
  • Does the error occurs on the openTrade call?, can you also include the solidity code behind that function?
    – Julissa DC
    Commented Aug 10, 2023 at 14:58
  • Try console logging the allowance after you approve to ensure that the contract was approved correctly
    – Rohan Nero
    Commented Aug 10, 2023 at 18:47

1 Answer 1

0

Try waiting for the first apporve transaction to get executed on the blockchain as it may be in progress when you are calling the trade function .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.