staking.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:polkawallet_sdk/polkawallet_sdk.dart';
  4. import 'package:polkawallet_sdk/storage/keyring.dart';
  5. import 'keyring.dart';
  6. class StakingPage extends StatefulWidget {
  7. static const String route = '/staking';
  8. final WalletSDK sdk;
  9. final Keyring keyring;
  10. final Function(BuildContext, String, String) showResult;
  11. StakingPage(this.sdk, this.keyring, this.showResult);
  12. @override
  13. _StakingPageState createState() => _StakingPageState();
  14. }
  15. class _StakingPageState extends State<StakingPage> {
  16. final String _testPubKey =
  17. '0xe611c2eced1b561183f88faed0dd7d88d5fafdf16f5840c63ec36d8c31136f61';
  18. bool _submitting = false;
  19. Future<void> _queryElectedInfo() async {
  20. setState(() {
  21. _submitting = true;
  22. });
  23. final res = await widget.sdk.api.staking.queryElectedInfo();
  24. widget.showResult(
  25. context, 'queryElectedInfo', JsonEncoder.withIndent(' ').convert(res));
  26. setState(() {
  27. _submitting = false;
  28. });
  29. }
  30. Future<void> _queryNominations() async {
  31. setState(() {
  32. _submitting = true;
  33. });
  34. final res = await widget.sdk.api.staking.queryNominations();
  35. widget.showResult(
  36. context, 'queryNominations', JsonEncoder.withIndent(' ').convert(res));
  37. setState(() {
  38. _submitting = false;
  39. });
  40. }
  41. Future<void> _queryBonded() async {
  42. setState(() {
  43. _submitting = true;
  44. });
  45. final res = await widget.sdk.api.staking.queryBonded([_testPubKey]);
  46. widget.showResult(context, 'queryBonded',
  47. JsonEncoder.withIndent(' ').convert(res.toString()));
  48. setState(() {
  49. _submitting = false;
  50. });
  51. }
  52. Future<void> _getAccountRewardsEraOptions() async {
  53. setState(() {
  54. _submitting = true;
  55. });
  56. final res = await widget.sdk.api.staking.getAccountRewardsEraOptions();
  57. widget.showResult(context, 'getAccountRewardsEraOptions',
  58. JsonEncoder.withIndent(' ').convert(res.toString()));
  59. setState(() {
  60. _submitting = false;
  61. });
  62. }
  63. @override
  64. Widget build(BuildContext context) {
  65. return Scaffold(
  66. appBar: AppBar(
  67. title: Text('Staking API'),
  68. ),
  69. body: SafeArea(
  70. child: ListView(
  71. children: [
  72. ListTile(
  73. title: Text('queryElectedInfo'),
  74. subtitle: Text('sdk.api.staking.queryElectedInfo()'),
  75. trailing: SubmitButton(
  76. needConnect: widget.sdk.api.connectedNode == null,
  77. submitting: _submitting,
  78. call: _queryElectedInfo,
  79. ),
  80. ),
  81. Divider(),
  82. ListTile(
  83. title: Text('queryNominations'),
  84. subtitle: Text('sdk.api.staking.queryNominations()'),
  85. trailing: SubmitButton(
  86. needConnect: widget.sdk.api.connectedNode == null,
  87. submitting: _submitting,
  88. call: _queryNominations,
  89. ),
  90. ),
  91. Divider(),
  92. ListTile(
  93. title: Text('queryBonded'),
  94. subtitle: Text('sdk.api.staking.queryBonded(["$_testPubKey"])'),
  95. trailing: SubmitButton(
  96. needConnect: widget.sdk.api.connectedNode == null,
  97. submitting: _submitting,
  98. call: _queryBonded,
  99. ),
  100. ),
  101. Divider(),
  102. ListTile(
  103. title: Text('getAccountRewardsEraOptions'),
  104. subtitle: Text('sdk.api.staking.getAccountRewardsEraOptions()'),
  105. trailing: SubmitButton(
  106. needConnect: widget.sdk.api.connectedNode == null,
  107. submitting: _submitting,
  108. call: _getAccountRewardsEraOptions,
  109. ),
  110. ),
  111. Divider(),
  112. ],
  113. ),
  114. ), // This trailing comma makes auto-formatting nicer for build methods.
  115. );
  116. }
  117. }