From c64f2d73d34461251d1f4e1b79c994fd0d9fff61 Mon Sep 17 00:00:00 2001 From: marshall <99344331+marshall2112@users.noreply.github.com> Date: Wed, 17 Apr 2024 08:52:12 -0400 Subject: [PATCH 1/3] Estimate gas instead of hardcoded --- .../Pages/Core/DappPages/Borrow/index.tsx | 50 +++++++++++++------ apps/dapp/src/utils/ethers.ts | 21 ++++++++ 2 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 apps/dapp/src/utils/ethers.ts diff --git a/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx b/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx index ded112d6e..4ab98c3fe 100644 --- a/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx +++ b/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx @@ -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 { estimateAndSubmit } from 'utils/ethers'; export type State = { supplyValue: string; @@ -300,9 +301,12 @@ 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 txn = await estimateAndSubmit(signer, populatedTransaction); + const receipt = await txn.wait(); + openNotification({ title: `Supplied ${state.supplyValue} TEMPLE`, hash: receipt.transactionHash, @@ -329,10 +333,11 @@ 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 txn = await estimateAndSubmit(signer, populatedTransaction); + const receipt = await txn.wait(); + openNotification({ title: `Withdrew ${state.withdrawValue} TEMPLE`, hash: receipt.transactionHash, @@ -359,8 +364,13 @@ export const BorrowPage = () => { getTokenInfo(state.outputToken).decimals ); try { - const tx = await tlcContract.borrow(amount, wallet, { gasLimit: 500000 }); - const receipt = await tx.wait(); + const preparedTransaction = await tlcContract.populateTransaction.borrow( + amount, + wallet + ); + const txn = await estimateAndSubmit(signer, preparedTransaction); + const receipt = await txn.wait(); + openNotification({ title: `Borrowed ${state.borrowValue} DAI`, hash: receipt.transactionHash, @@ -395,9 +405,15 @@ 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 preparedTransaction = await tlcContract.populateTransaction.repay( + amount, + wallet + ); + const txn = await estimateAndSubmit(signer, preparedTransaction); + const receipt = await txn.wait(); + openNotification({ title: `Repaid ${state.repayValue} DAI`, hash: receipt.transactionHash, @@ -432,9 +448,13 @@ 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 txn = await estimateAndSubmit(signer, populatedTransaction); + const receipt = await txn.wait(); + openNotification({ title: `Repaid ${ accountPosition diff --git a/apps/dapp/src/utils/ethers.ts b/apps/dapp/src/utils/ethers.ts new file mode 100644 index 000000000..ddce8f866 --- /dev/null +++ b/apps/dapp/src/utils/ethers.ts @@ -0,0 +1,21 @@ +import { ContractTransaction, PopulatedTransaction, Signer } from 'ethers'; + +const GAS_ESTIMATE_BUFFER_PERCENT = 25; + +async function estimateAndSubmit( + signer: Signer, + populatedTransaction: PopulatedTransaction +): Promise { + 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 { estimateAndSubmit }; From d9885b3e7cf32b1955d7901ea9901f722c296a10 Mon Sep 17 00:00:00 2001 From: marshall <99344331+marshall2112@users.noreply.github.com> Date: Thu, 18 Apr 2024 11:28:04 -0400 Subject: [PATCH 2/3] Feedback applied --- .../Pages/Core/DappPages/Borrow/index.tsx | 17 ++++++----------- apps/dapp/src/utils/ethers.ts | 10 +++++++++- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx b/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx index 4ab98c3fe..f5631ff16 100644 --- a/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx +++ b/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx @@ -27,7 +27,7 @@ import { TlcChart } from './Chart'; import env from 'constants/env'; import { useConnectWallet } from '@web3-onboard/react'; import Tooltip from 'components/Tooltip/Tooltip'; -import { estimateAndSubmit } from 'utils/ethers'; +import { estimateAndMine } from 'utils/ethers'; export type State = { supplyValue: string; @@ -304,8 +304,7 @@ export const BorrowPage = () => { const populatedTransaction = await tlcContract.populateTransaction.addCollateral(amount, wallet); - const txn = await estimateAndSubmit(signer, populatedTransaction); - const receipt = await txn.wait(); + const receipt = await estimateAndMine(signer, populatedTransaction); openNotification({ title: `Supplied ${state.supplyValue} TEMPLE`, @@ -335,8 +334,7 @@ export const BorrowPage = () => { try { const populatedTransaction = await tlcContract.populateTransaction.removeCollateral(amount, wallet); - const txn = await estimateAndSubmit(signer, populatedTransaction); - const receipt = await txn.wait(); + const receipt = await estimateAndMine(signer, populatedTransaction); openNotification({ title: `Withdrew ${state.withdrawValue} TEMPLE`, @@ -368,8 +366,7 @@ export const BorrowPage = () => { amount, wallet ); - const txn = await estimateAndSubmit(signer, preparedTransaction); - const receipt = await txn.wait(); + const receipt = await estimateAndMine(signer, preparedTransaction); openNotification({ title: `Borrowed ${state.borrowValue} DAI`, @@ -411,8 +408,7 @@ export const BorrowPage = () => { amount, wallet ); - const txn = await estimateAndSubmit(signer, preparedTransaction); - const receipt = await txn.wait(); + const receipt = await estimateAndMine(signer, preparedTransaction); openNotification({ title: `Repaid ${state.repayValue} DAI`, @@ -452,8 +448,7 @@ export const BorrowPage = () => { // Note RepayAll vs Repay const populatedTransaction = await tlcContract.populateTransaction.repayAll(wallet); - const txn = await estimateAndSubmit(signer, populatedTransaction); - const receipt = await txn.wait(); + const receipt = await estimateAndMine(signer, populatedTransaction); openNotification({ title: `Repaid ${ diff --git a/apps/dapp/src/utils/ethers.ts b/apps/dapp/src/utils/ethers.ts index ddce8f866..89323904f 100644 --- a/apps/dapp/src/utils/ethers.ts +++ b/apps/dapp/src/utils/ethers.ts @@ -2,6 +2,14 @@ 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 @@ -18,4 +26,4 @@ async function estimateAndSubmit( return signer.sendTransaction(populatedTransaction); } -export { estimateAndSubmit }; +export { estimateAndMine }; From 525c9160034b8d70632b9212901ac8b5c9f60f68 Mon Sep 17 00:00:00 2001 From: marshall <99344331+marshall2112@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:35:55 -0400 Subject: [PATCH 3/3] Feedback applied --- .../src/components/Pages/Core/DappPages/Borrow/index.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx b/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx index f5631ff16..018e9684f 100644 --- a/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx +++ b/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx @@ -362,11 +362,11 @@ export const BorrowPage = () => { getTokenInfo(state.outputToken).decimals ); try { - const preparedTransaction = await tlcContract.populateTransaction.borrow( + const populatedTransaction = await tlcContract.populateTransaction.borrow( amount, wallet ); - const receipt = await estimateAndMine(signer, preparedTransaction); + const receipt = await estimateAndMine(signer, populatedTransaction); openNotification({ title: `Borrowed ${state.borrowValue} DAI`, @@ -404,11 +404,11 @@ export const BorrowPage = () => { ); // Note Repay vs RepayAll - const preparedTransaction = await tlcContract.populateTransaction.repay( + const populatedTransaction = await tlcContract.populateTransaction.repay( amount, wallet ); - const receipt = await estimateAndMine(signer, preparedTransaction); + const receipt = await estimateAndMine(signer, populatedTransaction); openNotification({ title: `Repaid ${state.repayValue} DAI`,