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