Skip to content

Commit

Permalink
Merge pull request #1010 from TempleDAO/borrow-gas-estimate
Browse files Browse the repository at this point in the history
Estimate gas instead of hardcoded
  • Loading branch information
marshall2112 authored Apr 23, 2024
2 parents 552a43e + 525c916 commit 8277c45
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
45 changes: 30 additions & 15 deletions apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { TlcChart } from './Chart';
import env from 'constants/env';
import { useConnectWallet } from '@web3-onboard/react';
import Tooltip from 'components/Tooltip/Tooltip';
import { estimateAndMine } from 'utils/ethers';

export type State = {
supplyValue: string;
Expand Down Expand Up @@ -300,9 +301,11 @@ export const BorrowPage = () => {
env.contracts.tlc,
amount
);
// Add collateral
const tx = await tlcContract.addCollateral(amount, wallet);
const receipt = await tx.wait();

const populatedTransaction =
await tlcContract.populateTransaction.addCollateral(amount, wallet);
const receipt = await estimateAndMine(signer, populatedTransaction);

openNotification({
title: `Supplied ${state.supplyValue} TEMPLE`,
hash: receipt.transactionHash,
Expand All @@ -329,10 +332,10 @@ export const BorrowPage = () => {
getTokenInfo(state.inputToken).decimals
);
try {
const tx = await tlcContract.removeCollateral(amount, wallet, {
gasLimit: 160000,
});
const receipt = await tx.wait();
const populatedTransaction =
await tlcContract.populateTransaction.removeCollateral(amount, wallet);
const receipt = await estimateAndMine(signer, populatedTransaction);

openNotification({
title: `Withdrew ${state.withdrawValue} TEMPLE`,
hash: receipt.transactionHash,
Expand All @@ -359,8 +362,12 @@ export const BorrowPage = () => {
getTokenInfo(state.outputToken).decimals
);
try {
const tx = await tlcContract.borrow(amount, wallet, { gasLimit: 500000 });
const receipt = await tx.wait();
const populatedTransaction = await tlcContract.populateTransaction.borrow(
amount,
wallet
);
const receipt = await estimateAndMine(signer, populatedTransaction);

openNotification({
title: `Borrowed ${state.borrowValue} DAI`,
hash: receipt.transactionHash,
Expand Down Expand Up @@ -395,9 +402,14 @@ export const BorrowPage = () => {
env.contracts.tlc,
amount
);
// Repay DAI
const tx = await tlcContract.repay(amount, wallet, { gasLimit: 400000 });
const receipt = await tx.wait();

// Note Repay vs RepayAll
const populatedTransaction = await tlcContract.populateTransaction.repay(
amount,
wallet
);
const receipt = await estimateAndMine(signer, populatedTransaction);

openNotification({
title: `Repaid ${state.repayValue} DAI`,
hash: receipt.transactionHash,
Expand Down Expand Up @@ -432,9 +444,12 @@ export const BorrowPage = () => {
env.contracts.tlc,
amount
);
// Repay DAI
const tx = await tlcContract.repayAll(wallet, { gasLimit: 400000 });
const receipt = await tx.wait();

// Note RepayAll vs Repay
const populatedTransaction =
await tlcContract.populateTransaction.repayAll(wallet);
const receipt = await estimateAndMine(signer, populatedTransaction);

openNotification({
title: `Repaid ${
accountPosition
Expand Down
29 changes: 29 additions & 0 deletions apps/dapp/src/utils/ethers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ContractTransaction, PopulatedTransaction, Signer } from 'ethers';

const GAS_ESTIMATE_BUFFER_PERCENT = 25;

async function estimateAndMine(
signer: Signer,
populatedTransaction: PopulatedTransaction
) {
const txn = await estimateAndSubmit(signer, populatedTransaction);
return txn.wait();
}

async function estimateAndSubmit(
signer: Signer,
populatedTransaction: PopulatedTransaction
): Promise<ContractTransaction> {
if (!signer.provider) {
throw new Error('No provider found');
}
const gasEstimate = await signer.provider.estimateGas(populatedTransaction);
const gas = gasEstimate.add(gasEstimate.div(GAS_ESTIMATE_BUFFER_PERCENT));

populatedTransaction.gasLimit = gas;
populatedTransaction.from = await signer.getAddress();

return signer.sendTransaction(populatedTransaction);
}

export { estimateAndMine };

0 comments on commit 8277c45

Please sign in to comment.