Skip to content

Commit

Permalink
v4.0.1
Browse files Browse the repository at this point in the history
Add amount to `ADATransactionOutput` model #9
  • Loading branch information
mrtnetwork committed Aug 30, 2024
1 parent e55c469 commit 1dde91e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 16 additions & 0 deletions example/lib/a.dart
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ class ADATransactionUTXOSResponse {
/// List of outputs
final List<ADATransactionOutput> outputs;

ADATransactionUTXOSResponse({
const ADATransactionUTXOSResponse({
required this.hash,
required this.inputs,
required this.outputs,
});

factory ADATransactionUTXOSResponse.fromJson(Map<String, dynamic> json) {
return ADATransactionUTXOSResponse(
hash: json['hash'],
inputs: List<ADATransactionInput>.from((json['inputs'] as List)
.map((inputJson) => ADATransactionInput.fromJson(inputJson))),
outputs: List<ADATransactionOutput>.from((json['outputs'] as List)
.map((outputJson) => ADATransactionOutput.fromJson(outputJson))),
);
hash: json['hash'],
inputs: List<ADATransactionInput>.from((json['inputs'] as List)
.map((inputJson) => ADATransactionInput.fromJson(inputJson))),
outputs: List<ADATransactionOutput>.from((json['outputs'] as List)
.map((outputJson) => ADATransactionOutput.fromJson(outputJson))));
}

Map<String, dynamic> toJson() {
Expand Down Expand Up @@ -63,10 +62,14 @@ class ADATransactionInput {
/// Whether the input is a reference transaction input
final bool reference;

/// inputs amounts
final List<ADATransactionAmount> amount;

ADATransactionInput({
required this.address,
required this.txHash,
required this.outputIndex,
required this.amount,
this.dataHash,
this.inlineDatum,
this.referenceScriptHash,
Expand All @@ -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() ??
[],
);
}

Expand All @@ -97,6 +104,7 @@ class ADATransactionInput {
'reference_script_hash': referenceScriptHash,
'collateral': collateral,
'reference': reference,
'amount': amount.map((e) => e.toJson()).toList()
};
}

Expand Down Expand Up @@ -125,9 +133,13 @@ class ADATransactionOutput {
/// The hash of the reference script of the output
final String? referenceScriptHash;

/// output amounts
final List<ADATransactionAmount> amount;

ADATransactionOutput({
required this.address,
required this.outputIndex,
required this.amount,
this.dataHash,
this.inlineDatum,
required this.collateral,
Expand All @@ -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() ??
[],
);
}

Expand All @@ -153,6 +169,7 @@ class ADATransactionOutput {
'inline_datum': inlineDatum,
'collateral': collateral,
'reference_script_hash': referenceScriptHash,
'amount': amount.map((e) => e.toJson()).toList()
};
}

Expand All @@ -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<String, dynamic> json) {
return ADATransactionAmount(unit: json["unit"], quantity: json["quantity"]);
}
Map<String, dynamic> toJson() {
return {"unit": unit, "quantity": quantity};
}
}
4 changes: 3 additions & 1 deletion lib/tron/src/protbuf/decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ProtocolBufferDecoder {

static _Result _decodeInt(List<int> data) {
final index = data.indexWhere((element) => (element & 0x80) == 0);
if (index <= 4) {
if (index < 4) {
return _decodeVarint(data);
}
return _decodeBigVarint(data);
Expand Down Expand Up @@ -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()});
Expand Down
4 changes: 3 additions & 1 deletion test/tron/json_buff_serialization_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:on_chain/tron/tron.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1dde91e

Please sign in to comment.