|
| 1 | +import 'package:convert/convert.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_form_builder/flutter_form_builder.dart'; |
| 4 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 5 | +import 'package:flutter_rust_bridge/flutter_rust_bridge.dart'; |
| 6 | +import 'package:form_builder_validators/form_builder_validators.dart'; |
| 7 | +import 'package:gap/gap.dart'; |
| 8 | +import 'package:go_router/go_router.dart'; |
| 9 | +import 'package:zkool/src/rust/api/issuance.dart'; |
| 10 | +import 'package:zkool/src/rust/api/pay.dart'; |
| 11 | +import 'package:zkool/store.dart'; |
| 12 | +import 'package:zkool/utils.dart'; |
| 13 | +import 'package:zkool/widgets/error_display.dart'; |
| 14 | + |
| 15 | +class ZsaHoldingsPage extends ConsumerStatefulWidget { |
| 16 | + const ZsaHoldingsPage({super.key}); |
| 17 | + |
| 18 | + @override |
| 19 | + ConsumerState<ZsaHoldingsPage> createState() => _ZsaHoldingsPageState(); |
| 20 | +} |
| 21 | + |
| 22 | +class _ZsaHoldingsPageState extends ConsumerState<ZsaHoldingsPage> { |
| 23 | + @override |
| 24 | + Widget build(BuildContext context) { |
| 25 | + final tt = Theme.of(context).textTheme; |
| 26 | + |
| 27 | + final accountData = ref.watch(getCurrentAccountProvider); |
| 28 | + |
| 29 | + return accountData.when( |
| 30 | + loading: () => blank(context), |
| 31 | + error: (error, stack) => showError(error), |
| 32 | + data: (data) { |
| 33 | + final zsas = data?.zsas ?? []; |
| 34 | + |
| 35 | + return Scaffold( |
| 36 | + appBar: AppBar( |
| 37 | + title: const Text("ZSA Holdings"), |
| 38 | + leading: IconButton( |
| 39 | + icon: const Icon(Icons.arrow_back), |
| 40 | + onPressed: () => GoRouter.of(context).pop(), |
| 41 | + ), |
| 42 | + actions: [ |
| 43 | + IconButton( |
| 44 | + tooltip: "Issue new token", |
| 45 | + icon: const Icon(Icons.add), |
| 46 | + onPressed: () => GoRouter.of(context).push("/zsa/issue"), |
| 47 | + ), |
| 48 | + ], |
| 49 | + ), |
| 50 | + body: CustomScrollView( |
| 51 | + slivers: [ |
| 52 | + if (zsas.isEmpty) |
| 53 | + SliverFillRemaining( |
| 54 | + child: Center( |
| 55 | + child: Text("Any ZSA tokens you receive will appear here.", style: tt.bodyMedium), |
| 56 | + ), |
| 57 | + ) |
| 58 | + else |
| 59 | + SliverFixedExtentList.builder( |
| 60 | + itemCount: zsas.length, |
| 61 | + itemExtent: 64, |
| 62 | + itemBuilder: (context, index) { |
| 63 | + final h = zsas[index]; |
| 64 | + |
| 65 | + final displayName = h.assetName.isNotEmpty |
| 66 | + ? h.assetName |
| 67 | + : hex.encode(h.assetDescHash.sublist(0, 8)); |
| 68 | + |
| 69 | + return Column( |
| 70 | + children: [ |
| 71 | + Expanded( |
| 72 | + child: ListTile( |
| 73 | + leading: CircleAvatar( |
| 74 | + backgroundColor: Colors.blue, |
| 75 | + child: Text( |
| 76 | + initials(displayName), |
| 77 | + style: tt.titleMedium?.copyWith(color: Colors.white), |
| 78 | + ), |
| 79 | + ), |
| 80 | + title: Text(displayName), |
| 81 | + subtitle: Text(hex.encode(h.assetDescHash.sublist(0, 8))), |
| 82 | + trailing: Text(h.balance.toString(), style: tt.titleMedium), |
| 83 | + ), |
| 84 | + ), |
| 85 | + const Divider(height: 1, thickness: 1, indent: 16, endIndent: 16), |
| 86 | + ], |
| 87 | + ); |
| 88 | + }, |
| 89 | + ), |
| 90 | + ], |
| 91 | + ), |
| 92 | + ); |
| 93 | + }, |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +class IssueAssetPage extends ConsumerStatefulWidget { |
| 99 | + const IssueAssetPage({super.key}); |
| 100 | + |
| 101 | + @override |
| 102 | + ConsumerState<IssueAssetPage> createState() => _IssueAssetPageState(); |
| 103 | +} |
| 104 | + |
| 105 | +class _IssueAssetPageState extends ConsumerState<IssueAssetPage> { |
| 106 | + static final _maxSupply = BigInt.from(21000000) * BigInt.from(100000000); |
| 107 | + final _formKey = GlobalKey<FormBuilderState>(); |
| 108 | + |
| 109 | + @override |
| 110 | + Widget build(BuildContext context) { |
| 111 | + return Scaffold( |
| 112 | + appBar: AppBar( |
| 113 | + title: const Text("Issue New Token"), |
| 114 | + actions: [ |
| 115 | + IconButton( |
| 116 | + tooltip: "Issue", |
| 117 | + icon: const Icon(Icons.check), |
| 118 | + onPressed: _issue, |
| 119 | + ), |
| 120 | + ], |
| 121 | + ), |
| 122 | + body: Padding( |
| 123 | + padding: const EdgeInsets.all(16), |
| 124 | + child: FormBuilder( |
| 125 | + key: _formKey, |
| 126 | + initialValue: const { |
| 127 | + "first_issuance": false, |
| 128 | + "finalize": false, |
| 129 | + }, |
| 130 | + child: Column( |
| 131 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 132 | + children: [ |
| 133 | + // TODO: to support adding supply to an existing asset, we need |
| 134 | + // an optional asset_desc_hash field. The asset name alone doesn't |
| 135 | + // identify the asset — the desc_hash is the canonical identifier. |
| 136 | + FormBuilderTextField( |
| 137 | + name: "asset_name", |
| 138 | + decoration: const InputDecoration(labelText: "Asset Name"), |
| 139 | + validator: FormBuilderValidators.required(), |
| 140 | + ), |
| 141 | + const Gap(16), |
| 142 | + FormBuilderTextField( |
| 143 | + name: "amount", |
| 144 | + decoration: const InputDecoration(labelText: "Amount"), |
| 145 | + keyboardType: TextInputType.number, |
| 146 | + validator: FormBuilderValidators.compose([ |
| 147 | + FormBuilderValidators.required(), |
| 148 | + FormBuilderValidators.integer(), |
| 149 | + (v) { |
| 150 | + if (v == null) return null; |
| 151 | + final n = BigInt.tryParse(v); |
| 152 | + if (n == null) return "Invalid number"; |
| 153 | + if (n <= BigInt.zero) return "Must be greater than 0"; |
| 154 | + if (n > _maxSupply) return "Exceeds max supply of 21 million"; |
| 155 | + return null; |
| 156 | + }, |
| 157 | + ]), |
| 158 | + ), |
| 159 | + const Gap(16), |
| 160 | + FormBuilderSwitch( |
| 161 | + name: "first_issuance", |
| 162 | + title: const Text("First Issuance"), |
| 163 | + subtitle: const Text("Include a zero-value reference note (ZIP-227)"), |
| 164 | + ), |
| 165 | + FormBuilderSwitch( |
| 166 | + name: "finalize", |
| 167 | + title: const Text("Finalize"), |
| 168 | + subtitle: const Text("Prevent any future issuance of this asset"), |
| 169 | + ), |
| 170 | + ], |
| 171 | + ), |
| 172 | + ), |
| 173 | + ), |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + Future<void> _issue() async { |
| 178 | + final form = _formKey.currentState!; |
| 179 | + if (!form.saveAndValidate()) return; |
| 180 | + |
| 181 | + final assetName = form.value["asset_name"] as String; |
| 182 | + final amount = form.value["amount"] as String; |
| 183 | + final firstIssuance = form.value["first_issuance"] as bool; |
| 184 | + final finalize = form.value["finalize"] as bool; |
| 185 | + |
| 186 | + final confirmed = await confirmDialog( |
| 187 | + context, |
| 188 | + title: "Issue $assetName", |
| 189 | + message: "Issue $amount units of $assetName?${finalize ? ' This will finalize the asset.' : ''}", |
| 190 | + ); |
| 191 | + if (!confirmed) return; |
| 192 | + |
| 193 | + try { |
| 194 | + final txBytes = await issueAsset( |
| 195 | + assetName: assetName, |
| 196 | + amount: BigInt.parse(amount), |
| 197 | + firstIssuance: firstIssuance, |
| 198 | + finalize: finalize, |
| 199 | + idAccount: coinContext.coin.account, |
| 200 | + c: coinContext.coin, |
| 201 | + ); |
| 202 | + final height = ref.read(currentHeightProvider) ?? 1; |
| 203 | + final txid = await broadcastTransaction( |
| 204 | + height: height, |
| 205 | + txBytes: txBytes, |
| 206 | + c: coinContext.coin, |
| 207 | + ); |
| 208 | + if (!mounted) return; |
| 209 | + ScaffoldMessenger.of(context).showSnackBar( |
| 210 | + SnackBar(content: Text("Transaction broadcast: $txid")), |
| 211 | + ); |
| 212 | + GoRouter.of(context).pop(); |
| 213 | + } on AnyhowException catch (e) { |
| 214 | + if (mounted) await showException(context, e.message); |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments