UNPKG

@privy-io/js-sdk-core

Version:

Vanilla JS client for the Privy Auth API

2 lines (1 loc) 1.39 kB
import{parseUnits as e}from"@ethersproject/units";import t from"fetch-retry";import{polygon as r,polygonAmoy as a}from"@privy-io/chains";let o=t(fetch,{retries:3,retryDelay:500});const i=e=>[r.id,a.id].includes(e);let s=t=>({maxPriorityFee:e(t.maxPriorityFee.toFixed(9),"gwei").toHexString(),maxFee:e(t.maxFee.toFixed(9),"gwei").toHexString()}),n=e=>({safeLow:s(e.safeLow),standard:s(e.standard),fast:s(e.fast)});const c=async e=>{let t="";switch(e){case r.id:t="https://gasstation.polygon.technology/v2";break;case a.id:t="https://gasstation.polygon.technology/amoy";break;default:throw Error(`chainId ${e} does not support polygon gas stations`)}let i=await o(t),s=await i.json();if(i.status>399)throw s;return n(s)};async function d(e){if(!i(e.chainId))throw Error("Invalid chain ID for Polygon gas estimation.");if(void 0===e.type&&(e.type=2),e.maxPriorityFeePerGas&&e.maxFeePerGas)return e;try{let{standard:t}=await c(e.chainId);e.maxPriorityFeePerGas||(e.maxPriorityFeePerGas=t.maxPriorityFee),e.maxFeePerGas||(e.maxFeePerGas=t.maxFee),e.gasPrice&&(console.warn("`gasPrice` is not supported on this chain and will be ignored. Use `maxPriorityFeePerGas` and/or `maxFeePerGas` instead."),delete e.gasPrice)}catch(e){throw Error(`Failed to set gas prices from Polygon gas station with error: ${e}.`)}return e}export{d as defaultGasForPolygon,c as getPolygonGasStationFees,i as isPolygon};