Skip to content

Commit

Permalink
fix(types): Make types index private
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlVS committed Jan 17, 2025
1 parent e37a463 commit f4edd33
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 60 deletions.
6 changes: 3 additions & 3 deletions packages/komodo_defi_types/index_generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion packages/komodo_defi_types/lib/komodo_defi_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
137 changes: 137 additions & 0 deletions packages/komodo_defi_types/lib/src/transactions/fee_info.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> json) {
final feeType = WithdrawalFeeType.parse(json.value<String>('type'));
final coin = json.value<String>('coin');

switch (feeType) {
case WithdrawalFeeType.eth:
final gas = json.valueOrNull<int>('gas');
final gasPriceString = json.valueOrNull<String>('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<String, dynamic> 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<Object?> 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<String, dynamic> toJson() => {
...super.toJson(),
if (gas != null) 'gas': gas,
if (gasPrice != null) 'gas_price': gasPrice.toString(),
};

@override
List<Object?> get props => [coin, amount, gas, gasPrice];
}
53 changes: 53 additions & 0 deletions packages/komodo_defi_types/lib/src/types.dart
Original file line number Diff line number Diff line change
@@ -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';
56 changes: 0 additions & 56 deletions packages/komodo_defi_types/lib/types.dart

This file was deleted.

0 comments on commit f4edd33

Please sign in to comment.