private static async Task <Dictionary <int, int> > EstimateHalfFeesAsync(this IRPCClient rpc, IDictionary <int, int> estimations, int smallTarget, int smallFee, int largeTarget, int largeFee, EstimateSmartFeeMode estimateMode = EstimateSmartFeeMode.Conservative, bool simulateIfRegTest = false, bool tolerateBitcoinCoreBrainfuck = true) { var newEstimations = new Dictionary <int, int>(); foreach (var est in estimations) { newEstimations.TryAdd(est.Key, est.Value); } if (Math.Abs(smallTarget - largeTarget) <= 1) { return(newEstimations); } if (smallFee == 0) { var smallFeeResult = await rpc.EstimateSmartFeeAsync(smallTarget, estimateMode, simulateIfRegTest, tolerateBitcoinCoreBrainfuck); smallFee = (int)Math.Ceiling(smallFeeResult.FeeRate.SatoshiPerByte); newEstimations.TryAdd(smallTarget, smallFee); } if (largeFee == 0) { var largeFeeResult = await rpc.EstimateSmartFeeAsync(largeTarget, estimateMode, simulateIfRegTest, tolerateBitcoinCoreBrainfuck); largeFee = (int)Math.Ceiling(largeFeeResult.FeeRate.SatoshiPerByte); largeTarget = largeFeeResult.Blocks; newEstimations.TryAdd(largeTarget, largeFee); } int halfTarget = (smallTarget + largeTarget) / 2; var halfFeeResult = await rpc.EstimateSmartFeeAsync(halfTarget, estimateMode, simulateIfRegTest, tolerateBitcoinCoreBrainfuck); int halfFee = (int)Math.Ceiling(halfFeeResult.FeeRate.SatoshiPerByte); halfTarget = halfFeeResult.Blocks; newEstimations.TryAdd(halfTarget, halfFee); if (smallFee != halfFee) { var smallEstimations = await rpc.EstimateHalfFeesAsync(newEstimations, smallTarget, smallFee, halfTarget, halfFee, estimateMode, simulateIfRegTest, tolerateBitcoinCoreBrainfuck); foreach (var est in smallEstimations) { newEstimations.TryAdd(est.Key, est.Value); } } if (largeFee != halfFee) { var largeEstimations = await rpc.EstimateHalfFeesAsync(newEstimations, halfTarget, halfFee, largeTarget, largeFee, estimateMode, simulateIfRegTest, tolerateBitcoinCoreBrainfuck); foreach (var est in largeEstimations) { newEstimations.TryAdd(est.Key, est.Value); } } return(newEstimations); }
public static async Task <AllFeeEstimate> EstimateAllFeeAsync(this IRPCClient rpc, EstimateSmartFeeMode estimateMode = EstimateSmartFeeMode.Conservative, bool simulateIfRegTest = false, bool tolerateBitcoinCoreBrainfuck = true) { var rpcStatus = await rpc.GetRpcStatusAsync(CancellationToken.None).ConfigureAwait(false); var estimations = await rpc.EstimateHalfFeesAsync(new Dictionary <int, int>(), 2, 0, Constants.SevenDaysConfirmationTarget, 0, estimateMode, simulateIfRegTest, tolerateBitcoinCoreBrainfuck).ConfigureAwait(false); var allFeeEstimate = new AllFeeEstimate(estimateMode, estimations, rpcStatus.Synchronized); return(allFeeEstimate); }
public static async Task <AllFeeEstimate> EstimateAllFeeAsync(this IRPCClient rpc, EstimateSmartFeeMode estimateMode = EstimateSmartFeeMode.Conservative, bool simulateIfRegTest = false) { var rpcStatus = await rpc.GetRpcStatusAsync(CancellationToken.None).ConfigureAwait(false); var mempoolInfo = await rpc.GetMempoolInfoAsync().ConfigureAwait(false); var sanityFeeRate = mempoolInfo.GetSanityFeeRate(); var estimations = await rpc.EstimateHalfFeesAsync(new Dictionary <int, int>(), 2, 0, Constants.SevenDaysConfirmationTarget, 0, sanityFeeRate, estimateMode, simulateIfRegTest).ConfigureAwait(false); var allFeeEstimate = new AllFeeEstimate(estimateMode, estimations, rpcStatus.Synchronized); return(allFeeEstimate); }
private static async Task <Dictionary <int, int> > EstimateHalfFeesAsync(this IRPCClient rpc, IDictionary <int, int> estimations, int smallTarget, int smallTargetFee, int largeTarget, int largeTargetFee, FeeRate sanityFeeRate, EstimateSmartFeeMode estimateMode = EstimateSmartFeeMode.Conservative, bool simulateIfRegTest = false) { var newEstimations = new Dictionary <int, int>(); foreach (var est in estimations) { newEstimations.TryAdd(est.Key, est.Value); } if (Math.Abs(smallTarget - largeTarget) <= 1) { return(newEstimations); } if (smallTargetFee == 0) { var smallTargetFeeResult = await rpc.EstimateSmartFeeAsync(smallTarget, sanityFeeRate, estimateMode, simulateIfRegTest); smallTargetFee = (int)Math.Ceiling(smallTargetFeeResult.FeeRate.SatoshiPerByte); newEstimations.TryAdd(smallTarget, smallTargetFee); } if (largeTargetFee == 0) { var largeTargetFeeResult = await rpc.EstimateSmartFeeAsync(largeTarget, sanityFeeRate, estimateMode, simulateIfRegTest); largeTargetFee = (int)Math.Ceiling(largeTargetFeeResult.FeeRate.SatoshiPerByte); // Blocks should never be larger than the target that we asked for, so it's just a sanity check. largeTarget = Math.Min(largeTarget, largeTargetFeeResult.Blocks); newEstimations.TryAdd(largeTarget, largeTargetFee); } int halfTarget = (smallTarget + largeTarget) / 2; var halfFeeResult = await rpc.EstimateSmartFeeAsync(halfTarget, sanityFeeRate, estimateMode, simulateIfRegTest); int halfTargetFee = (int)Math.Ceiling(halfFeeResult.FeeRate.SatoshiPerByte); // Blocks should never be larger than the target that we asked for, so it's just a sanity check. halfTarget = Math.Min(halfTarget, halfFeeResult.Blocks); newEstimations.TryAdd(halfTarget, halfTargetFee); if (smallTargetFee > halfTargetFee) { var smallEstimations = await rpc.EstimateHalfFeesAsync(newEstimations, smallTarget, smallTargetFee, halfTarget, halfTargetFee, sanityFeeRate, estimateMode, simulateIfRegTest); foreach (var est in smallEstimations) { newEstimations.TryAdd(est.Key, est.Value); } } if (largeTargetFee < halfTargetFee) { var largeEstimations = await rpc.EstimateHalfFeesAsync(newEstimations, halfTarget, halfTargetFee, largeTarget, largeTargetFee, sanityFeeRate, estimateMode, simulateIfRegTest); foreach (var est in largeEstimations) { newEstimations.TryAdd(est.Key, est.Value); } } return(newEstimations); }