@openocean.finance/widget-sdk
Version:
OpenOcean Any-to-Any Cross-Chain-Swap SDK
67 lines • 2.46 kB
JavaScript
import { getBlock } from 'viem/actions';
import { config } from '../../config.js';
import { median } from '../../utils/median.js';
export const convertExtendedChain = (chain) => ({
...chain,
...chain.metamask,
blockExplorers: chain.metamask.blockExplorerUrls.reduce((blockExplorers, blockExplorer, index) => {
blockExplorers[index === 0 ? 'default' : `${index}`] = {
name: blockExplorer,
url: blockExplorer,
};
return blockExplorers;
}, {}),
name: chain.metamask.chainName,
rpcUrls: {
default: { http: chain.metamask.rpcUrls },
public: { http: chain.metamask.rpcUrls },
},
contracts: {
...(chain.multicallAddress
? { multicall3: { address: chain.multicallAddress } }
: undefined),
},
});
export function isExtendedChain(chain) {
return (typeof chain === 'object' &&
chain !== null &&
'key' in chain &&
'chainType' in chain &&
'coin' in chain &&
'mainnet' in chain &&
'logoURI' in chain &&
typeof chain.metamask === 'object' &&
chain.metamask !== null &&
typeof chain.nativeToken === 'object' &&
chain.nativeToken !== null);
}
export const getMaxPriorityFeePerGas = async (client) => {
const block = await getBlock(client, {
includeTransactions: true,
});
const maxPriorityFeePerGasList = block.transactions
.filter((tx) => tx.maxPriorityFeePerGas)
.map((tx) => tx.maxPriorityFeePerGas);
if (!maxPriorityFeePerGasList.length) {
return;
}
let maxPriorityFeePerGasSum = 0n;
for (const value of maxPriorityFeePerGasList) {
maxPriorityFeePerGasSum += value;
}
const maxPriorityFeePerGasMedian = median(maxPriorityFeePerGasList) ?? 0n;
const maxPriorityFeePerGasAvg = maxPriorityFeePerGasSum / BigInt(maxPriorityFeePerGasList.length);
return maxPriorityFeePerGasMedian > maxPriorityFeePerGasAvg
? maxPriorityFeePerGasAvg
: maxPriorityFeePerGasMedian;
};
// Multicall
export const getMulticallAddress = async (chainId) => {
const chains = await config.getChains();
return chains.find((chain) => chain.id === chainId)
?.multicallAddress;
};
// Modified viem retryDelay exponential backoff function.
export const retryDelay = ({ count }) => Math.min(~~(1 << count) * 200, 3000);
export const retryCount = 30;
//# sourceMappingURL=utils.js.map