-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1010 from TempleDAO/borrow-gas-estimate
Estimate gas instead of hardcoded
- Loading branch information
Showing
2 changed files
with
59 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |