Commit 6dc5c1bb authored by Sergei Zubov's avatar Sergei Zubov

Add fee type to estimate fee request

If fee type is "Included", fees is charged from receiver. If fee
type is "Extra", fee is charged from sender.
parent 00fb193c
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.AspNetCore.Mvc;
......@@ -12,7 +13,7 @@ using Stratis.Bitcoin.Utilities.JsonErrors;
namespace Stratis.Bitcoin.Features.Wallet.Controllers
{
/// <summary>
/// Controller providing operations on a wallet.
/// Controller providing operations on a wallet.
/// </summary>
[Route("api/[controller]")]
public class DeStreamWalletController : Controller
......@@ -30,7 +31,7 @@ namespace Stratis.Bitcoin.Features.Wallet.Controllers
}
/// <summary>
/// Builds a transaction.
/// Builds a transaction.
/// </summary>
/// <param name="request">The transaction parameters.</param>
/// <returns>All the details of the transaction, including the hex used to execute it.</returns>
......@@ -58,16 +59,7 @@ namespace Stratis.Bitcoin.Features.Wallet.Controllers
true // We shuffle transaction outputs by default as it's better for anonymity.
};
if (Enum.TryParse(request.FeeType, out DeStreamFeeType feeType))
{
if (feeType == DeStreamFeeType.Included)
{
foreach (Recipient recipient in context.Recipients)
recipient.Amount = this._network.SubtractFee(recipient.Amount);
}
}
else
throw new FormatException($"FeeType {request.FeeType} is not a valid DeStreamFeeType");
this.ProcessFeeType(request.FeeType, context.Recipients);
Transaction transactionResult = this._walletTransactionHandler.BuildTransaction(context);
......@@ -86,5 +78,63 @@ namespace Stratis.Bitcoin.Features.Wallet.Controllers
return ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString());
}
}
/// <summary>
/// Gets a transaction fee estimate.
/// Fee can be estimated by creating a <see cref="TransactionBuildContext" /> with no password
/// and then building the transaction and retrieving the fee from the context.
/// </summary>
/// <param name="request">The transaction parameters.</param>
/// <returns>The estimated fee for the transaction.</returns>
[Route("estimate-txfee")]
[HttpGet]
public IActionResult GetTransactionFeeEstimate([FromQuery] TxFeeEstimateRequest request)
{
Guard.NotNull(request, nameof(request));
// checks the request is valid
if (!this.ModelState.IsValid) return WalletController.BuildErrorResponse(this.ModelState);
try
{
Script destination = BitcoinAddress.Create(request.DestinationAddress, this._network).ScriptPubKey;
var context = new TransactionBuildContext(
new WalletAccountReference(request.WalletName, request.AccountName),
new[] {new Recipient {Amount = request.Amount, ScriptPubKey = destination}}.ToList())
{
MinConfirmations = request.AllowUnconfirmed ? 0 : 1
};
this.ProcessFeeType(request.FeeType, context.Recipients);
return this.Json(this._walletTransactionHandler.EstimateFee(context));
}
catch (Exception e)
{
this._logger.LogError("Exception occurred: {0}", e.ToString());
return ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString());
}
}
/// <summary>
/// Modifies transfer funds amount according to fee type.
/// For <see cref="DeStreamFeeType.Extra" /> fee type, fee is subtracted from transfer funds.
/// For <see cref="DeStreamFeeType.Included" /> fee type, nothing changed.
/// </summary>
/// <param name="requestFeeType">Fee type in request</param>
/// <param name="recipients">Recipients</param>
/// <exception cref="FormatException">Fee type string is invalid</exception>
private void ProcessFeeType(string requestFeeType, IEnumerable<Recipient> recipients)
{
if (Enum.TryParse(requestFeeType, out DeStreamFeeType feeType))
{
if (feeType != DeStreamFeeType.Included) return;
foreach (Recipient recipient in recipients)
recipient.Amount = this._network.SubtractFee(recipient.Amount);
}
else
throw new FormatException($"FeeType {requestFeeType} is not a valid DeStreamFeeType");
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment