apiTx.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:polkawallet_sdk/api/api.dart';
  4. import 'package:polkawallet_sdk/api/types/txInfoData.dart';
  5. import 'package:polkawallet_sdk/service/tx.dart';
  6. class ApiTx {
  7. ApiTx(this.apiRoot, this.service);
  8. final PolkawalletApi apiRoot;
  9. final ServiceTx service;
  10. /// Estimate tx fees, [params] will be ignored if we have [rawParam].
  11. Future<TxFeeEstimateResult?> estimateFees(TxInfoData txInfo, List params,
  12. {String? rawParam, String? jsApi}) async {
  13. final String param = rawParam != null ? rawParam : jsonEncode(params);
  14. final Map tx = txInfo.toJson();
  15. final res = await (service.estimateFees(tx, param, jsApi: jsApi));
  16. if (res == null) {
  17. return null;
  18. }
  19. return TxFeeEstimateResult.fromJson(Map<String, dynamic>.from(res));
  20. }
  21. // Future<dynamic> _testSendTx() async {
  22. // Completer c = new Completer();
  23. // void onComplete(res) {
  24. // c.complete(res);
  25. // }
  26. //
  27. // Timer(Duration(seconds: 6), () => onComplete({'hash': '0x79867'}));
  28. // return c.future;
  29. // }
  30. /// Send tx, [params] will be ignored if we have [rawParam].
  31. /// [onStatusChange] is a callback when tx status change.
  32. /// @return txHash [string] if tx finalized success.
  33. Future<Map?> signAndSend(
  34. TxInfoData txInfo,
  35. List params,
  36. String password, {
  37. Function(String)? onStatusChange,
  38. String? rawParam,
  39. }) async {
  40. final param = rawParam != null ? rawParam : jsonEncode(params);
  41. final Map tx = txInfo.toJson();
  42. print(tx);
  43. print(param);
  44. final res = await (service.signAndSend(
  45. tx,
  46. param,
  47. password,
  48. onStatusChange ?? (status) => print(status),
  49. ));
  50. if (res?['error'] != null) {
  51. throw Exception(res?['error']);
  52. }
  53. return res;
  54. }
  55. }