|
| 1 | +import 'dart:convert'; |
| 2 | +import 'transactions/utxo.dart'; |
| 3 | +import 'package:http/http.dart' as http; |
| 4 | + |
| 5 | +class RPCError implements Exception { |
| 6 | + const RPCError(this.errorCode, this.message, this.data, |
| 7 | + {required this.request}); |
| 8 | + |
| 9 | + final int errorCode; |
| 10 | + final String message; |
| 11 | + final dynamic data; |
| 12 | + final Map<String, dynamic> request; |
| 13 | + |
| 14 | + @override |
| 15 | + String toString() { |
| 16 | + return 'RPCError: got code $errorCode with msg "$message".'; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +Map<String, dynamic> parseError( |
| 21 | + Map<String, dynamic> data, Map<String, dynamic> request) { |
| 22 | + final error = data['error']; |
| 23 | + if (error == null) return data; |
| 24 | + final code = (error['code'] ?? 0); |
| 25 | + final message = error['message']; |
| 26 | + final errorData = error['data']; |
| 27 | + throw RPCError(code, message, errorData, request: request); |
| 28 | +} |
| 29 | + |
| 30 | +class BTCRpcHelper { |
| 31 | + BTCRpcHelper( |
| 32 | + |
| 33 | + ///The link is for testing, it might not work, please use your RPC service |
| 34 | + {this.url = |
| 35 | + "https://serene-wild-dew.btc-testnet.discover.quiknode.pro/33a88cd7b9e1515949682b452f10c134ae4c2959/", |
| 36 | + Map<String, String>? header}) |
| 37 | + : _header = header ?? |
| 38 | + { |
| 39 | + 'Content-Type': 'application/json', |
| 40 | + 'x-api-key': "0fd2f4ca-25ac-4e19-a6c2-e66696ba4c8b" |
| 41 | + }; |
| 42 | + final String url; |
| 43 | + final Map<String, String> _header; |
| 44 | + |
| 45 | + int _currentRequestId = 1; |
| 46 | + |
| 47 | + Future<T?> call<T>( |
| 48 | + String function, [ |
| 49 | + List<dynamic>? params, |
| 50 | + ]) async { |
| 51 | + http.Client client = http.Client(); |
| 52 | + try { |
| 53 | + params ??= []; |
| 54 | + final payload = { |
| 55 | + 'jsonrpc': '2.0', |
| 56 | + 'method': function, |
| 57 | + 'params': params, |
| 58 | + 'id': _currentRequestId++, |
| 59 | + }; |
| 60 | + |
| 61 | + final response = await client |
| 62 | + .post( |
| 63 | + Uri.parse(url), |
| 64 | + headers: _header, |
| 65 | + body: json.encode(payload), |
| 66 | + ) |
| 67 | + .timeout(const Duration(seconds: 30)); |
| 68 | + final data = parseError(json.decode(response.body), payload); |
| 69 | + |
| 70 | + final result = data['result']; |
| 71 | + |
| 72 | + return result; |
| 73 | + } finally { |
| 74 | + client.close(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + Future<BigInt> getSmartEstimate() async { |
| 79 | + final data = await call("estimatesmartfee", [2, "CONSERVATIVE"]); |
| 80 | + return priceToBtcUnit(data['feerate']); |
| 81 | + } |
| 82 | + |
| 83 | + Future<String> sendRawTransaction(String txDigit) async { |
| 84 | + final data = await call<String>("sendrawtransaction", [txDigit]); |
| 85 | + return data!; |
| 86 | + } |
| 87 | + |
| 88 | + ///This method is for testing, it may not work, please use your RPC service |
| 89 | + Future<List<UTXO>> getUtxo(String address, |
| 90 | + {String? url, |
| 91 | + Map<String, String> header = const { |
| 92 | + 'Content-Type': 'application/json', |
| 93 | + 'api-key': "dc0cdbc2-d3fc-4ae8-ae45-0a44bc28b5f9" |
| 94 | + }}) async { |
| 95 | + http.Client client = http.Client(); |
| 96 | + try { |
| 97 | + String u = |
| 98 | + url ?? "https://btcbook-testnet.nownodes.io/api/v2/utxo/$address"; |
| 99 | + |
| 100 | + final response = await client |
| 101 | + .get( |
| 102 | + Uri.parse(u), |
| 103 | + headers: header, |
| 104 | + ) |
| 105 | + .timeout(const Duration(seconds: 30)); |
| 106 | + final data = json.decode(response.body) as List<dynamic>; |
| 107 | + return data.map((e) => UTXO.fromJson(e)).toList(); |
| 108 | + } finally { |
| 109 | + client.close(); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/// This converter is not accurate |
| 115 | +BigInt priceToBtcUnit(double price, {double decimal = 1e8}) { |
| 116 | + final dec = price * decimal; |
| 117 | + return BigInt.from(dec); |
| 118 | +} |
0 commit comments