|
1 | 1 | import { ethers } from "ethers";
|
2 | 2 | import { TransactionTypes } from "ethers/lib/utils";
|
| 3 | +import { FeeEstimation } from "@ledgerhq/coin-framework/api/types"; |
3 | 4 | import ERC20ABI from "../abis/erc20.abi.json";
|
| 5 | +import { ApiFeeData, ApiGasOptions } from "../types"; |
| 6 | + |
| 7 | +export function isApiGasOptions(options: unknown): options is ApiGasOptions { |
| 8 | + if (!options || typeof options !== "object") return false; |
| 9 | + |
| 10 | + return ( |
| 11 | + "slow" in options && |
| 12 | + isApiFeeData(options.slow) && |
| 13 | + "medium" in options && |
| 14 | + isApiFeeData(options.medium) && |
| 15 | + "fast" in options && |
| 16 | + isApiFeeData(options.fast) |
| 17 | + ); |
| 18 | +} |
| 19 | + |
| 20 | +export function isApiFeeData(fees: unknown): fees is ApiFeeData { |
| 21 | + if (!fees || typeof fees !== "object") return false; |
| 22 | + |
| 23 | + const isBigIntOrNull = (value: unknown): boolean => value === null || typeof value === "bigint"; |
| 24 | + |
| 25 | + return ( |
| 26 | + "maxFeePerGas" in fees && |
| 27 | + isBigIntOrNull(fees.maxFeePerGas) && |
| 28 | + "maxPriorityFeePerGas" in fees && |
| 29 | + isBigIntOrNull(fees.maxPriorityFeePerGas) && |
| 30 | + "gasPrice" in fees && |
| 31 | + isBigIntOrNull(fees.gasPrice) && |
| 32 | + "nextBaseFee" in fees && |
| 33 | + isBigIntOrNull(fees.nextBaseFee) |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +type LegacyFeeEstimation = FeeEstimation & { parameters: { gasPrice: bigint } }; |
| 38 | +type Eip1559FeeEstimation = FeeEstimation & { |
| 39 | + parameters: { maxFeePerGas: bigint; maxPriorityFeePerGas: bigint }; |
| 40 | +}; |
| 41 | + |
| 42 | +export function isLegacyFeeEstimation( |
| 43 | + estimation: FeeEstimation, |
| 44 | +): estimation is LegacyFeeEstimation { |
| 45 | + return typeof estimation.parameters?.gasPrice === "bigint"; |
| 46 | +} |
| 47 | + |
| 48 | +export function isEip1559FeeEstimation( |
| 49 | + estimation: FeeEstimation, |
| 50 | +): estimation is Eip1559FeeEstimation { |
| 51 | + return ( |
| 52 | + typeof estimation.parameters?.maxFeePerGas === "bigint" && |
| 53 | + typeof estimation.parameters?.maxPriorityFeePerGas === "bigint" |
| 54 | + ); |
| 55 | +} |
4 | 56 |
|
5 | 57 | export function getTransactionType(intentType: string): TransactionTypes {
|
6 | 58 | if (!["send-legacy", "send-eip1559"].includes(intentType)) {
|
|
0 commit comments