Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block number to BlockInformation, add BlockInformationWithTransac… #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 57 additions & 8 deletions lib/src/core/block_information.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> 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<TransactionInformation> 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<String, dynamic> map) {
return BlockInformationWithTransactions(
number: _parseNumber(map),
baseFeePerGas: _parseBaseFeePerGas(map),
timestamp: _parseTimestamp(map),
transactions: _parseTransactions(map),
);
}

static List<TransactionInformation> _parseTransactions(Map<String, dynamic> map) {
return (map['transactions'] as List)
.cast<Map<String, dynamic>>()
.map((map) => TransactionInformation.fromMap(map))
.toList(growable: false);
}
}

int? _parseNumber(Map<String, dynamic> map) {
if (!map.containsKey('number')) return null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so some blocks dont have a number? can you add some tests for this?

return hexToDartInt(map['number'] as String);
}

EtherAmount? _parseBaseFeePerGas(Map<String, dynamic> map) {
if (!map.containsKey('baseFeePerGas')) return null;

return EtherAmount.fromUnitAndValue(
EtherUnit.wei,
hexToInt(map['baseFeePerGas'] as String),
);
}

DateTime _parseTimestamp(Map<String, dynamic> map) {
return DateTime.fromMillisecondsSinceEpoch(
hexToDartInt(map['timestamp'] as String) * 1000,
isUtc: true,
);
}
11 changes: 11 additions & 0 deletions lib/src/core/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,24 @@ class Web3Client {
.then((s) => hexToInt(s).toInt());
}

// TODO: Add getBlockByNumberWithTransactionHashes, then deprecate this.
Future<BlockInformation> getBlockInformation(
{String blockNumber = 'latest', bool isContainFullObj = true}) {
return _makeRPCCall<Map<String, dynamic>>(
'eth_getBlockByNumber', [blockNumber, isContainFullObj])
.then((json) => BlockInformation.fromJson(json));
}

Future<BlockInformationWithTransactions> getBlockByNumberWithTransactions(
BlockNum blockNum,
) async {
final map = await _makeRPCCall<Map<String, dynamic>>(
'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
Expand Down