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