|
| 1 | +<script lang="ts"> |
| 2 | + import anime from 'animejs' |
| 3 | + import GasCard from './GasCard.svelte' |
| 4 | + import gasModule from '@web3-onboard/gas' |
| 5 | + import { onMount } from 'svelte' |
| 6 | + import { ethers } from 'ethers' |
| 7 | + import type { GasPrice, RPCGasPrice, GasData } from './types' |
| 8 | +
|
| 9 | + let cardBg: any |
| 10 | + let animation: any |
| 11 | + $: if (cardBg) { |
| 12 | + animation = anime({ |
| 13 | + targets: '.Gas--card-bg', |
| 14 | + scaleY: [0, 1], |
| 15 | + duration: 5000, |
| 16 | + loop: false, |
| 17 | + easing: 'linear', |
| 18 | + autoplay: false |
| 19 | + }) |
| 20 | + } |
| 21 | + let ethMainnetGasBlockPrices |
| 22 | + let rpcGasData: RPCGasPrice |
| 23 | + onMount(() => { |
| 24 | + ethMainnetGasBlockPrices = gasModule.stream({ |
| 25 | + chains: ['0x1'], |
| 26 | + apiKey: '7eeb406c-82cb-4348-8ab5-b8cd3b684fff', |
| 27 | + endpoint: 'blockPrices' |
| 28 | + }) |
| 29 | + ethMainnetGasBlockPrices.subscribe(() => { |
| 30 | + async function getEtherGasFromRPC() { |
| 31 | + const INFURA_ID = '03af2f609bfd4782900a84da1ac65000' |
| 32 | + const infuraRPC = `https://mainnet.infura.io/v3/${INFURA_ID}` |
| 33 | + const customHttpProvider = new ethers.providers.JsonRpcProvider(infuraRPC) |
| 34 | + const fee = await customHttpProvider.getFeeData() |
| 35 | + if (fee.gasPrice && fee.maxFeePerGas && fee.maxPriorityFeePerGas) { |
| 36 | + rpcGasData = { |
| 37 | + price: ethers.utils.formatUnits(fee.gasPrice, 'gwei'), |
| 38 | + maxPriorityFeePerGas: ethers.utils.formatUnits(fee.maxPriorityFeePerGas, 'gwei'), |
| 39 | + maxFeePerGas: ethers.utils.formatUnits(fee.maxFeePerGas, 'gwei') |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + getEtherGasFromRPC() |
| 44 | + animation?.restart() |
| 45 | + }) |
| 46 | + }) |
| 47 | +
|
| 48 | + const CONF_PERCENTAGES: number[] = [99, 95, 90, 80, 70] |
| 49 | +
|
| 50 | + const gasPricesDefaults: GasPrice[] = CONF_PERCENTAGES.map((confidence) => ({ |
| 51 | + confidence, |
| 52 | + price: null, |
| 53 | + maxFeePerGas: null, |
| 54 | + maxPriorityFeePerGas: null |
| 55 | + })) |
| 56 | +
|
| 57 | + const GAS_DATA_DEFAULT: GasData = { |
| 58 | + estimatedPrices: gasPricesDefaults, |
| 59 | + baseFeePerGas: null, |
| 60 | + blockNumber: null, |
| 61 | + maxPrice: null, |
| 62 | + estimatedTransactionCount: null, |
| 63 | + seconds: null |
| 64 | + } |
| 65 | +</script> |
| 66 | + |
| 67 | +<div class="Gas px-6 p-4"> |
| 68 | + <div class="flex whitespace-nowrap mb-3 text-sm select-none"> |
| 69 | + <span class="flex items-center">MORE LIKELY</span> |
| 70 | + <span |
| 71 | + class="bg-gradient-to-r from-[#5aea98] via-[#5dea5a] via-[#bcea5a] via-[#ffe600] to-[#eab05a] h-[1px] mx-2 my-3 w-full rounded-full" |
| 72 | + /> |
| 73 | + <span class="flex items-center">LESS LIKELY</span> |
| 74 | + </div> |
| 75 | + <div class="w-0 h-0 text-transparent selection:bg-none">.</div> |
| 76 | + <div class="flex flex-nowrap justify-evenly "> |
| 77 | + {#each ($ethMainnetGasBlockPrices && $ethMainnetGasBlockPrices[0]?.blockPrices[0]?.estimatedPrices) || GAS_DATA_DEFAULT.estimatedPrices as gasData} |
| 78 | + <GasCard bind:cardBg {gasData} rpcGasForDiff={rpcGasData} gasPriceFrom={'bn'} /> |
| 79 | + {/each} |
| 80 | + </div> |
| 81 | + <div class="flex mt-4"> |
| 82 | + <GasCard bind:cardBg gasData={rpcGasData} rpcGasForDiff={undefined} gasPriceFrom={'rpc'} /> |
| 83 | + </div> |
| 84 | +</div> |
0 commit comments