dev
dart 106 lines 3.87 KB
Raw
1 import "package:cw_evm/evm_chain_transaction_priority.dart";
2 import "package:web3dart/web3dart.dart" show EtherAmount, EtherUnit;
3
4 /// Utility class for chain-specific EVM chain operations
5 class EVMChainUtils {
6 static int getTotalPriorityFee(EVMChainTransactionPriority priority, int chainId) => switch (chainId) {
7 1 => _ethereumPriorityFee(priority),
8 137 => _polygonPriorityFee(priority),
9 8453 => _basePriorityFee(priority),
10 56 => _ethereumPriorityFee(priority),
11 42161 => 0, // Arbitrum doesn't use priority fees
12 _ => _ethereumPriorityFee(priority),
13 };
14
15 static bool hasPriorityFee(int chainId) => switch (chainId) {
16 42161 => false, // Arbitrum doesn't use priority fees
17 _ => true,
18 };
19
20 static int computeBufferedMaxFeePerGasWei({
21 required int? gasBaseFee,
22 required int gasPrice,
23 required int priorityFeeWei,
24 required bool chainHasPriorityFee,
25 }) {
26 if (gasBaseFee != null && gasBaseFee > 0) {
27 final baseFeeWithPriority = gasBaseFee + priorityFeeWei;
28 final bufferMultiplier = chainHasPriorityFee ? 115 : 105;
29 final bufferPercent = (baseFeeWithPriority * bufferMultiplier) ~/ 100;
30 final bufferMin = baseFeeWithPriority + (baseFeeWithPriority ~/ 100);
31 return bufferPercent > bufferMin ? bufferPercent : bufferMin;
32 }
33 return gasPrice + priorityFeeWei;
34 }
35
36 static String getTransactionHistoryFileName(int chainId) => switch (chainId) {
37 1 => "transactions.json", // Ethereum
38 137 => "polygon_transactions.json",
39 8453 => "base_transactions.json",
40 42161 => "arbitrum_transactions.json",
41 56 => "bsc_transactions.json",
42 _ => "transactions_$chainId.json", // Generic format for other chains
43 };
44
45 /// Get scan provider preference key for a wallet type
46 static String getScanProviderPreferenceKey(int chainId) => switch (chainId) {
47 1 => "use_etherscan",
48 137 => "use_polygonscan",
49 8453 => "use_basescan",
50 42161 => "use_arbiscan",
51 56 => "use_bscscan",
52 _ => "use_etherscan",
53 };
54
55 static String getDefaultTokenTag(int chainId) => switch (chainId) {
56 1 => "ETH",
57 137 => "POL",
58 8453 => "BASE",
59 42161 => "ARB",
60 56 => "BSC",
61 _ => "ETH",
62 };
63
64 static String getFeeCurrency(int chainId) => switch (chainId) {
65 1 => "ETH",
66 137 => "POL",
67 8453 => "ETH",
68 42161 => "ETH",
69 56 => "BNB",
70 _ => "ETH",
71 };
72
73 static String getDefaultTokenSymbol(int chainId) => switch (chainId) {
74 1 => "ETH",
75 137 => "POL",
76 8453 => "BASE",
77 42161 => "ARBITRUM",
78 56 => "BSC",
79 _ => "ETH",
80 };
81
82 static int _ethereumPriorityFee(EVMChainTransactionPriority priority) => EtherAmount.fromInt(EtherUnit.gwei, priority.tip).getInWei.toInt();
83
84 // Polygon priority fee calculation (minimum 25 gwei + additional based on priority)
85 static int _polygonPriorityFee(EVMChainTransactionPriority priority) {
86 const int minPriorityFee = 25;
87 final minPriorityFeeWei = EtherAmount.fromInt(EtherUnit.gwei, minPriorityFee).getInWei.toInt();
88
89 final int additionalPriorityFee = switch (priority) {
90 EVMChainTransactionPriority.slow => 0,
91 EVMChainTransactionPriority.medium =>
92 EtherAmount.fromInt(EtherUnit.gwei, 15).getInWei.toInt(),
93 EVMChainTransactionPriority.fast => EtherAmount.fromInt(EtherUnit.gwei, 35).getInWei.toInt(),
94 _ => 0,
95 };
96
97 return minPriorityFeeWei + additionalPriorityFee;
98 }
99
100 static int _basePriorityFee(EVMChainTransactionPriority priority) => switch (priority) {
101 EVMChainTransactionPriority.fast => EtherAmount.fromInt(EtherUnit.mwei, 5).getInWei.toInt(),
102 EVMChainTransactionPriority.medium => EtherAmount.fromInt(EtherUnit.mwei, 3).getInWei.toInt(),
103 EVMChainTransactionPriority.slow => EtherAmount.fromInt(EtherUnit.mwei, 1).getInWei.toInt(),
104 _ => EtherAmount.fromInt(EtherUnit.mwei, 1).getInWei.toInt(),
105 };
106 }