-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(types): Make types index private
- Loading branch information
Showing
5 changed files
with
194 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
packages/komodo_defi_types/lib/src/transactions/fee_info.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file was deleted.
Oops, something went wrong.