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)
}
openTrade
call?, can you also include the solidity code behind that function?