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';
|
2024-03-06 06:48:59 +00:00
|
|
|
import 'package:cw_zano/api/api_calls.dart' as calls;
|
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 {
|
|
|
|
PendingZanoTransaction(
|
2023-12-16 12:19:11 +00:00
|
|
|
{required this.zanoWallet,
|
2024-03-06 06:48:59 +00:00
|
|
|
required this.fee,
|
2023-12-16 08:49:23 +00:00
|
|
|
required this.intAmount,
|
|
|
|
//required this.stringAmount,
|
|
|
|
required this.hWallet,
|
|
|
|
required this.address,
|
|
|
|
required this.assetId,
|
|
|
|
required this.comment});
|
2023-10-02 14:17:35 +00:00
|
|
|
|
2023-12-16 12:19:11 +00:00
|
|
|
final ZanoWalletBase zanoWallet;
|
2023-12-16 08:49:23 +00:00
|
|
|
final int hWallet;
|
|
|
|
final int intAmount;
|
|
|
|
//final String stringAmount;
|
|
|
|
final int fee;
|
|
|
|
final String address;
|
|
|
|
final String assetId;
|
|
|
|
final String comment;
|
|
|
|
|
|
|
|
final CryptoCurrency cryptoCurrency = CryptoCurrency.zano;
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
@override
|
2023-12-16 08:49:23 +00:00
|
|
|
String get id => transferResult != null ? transferResult!.txHash : '';
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
String get hex => '';
|
|
|
|
|
|
|
|
@override
|
2023-12-14 04:51:16 +00:00
|
|
|
String get amountFormatted {
|
2023-12-16 08:49:23 +00:00
|
|
|
return AmountConverter.amountIntToString(cryptoCurrency, intAmount);
|
2023-12-14 04:51:16 +00:00
|
|
|
}
|
2023-10-02 14:17:35 +00:00
|
|
|
|
|
|
|
@override
|
2023-12-16 08:49:23 +00:00
|
|
|
String get feeFormatted => AmountConverter.amountIntToString(cryptoCurrency, fee);
|
|
|
|
|
|
|
|
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(
|
|
|
|
destinations: [
|
|
|
|
Destination(
|
|
|
|
amount: intAmount.toString(),
|
|
|
|
address: address,
|
|
|
|
assetId: assetId,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
fee: fee,
|
|
|
|
mixin: zanoMixin,
|
|
|
|
paymentId: '',
|
|
|
|
comment: comment,
|
|
|
|
pushPayer: false,
|
|
|
|
hideReceiver: false,
|
|
|
|
);
|
|
|
|
final result = await zanoWallet.invokeMethod(hWallet, 'transfer', params);
|
2023-12-16 08:49:23 +00:00
|
|
|
final map = jsonDecode(result);
|
2024-03-06 06:48:59 +00:00
|
|
|
if (map['result'] != null && map['result']['result'] != null) {
|
2023-12-16 08:49:23 +00:00
|
|
|
transferResult = TransferResult.fromJson(
|
2023-12-16 12:19:11 +00:00
|
|
|
map['result']['result'] as Map<String, dynamic>,
|
2023-12-16 08:49:23 +00:00
|
|
|
);
|
2023-12-16 12:19:11 +00:00
|
|
|
await zanoWallet.fetchTransactions();
|
|
|
|
} else if (map['result'] != null && map['result']['error'] != null) {
|
|
|
|
final String code;
|
|
|
|
if (map['result']['error']['code'] is int) {
|
|
|
|
code = (map['result']['error']['code'] as int).toString();
|
|
|
|
} else if (map['result']['error']['code'] is String) {
|
|
|
|
code = map['result']['error']['code'] as String;
|
|
|
|
} else {
|
|
|
|
code = '';
|
|
|
|
}
|
|
|
|
final message = map['result']['error']['message'] as String;
|
|
|
|
print('transfer error $code $message');
|
|
|
|
throw TransferException(code, message);
|
2023-12-16 08:49:23 +00:00
|
|
|
}
|
2023-10-02 14:17:35 +00:00
|
|
|
}
|
|
|
|
}
|