apiTx.dart 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. final Map<String, dynamic> resTyped =
  17. res.map((key, value) => MapEntry(key.toString(), value));
  18. return TxFeeEstimateResult.fromJson(resTyped);
  19. }
  20. // Future<dynamic> _testSendTx() async {
  21. // Completer c = new Completer();
  22. // void onComplete(res) {
  23. // c.complete(res);
  24. // }
  25. //
  26. // Timer(Duration(seconds: 6), () => onComplete({'hash': '0x79867'}));
  27. // return c.future;
  28. // }
  29. /// Send tx, [params] will be ignored if we have [rawParam].
  30. /// [onStatusChange] is a callback when tx status change.
  31. /// @return txHash [string] if tx finalized success.
  32. Future<Map> signAndSend(
  33. TxInfoData txInfo,
  34. List params,
  35. String password, {
  36. Function(String)? onStatusChange,
  37. Function(String)? msgIdCallback,
  38. Function(String)? uidCallback,
  39. String? rawParam,
  40. }) async {
  41. final param = rawParam != null ? rawParam : jsonEncode(params);
  42. final Map tx = txInfo.toJson();
  43. print(tx);
  44. print(param);
  45. final res = await service.signAndSend(
  46. tx,
  47. param,
  48. password,
  49. onStatusChange ?? (status) => print(status),
  50. msgIdCallback: msgIdCallback,
  51. uidCallback: uidCallback,
  52. );
  53. if (res?['error'] != null) {
  54. throw Exception(res?['error']);
  55. }
  56. return res ?? {};
  57. }
  58. }