diff --git a/CHANGELOG.md b/CHANGELOG.md index c4df90d..673b797 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 4.0.1 - Resolve issues with TRON model deserialization on the web. +- Add amount to `ADATransactionOutput` model #9 ## 4.0.0 diff --git a/example/lib/a.dart b/example/lib/a.dart new file mode 100644 index 0000000..e7d3ab9 --- /dev/null +++ b/example/lib/a.dart @@ -0,0 +1,16 @@ +// tag 32 4 0 +// decodeLength0 5 10002220000} start e087b7a12530013806400148e0bc8bc3983250e0f4a4ec9832a2010e4d5254205445535420544f4b454eaa011d68747470733a2f2f6769746875622e636f6d2f6d72746e6574776f726b +// tag 48 6 0 +// decodeLength0 1 1} start 013806400148e0bc8bc3983250e0f4a4ec9832a2010e4d5254205445535420544f4b454eaa011d68747470733a2f2f6769746875622e636f6d2f6d72746e6574776f726b +// tag 56 7 0 +// decodeLength0 1 6} start 06400148e0bc8bc3983250e0f4a4ec9832a2010e4d5254205445535420544f4b454eaa011d68747470733a2f2f6769746875622e636f6d2f6d72746e6574776f726b +// tag 64 8 0 +// decodeLength0 1 1} start 0148e0bc8bc3983250e0f4a4ec9832a2010e4d5254205445535420544f4b454eaa011d68747470733a2f2f6769746875622e636f6d2f6d72746e6574776f726b +// tag 72 9 0 +// decodeLength0 6 1724570066528} start e0bc8bc3983250e0f4a4ec9832a2010e4d5254205445535420544f4b454eaa011d68747470733a2f2f6769746875622e636f6d2f6d72746e6574776f726b +// tag 80 10 0 +// decodeLength0 6 1724656466528} start e0f4a4ec9832a2010e4d5254205445535420544f4b454eaa011d68747470733a2f2f6769746875622e636f6d2f6d72746e6574776f726b +// tag 162 20 2 +// decodeLength2 1 0e4d5254205445535420544f4b45 0e4d5254205445535420544f4b454eaa011d68747470733a2f2f6769746875622e636f6d2f6d72746e6574776f726b +// tag 170 21 2 +// decodeLength2 1 1d68747470733a2f2f6769746875622e636f6d2f6d72746e6574776f72 1d68747470733a2f2f6769746875622e636f6d2f6d72746e6574776f726b diff --git a/lib/ada/src/provider/blockfrost/models/models/transaction_utxos.dart b/lib/ada/src/provider/blockfrost/models/models/transaction_utxos.dart index 7e15839..97e89f8 100644 --- a/lib/ada/src/provider/blockfrost/models/models/transaction_utxos.dart +++ b/lib/ada/src/provider/blockfrost/models/models/transaction_utxos.dart @@ -8,7 +8,7 @@ class ADATransactionUTXOSResponse { /// List of outputs final List outputs; - ADATransactionUTXOSResponse({ + const ADATransactionUTXOSResponse({ required this.hash, required this.inputs, required this.outputs, @@ -16,12 +16,11 @@ class ADATransactionUTXOSResponse { factory ADATransactionUTXOSResponse.fromJson(Map json) { return ADATransactionUTXOSResponse( - hash: json['hash'], - inputs: List.from((json['inputs'] as List) - .map((inputJson) => ADATransactionInput.fromJson(inputJson))), - outputs: List.from((json['outputs'] as List) - .map((outputJson) => ADATransactionOutput.fromJson(outputJson))), - ); + hash: json['hash'], + inputs: List.from((json['inputs'] as List) + .map((inputJson) => ADATransactionInput.fromJson(inputJson))), + outputs: List.from((json['outputs'] as List) + .map((outputJson) => ADATransactionOutput.fromJson(outputJson)))); } Map toJson() { @@ -63,10 +62,14 @@ class ADATransactionInput { /// Whether the input is a reference transaction input final bool reference; + /// inputs amounts + final List amount; + ADATransactionInput({ required this.address, required this.txHash, required this.outputIndex, + required this.amount, this.dataHash, this.inlineDatum, this.referenceScriptHash, @@ -84,6 +87,10 @@ class ADATransactionInput { referenceScriptHash: json['reference_script_hash'], collateral: json['collateral'], reference: json['reference'], + amount: (json['amount'] as List?) + ?.map((e) => ADATransactionAmount.fromJson(e)) + .toList() ?? + [], ); } @@ -97,6 +104,7 @@ class ADATransactionInput { 'reference_script_hash': referenceScriptHash, 'collateral': collateral, 'reference': reference, + 'amount': amount.map((e) => e.toJson()).toList() }; } @@ -125,9 +133,13 @@ class ADATransactionOutput { /// The hash of the reference script of the output final String? referenceScriptHash; + /// output amounts + final List amount; + ADATransactionOutput({ required this.address, required this.outputIndex, + required this.amount, this.dataHash, this.inlineDatum, required this.collateral, @@ -142,6 +154,10 @@ class ADATransactionOutput { inlineDatum: json['inline_datum'], collateral: json['collateral'], referenceScriptHash: json['reference_script_hash'], + amount: (json['amount'] as List?) + ?.map((e) => ADATransactionAmount.fromJson(e)) + .toList() ?? + [], ); } @@ -153,6 +169,7 @@ class ADATransactionOutput { 'inline_datum': inlineDatum, 'collateral': collateral, 'reference_script_hash': referenceScriptHash, + 'amount': amount.map((e) => e.toJson()).toList() }; } @@ -161,3 +178,15 @@ class ADATransactionOutput { return "ADATransactionOutput${toJson()}"; } } + +class ADATransactionAmount { + final String unit; + final String quantity; + const ADATransactionAmount({required this.unit, required this.quantity}); + factory ADATransactionAmount.fromJson(Map json) { + return ADATransactionAmount(unit: json["unit"], quantity: json["quantity"]); + } + Map toJson() { + return {"unit": unit, "quantity": quantity}; + } +} diff --git a/lib/tron/src/protbuf/decoder.dart b/lib/tron/src/protbuf/decoder.dart index 2c73775..03e0291 100644 --- a/lib/tron/src/protbuf/decoder.dart +++ b/lib/tron/src/protbuf/decoder.dart @@ -70,7 +70,7 @@ class ProtocolBufferDecoder { static _Result _decodeInt(List data) { final index = data.indexWhere((element) => (element & 0x80) == 0); - if (index <= 4) { + if (index < 4) { return _decodeVarint(data); } return _decodeBigVarint(data); @@ -176,6 +176,8 @@ extension QuickProtocolBufferResult on ProtocolBufferDecoderResult { } return (value == 1 ? true : false) as T; } + } else if (value is BigInt && 0 is T) { + return (value as BigInt).toInt() as T; } throw TronPluginException("Invalid type.", details: {"type": "$T", "Excepted": value.runtimeType.toString()}); diff --git a/test/tron/json_buff_serialization_test.dart b/test/tron/json_buff_serialization_test.dart index 1e9732a..a300e08 100644 --- a/test/tron/json_buff_serialization_test.dart +++ b/test/tron/json_buff_serialization_test.dart @@ -1,3 +1,4 @@ +import 'package:blockchain_utils/blockchain_utils.dart'; import 'package:on_chain/tron/tron.dart'; import 'package:test/test.dart'; @@ -83,7 +84,8 @@ void issueTrc10() { final transaction = Transaction.fromJson(issueJson); expect(transaction.rawData.txID, "f8c7adadffe7b5baed4169287eaa9088739a66bec193855265e84d3f4ce29d4c"); - final decBuffer = Transaction.deserialize(transaction.toBuffer()); + final decBuffer = Transaction.deserialize(BytesUtils.fromHexString( + "0aca010a02f5b722086b94058b90a9d3894098958fc398325aab01080612a6010a2f747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e41737365744973737565436f6e747261637412730a1541084937b3f86ea7bbca86f2809809a65ed8a7ada9120a4d52544e4554574f524b1a034d525420e087b7a12530013806400148e0bc8bc3983250e0f4a4ec9832a2010e4d5254205445535420544f4b454eaa011d68747470733a2f2f6769746875622e636f6d2f6d72746e6574776f726b70b8c08bc39832")); expect(transaction.rawData.txID, decBuffer.rawData.txID); final decodeJson = Transaction.fromJson(decBuffer.toJson()); expect(transaction.rawData.txID, decodeJson.rawData.txID);