Line data Source code
1 : import 'package:walletkit_dart/walletkit_dart.dart';
2 :
3 : sealed class NetworkFees {
4 2 : const NetworkFees();
5 : }
6 :
7 : final class UtxoNetworkFees extends NetworkFees {
8 : final Amount nextBlock;
9 : final Amount secondBlock;
10 : final Amount hour;
11 : final Amount day;
12 :
13 1 : const UtxoNetworkFees({
14 : required this.nextBlock,
15 : required this.secondBlock,
16 : required this.hour,
17 : required this.day,
18 : });
19 :
20 1 : @override
21 : String toString() {
22 5 : return "UtxoNetworkFees{nextBlock: $nextBlock, secondBlock: $secondBlock, hour: $hour, day: $day}";
23 : }
24 :
25 0 : Amount getForPriority(FeePriority priority) => switch (priority) {
26 0 : FeePriority.nextBlock => nextBlock,
27 0 : FeePriority.secondBlock => secondBlock,
28 0 : FeePriority.hour => hour,
29 0 : FeePriority.day => day,
30 0 : _ => nextBlock,
31 : };
32 : }
33 :
34 : final class EvmNetworkFees extends NetworkFees {
35 : final BigInt lastBlock;
36 : final BigInt safe;
37 : final BigInt average;
38 : final BigInt fast;
39 :
40 : final BigInt safePriority;
41 : final BigInt averagePriority;
42 : final BigInt fastPriority;
43 :
44 1 : const EvmNetworkFees({
45 : required this.lastBlock,
46 : required this.safe,
47 : required this.average,
48 : required this.fast,
49 : required this.safePriority,
50 : required this.averagePriority,
51 : required this.fastPriority,
52 : });
53 :
54 0 : BigInt getFeeGWEI(FeePriority feePriority) => switch (feePriority) {
55 0 : FeePriority.high => fast,
56 0 : FeePriority.medium => average,
57 0 : FeePriority.low => safe,
58 0 : _ => safe,
59 : };
60 :
61 0 : BigInt getPriorityGWEI(FeePriority feePriority) => switch (feePriority) {
62 0 : FeePriority.high => fastPriority,
63 0 : FeePriority.medium => averagePriority,
64 0 : FeePriority.low => safePriority,
65 0 : _ => safePriority,
66 : };
67 :
68 0 : Amount getFeeAmount(FeePriority feePriority) => Amount(
69 0 : value: getFeeGWEI(feePriority),
70 : decimals: 18, // GWEI
71 : );
72 :
73 0 : Amount getPriorityAmount(FeePriority feePriority) => Amount(
74 0 : value: getPriorityGWEI(feePriority),
75 : decimals: 18, // GWEI
76 : );
77 :
78 0 : const EvmNetworkFees.fromBigInt({
79 : required BigInt lastBlock,
80 : required BigInt maxPriorityFeePerGas,
81 0 : }) : this(
82 : lastBlock: lastBlock,
83 : safe: lastBlock,
84 : average: lastBlock,
85 : fast: lastBlock,
86 : safePriority: maxPriorityFeePerGas,
87 : averagePriority: maxPriorityFeePerGas,
88 : fastPriority: maxPriorityFeePerGas,
89 : );
90 :
91 1 : factory EvmNetworkFees.fromJson(Map<String, dynamic> json) {
92 : if (json
93 : case {
94 2 : 'suggestBaseFee': String last,
95 2 : 'SafeGasPrice': String safe,
96 2 : 'ProposeGasPrice': String propose,
97 2 : 'FastGasPrice': String fast,
98 : }) {
99 1 : final safe_num = toGwei(safe);
100 1 : final propose_num = toGwei(propose);
101 1 : final fast_num = toGwei(fast);
102 1 : final last_num = toGwei(last);
103 :
104 1 : final safePriority = safe_num - last_num;
105 1 : final averagePriority = propose_num - last_num;
106 1 : final fastPriority = fast_num - last_num;
107 :
108 1 : return EvmNetworkFees(
109 : lastBlock: last_num,
110 : safe: safe_num,
111 : average: propose_num,
112 : fast: fast_num,
113 : safePriority: safePriority,
114 : averagePriority: averagePriority,
115 : fastPriority: fastPriority,
116 : );
117 : }
118 :
119 : if (json
120 : case {
121 0 : 'SafeGasPrice': String safe,
122 0 : 'ProposeGasPrice': String average,
123 0 : 'FastGasPrice': String fast,
124 : }) {
125 0 : final safe_num = toGwei(safe);
126 : final last_num = safe_num;
127 0 : final average_num = toGwei(average);
128 0 : final fast_num = toGwei(fast);
129 :
130 0 : return EvmNetworkFees(
131 : lastBlock: last_num,
132 : safe: safe_num,
133 : average: average_num,
134 : fast: fast_num,
135 0 : safePriority: BigInt.zero,
136 0 : averagePriority: BigInt.zero,
137 0 : fastPriority: BigInt.zero,
138 : );
139 : }
140 :
141 0 : throw UnimplementedError();
142 : }
143 :
144 1 : @override
145 : String toString() {
146 5 : return 'GasFeesEntity{lastBlock: $lastBlock, safe: $safe, average: $average, fast: $fast}';
147 : }
148 : }
149 :
150 1 : BigInt toGwei(String wei) {
151 4 : final decimals = wei.contains('.') ? wei.split('.')[1].length : 0;
152 1 : final wei_without_decimals = wei.replaceAll('.', '');
153 1 : final val = BigInt.parse(wei_without_decimals);
154 :
155 3 : final multiplier = BigInt.from(10).pow(9 - decimals);
156 :
157 1 : return val * multiplier;
158 : }
159 :
160 : final class TronNetworkFees extends NetworkFees {
161 : final int energyPrice = 420; // SUN
162 : final int bandWidthPrice = 1000; // SUN
163 :
164 : final int energyBalance;
165 : final int bandwidthBalance;
166 :
167 : final int bandWidthConsumed;
168 : final int energyConsumed;
169 :
170 0 : Amount get bandWidthFee {
171 0 : return Amount.from(
172 0 : value: bandwidthBalance >= bandWidthConsumed
173 : ? 0
174 0 : : bandWidthConsumed * bandWidthPrice,
175 : decimals: 6,
176 : );
177 : }
178 :
179 0 : Amount get energyFee {
180 0 : return Amount.from(
181 0 : value: energyBalance >= energyConsumed ? 0 : energyConsumed * energyPrice,
182 : decimals: 6,
183 : );
184 : }
185 :
186 0 : Amount get fee {
187 0 : return bandWidthFee + energyFee;
188 : }
189 :
190 0 : int get bandWidthUsed {
191 0 : return bandWidthConsumed > bandwidthBalance ? 0 : bandWidthConsumed;
192 : }
193 :
194 0 : int get energyUsed {
195 0 : return energyConsumed > energyBalance ? 0 : energyConsumed;
196 : }
197 :
198 0 : TronNetworkFees({
199 : required this.bandWidthConsumed,
200 : required this.energyConsumed,
201 : required this.energyBalance,
202 : required this.bandwidthBalance,
203 : });
204 :
205 0 : @override
206 : String toString() {
207 0 : return 'TronNetworkFees{energyPrice: $energyPrice, bandWidthPrice: $bandWidthPrice, energyBalance: $energyBalance, bandwidthBalance: $bandwidthBalance, bandWidthConsumed: $bandWidthConsumed, energyConsumed: $energyConsumed}';
208 : }
209 : }
|