tx.dart 3.8 KB

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