tx.dart 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:polkawallet_sdk/service/index.dart';
  4. class ServiceTx {
  5. ServiceTx(this.serviceRoot);
  6. final SubstrateService serviceRoot;
  7. Future<Map?> estimateFees(Map txInfo, String params, {String? jsApi}) async {
  8. dynamic res = await serviceRoot.webView!.evalJavascript(
  9. 'keyring.txFeeEstimate(${jsApi ?? 'api'}, ${jsonEncode(txInfo)}, $params)',
  10. );
  11. return res;
  12. }
  13. // Future<dynamic> _testSendTx() async {
  14. // Completer c = new Completer();
  15. // void onComplete(res) {
  16. // c.complete(res);
  17. // }
  18. //
  19. // Timer(Duration(seconds: 6), () => onComplete({'hash': '0x79867'}));
  20. // return c.future;
  21. // }
  22. Future<Map?> signAndSend(
  23. Map txInfo, String params, password, Function(String) onStatusChange,
  24. {Function(String)? msgIdCallback, Function(String)? uidCallback}) async {
  25. final msgId =
  26. "onStatusChange${serviceRoot.webView!.getEvalJavascriptUID()}";
  27. msgIdCallback?.call(msgId);
  28. serviceRoot.webView!.addMsgHandler(msgId, onStatusChange);
  29. final code =
  30. 'keyring.sendTx(api, ${jsonEncode(txInfo)}, $params, "$password", "$msgId")';
  31. // print(code);
  32. final dynamic res = await serviceRoot.webView!.evalJavascript(
  33. code,
  34. uidCallback: uidCallback,
  35. );
  36. serviceRoot.webView!.removeMsgHandler(msgId);
  37. return res;
  38. }
  39. }