Line data Source code
1 : import 'package:walletkit_dart/walletkit_dart.dart';
2 :
3 : enum FeePriority {
4 : custom('Custom'),
5 : low("slow"),
6 : medium("medium"),
7 : high("fast"),
8 : nextBlock("nextBlock", "fast"),
9 : secondBlock("secondBlock", "medium"),
10 : hour("hour", "slow"),
11 : day("day", "slow");
12 :
13 : final String displayName;
14 : final String assetName;
15 :
16 : const FeePriority(this.displayName, [String? assetName])
17 : : assetName = assetName ?? displayName;
18 :
19 0 : static List<FeePriority> get evm {
20 0 : return [
21 : low,
22 : medium,
23 : high,
24 : ];
25 : }
26 :
27 0 : static List<FeePriority> get utxo {
28 0 : return [
29 : nextBlock,
30 : secondBlock,
31 : hour,
32 : day,
33 : ];
34 : }
35 : }
36 :
37 : sealed class FeeInformation {
38 0 : const FeeInformation();
39 :
40 : Json toJson();
41 :
42 0 : factory FeeInformation.fromJson(Map json) {
43 : return switch (json) {
44 : {
45 0 : 'gasLimit': int gasLimit,
46 0 : 'gasPrice': Map gasPrice,
47 : } =>
48 0 : EvmFeeInformation(
49 : gasLimit: gasLimit,
50 0 : gasPrice: EvmLegacyGasPrice(gasPrice: Amount.fromJson(gasPrice)),
51 : ),
52 : {
53 0 : 'gasLimit': int gasLimit,
54 0 : 'maxFeePerGas': Map maxFeePerGas,
55 0 : 'maxPriorityFeePerGas': Map maxPriorityFeePerGas,
56 : } =>
57 0 : EvmFeeInformation(
58 : gasLimit: gasLimit,
59 0 : gasPrice: EvmType2GasPrice(
60 0 : maxFeePerGas: Amount.fromJson(maxFeePerGas),
61 0 : maxPriorityFeePerGas: Amount.fromJson(maxPriorityFeePerGas),
62 : ),
63 : ),
64 : {
65 0 : 'feePerByte': Map feePerByte,
66 0 : 'fee': Map? fee,
67 : } =>
68 0 : UtxoFeeInformation(
69 0 : feePerByte: Amount.fromJson(feePerByte),
70 0 : fee: fee != null ? Amount.fromJson(fee) : null,
71 : ),
72 : {
73 0 : 'feeLimit': Map feeLimit,
74 : } =>
75 0 : TronFeeInformation(
76 0 : feeLimit: Amount.fromJson(feeLimit),
77 : ),
78 0 : _ => throw FormatException('Unknown FeeInformation'),
79 : };
80 : }
81 : }
82 :
83 : final class EvmFeeInformation<T extends EvmGasPrice> extends FeeInformation {
84 : final int? gasLimit;
85 : final T? gasPrice;
86 :
87 0 : const EvmFeeInformation({
88 : this.gasLimit,
89 : this.gasPrice,
90 : });
91 :
92 0 : Amount? get maxFee {
93 0 : if (gasLimit == null) {
94 : return null;
95 : }
96 0 : if (gasPrice == null) {
97 : return null;
98 : }
99 :
100 0 : return switch (gasPrice!) {
101 0 : EvmLegacyGasPrice gasPrice => gasPrice.gasPrice *
102 0 : Amount.convert(
103 0 : value: gasLimit!,
104 : decimals: 0,
105 : ),
106 0 : EvmType2GasPrice gasPrice =>
107 0 : gasPrice.maxFeePerGas * Amount.convert(value: gasLimit!, decimals: 0),
108 : };
109 : }
110 :
111 0 : EvmFeeInformation<T> copyWith({
112 : int? gasLimit,
113 : T? gasPrice,
114 : }) {
115 0 : return EvmFeeInformation(
116 0 : gasLimit: gasLimit ?? this.gasLimit,
117 0 : gasPrice: gasPrice ?? this.gasPrice,
118 : );
119 : }
120 :
121 0 : Json toJson() {
122 0 : return {
123 0 : 'gasLimit': gasLimit,
124 0 : ...?gasPrice?.toJson(),
125 : };
126 : }
127 : }
128 :
129 : sealed class EvmGasPrice {
130 1 : const EvmGasPrice();
131 :
132 : Json toJson();
133 : }
134 :
135 : final class EvmLegacyGasPrice extends EvmGasPrice {
136 : final Amount gasPrice;
137 :
138 0 : const EvmLegacyGasPrice({
139 : required this.gasPrice,
140 : });
141 :
142 0 : Json toJson() {
143 0 : return {
144 0 : 'gasPrice': gasPrice.toJson(),
145 : };
146 : }
147 : }
148 :
149 : final class EvmType2GasPrice extends EvmGasPrice {
150 : final Amount maxFeePerGas;
151 : final Amount maxPriorityFeePerGas;
152 :
153 1 : const EvmType2GasPrice({
154 : required this.maxFeePerGas,
155 : required this.maxPriorityFeePerGas,
156 : });
157 :
158 0 : Json toJson() {
159 0 : return {
160 0 : 'maxPriorityFeePerGas': maxPriorityFeePerGas.toJson(),
161 0 : 'maxFeePerGas': maxFeePerGas.toJson(),
162 : };
163 : }
164 :
165 0 : EvmType2GasPrice copyWith({
166 : Amount? maxFeePerGas,
167 : Amount? maxPriorityFeePerGas,
168 : }) {
169 0 : return EvmType2GasPrice(
170 0 : maxFeePerGas: maxFeePerGas ?? this.maxFeePerGas,
171 0 : maxPriorityFeePerGas: maxPriorityFeePerGas ?? this.maxPriorityFeePerGas,
172 : );
173 : }
174 : }
175 :
176 : final class UtxoFeeInformation extends FeeInformation {
177 : final Amount feePerByte;
178 : final Amount? fee;
179 :
180 0 : const UtxoFeeInformation({
181 : required this.feePerByte,
182 : this.fee,
183 : });
184 :
185 0 : Json toJson() {
186 0 : return {
187 0 : 'feePerByte': feePerByte.toJson(),
188 0 : 'fee': fee?.toJson(),
189 : };
190 : }
191 : }
192 :
193 : final class TronFeeInformation extends FeeInformation {
194 : final Amount feeLimit;
195 :
196 0 : const TronFeeInformation({
197 : required this.feeLimit,
198 : });
199 :
200 0 : Json toJson() {
201 0 : return {
202 0 : 'feeLimit': feeLimit.toJson(),
203 : };
204 : }
205 : }
|