Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Commit 3a9349b

Browse files
authored
Issue simolus3#210 Add timestamp to BlockInformation (simolus3#241)
* Issue simolus3#210 Add timestamp to BlockInformation * Change timestamp to DateTime * Add test for Issue simolus3#210
1 parent 5c646cd commit 3a9349b

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

lib/src/core/block_information.dart

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@ import 'package:web3dart/src/crypto/formatting.dart';
22
import 'package:web3dart/web3dart.dart';
33

44
class BlockInformation {
5-
EtherAmount? baseFeePerGas;
5+
final EtherAmount? baseFeePerGas;
6+
final DateTime timestamp;
67

7-
BlockInformation({this.baseFeePerGas});
8+
BlockInformation({
9+
required this.baseFeePerGas,
10+
required this.timestamp,
11+
});
812

913
factory BlockInformation.fromJson(Map<String, dynamic> json) {
1014
return BlockInformation(
11-
baseFeePerGas: json.containsKey('baseFeePerGas')
12-
? EtherAmount.fromUnitAndValue(
13-
EtherUnit.wei, hexToInt(json['baseFeePerGas'] as String))
14-
: null);
15+
baseFeePerGas: json.containsKey('baseFeePerGas')
16+
? EtherAmount.fromUnitAndValue(
17+
EtherUnit.wei, hexToInt(json['baseFeePerGas'] as String))
18+
: null,
19+
timestamp: DateTime.fromMillisecondsSinceEpoch(
20+
hexToDartInt(json['timestamp'] as String) * 1000,
21+
isUtc: true,
22+
),
23+
);
1524
}
1625

1726
bool get isSupportEIP1559 => baseFeePerGas != null;

test/infura_integration_test.dart

+15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ void main() {
3535
expect(balance >= BigInt.parse('410243042034234643784017156276017'),
3636
isTrue);
3737
});
38+
39+
test('Web3Client.getBlockInformation', () async {
40+
final blockInfo = await client.getBlockInformation(
41+
blockNumber: const BlockNum.exact(14074702).toBlockParam(),
42+
);
43+
44+
expect(
45+
blockInfo.timestamp.millisecondsSinceEpoch == 1643113026000,
46+
isTrue,
47+
);
48+
expect(
49+
blockInfo.timestamp.isUtc == true,
50+
isTrue,
51+
);
52+
});
3853
},
3954
skip: infuraProjectId == null || infuraProjectId.length < 32
4055
? 'Tests require the INFURA_ID environment variable'

0 commit comments

Comments
 (0)