Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify TronTransaction raw transaction handling and remov… #126

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:

generate-docs:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
permissions:
contents: write # Required for pushing to gh-pages
pages: write # Required for deploying to Pages
Expand Down
3 changes: 1 addition & 2 deletions lib/src/crypto/tron/entities/tron_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ base class TronTransaction extends EVMTransaction {
);

final rawData = raw_data_hex.hexToBytes;
final tron.Transaction_raw rawTx =
tron.Transaction_raw.fromBuffer(rawData);
final Transaction_raw rawTx = Transaction_raw.fromBuffer(rawData);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add a null or empty-check for rawTx.contract to prevent runtime errors.

If rawTx.contract is empty, calling rawTx.contract.first will cause an out-of-range error. Consider adding a guard clause before accessing .first.

Proposed fix:

 if (json
     case {
       "txID": String hash,
       "net_usage": int _,
       "net_fee": int netFee,
       "energy_usage": int _,
       "energy_fee": int energyFee,
       "blockNumber": int block,
       "block_timestamp": int block_timestamp,
       "raw_data_hex": String raw_data_hex,
     }) {
   final fee = Amount(
     value: BigInt.from(netFee + energyFee),
     decimals: 6,
   );

   final rawData = raw_data_hex.hexToBytes;
+  // Guard check for empty contract list
+  if (rawData.isEmpty) {
+    return null; // or handle appropriately
+  }
 
-  final Transaction_raw rawTx = Transaction_raw.fromBuffer(rawData);
+  final rawTx = Transaction_raw.fromBuffer(rawData);
+  if (rawTx.contract.isEmpty) {
+    return null; // or handle appropriately
+  }
   final contract = rawTx.contract.first;
   ...
 }

Committable suggestion skipped: line range outside the PR's diff.


final contract = rawTx.contract.first;
final contractType = contract.type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ class ElectrumXClient {
return ElectrumOutput.fromJson(_outputs[index]);
}
})
];
].nonNulls;

return UTXOTransaction.create(
json: mainTxJson,
addressTypes: addressTypes,
type: type,
nodes: nodes,
spentOutputs: outputs.whereType<ElectrumOutput>(),
spentOutputs: outputs,
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/crypto/utxo/utils/send.dart
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ List<Input> signInputs({
walletPath: walletPath,
);
} else
throw SendFailure("Can't sign input without node");
throw SendFailure("Can't sign input without node: $output $input");
}

if (tx is BTCRawTransaction && output.scriptPubKey.isSegwit) {
Expand Down
6 changes: 6 additions & 0 deletions lib/src/crypto/utxo/utxo_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ Future<UTXOTxInfo> fetchUTXOTransactions({
min: minEndpoints,
);

print(
"Selected ${endpoints.map(
(e) => "$e",
)}",
);

Comment on lines +260 to +265
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Replace print statement with Logger utility

The code already imports and uses a Logger utility throughout the file (e.g., Logger.log, Logger.logFetch). For consistency and better control over logging, consider using the Logger utility instead of print.

-  print(
-    "Selected ${endpoints.map(
-      (e) => "$e",
-    )}",
-  );
+  Logger.log(
+    "Selected ${endpoints.map(
+      (e) => "$e",
+    )}",
+  );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
print(
"Selected ${endpoints.map(
(e) => "$e",
)}",
);
Logger.log(
"Selected ${endpoints.map(
(e) => "$e",
)}",
);

final isolateManager = IsolateManager();

///
Expand Down
2 changes: 0 additions & 2 deletions lib/src/domain/entities/generic_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import 'package:walletkit_dart/src/common/http_repository.dart';
import 'package:walletkit_dart/src/crypto/utxo/entities/payments/p2h.dart';
import 'package:walletkit_dart/src/crypto/utxo/utils/pubkey_to_address.dart';
import 'package:walletkit_dart/walletkit_dart.dart';
import 'package:walletkit_dart/src/crypto/tron/repositories/rpc/core/Tron.pb.dart'
as tron;

part '../../crypto/evm/entities/transactions/evm_transaction.dart';
part '../../crypto/utxo/entities/transactions/utxo_transaction.dart';
Expand Down
4 changes: 3 additions & 1 deletion test/no_ci/input_simulation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Future<(UTXOTransaction, String?)> fetchUTXOTXByHash(
Iterable<NodeWithAddress> nodes,
Iterable<AddressType> addressTypes,
) async {
final (result, _, _) = await fetchFromRandomElectrumXNode(
final (result, _, e) = await fetchFromRandomElectrumXNode(
(client) async {
return (
await client.getTransactionForSimulation(
Expand All @@ -221,6 +221,8 @@ Future<(UTXOTransaction, String?)> fetchUTXOTXByHash(
timeout: Duration(seconds: 10),
);

expect(e, isNull);

expect(result, isNotNull, reason: "Result is null for $hash");

return result!;
Expand Down
Loading