diff --git a/analysis_options.yaml b/analysis_options.yaml index 8b8bd247..b04be64c 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -96,7 +96,7 @@ linter: #- lines_longer_than_80_chars ignored because it's annoying for long literals in tests - non_constant_identifier_names - null_closures - - omit_local_variable_types +# - omit_local_variable_types - one_member_abstracts - only_throw_errors - overridden_fields diff --git a/lib/src/core/block_information.dart b/lib/src/core/block_information.dart index 8ac5a47c..45dae6ae 100644 --- a/lib/src/core/block_information.dart +++ b/lib/src/core/block_information.dart @@ -1,17 +1,120 @@ +import 'package:web3dart/credentials.dart'; import 'package:web3dart/src/crypto/formatting.dart'; import 'package:web3dart/web3dart.dart'; class BlockInformation { - EtherAmount? baseFeePerGas; + final EthereumAddress? from; // Author + final String? boundary; + final String? difficulty; + final String? extraData; + final String? gasLimit; + final String? gasUsed; + final String? hash; + final String? logsBloom; + final EthereumAddress? miner; + final String? mixHash; + final String? nonce; + final EtherAmount? baseFeePerGas; + final String? number; + final String? parentHash; + final String? receiptsRoot; + final String? seedHash; + final String? sha3Uncles; + final String? size; + final String? stateRoot; + final String? timestamp; + final String? totalDifficulty; + final List? transactions; + final String? transactionsRoot; + final List? uncles; - BlockInformation({this.baseFeePerGas}); + BlockInformation({ + this.from, + this.boundary, + this.difficulty, + this.extraData, + this.gasLimit, + this.gasUsed, + this.hash, + this.logsBloom, + this.miner, + this.mixHash, + this.nonce, + this.baseFeePerGas, + this.number, + this.parentHash, + this.receiptsRoot, + this.seedHash, + this.sha3Uncles, + this.size, + this.stateRoot, + this.timestamp, + this.totalDifficulty, + this.transactions, + this.transactionsRoot, + this.uncles, + }); factory BlockInformation.fromJson(Map json) { + final List>? _list = List.castFrom(json['transactions'] as List); + List? _transactions; + if (_list != null) { + _transactions = _list.map((Map e) => TransactionInformation.fromMap(e)).toList(); + } else { + _transactions = null; + } + + final EthereumAddress? _from = json.containsKey('author') ? EthereumAddress.fromHex(json['author'] as String) : null; + final String? _boundary = json.containsKey('boundary') ? json['boundary'] as String : null; + final String? _difficulty = json.containsKey('difficulty') ? json['difficulty'] as String : null; + final String? _extraData = json.containsKey('extraData') ? json['extraData'] as String : null; + final String? _gasLimit = json.containsKey('gasLimit') ? json['gasLimit'] as String : null; + final String? _gasUsed = json.containsKey('gasUsed') ? json['gasUsed'] as String : null; + final String? _hash = json.containsKey('hash') ? json['hash'] as String : null; + final String? _logsBloom = json.containsKey('logsBloom') ? json['logsBloom'] as String : null; + final EthereumAddress? _miner = json.containsKey('miner') ? EthereumAddress.fromHex(json['miner'] as String) : null; + final String? _mixHash = json.containsKey('mixHash') ? json['mixHash'] as String : null; + final String? _nonce = json.containsKey('nonce') ? json['nonce'] as String : null; + final EtherAmount? _baseFeePerGas = json.containsKey('baseFeePerGas') ? EtherAmount.fromUnitAndValue(EtherUnit.wei, hexToInt(json['baseFeePerGas'] as String)) : null; + final String? _number = json.containsKey('number') ? json['number'] as String : null; + final String? _parentHash = json.containsKey('parentHash') ? json['parentHash'] as String : null; + final String? _receiptsRoot = json.containsKey('receiptsRoot') ? json['receiptsRoot'] as String : null; + final String? _seedHash = json.containsKey('seedHash') ? json['seedHash'] as String : null; + final String? _sha3Uncles = json.containsKey('sha3Uncles') ? json['sha3Uncles'] as String : null; + final String? _size = json.containsKey('size') ? json['size'] as String : null; + final String? _stateRoot = json.containsKey('stateRoot') ? json['size'] as String : null; + final String? _timestamp = json.containsKey('timestamp') ? json['timestamp'] as String : null; + final String? _totalDifficulty = json.containsKey('totalDifficulty') ? json['totalDifficulty'] as String : null; + final String? _transactionsRoot = json.containsKey('transactionsRoot') ? json['transactionsRoot'] as String : null; + final List? _uncles = json.containsKey('uncles') ? json['uncles'] as List : null; + + return BlockInformation( - baseFeePerGas: json.containsKey('baseFeePerGas') - ? EtherAmount.fromUnitAndValue( - EtherUnit.wei, hexToInt(json['baseFeePerGas'] as String)) - : null); + from: _from, + boundary: _boundary, + difficulty: _difficulty, + extraData: _extraData, + gasLimit: _gasLimit, + gasUsed: _gasUsed, + hash: _hash, + logsBloom: _logsBloom, + miner: _miner, + mixHash: _mixHash, + nonce: _nonce, + baseFeePerGas: _baseFeePerGas, + number: _number, + parentHash: _parentHash, + receiptsRoot: _receiptsRoot, + seedHash: _seedHash, + sha3Uncles: _sha3Uncles, + size: _size, + stateRoot: _stateRoot, + timestamp: _timestamp, + totalDifficulty: _totalDifficulty, + transactions: _transactions, + transactionsRoot: _transactionsRoot, + uncles: _uncles, + ); } bool get isSupportEIP1559 => baseFeePerGas != null; diff --git a/lib/src/core/client.dart b/lib/src/core/client.dart index a56def6d..6c875f09 100644 --- a/lib/src/core/client.dart +++ b/lib/src/core/client.dart @@ -187,7 +187,7 @@ class Web3Client { {String blockNumber = 'latest', bool isContainFullObj = true}) { return _makeRPCCall>( 'eth_getBlockByNumber', [blockNumber, isContainFullObj]) - .then((json) => BlockInformation.fromJson(json)); + .then((Map json) => BlockInformation.fromJson(json)); } /// Gets the balance of the account with the specified address. diff --git a/lib/src/core/transaction_information.dart b/lib/src/core/transaction_information.dart index 73b38879..6ca95f59 100644 --- a/lib/src/core/transaction_information.dart +++ b/lib/src/core/transaction_information.dart @@ -1,28 +1,47 @@ part of 'package:web3dart/web3dart.dart'; class TransactionInformation { - TransactionInformation.fromMap(Map map) - : blockHash = map['blockHash'] as String, - blockNumber = map['blockNumber'] != null - ? BlockNum.exact(int.parse(map['blockNumber'] as String)) - : const BlockNum.pending(), - from = EthereumAddress.fromHex(map['from'] as String), - gas = int.parse(map['gas'] as String), - gasPrice = EtherAmount.inWei(BigInt.parse(map['gasPrice'] as String)), - hash = map['hash'] as String, - input = hexToBytes(map['input'] as String), - nonce = int.parse(map['nonce'] as String), - to = map['to'] != null + + TransactionInformation({ + this.blockHash, + required this.blockNumber, + required this.from, + required this.gas, + required this.gasPrice, + required this.hash, + required this.input, + required this.nonce, + this.to, + this.transactionIndex, + required this.value, + required this.r, + required this.s, + required this.v + }); + + factory TransactionInformation.fromMap(Map map) { + + return TransactionInformation( + blockHash: map['blockHash'] as String, + blockNumber: map['blockNumber'] != null ? BlockNum.exact( + int.parse(map['blockNumber'] as String)) : const BlockNum.pending(), + from: EthereumAddress.fromHex(map['from'] as String), + gas: int.parse(map['gas'] as String), + gasPrice: EtherAmount.inWei(BigInt.parse(map['gasPrice'] as String)), + hash: map['hash'] as String, + input: hexToBytes(map['input'] as String), + nonce: int.parse(map['nonce'] as String), + to: map['to'] != null ? EthereumAddress.fromHex(map['to'] as String) : null, - transactionIndex = map['transactionIndex'] != null - ? int.parse(map['transactionIndex'] as String) - : null, - value = EtherAmount.inWei(BigInt.parse(map['value'] as String)), - v = int.parse(map['v'] as String), - r = hexToInt(map['r'] as String), - s = hexToInt(map['s'] as String); - + transactionIndex: map['transactionIndex'] != null ? int.parse( + map['transactionIndex'] as String) : null, + value: EtherAmount.inWei(BigInt.parse(map['value'] as String)), + v: int.parse(map['v'] as String), + r: hexToInt(map['r'] as String), + s: hexToInt(map['s'] as String) + ); + } /// The hash of the block containing this transaction. If this transaction has /// not been mined yet and is thus in no block, it will be `null` final String? blockHash; @@ -90,38 +109,41 @@ class TransactionReceipt { this.effectiveGasPrice, this.logs = const []}); - TransactionReceipt.fromMap(Map map) - : transactionHash = hexToBytes(map['transactionHash'] as String), - transactionIndex = hexToDartInt(map['transactionIndex'] as String), - blockHash = hexToBytes(map['blockHash'] as String), - blockNumber = map['blockNumber'] != null - ? BlockNum.exact(int.parse(map['blockNumber'] as String)) - : const BlockNum.pending(), - from = map['from'] != null - ? EthereumAddress.fromHex(map['from'] as String) - : null, - to = map['to'] != null - ? EthereumAddress.fromHex(map['to'] as String) - : null, - cumulativeGasUsed = hexToInt(map['cumulativeGasUsed'] as String), - gasUsed = - map['gasUsed'] != null ? hexToInt(map['gasUsed'] as String) : null, - effectiveGasPrice = map['effectiveGasPrice'] != null - ? EtherAmount.inWei( - BigInt.parse(map['effectiveGasPrice'] as String)) - : null, - contractAddress = map['contractAddress'] != null - ? EthereumAddress.fromHex(map['contractAddress'] as String) - : null, - status = map['status'] != null - ? (hexToDartInt(map['status'] as String) == 1) - : null, - logs = map['logs'] != null - ? (map['logs'] as List) - .map((log) => FilterEvent.fromMap(log as Map)) - .toList() - : []; - + factory TransactionReceipt.fromMap(Map map) { + return TransactionReceipt( + transactionHash: hexToBytes(map['transactionHash'] as String), + transactionIndex: hexToDartInt(map['transactionIndex'] as String), + blockHash: hexToBytes(map['blockHash'] as String), + blockNumber: map['blockNumber'] != null + ? BlockNum.exact(int.parse(map['blockNumber'] as String)) + : const BlockNum.pending(), + from: map['from'] != null + ? EthereumAddress.fromHex(map['from'] as String) + : null, + to: map['to'] != null + ? EthereumAddress.fromHex(map['to'] as String) + : null, + cumulativeGasUsed: hexToInt(map['cumulativeGasUsed'] as String), + gasUsed: + map['gasUsed'] != null ? hexToInt(map['gasUsed'] as String) : null, + effectiveGasPrice: map['effectiveGasPrice'] != null + ? EtherAmount.inWei( + BigInt.parse(map['effectiveGasPrice'] as String)) + : null, + contractAddress: map['contractAddress'] != null + ? EthereumAddress.fromHex(map['contractAddress'] as String) + : null, + status: map['status'] != null + ? (hexToDartInt(map['status'] as String) == 1) + : null, + logs: map['logs'] != null + ? (map['logs'] as List) + .map((log) => FilterEvent.fromMap(log as Map)) + .toList() + : [ + ] + ); + } /// Hash of the transaction (32 bytes). final Uint8List transactionHash;