Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Issue #210 Add timestamp to BlockInformation (#241)
Browse files Browse the repository at this point in the history
* Issue #210 Add timestamp to BlockInformation

* Change timestamp to DateTime

* Add test for Issue #210
  • Loading branch information
alexeyinkin authored Jan 25, 2022
1 parent 5c646cd commit 3a9349b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/src/core/block_information.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ import 'package:web3dart/src/crypto/formatting.dart';
import 'package:web3dart/web3dart.dart';

class BlockInformation {
EtherAmount? baseFeePerGas;
final EtherAmount? baseFeePerGas;
final DateTime timestamp;

BlockInformation({this.baseFeePerGas});
BlockInformation({
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);
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,
),
);
}

bool get isSupportEIP1559 => baseFeePerGas != null;
Expand Down
15 changes: 15 additions & 0 deletions test/infura_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ void main() {
expect(balance >= BigInt.parse('410243042034234643784017156276017'),
isTrue);
});

test('Web3Client.getBlockInformation', () async {
final blockInfo = await client.getBlockInformation(
blockNumber: const BlockNum.exact(14074702).toBlockParam(),
);

expect(
blockInfo.timestamp.millisecondsSinceEpoch == 1643113026000,
isTrue,
);
expect(
blockInfo.timestamp.isUtc == true,
isTrue,
);
});
},
skip: infuraProjectId == null || infuraProjectId.length < 32
? 'Tests require the INFURA_ID environment variable'
Expand Down

0 comments on commit 3a9349b

Please sign in to comment.