diff --git a/lib/src/core/block_information.dart b/lib/src/core/block_information.dart index 01287851..3625c5db 100644 --- a/lib/src/core/block_information.dart +++ b/lib/src/core/block_information.dart @@ -2,26 +2,75 @@ import 'package:web3dart/src/crypto/formatting.dart'; import 'package:web3dart/web3dart.dart'; class BlockInformation { + final int? number; final EtherAmount? baseFeePerGas; final DateTime timestamp; BlockInformation({ + required this.number, required this.baseFeePerGas, required this.timestamp, }); factory BlockInformation.fromJson(Map json) { return BlockInformation( - baseFeePerGas: json.containsKey('baseFeePerGas') - ? EtherAmount.fromUnitAndValue( - EtherUnit.wei, hexToInt(json['baseFeePerGas'] as String)) - : null, - timestamp: DateTime.fromMillisecondsSinceEpoch( - hexToDartInt(json['timestamp'] as String) * 1000, - isUtc: true, - ), + number: _parseNumber(json), + baseFeePerGas: _parseBaseFeePerGas(json), + timestamp: _parseTimestamp(json), ); } bool get isSupportEIP1559 => baseFeePerGas != null; } + +class BlockInformationWithTransactions extends BlockInformation { + final List transactions; + + BlockInformationWithTransactions({ + required int? number, + required EtherAmount? baseFeePerGas, + required DateTime timestamp, + required this.transactions, + }) : super( + number: number, + baseFeePerGas: baseFeePerGas, + timestamp: timestamp, + ); + + factory BlockInformationWithTransactions.fromMap(Map map) { + return BlockInformationWithTransactions( + number: _parseNumber(map), + baseFeePerGas: _parseBaseFeePerGas(map), + timestamp: _parseTimestamp(map), + transactions: _parseTransactions(map), + ); + } + + static List _parseTransactions(Map map) { + return (map['transactions'] as List) + .cast>() + .map((map) => TransactionInformation.fromMap(map)) + .toList(growable: false); + } +} + +int? _parseNumber(Map map) { + if (!map.containsKey('number')) return null; + return hexToDartInt(map['number'] as String); +} + +EtherAmount? _parseBaseFeePerGas(Map map) { + if (!map.containsKey('baseFeePerGas')) return null; + + return EtherAmount.fromUnitAndValue( + EtherUnit.wei, + hexToInt(map['baseFeePerGas'] as String), + ); +} + +DateTime _parseTimestamp(Map map) { + return DateTime.fromMillisecondsSinceEpoch( + hexToDartInt(map['timestamp'] as String) * 1000, + isUtc: true, + ); +} diff --git a/lib/src/core/client.dart b/lib/src/core/client.dart index a56def6d..eca0c6a2 100644 --- a/lib/src/core/client.dart +++ b/lib/src/core/client.dart @@ -183,6 +183,7 @@ class Web3Client { .then((s) => hexToInt(s).toInt()); } + // TODO: Add getBlockByNumberWithTransactionHashes, then deprecate this. Future getBlockInformation( {String blockNumber = 'latest', bool isContainFullObj = true}) { return _makeRPCCall>( @@ -190,6 +191,16 @@ class Web3Client { .then((json) => BlockInformation.fromJson(json)); } + Future getBlockByNumberWithTransactions( + BlockNum blockNum, + ) async { + final map = await _makeRPCCall>( + 'eth_getBlockByNumber', + [blockNum.toBlockParam(), true], + ); + return BlockInformationWithTransactions.fromMap(map); + } + /// Gets the balance of the account with the specified address. /// /// This function allows specifying a custom block mined in the past to get