tx.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:polkawallet_sdk/api/types/txInfoData.dart';
  4. import 'package:polkawallet_sdk/polkawallet_sdk.dart';
  5. import 'package:polkawallet_sdk/storage/keyring.dart';
  6. import 'package:polkawallet_sdk_example/pages/keyring.dart';
  7. class TxPage extends StatefulWidget {
  8. TxPage(this.sdk, this.keyring, this.showResult);
  9. final WalletSDK sdk;
  10. final Keyring keyring;
  11. final Function(BuildContext, String, String) showResult;
  12. static const String route = '/tx';
  13. @override
  14. _TxPageState createState() => _TxPageState();
  15. }
  16. class _TxPageState extends State<TxPage> {
  17. final String _testPubKey =
  18. '0xe611c2eced1b561183f88faed0dd7d88d5fafdf16f5840c63ec36d8c31136f61';
  19. final String _testAddress =
  20. '16CfHoeSifpXMtxVvNAkwgjaeBXK8rAm2CYJvQw4MKMjVHgm';
  21. final String _testAddressGav =
  22. 'FcxNWVy5RESDsErjwyZmPCW6Z8Y3fbfLzmou34YZTrbcraL';
  23. final _testPass = 'a123456';
  24. bool _submitting = false;
  25. String? _status;
  26. Future<void> _estimateTxFee() async {
  27. setState(() {
  28. _submitting = true;
  29. });
  30. final sender = TxSenderData(_testAddress, _testPubKey);
  31. final txInfo = TxInfoData('balances', 'transfer', sender);
  32. final res = await widget.sdk.api.tx.estimateFees(txInfo, [
  33. // params.to
  34. _testAddressGav,
  35. // params.amount
  36. '10000000000'
  37. ]);
  38. widget.showResult(context, 'estimateTxFees',
  39. JsonEncoder.withIndent(' ').convert(res.toJson()));
  40. setState(() {
  41. _submitting = false;
  42. });
  43. }
  44. Future<void> _sendTx() async {
  45. if (widget.keyring.keyPairs.length == 0) {
  46. widget.showResult(
  47. context,
  48. 'sendTx',
  49. 'should import keyPair to init test account.',
  50. );
  51. return;
  52. }
  53. setState(() {
  54. _submitting = true;
  55. });
  56. final sender = TxSenderData(
  57. widget.keyring.keyPairs[0].address,
  58. widget.keyring.keyPairs[0].pubKey,
  59. );
  60. final txInfo = TxInfoData('balances', 'transfer', sender);
  61. try {
  62. final hash = await widget.sdk.api.tx.signAndSend(
  63. txInfo,
  64. [
  65. // params.to
  66. // _testAddressGav,
  67. 'GvrJix8vF8iKgsTAfuazEDrBibiM6jgG66C6sT2W56cEZr3',
  68. // params.amount
  69. '10000000000'
  70. ],
  71. _testPass,
  72. onStatusChange: (status) {
  73. print(status);
  74. setState(() {
  75. _status = status;
  76. });
  77. },
  78. );
  79. widget.showResult(context, 'sendTx', hash.toString());
  80. } catch (err) {
  81. widget.showResult(context, 'sendTx', err.toString());
  82. }
  83. setState(() {
  84. _submitting = false;
  85. });
  86. }
  87. @override
  88. Widget build(BuildContext context) {
  89. return Scaffold(
  90. appBar: AppBar(
  91. title: Text('keyring API'),
  92. ),
  93. body: SafeArea(
  94. child: ListView(
  95. children: [
  96. ListTile(
  97. title: Text('send tx status: $_status'),
  98. ),
  99. Divider(),
  100. ListTile(
  101. title: Text('estimateTxFee'),
  102. subtitle: Text(
  103. 'sdk.api.tx.estimateTxFee(txInfo, ["$_testAddress", "10000000000"])'),
  104. trailing: SubmitButton(
  105. needConnect: widget.sdk.api.connectedNode == null,
  106. submitting: _submitting,
  107. call: _estimateTxFee,
  108. ),
  109. ),
  110. Divider(),
  111. ListTile(
  112. title: Text('sendTx'),
  113. subtitle: Text('sdk.api.tx.sendTx'),
  114. trailing: SubmitButton(
  115. needConnect: widget.sdk.api.connectedNode == null,
  116. submitting: _submitting,
  117. call: _sendTx,
  118. ),
  119. ),
  120. Divider(),
  121. ],
  122. ),
  123. ), // This trailing comma makes auto-formatting nicer for build methods.
  124. );
  125. }
  126. }