2023-12-16 08:49:23 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-12-16 12:19:11 +00:00
|
|
|
import 'package:cw_zano/api/exceptions/transfer_exception.dart';
|
2023-12-16 08:49:23 +00:00
|
|
|
import 'package:cw_zano/api/model/destination.dart';
|
|
|
|
import 'package:cw_zano/api/model/transfer_params.dart';
|
|
|
|
import 'package:cw_zano/api/model/transfer_result.dart';
|
2023-10-02 14:17:35 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
|
|
|
import 'package:cw_core/amount_converter.dart';
|
|
|
|
import 'package:cw_core/pending_transaction.dart';
|
2023-12-16 08:49:23 +00:00
|
|
|
import 'package:cw_zano/zano_wallet.dart';
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
class PendingZanoTransaction with PendingTransaction {
|
2024-03-08 10:50:34 +00:00
|
|
|
PendingZanoTransaction({
|
|
|
|
required this.zanoWallet,
|
|
|
|
required this.destinations,
|
|
|
|
required this.fee,
|
|
|
|
required this.comment,
|
|
|
|
});
|
2023-10-02 14:17:35 +00:00
|
|
|
|
2023-12-16 12:19:11 +00:00
|
|
|
final ZanoWalletBase zanoWallet;
|
2024-03-08 10:50:34 +00:00
|
|
|
final List<Destination> destinations;
|
2023-12-16 08:49:23 +00:00
|
|
|
final int fee;
|
|
|
|
final String comment;
|
|
|
|
|
2023-10-02 14:17:35 +00:00
|
|
|
@override
|
2024-03-08 10:50:34 +00:00
|
|
|
String get id => transferResult?.txHash ?? '';
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
String get hex => '';
|
|
|
|
|
|
|
|
@override
|
2024-03-08 10:50:34 +00:00
|
|
|
String get amountFormatted => AmountConverter.amountIntToString(CryptoCurrency.zano, destinations.first.amount);
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
@override
|
2024-03-08 10:50:34 +00:00
|
|
|
String get feeFormatted => AmountConverter.amountIntToString(CryptoCurrency.zano, fee);
|
2023-12-16 08:49:23 +00:00
|
|
|
|
|
|
|
TransferResult? transferResult;
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> commit() async {
|
2024-03-06 06:48:59 +00:00
|
|
|
final params = TransferParams(
|
2024-03-08 10:50:34 +00:00
|
|
|
destinations: destinations,
|
2024-03-06 06:48:59 +00:00
|
|
|
fee: fee,
|
|
|
|
mixin: zanoMixin,
|
|
|
|
paymentId: '',
|
|
|
|
comment: comment,
|
|
|
|
pushPayer: false,
|
2024-03-08 10:50:34 +00:00
|
|
|
hideReceiver: true,
|
2024-03-06 06:48:59 +00:00
|
|
|
);
|
2024-03-08 10:50:34 +00:00
|
|
|
final result = await zanoWallet.invokeMethod('transfer', params);
|
2023-12-16 08:49:23 +00:00
|
|
|
final map = jsonDecode(result);
|
2024-03-08 10:50:34 +00:00
|
|
|
final resultMap = map['result'] as Map<String, dynamic>?;
|
|
|
|
if (resultMap != null) {
|
|
|
|
final transferResultMap = resultMap['result'] as Map<String, dynamic>?;
|
|
|
|
if (transferResultMap != null) {
|
|
|
|
transferResult = TransferResult.fromJson(transferResultMap);
|
|
|
|
print('transfer success hash ${transferResult!.txHash}');
|
|
|
|
await zanoWallet.fetchTransactions();
|
2023-12-16 12:19:11 +00:00
|
|
|
} else {
|
2024-03-08 10:50:34 +00:00
|
|
|
final errorCode = resultMap['error']['code'];
|
|
|
|
final code = errorCode is int ? errorCode.toString() : errorCode as String? ?? '';
|
|
|
|
final message = resultMap['error']['message'] as String? ?? '';
|
|
|
|
print('transfer error $code $message');
|
|
|
|
throw TransferException(code, message);
|
2023-12-16 12:19:11 +00:00
|
|
|
}
|
2023-12-16 08:49:23 +00:00
|
|
|
}
|
2023-10-02 14:17:35 +00:00
|
|
|
}
|
|
|
|
}
|