@@ -10,6 +10,10 @@ const { proposerUrl } = global.config;
10
10
11
11
const options = global . config . WEB3_OPTIONS ;
12
12
13
+ // This is hardcoded because we just use it for all estimation.
14
+ const gasEstimateEndpoint =
15
+ 'https://vqxy02tr5e.execute-api.us-east-2.amazonaws.com/production/estimateGas' ;
16
+
13
17
// returns a web3 contract instance
14
18
export async function getContractInstance ( contractName , deployedAddress ) {
15
19
const web3 = Web3 . connection ( ) ;
@@ -41,18 +45,31 @@ export function getContractAddress(contractName) {
41
45
*/
42
46
export async function submitTransaction ( unsignedTransaction , contractAddress , fee ) {
43
47
const web3 = Web3 . connection ( ) ;
44
- let gasPrice = 20000000000 ;
45
- const gas = ( await web3 . eth . getBlock ( 'latest' ) ) . gasLimit ;
46
- const blockGasPrice = 2 * Number ( await web3 . eth . getGasPrice ( ) ) ;
47
- if ( blockGasPrice > gasPrice ) gasPrice = blockGasPrice ;
48
+ const blockGasPrice = Number ( await web3 . eth . getGasPrice ( ) ) ;
48
49
const from = await Web3 . getAccount ( ) ;
50
+ let proposedGasPrice = blockGasPrice ; // This is the backup value if external estimation fails;
51
+ try {
52
+ // Call the endpoint to estimate the gas fee.
53
+ const res = ( await axios . get ( gasEstimateEndpoint ) ) . data . result ;
54
+ proposedGasPrice = Number ( res ?. ProposeGasPrice ) * 10 ** 9 || blockGasPrice ;
55
+ } catch ( error ) {
56
+ console . log ( 'Gas Estimation Failed: ' , error ) ;
57
+ }
58
+ // Estimate the gasLimit
59
+ const gasLimit = await web3 . eth . estimateGas ( {
60
+ from,
61
+ to : contractAddress ,
62
+ data : unsignedTransaction ,
63
+ } ) ;
64
+
65
+ const gasLimitWithBuffer = Math . ceil ( Number ( gasLimit ) * 1.1 ) ; // 10% seems a reasonable buffer.
66
+
49
67
const tx = {
50
68
from,
51
69
to : contractAddress ,
52
70
data : unsignedTransaction ,
53
- gas : web3 . utils . toHex ( gas ) ,
54
- gasPrice : web3 . utils . toHex ( gasPrice ) ,
55
- // maxPriorityFeePerGas: web3.utils.toHex(1 * 10 ** 9),
71
+ gas : web3 . utils . toHex ( gasLimitWithBuffer ) ,
72
+ gasPrice : web3 . utils . toHex ( proposedGasPrice ) ,
56
73
} ;
57
74
58
75
if ( fee ) tx . value = web3 . utils . toHex ( fee ) ;
0 commit comments