@b3dotfun/sdk 0.0.88-alpha.8 → 0.0.88-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/anyspend/constants/index.d.ts +2 -0
- package/dist/cjs/anyspend/constants/index.js +3 -1
- package/dist/cjs/anyspend/index.d.ts +2 -0
- package/dist/cjs/anyspend/index.js +3 -0
- package/dist/cjs/anyspend/react/components/AnySpend.js +4 -1
- package/dist/cjs/anyspend/react/components/AnySpendCustomExactIn.js +22 -18
- package/dist/cjs/anyspend/react/components/common/GasIndicator.d.ts +6 -0
- package/dist/cjs/anyspend/react/components/common/GasIndicator.js +34 -0
- package/dist/cjs/anyspend/react/hooks/index.d.ts +1 -0
- package/dist/cjs/anyspend/react/hooks/index.js +1 -0
- package/dist/cjs/anyspend/react/hooks/useGasPrice.d.ts +37 -0
- package/dist/cjs/anyspend/react/hooks/useGasPrice.js +43 -0
- package/dist/cjs/anyspend/services/gas.d.ts +21 -0
- package/dist/cjs/anyspend/services/gas.js +65 -0
- package/dist/cjs/anyspend/types/gas.d.ts +61 -0
- package/dist/cjs/anyspend/types/gas.js +2 -0
- package/dist/esm/anyspend/constants/index.d.ts +2 -0
- package/dist/esm/anyspend/constants/index.js +2 -0
- package/dist/esm/anyspend/index.d.ts +2 -0
- package/dist/esm/anyspend/index.js +3 -0
- package/dist/esm/anyspend/react/components/AnySpend.js +5 -2
- package/dist/esm/anyspend/react/components/AnySpendCustomExactIn.js +5 -1
- package/dist/esm/anyspend/react/components/common/GasIndicator.d.ts +6 -0
- package/dist/esm/anyspend/react/components/common/GasIndicator.js +31 -0
- package/dist/esm/anyspend/react/hooks/index.d.ts +1 -0
- package/dist/esm/anyspend/react/hooks/index.js +1 -0
- package/dist/esm/anyspend/react/hooks/useGasPrice.d.ts +37 -0
- package/dist/esm/anyspend/react/hooks/useGasPrice.js +40 -0
- package/dist/esm/anyspend/services/gas.d.ts +21 -0
- package/dist/esm/anyspend/services/gas.js +59 -0
- package/dist/esm/anyspend/types/gas.d.ts +61 -0
- package/dist/esm/anyspend/types/gas.js +1 -0
- package/dist/styles/index.css +1 -1
- package/dist/types/anyspend/constants/index.d.ts +2 -0
- package/dist/types/anyspend/index.d.ts +2 -0
- package/dist/types/anyspend/react/components/common/GasIndicator.d.ts +6 -0
- package/dist/types/anyspend/react/hooks/index.d.ts +1 -0
- package/dist/types/anyspend/react/hooks/useGasPrice.d.ts +37 -0
- package/dist/types/anyspend/services/gas.d.ts +21 -0
- package/dist/types/anyspend/types/gas.d.ts +61 -0
- package/package.json +1 -1
- package/src/anyspend/constants/index.ts +3 -0
- package/src/anyspend/index.ts +4 -0
- package/src/anyspend/react/components/AnySpend.tsx +10 -0
- package/src/anyspend/react/components/AnySpendCustomExactIn.tsx +10 -0
- package/src/anyspend/react/components/common/GasIndicator.tsx +59 -0
- package/src/anyspend/react/hooks/index.ts +1 -0
- package/src/anyspend/react/hooks/useGasPrice.ts +70 -0
- package/src/anyspend/services/gas.test.ts +31 -0
- package/src/anyspend/services/gas.ts +73 -0
- package/src/anyspend/types/gas.ts +66 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/** Gas level classification based on historical percentiles */
|
|
2
|
+
export type GasLevel = "low" | "normal" | "elevated" | "high" | "spike";
|
|
3
|
+
/** Gas prices in Gwei for different speed tiers */
|
|
4
|
+
export interface GasPrices {
|
|
5
|
+
low: string;
|
|
6
|
+
standard: string;
|
|
7
|
+
fast: string;
|
|
8
|
+
instant?: string;
|
|
9
|
+
}
|
|
10
|
+
/** EIP-1559 specific gas data */
|
|
11
|
+
export interface Eip1559Data {
|
|
12
|
+
baseFee: string;
|
|
13
|
+
maxPriorityFee: GasPrices;
|
|
14
|
+
maxFee: GasPrices;
|
|
15
|
+
}
|
|
16
|
+
/** Spike analysis comparing current gas to historical data */
|
|
17
|
+
export interface GasSpikeAnalysis {
|
|
18
|
+
level: GasLevel;
|
|
19
|
+
percentile: number;
|
|
20
|
+
/** Ratio to 1h median (primary spike detection) */
|
|
21
|
+
vs1h: number;
|
|
22
|
+
/** Ratio to 4h median (short-term context) */
|
|
23
|
+
vs4h: number;
|
|
24
|
+
/** Ratio to 24h median (daily context) */
|
|
25
|
+
vs24h: number;
|
|
26
|
+
recommendation: string;
|
|
27
|
+
}
|
|
28
|
+
/** Full gas response from the gas oracle */
|
|
29
|
+
export interface GasOracleResponse {
|
|
30
|
+
chainId: number;
|
|
31
|
+
chainName: string;
|
|
32
|
+
timestamp: string;
|
|
33
|
+
blockNumber?: number;
|
|
34
|
+
source: "blocknative" | "rpc";
|
|
35
|
+
gasPrice: GasPrices;
|
|
36
|
+
eip1559?: Eip1559Data;
|
|
37
|
+
analysis: GasSpikeAnalysis;
|
|
38
|
+
cached: boolean;
|
|
39
|
+
cacheAge?: number;
|
|
40
|
+
}
|
|
41
|
+
/** Simplified gas data for UI display */
|
|
42
|
+
export interface GasPriceData {
|
|
43
|
+
chainId: number;
|
|
44
|
+
chainName: string;
|
|
45
|
+
/** Standard gas price in Gwei */
|
|
46
|
+
gasPriceGwei: string;
|
|
47
|
+
/** Base fee in Gwei (EIP-1559 chains) */
|
|
48
|
+
baseFeeGwei?: string;
|
|
49
|
+
/** Gas level classification */
|
|
50
|
+
level: GasLevel;
|
|
51
|
+
/** Whether gas is currently spiking (elevated, high, or spike) */
|
|
52
|
+
isSpike: boolean;
|
|
53
|
+
/** Human-readable recommendation */
|
|
54
|
+
recommendation: string;
|
|
55
|
+
/** Ratio to recent median (1 = normal, >1.5 = elevated) */
|
|
56
|
+
vsMedian: number;
|
|
57
|
+
/** Data source */
|
|
58
|
+
source: "blocknative" | "rpc";
|
|
59
|
+
/** Timestamp of the data */
|
|
60
|
+
timestamp: string;
|
|
61
|
+
}
|
package/package.json
CHANGED
|
@@ -4,6 +4,9 @@ import { components } from "../types/api";
|
|
|
4
4
|
export const ANYSPEND_MAINNET_BASE_URL = process.env.NEXT_PUBLIC_ANYSPEND_BASE_URL || "https://mainnet.anyspend.com";
|
|
5
5
|
|
|
6
6
|
export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
7
|
+
export const GAS_ORACLE_BASE_URL = process.env.NEXT_PUBLIC_GAS_ORACLE_URL || "https://gas-oracle.sean-430.workers.dev";
|
|
8
|
+
|
|
9
|
+
export const RELAY_ETH_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
7
10
|
export const RELAY_SOL_ADDRESS = "11111111111111111111111111111111";
|
|
8
11
|
|
|
9
12
|
export const RELAY_SOLANA_MAINNET_CHAIN_ID = 792703809;
|
package/src/anyspend/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Types
|
|
2
2
|
export * from "./types/api";
|
|
3
|
+
export * from "./types/gas";
|
|
3
4
|
|
|
4
5
|
// Utils
|
|
5
6
|
export * from "./utils/address";
|
|
@@ -14,6 +15,9 @@ export * from "./utils/validation";
|
|
|
14
15
|
// Constants
|
|
15
16
|
export * from "./constants";
|
|
16
17
|
|
|
18
|
+
// Services
|
|
19
|
+
export * from "./services/gas";
|
|
20
|
+
|
|
17
21
|
// Abis
|
|
18
22
|
export * from "./abis/abiUsdcBase";
|
|
19
23
|
export * from "./abis/erc20Staking";
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
useAnyspendCreateOrder,
|
|
15
15
|
useAnyspendOrderAndTransactions,
|
|
16
16
|
useAnyspendQuote,
|
|
17
|
+
useGasPrice,
|
|
17
18
|
useGeoOnrampOptions,
|
|
18
19
|
} from "@b3dotfun/sdk/anyspend/react";
|
|
19
20
|
import {
|
|
@@ -55,6 +56,7 @@ import { CryptoPaySection } from "./common/CryptoPaySection";
|
|
|
55
56
|
import { CryptoReceiveSection } from "./common/CryptoReceiveSection";
|
|
56
57
|
import { FeeDetailPanel } from "./common/FeeDetailPanel";
|
|
57
58
|
import { FiatPaymentMethod, FiatPaymentMethodComponent } from "./common/FiatPaymentMethod";
|
|
59
|
+
import { GasIndicator } from "./common/GasIndicator";
|
|
58
60
|
import { OrderDetails, OrderDetailsLoadingView } from "./common/OrderDetails";
|
|
59
61
|
import { OrderHistory } from "./common/OrderHistory";
|
|
60
62
|
import { PanelOnramp } from "./common/PanelOnramp";
|
|
@@ -557,6 +559,9 @@ function AnySpendInner({
|
|
|
557
559
|
const { geoData, coinbaseAvailablePaymentMethods, stripeOnrampSupport, stripeWeb2Support } =
|
|
558
560
|
useGeoOnrampOptions(srcAmountOnRamp);
|
|
559
561
|
|
|
562
|
+
// Get gas price for source chain (where the user pays from)
|
|
563
|
+
const { gasPrice: gasPriceData, isLoading: isLoadingGas } = useGasPrice(selectedSrcChainId);
|
|
564
|
+
|
|
560
565
|
// Helper function to map payment method to onramp vendor
|
|
561
566
|
const getOnrampVendor = (paymentMethod: FiatPaymentMethod): "coinbase" | "stripe" | "stripe-web2" | undefined => {
|
|
562
567
|
switch (paymentMethod) {
|
|
@@ -1243,6 +1248,11 @@ function AnySpendInner({
|
|
|
1243
1248
|
)}
|
|
1244
1249
|
</div>
|
|
1245
1250
|
|
|
1251
|
+
{/* Gas indicator - show when source chain has gas data */}
|
|
1252
|
+
{gasPriceData && !isLoadingGas && activeTab === "crypto" && (
|
|
1253
|
+
<GasIndicator gasPrice={gasPriceData} className="mt-2 w-full" />
|
|
1254
|
+
)}
|
|
1255
|
+
|
|
1246
1256
|
{/* Main button section */}
|
|
1247
1257
|
<motion.div
|
|
1248
1258
|
initial={{ opacity: 0, y: 20, filter: "blur(10px)" }}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useGasPrice } from "@b3dotfun/sdk/anyspend/react";
|
|
1
2
|
import { components } from "@b3dotfun/sdk/anyspend/types/api";
|
|
2
3
|
import { GetQuoteResponse } from "@b3dotfun/sdk/anyspend/types/api_req_res";
|
|
3
4
|
import { normalizeAddress } from "@b3dotfun/sdk/anyspend/utils";
|
|
@@ -25,6 +26,7 @@ import { CryptoPaymentMethod, CryptoPaymentMethodType } from "./common/CryptoPay
|
|
|
25
26
|
import { CryptoReceiveSection } from "./common/CryptoReceiveSection";
|
|
26
27
|
import { FeeDetailPanel } from "./common/FeeDetailPanel";
|
|
27
28
|
import { FiatPaymentMethod, FiatPaymentMethodComponent } from "./common/FiatPaymentMethod";
|
|
29
|
+
import { GasIndicator } from "./common/GasIndicator";
|
|
28
30
|
import { OrderDetails } from "./common/OrderDetails";
|
|
29
31
|
import { PanelOnramp } from "./common/PanelOnramp";
|
|
30
32
|
import { PointsDetailPanel } from "./common/PointsDetailPanel";
|
|
@@ -168,6 +170,9 @@ function AnySpendCustomExactInInner({
|
|
|
168
170
|
|
|
169
171
|
const { connectedEOAWallet } = useAccountWallet();
|
|
170
172
|
const setActiveWallet = useSetActiveWallet();
|
|
173
|
+
|
|
174
|
+
// Get gas price for source chain (where the user pays from)
|
|
175
|
+
const { gasPrice: gasPriceData, isLoading: isLoadingGas } = useGasPrice(selectedSrcChainId);
|
|
171
176
|
const appliedPreferEoa = useRef(false);
|
|
172
177
|
|
|
173
178
|
useEffect(() => {
|
|
@@ -438,6 +443,11 @@ function AnySpendCustomExactInInner({
|
|
|
438
443
|
</ShinyButton>
|
|
439
444
|
</motion.div>
|
|
440
445
|
|
|
446
|
+
{/* Gas indicator - show when source chain has gas data */}
|
|
447
|
+
{gasPriceData && !isLoadingGas && paymentType === "crypto" && (
|
|
448
|
+
<GasIndicator gasPrice={gasPriceData} className="mt-2 w-full" />
|
|
449
|
+
)}
|
|
450
|
+
|
|
441
451
|
{mainFooter ? mainFooter : null}
|
|
442
452
|
</div>
|
|
443
453
|
);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { cn } from "@b3dotfun/sdk/shared/utils/cn";
|
|
4
|
+
import { motion } from "motion/react";
|
|
5
|
+
import type { GasPriceData } from "../../../types/gas";
|
|
6
|
+
|
|
7
|
+
export interface GasIndicatorProps {
|
|
8
|
+
gasPrice: GasPriceData;
|
|
9
|
+
className?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const LEVEL_LABELS: Record<GasPriceData["level"], string> = {
|
|
13
|
+
low: "Low",
|
|
14
|
+
normal: "Normal",
|
|
15
|
+
elevated: "Elevated",
|
|
16
|
+
high: "High",
|
|
17
|
+
spike: "Spike",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const LEVEL_STYLES: Record<GasPriceData["level"], string> = {
|
|
21
|
+
low: "bg-green-500/20 text-green-500",
|
|
22
|
+
normal: "bg-as-surface-tertiary text-as-secondary",
|
|
23
|
+
elevated: "bg-yellow-500/20 text-yellow-600",
|
|
24
|
+
high: "bg-orange-500/20 text-orange-500",
|
|
25
|
+
spike: "bg-red-500/20 text-red-500",
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function formatGasPrice(gweiString: string): string {
|
|
29
|
+
const gwei = parseFloat(gweiString);
|
|
30
|
+
if (gwei < 0.001) return "<0.001";
|
|
31
|
+
if (gwei < 1) return gwei.toFixed(3);
|
|
32
|
+
if (gwei < 10) return gwei.toFixed(2);
|
|
33
|
+
return gwei.toFixed(1);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function GasIndicator({ gasPrice, className }: GasIndicatorProps) {
|
|
37
|
+
return (
|
|
38
|
+
<motion.div
|
|
39
|
+
initial={{ opacity: 0, y: 10 }}
|
|
40
|
+
animate={{ opacity: 1, y: 0 }}
|
|
41
|
+
transition={{ duration: 0.2 }}
|
|
42
|
+
className={cn(
|
|
43
|
+
"flex items-center justify-between rounded-lg px-3 py-2",
|
|
44
|
+
gasPrice.isSpike ? "bg-yellow-500/10" : "bg-as-surface-secondary",
|
|
45
|
+
className,
|
|
46
|
+
)}
|
|
47
|
+
>
|
|
48
|
+
<div className="flex items-center gap-2">
|
|
49
|
+
<span className="text-as-secondary text-xs">Gas on {gasPrice.chainName}</span>
|
|
50
|
+
</div>
|
|
51
|
+
<div className="flex items-center gap-2">
|
|
52
|
+
<span className={cn("rounded px-1.5 py-0.5 text-xs font-medium", LEVEL_STYLES[gasPrice.level])}>
|
|
53
|
+
{LEVEL_LABELS[gasPrice.level]}
|
|
54
|
+
</span>
|
|
55
|
+
<span className="text-as-primary text-xs font-medium">{formatGasPrice(gasPrice.gasPriceGwei)} Gwei</span>
|
|
56
|
+
</div>
|
|
57
|
+
</motion.div>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
@@ -7,6 +7,7 @@ export * from "./useAnyspendQuote";
|
|
|
7
7
|
export * from "./useAnyspendTokens";
|
|
8
8
|
export * from "./useCoinbaseOnrampOptions";
|
|
9
9
|
export * from "./useConnectedUserProfile";
|
|
10
|
+
export * from "./useGasPrice";
|
|
10
11
|
export * from "./useGeoOnrampOptions";
|
|
11
12
|
export * from "./useGetGeo";
|
|
12
13
|
export * from "./useHyperliquidTransfer";
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { useQuery } from "@tanstack/react-query";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import { gasService, isGasOracleSupported } from "../../services/gas";
|
|
4
|
+
import type { GasPriceData } from "../../types/gas";
|
|
5
|
+
|
|
6
|
+
export interface UseGasPriceOptions {
|
|
7
|
+
/** Refetch interval in ms (default: 10000 = 10s) */
|
|
8
|
+
refetchInterval?: number;
|
|
9
|
+
/** Whether to enable the query (default: true if chainId is supported) */
|
|
10
|
+
enabled?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface UseGasPriceResult {
|
|
14
|
+
/** Gas price data */
|
|
15
|
+
gasPrice: GasPriceData | undefined;
|
|
16
|
+
/** Whether the query is loading */
|
|
17
|
+
isLoading: boolean;
|
|
18
|
+
/** Whether there's an error */
|
|
19
|
+
isError: boolean;
|
|
20
|
+
/** Error object if any */
|
|
21
|
+
error: Error | null;
|
|
22
|
+
/** Whether gas is currently spiking */
|
|
23
|
+
isSpike: boolean;
|
|
24
|
+
/** Refetch function */
|
|
25
|
+
refetch: () => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* React hook to fetch current gas price for a chain.
|
|
30
|
+
*
|
|
31
|
+
* @param chainId - The chain ID to fetch gas price for
|
|
32
|
+
* @param options - Optional configuration
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* const { gasPrice, isSpike, isLoading } = useGasPrice(8453); // Base
|
|
37
|
+
*
|
|
38
|
+
* if (isSpike) {
|
|
39
|
+
* return <Warning>Gas prices are high: {gasPrice?.recommendation}</Warning>;
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export function useGasPrice(chainId: number | undefined, options: UseGasPriceOptions = {}): UseGasPriceResult {
|
|
44
|
+
const { refetchInterval = 10000, enabled } = options;
|
|
45
|
+
|
|
46
|
+
const isSupported = chainId !== undefined && isGasOracleSupported(chainId);
|
|
47
|
+
const queryEnabled = enabled ?? isSupported;
|
|
48
|
+
|
|
49
|
+
const { data, isLoading, isError, error, refetch } = useQuery({
|
|
50
|
+
queryKey: ["gasPrice", chainId],
|
|
51
|
+
queryFn: () => gasService.fetch(chainId as number),
|
|
52
|
+
enabled: queryEnabled && chainId !== undefined,
|
|
53
|
+
refetchInterval,
|
|
54
|
+
staleTime: 5000, // Consider data fresh for 5s
|
|
55
|
+
retry: 2,
|
|
56
|
+
refetchOnWindowFocus: true,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
return useMemo(
|
|
60
|
+
() => ({
|
|
61
|
+
gasPrice: data,
|
|
62
|
+
isLoading,
|
|
63
|
+
isError,
|
|
64
|
+
error: error as Error | null,
|
|
65
|
+
isSpike: data?.isSpike ?? false,
|
|
66
|
+
refetch,
|
|
67
|
+
}),
|
|
68
|
+
[data, isLoading, isError, error, refetch],
|
|
69
|
+
);
|
|
70
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { gasService, isGasOracleSupported } from "./gas";
|
|
3
|
+
|
|
4
|
+
describe("gasService", () => {
|
|
5
|
+
it("should check supported chains", () => {
|
|
6
|
+
expect(isGasOracleSupported(8453)).toBe(true); // Base
|
|
7
|
+
expect(isGasOracleSupported(1)).toBe(true); // Ethereum
|
|
8
|
+
expect(isGasOracleSupported(999999)).toBe(false); // Unknown
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("should fetch gas price for Base", async () => {
|
|
12
|
+
const gas = await gasService.fetch(8453);
|
|
13
|
+
|
|
14
|
+
console.log("Gas data:", gas);
|
|
15
|
+
|
|
16
|
+
expect(gas.chainId).toBe(8453);
|
|
17
|
+
expect(gas.chainName).toBe("Base");
|
|
18
|
+
expect(gas.level).toMatch(/low|normal|elevated|high|spike/);
|
|
19
|
+
expect(typeof gas.gasPriceGwei).toBe("string");
|
|
20
|
+
expect(typeof gas.isSpike).toBe("boolean");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should fetch gas price for Ethereum", async () => {
|
|
24
|
+
const gas = await gasService.fetch(1);
|
|
25
|
+
|
|
26
|
+
console.log("ETH Gas:", gas);
|
|
27
|
+
|
|
28
|
+
expect(gas.chainId).toBe(1);
|
|
29
|
+
expect(gas.source).toBe("blocknative"); // ETH uses Blocknative
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { GAS_ORACLE_BASE_URL } from "../constants";
|
|
2
|
+
import type { GasOracleResponse, GasPriceData } from "../types/gas";
|
|
3
|
+
|
|
4
|
+
/** Supported chain IDs for gas oracle */
|
|
5
|
+
export const GAS_ORACLE_SUPPORTED_CHAINS = [
|
|
6
|
+
1, // Ethereum
|
|
7
|
+
137, // Polygon
|
|
8
|
+
42161, // Arbitrum
|
|
9
|
+
8453, // Base
|
|
10
|
+
56, // BSC
|
|
11
|
+
10, // Optimism
|
|
12
|
+
43114, // Avalanche
|
|
13
|
+
8333, // B3
|
|
14
|
+
2741, // Abstract
|
|
15
|
+
4689, // IoTeX
|
|
16
|
+
3338, // Peaq
|
|
17
|
+
1329, // Sei
|
|
18
|
+
] as const;
|
|
19
|
+
|
|
20
|
+
export type GasOracleSupportedChainId = (typeof GAS_ORACLE_SUPPORTED_CHAINS)[number];
|
|
21
|
+
|
|
22
|
+
/** Check if a chain is supported by the gas oracle */
|
|
23
|
+
export function isGasOracleSupported(chainId: number): chainId is GasOracleSupportedChainId {
|
|
24
|
+
return GAS_ORACLE_SUPPORTED_CHAINS.includes(chainId as GasOracleSupportedChainId);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Fetch gas price data from the gas oracle */
|
|
28
|
+
export async function fetchGasPrice(chainId: number): Promise<GasOracleResponse> {
|
|
29
|
+
const response = await fetch(`${GAS_ORACLE_BASE_URL}/gas/${chainId}`);
|
|
30
|
+
|
|
31
|
+
if (!response.ok) {
|
|
32
|
+
throw new Error(`Failed to fetch gas price for chain ${chainId}: ${response.status}`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return response.json();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Transform raw oracle response to simplified UI data */
|
|
39
|
+
export function toGasPriceData(response: GasOracleResponse): GasPriceData {
|
|
40
|
+
const { analysis } = response;
|
|
41
|
+
const isSpike = analysis.level === "elevated" || analysis.level === "high" || analysis.level === "spike";
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
chainId: response.chainId,
|
|
45
|
+
chainName: response.chainName,
|
|
46
|
+
gasPriceGwei: response.gasPrice.standard,
|
|
47
|
+
baseFeeGwei: response.eip1559?.baseFee,
|
|
48
|
+
level: analysis.level,
|
|
49
|
+
isSpike,
|
|
50
|
+
recommendation: analysis.recommendation,
|
|
51
|
+
vsMedian: analysis.vs1h, // Use 1h as primary comparison
|
|
52
|
+
source: response.source,
|
|
53
|
+
timestamp: response.timestamp,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Gas service for fetching and transforming gas data */
|
|
58
|
+
export const gasService = {
|
|
59
|
+
/** Fetch raw gas oracle response */
|
|
60
|
+
fetchRaw: fetchGasPrice,
|
|
61
|
+
|
|
62
|
+
/** Fetch and transform to UI-friendly format */
|
|
63
|
+
fetch: async (chainId: number): Promise<GasPriceData> => {
|
|
64
|
+
const response = await fetchGasPrice(chainId);
|
|
65
|
+
return toGasPriceData(response);
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
/** Check if chain is supported */
|
|
69
|
+
isSupported: isGasOracleSupported,
|
|
70
|
+
|
|
71
|
+
/** List of supported chain IDs */
|
|
72
|
+
supportedChains: GAS_ORACLE_SUPPORTED_CHAINS,
|
|
73
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** Gas level classification based on historical percentiles */
|
|
2
|
+
export type GasLevel = "low" | "normal" | "elevated" | "high" | "spike";
|
|
3
|
+
|
|
4
|
+
/** Gas prices in Gwei for different speed tiers */
|
|
5
|
+
export interface GasPrices {
|
|
6
|
+
low: string;
|
|
7
|
+
standard: string;
|
|
8
|
+
fast: string;
|
|
9
|
+
instant?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** EIP-1559 specific gas data */
|
|
13
|
+
export interface Eip1559Data {
|
|
14
|
+
baseFee: string;
|
|
15
|
+
maxPriorityFee: GasPrices;
|
|
16
|
+
maxFee: GasPrices;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Spike analysis comparing current gas to historical data */
|
|
20
|
+
export interface GasSpikeAnalysis {
|
|
21
|
+
level: GasLevel;
|
|
22
|
+
percentile: number;
|
|
23
|
+
/** Ratio to 1h median (primary spike detection) */
|
|
24
|
+
vs1h: number;
|
|
25
|
+
/** Ratio to 4h median (short-term context) */
|
|
26
|
+
vs4h: number;
|
|
27
|
+
/** Ratio to 24h median (daily context) */
|
|
28
|
+
vs24h: number;
|
|
29
|
+
recommendation: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Full gas response from the gas oracle */
|
|
33
|
+
export interface GasOracleResponse {
|
|
34
|
+
chainId: number;
|
|
35
|
+
chainName: string;
|
|
36
|
+
timestamp: string;
|
|
37
|
+
blockNumber?: number;
|
|
38
|
+
source: "blocknative" | "rpc";
|
|
39
|
+
gasPrice: GasPrices;
|
|
40
|
+
eip1559?: Eip1559Data;
|
|
41
|
+
analysis: GasSpikeAnalysis;
|
|
42
|
+
cached: boolean;
|
|
43
|
+
cacheAge?: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Simplified gas data for UI display */
|
|
47
|
+
export interface GasPriceData {
|
|
48
|
+
chainId: number;
|
|
49
|
+
chainName: string;
|
|
50
|
+
/** Standard gas price in Gwei */
|
|
51
|
+
gasPriceGwei: string;
|
|
52
|
+
/** Base fee in Gwei (EIP-1559 chains) */
|
|
53
|
+
baseFeeGwei?: string;
|
|
54
|
+
/** Gas level classification */
|
|
55
|
+
level: GasLevel;
|
|
56
|
+
/** Whether gas is currently spiking (elevated, high, or spike) */
|
|
57
|
+
isSpike: boolean;
|
|
58
|
+
/** Human-readable recommendation */
|
|
59
|
+
recommendation: string;
|
|
60
|
+
/** Ratio to recent median (1 = normal, >1.5 = elevated) */
|
|
61
|
+
vsMedian: number;
|
|
62
|
+
/** Data source */
|
|
63
|
+
source: "blocknative" | "rpc";
|
|
64
|
+
/** Timestamp of the data */
|
|
65
|
+
timestamp: string;
|
|
66
|
+
}
|