@privy-io/js-sdk-core
Version:
Vanilla JS client for the Privy Auth API
2 lines (1 loc) • 2.24 kB
JavaScript
import{BigNumber as e}from"@ethersproject/bignumber";import{PrivyProviderRpcError as t}from"../errors.mjs";import{isOpStack as r,estimateOpStackL1Gas as a}from"../gas/op-stack.mjs";import{QuantityToBigNumber as i}from"./ethers.mjs";import"../types.mjs";import"@ethersproject/contracts";import"@ethersproject/transactions";import"@privy-io/chains";const s=async(e,t)=>{if(void 0===e.type&&(e.type=2),2===e.type){if(!e.maxFeePerGas||!e.maxPriorityFeePerGas){let r=await t.getFeeData();e.maxFeePerGas||(e.maxFeePerGas=r.maxFeePerGas?.toHexString()),e.maxPriorityFeePerGas||(e.maxPriorityFeePerGas=r.maxPriorityFeePerGas?.toHexString())}}else if(!e.gasPrice){let r=await t.getFeeData();e.gasPrice=r.gasPrice?.toHexString()}return e};async function o(s,o){if(!s.gasLimit)throw new t("gasLimit was not successfully set for transaction.");let n=i(s.gasLimit),m=e.from(0);if(2==s.type){if(!s.maxFeePerGas)throw new t("maxFeePerGas was not successfully set for transaction of type 2.");m=i(s.maxFeePerGas)}else{if(!s.gasPrice)throw new t("gasPrice was not successfully set for transaction of type 0 or 1.");m=i(s.gasPrice)}let c=n.mul(m),f=e.from(0);if(s.chainId&&r(s.chainId))try{f=await a(s,o),c=c.add(f)}catch(e){}return{totalGasEstimate:c,l1ExecutionFeeEstimate:f}}async function n(e,t){try{return(await t.estimateGas(e)).toHexString()}catch(r){let a;if("message"in r&&r.message.includes("insufficient funds")&&(console.warn(`Gas estimation failed with error: ${r}. Retrying gas estimation by omitting the 'value'`),a=await async function({txRequest:e,provider:t}){try{let r={...e,value:void 0};return(await t.estimateGas(r)).toHexString()}catch(e){return console.warn(`Gas estimation failed with error: ${e} when omitting the 'value'`),null}}({txRequest:e,provider:t})),a||(console.warn(`Gas estimation failed with error: ${r}. Retrying gas estimation by omitting the 'from'`),a=await async function({txRequest:e,provider:t}){try{let r={...e,from:void 0};return(await t.estimateGas(r)).toHexString()}catch(e){return console.warn(`Gas estimation failed with error: ${e} when omitting the 'from' address`),null}}({txRequest:e,provider:t})),!a)throw r;return a}}export{o as calculateTotalGasEstimate,s as defaultGasForEvmChain,n as defaultGasLimit};