From f4edd33bcf9a6729d38166f31bd239b341edc291 Mon Sep 17 00:00:00 2001 From: CharlVS <77973576+CharlVS@users.noreply.github.com> Date: Fri, 17 Jan 2025 17:07:29 +0100 Subject: [PATCH] fix(types): Make types index private --- .../komodo_defi_types/index_generator.yaml | 6 +- .../lib/komodo_defi_types.dart | 2 +- .../lib/src/transactions/fee_info.dart | 137 ++++++++++++++++++ packages/komodo_defi_types/lib/src/types.dart | 53 +++++++ packages/komodo_defi_types/lib/types.dart | 56 ------- 5 files changed, 194 insertions(+), 60 deletions(-) create mode 100644 packages/komodo_defi_types/lib/src/transactions/fee_info.dart create mode 100644 packages/komodo_defi_types/lib/src/types.dart delete mode 100644 packages/komodo_defi_types/lib/types.dart diff --git a/packages/komodo_defi_types/index_generator.yaml b/packages/komodo_defi_types/index_generator.yaml index 74baa24..2852cbd 100644 --- a/packages/komodo_defi_types/index_generator.yaml +++ b/packages/komodo_defi_types/index_generator.yaml @@ -8,12 +8,12 @@ index_generator: - '{_,**/_}*.dart' indexes: # Default index and library name - - path: lib + - path: lib/src name: types include: - - 'src/**' + - '**' exclude: - - 'src/utils/**' + - 'utils/**' comments: | Generated by the `index_generator` package with the `index_generator.yaml` configuration file. docs: diff --git a/packages/komodo_defi_types/lib/komodo_defi_types.dart b/packages/komodo_defi_types/lib/komodo_defi_types.dart index 7a87367..77541a7 100644 --- a/packages/komodo_defi_types/lib/komodo_defi_types.dart +++ b/packages/komodo_defi_types/lib/komodo_defi_types.dart @@ -16,7 +16,7 @@ export 'src/auth/kdf_user.dart'; // Aliased/proxied types export 'src/exported_rpc_types.dart'; export 'src/komodo_defi_types_base.dart'; -export 'types.dart'; +export 'src/types.dart'; // Export activation params types // export 'packages:komodo_defi_rpc_methods/lib/src/common_structures/activation/activation_params/activation_params_index.dart diff --git a/packages/komodo_defi_types/lib/src/transactions/fee_info.dart b/packages/komodo_defi_types/lib/src/transactions/fee_info.dart new file mode 100644 index 0000000..c5ed212 --- /dev/null +++ b/packages/komodo_defi_types/lib/src/transactions/fee_info.dart @@ -0,0 +1,137 @@ +import 'package:decimal/decimal.dart'; +import 'package:equatable/equatable.dart'; +import 'package:komodo_defi_types/komodo_defi_type_utils.dart'; +import 'package:komodo_defi_types/komodo_defi_types.dart'; + +/// Base abstract class for fee information +abstract class FeeInfo extends Equatable { + const FeeInfo({ + required this.coin, + required this.amount, + }); + + /// Factory constructor to create the appropriate fee type from JSON + factory FeeInfo.fromJson(Map json) { + final feeType = WithdrawalFeeType.parse(json.value('type')); + final coin = json.value('coin'); + + switch (feeType) { + case WithdrawalFeeType.eth: + final gas = json.valueOrNull('gas'); + final gasPriceString = json.valueOrNull('gas_price'); + + if (gas != null && gasPriceString != null) { + return EthFeeInfo.fromGasParams( + coin: coin, + gasPrice: Decimal.parse(gasPriceString), + gasLimit: gas, + ); + } + + return EthFeeInfo( + coin: coin, + amount: Decimal.parse(json.value('amount')), + gas: gas, + gasPrice: + gasPriceString != null ? Decimal.parse(gasPriceString) : null, + ); + + case WithdrawalFeeType.utxo: + return UtxoFeeInfo( + coin: coin, + amount: Decimal.parse(json.value('amount')), + ); + + default: + throw ArgumentError('Unknown fee type: $feeType'); + } + } + + /// The coin identifier the fee is paid in + final String coin; + + /// The total fee amount in the native coin unit + final Decimal amount; + + /// The type of fee (UTXO, ETH, etc) + WithdrawalFeeType get type; + + /// Gets the total fee amount in the native coin unit + Decimal get totalFee => amount; + + /// Convert to JSON representation + Map toJson() => { + 'type': type.toString(), + 'amount': amount.toString(), + 'total_fee': totalFee.toString(), + 'coin': coin, + }; + + @override + String toString() => toJson().toString(); +} + +/// Fee information for UTXO-based coins +class UtxoFeeInfo extends FeeInfo { + const UtxoFeeInfo({ + required super.coin, + required super.amount, + }); + + @override + WithdrawalFeeType get type => WithdrawalFeeType.utxo; + + @override + List get props => [coin, amount]; +} + +/// Fee information for ETH/ERC20 coins +class EthFeeInfo extends FeeInfo { + const EthFeeInfo({ + required super.coin, + required super.amount, + this.gas, + this.gasPrice, + }); + + factory EthFeeInfo.fromGasParams({ + required String coin, + required Decimal gasPrice, + required int gasLimit, + }) { + final totalFee = _calculateTotalFee(gasPrice, gasLimit); + return EthFeeInfo( + coin: coin, + amount: totalFee, + gas: gasLimit, + gasPrice: gasPrice, + ); + } + + /// Gas limit for transaction + final int? gas; + + /// Gas price in Gwei + final Decimal? gasPrice; + + @override + WithdrawalFeeType get type => WithdrawalFeeType.eth; + + /// Calculate total fee from gas parameters (gas price in Gwei) + static Decimal _calculateTotalFee(Decimal gasPriceGwei, int gasUnits) { + return (gasPriceGwei.toRational() * + (Decimal.fromInt(gasUnits).toRational() / + Decimal.fromInt(1000000000).toRational())) + .toDecimal(scaleOnInfinitePrecision: 18); + } + + @override + Map toJson() => { + ...super.toJson(), + if (gas != null) 'gas': gas, + if (gasPrice != null) 'gas_price': gasPrice.toString(), + }; + + @override + List get props => [coin, amount, gas, gasPrice]; +} diff --git a/packages/komodo_defi_types/lib/src/types.dart b/packages/komodo_defi_types/lib/src/types.dart new file mode 100644 index 0000000..ce28f99 --- /dev/null +++ b/packages/komodo_defi_types/lib/src/types.dart @@ -0,0 +1,53 @@ +// Generated by the `index_generator` package with the `index_generator.yaml` configuration file. + +/// Generic types used throughout the Komodo DeFi Framework ecosystem. +library types; + +export 'activation/activation_progress.dart'; +export 'activation/activation_strategy.dart'; +export 'api/api_client.dart'; +export 'assets/asset.dart'; +export 'assets/asset_id.dart'; +export 'assets/asset_symbol.dart'; +export 'assets/chain.dart'; +export 'auth/auth_options.dart'; +export 'auth/auth_result.dart'; +export 'auth/exceptions/auth_exception.dart'; +export 'auth/exceptions/incorrect_password_exception.dart'; +export 'auth/kdf_user.dart'; +export 'chains/chain.dart'; +export 'chains/utxo_chain.dart'; +export 'coin/coin.dart'; +export 'coin_classes/coin_subclasses.dart'; +export 'coin_classes/protocol_class.dart'; +export 'cryptography/mnemonic.dart'; +export 'exceptions/http_exceptions.dart'; +export 'exported_rpc_types.dart'; +export 'generic/result.dart'; +export 'generic/sync_status.dart'; +export 'komodo_defi_types_base.dart'; +export 'legacy/legacy_coin_model.dart'; +export 'protocols/base/exceptions.dart'; +export 'protocols/base/protocol_class.dart'; +export 'protocols/erc20/erc20_protocol.dart'; +export 'protocols/protocols.dart'; +export 'protocols/qtum/qtum_protocol.dart'; +export 'protocols/sia/sia_protocol.dart'; +export 'protocols/slp/slp_protocol.dart'; +export 'protocols/tendermint/tendermint_protocol.dart'; +export 'protocols/utxo/utxo_protocol.dart'; +export 'protocols/zhtlc/zhtlc_protocol.dart'; +export 'public_key/asset_pubkeys.dart'; +export 'public_key/derivation_method.dart'; +export 'public_key/pubkey.dart'; +export 'public_key/pubkey_strategy.dart'; +export 'public_key/token_balance_map.dart'; +export 'public_key/wallet_balance.dart'; +export 'transactions/balance_changes.dart'; +export 'transactions/fee_info.dart'; +export 'transactions/transaction.dart'; +export 'transactions/transaction_history_strategy.dart'; +export 'transactions/transaction_pagination_strategy.dart'; +export 'transactions/transaction_results_page.dart'; +export 'withdrawal/withdrawal_enums.dart'; +export 'withdrawal/withdrawal_types.dart'; diff --git a/packages/komodo_defi_types/lib/types.dart b/packages/komodo_defi_types/lib/types.dart deleted file mode 100644 index fad22db..0000000 --- a/packages/komodo_defi_types/lib/types.dart +++ /dev/null @@ -1,56 +0,0 @@ -// Generated by the `index_generator` package with the `index_generator.yaml` configuration file. - -@Deprecated( - 'Use the `komodo_defi_types` package or `komodo_defi_type_utils` ' - 'package instead.', -) - -/// Generic types used throughout the Komodo DeFi Framework ecosystem. -library types; - -export 'src/activation/activation_progress.dart'; -export 'src/activation/activation_strategy.dart'; -export 'src/api/api_client.dart'; -export 'src/assets/asset.dart'; -export 'src/assets/asset_id.dart'; -export 'src/assets/asset_symbol.dart'; -export 'src/assets/chain.dart'; -export 'src/auth/auth_options.dart'; -export 'src/auth/auth_result.dart'; -export 'src/auth/exceptions/auth_exception.dart'; -export 'src/auth/exceptions/incorrect_password_exception.dart'; -export 'src/auth/kdf_user.dart'; -export 'src/chains/chain.dart'; -export 'src/chains/utxo_chain.dart'; -export 'src/coin/coin.dart'; -export 'src/coin_classes/coin_subclasses.dart'; -export 'src/coin_classes/protocol_class.dart'; -export 'src/cryptography/mnemonic.dart'; -export 'src/exceptions/http_exceptions.dart'; -export 'src/generic/result.dart'; -export 'src/generic/sync_status.dart'; -export 'src/komodo_defi_types_base.dart'; -export 'src/legacy/legacy_coin_model.dart'; -export 'src/protocols/base/exceptions.dart'; -export 'src/protocols/base/protocol_class.dart'; -export 'src/protocols/erc20/erc20_protocol.dart'; -export 'src/protocols/protocols.dart'; -export 'src/protocols/qtum/qtum_protocol.dart'; -export 'src/protocols/sia/sia_protocol.dart'; -export 'src/protocols/slp/slp_protocol.dart'; -export 'src/protocols/tendermint/tendermint_protocol.dart'; -export 'src/protocols/utxo/utxo_protocol.dart'; -export 'src/protocols/zhtlc/zhtlc_protocol.dart'; -export 'src/public_key/asset_pubkeys.dart'; -export 'src/public_key/derivation_method.dart'; -export 'src/public_key/pubkey.dart'; -export 'src/public_key/pubkey_strategy.dart'; -export 'src/public_key/token_balance_map.dart'; -export 'src/public_key/wallet_balance.dart'; -export 'src/transactions/balance_changes.dart'; -export 'src/transactions/transaction.dart'; -export 'src/transactions/transaction_history_strategy.dart'; -export 'src/transactions/transaction_pagination_strategy.dart'; -export 'src/transactions/transaction_results_page.dart'; -export 'src/withdrawal/withdrawal_enums.dart'; -export 'src/withdrawal/withdrawal_types.dart';