| 1 | import 'dart:typed_data'; |
| 2 | |
| 3 | import 'package:cw_core/amount/money.dart'; |
| 4 | import 'package:cw_core/pending_transaction.dart'; |
| 5 | import 'package:web3dart/crypto.dart'; |
| 6 | import 'package:hex/hex.dart' as Hex; |
| 7 | |
| 8 | class PendingEVMChainTransaction with PendingTransaction { |
| 9 | final Function sendTransaction; |
| 10 | final Uint8List signedTransaction; |
| 11 | final bool isInfiniteApproval; |
| 12 | |
| 13 | PendingEVMChainTransaction({ |
| 14 | required this.sendTransaction, |
| 15 | required this.signedTransaction, |
| 16 | required this.fee, |
| 17 | required this.amount, |
| 18 | this.isInfiniteApproval = false, |
| 19 | }); |
| 20 | |
| 21 | @override |
| 22 | String get amountFormatted { |
| 23 | if (isInfiniteApproval) return "∞"; |
| 24 | return amount.toString(); |
| 25 | } |
| 26 | |
| 27 | @override |
| 28 | final Money amount; |
| 29 | |
| 30 | @override |
| 31 | final Money fee; |
| 32 | |
| 33 | @override |
| 34 | Future<void> commit() async => await sendTransaction(); |
| 35 | |
| 36 | @override |
| 37 | String get feeFormatted => fee.toStringWithSymbol(fractionalDigits: 10); |
| 38 | |
| 39 | @override |
| 40 | String get feeFormattedValue => fee.toStringWithPrecision(fractionalDigits: 10); |
| 41 | |
| 42 | @override |
| 43 | String get hex => bytesToHex(signedTransaction, include0x: true); |
| 44 | |
| 45 | @override |
| 46 | String get id { |
| 47 | final String eip1559Hex = '0x02${hex.substring(2)}'; |
| 48 | final Uint8List bytes = Uint8List.fromList(Hex.HEX.decode(eip1559Hex.substring(2))); |
| 49 | |
| 50 | final txid = keccak256(bytes); |
| 51 | |
| 52 | return '0x${Hex.HEX.encode(txid)}'; |
| 53 | } |
| 54 | |
| 55 | @override |
| 56 | String? get evmTxHashFromRawHex { |
| 57 | final no0x = hex.startsWith('0x') ? hex.substring(2) : hex; |
| 58 | final bytes = Uint8List.fromList(Hex.HEX.decode(no0x)); |
| 59 | final digest = keccak256(bytes); |
| 60 | return '0x${Hex.HEX.encode(digest)}'; |
| 61 | } |
| 62 | |
| 63 | @override |
| 64 | Future<Map<String, String>> commitUR() => throw UnimplementedError(); |
| 65 | } |