|
1 | | -// Copyright (c) 2010-2022 The Bitcoin Core developers |
| 1 | +// Copyright (c) 2009-2010 Satoshi Nakamoto |
| 2 | +// Copyright (c) 2009-2022 The Bitcoin Core developers |
2 | 3 | // Distributed under the MIT software license, see the accompanying |
3 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | 5 |
|
5 | | -#include <util/error.h> |
| 6 | +#include <common/messages.h> |
6 | 7 |
|
7 | 8 | #include <common/types.h> |
| 9 | +#include <policy/fees.h> |
8 | 10 | #include <tinyformat.h> |
| 11 | +#include <util/strencodings.h> |
| 12 | +#include <util/string.h> |
9 | 13 | #include <util/translation.h> |
10 | 14 |
|
11 | 15 | #include <cassert> |
| 16 | +#include <map> |
12 | 17 | #include <string> |
| 18 | +#include <utility> |
| 19 | +#include <vector> |
13 | 20 |
|
14 | | -using common::PSBTError; |
| 21 | +namespace common { |
| 22 | +std::string StringForFeeReason(FeeReason reason) |
| 23 | +{ |
| 24 | + static const std::map<FeeReason, std::string> fee_reason_strings = { |
| 25 | + {FeeReason::NONE, "None"}, |
| 26 | + {FeeReason::HALF_ESTIMATE, "Half Target 60% Threshold"}, |
| 27 | + {FeeReason::FULL_ESTIMATE, "Target 85% Threshold"}, |
| 28 | + {FeeReason::DOUBLE_ESTIMATE, "Double Target 95% Threshold"}, |
| 29 | + {FeeReason::CONSERVATIVE, "Conservative Double Target longer horizon"}, |
| 30 | + {FeeReason::MEMPOOL_MIN, "Mempool Min Fee"}, |
| 31 | + {FeeReason::PAYTXFEE, "PayTxFee set"}, |
| 32 | + {FeeReason::FALLBACK, "Fallback fee"}, |
| 33 | + {FeeReason::REQUIRED, "Minimum Required Fee"}, |
| 34 | + }; |
| 35 | + auto reason_string = fee_reason_strings.find(reason); |
| 36 | + |
| 37 | + if (reason_string == fee_reason_strings.end()) return "Unknown"; |
| 38 | + |
| 39 | + return reason_string->second; |
| 40 | +} |
| 41 | + |
| 42 | +const std::vector<std::pair<std::string, FeeEstimateMode>>& FeeModeMap() |
| 43 | +{ |
| 44 | + static const std::vector<std::pair<std::string, FeeEstimateMode>> FEE_MODES = { |
| 45 | + {"unset", FeeEstimateMode::UNSET}, |
| 46 | + {"economical", FeeEstimateMode::ECONOMICAL}, |
| 47 | + {"conservative", FeeEstimateMode::CONSERVATIVE}, |
| 48 | + }; |
| 49 | + return FEE_MODES; |
| 50 | +} |
| 51 | + |
| 52 | +std::string FeeModes(const std::string& delimiter) |
| 53 | +{ |
| 54 | + return Join(FeeModeMap(), delimiter, [&](const std::pair<std::string, FeeEstimateMode>& i) { return i.first; }); |
| 55 | +} |
| 56 | + |
| 57 | +std::string InvalidEstimateModeErrorMessage() |
| 58 | +{ |
| 59 | + return "Invalid estimate_mode parameter, must be one of: \"" + FeeModes("\", \"") + "\""; |
| 60 | +} |
| 61 | + |
| 62 | +bool FeeModeFromString(const std::string& mode_string, FeeEstimateMode& fee_estimate_mode) |
| 63 | +{ |
| 64 | + auto searchkey = ToUpper(mode_string); |
| 65 | + for (const auto& pair : FeeModeMap()) { |
| 66 | + if (ToUpper(pair.first) == searchkey) { |
| 67 | + fee_estimate_mode = pair.second; |
| 68 | + return true; |
| 69 | + } |
| 70 | + } |
| 71 | + return false; |
| 72 | +} |
15 | 73 |
|
16 | 74 | bilingual_str PSBTErrorString(PSBTError err) |
17 | 75 | { |
@@ -74,3 +132,4 @@ bilingual_str AmountErrMsg(const std::string& optname, const std::string& strVal |
74 | 132 | { |
75 | 133 | return strprintf(_("Invalid amount for -%s=<amount>: '%s'"), optname, strValue); |
76 | 134 | } |
| 135 | +} // namespace common |
0 commit comments