mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-22 14:22:40 +01:00
Cheat mode to mine blocks (#3163)
This commit is contained in:
parent
6fec929656
commit
8f0d82d219
2 changed files with 74 additions and 21 deletions
|
@ -17,6 +17,12 @@ namespace BTCPayServer.Controllers
|
|||
public Decimal Amount { get; set; }
|
||||
public string CryptoCode { get; set; } = "BTC";
|
||||
}
|
||||
|
||||
public class MineBlocksRequest
|
||||
{
|
||||
public int BlockCount { get; set; }
|
||||
public string CryptoCode { get; set; } = "BTC";
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("i/{invoiceId}/test-payment")]
|
||||
|
@ -57,6 +63,45 @@ namespace BTCPayServer.Controllers
|
|||
});
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("i/{invoiceId}/mine-blocks")]
|
||||
[CheatModeRoute]
|
||||
public async Task<IActionResult> MineBlock(string invoiceId, MineBlocksRequest request, [FromServices] Cheater cheater)
|
||||
{
|
||||
var invoice = await _InvoiceRepository.GetInvoice(invoiceId);
|
||||
var store = await _StoreRepository.FindStore(invoice.StoreId);
|
||||
|
||||
// TODO support altcoins, not just bitcoin
|
||||
var network = _NetworkProvider.GetNetwork<BTCPayNetwork>(request.CryptoCode);
|
||||
var paymentMethodId = store.GetDefaultPaymentId() ?? store.GetEnabledPaymentIds(_NetworkProvider).FirstOrDefault(p => p.CryptoCode == request.CryptoCode && p.PaymentType == PaymentTypes.BTCLike);
|
||||
var bitcoinAddressString = invoice.GetPaymentMethod(paymentMethodId).GetPaymentMethodDetails().GetPaymentDestination();
|
||||
var bitcoinAddressObj = BitcoinAddress.Create(bitcoinAddressString, network.NBitcoinNetwork);
|
||||
|
||||
// Mine the blocks
|
||||
try
|
||||
{
|
||||
if (request.BlockCount > 0)
|
||||
{
|
||||
cheater.CashCow.GenerateToAddress(request.BlockCount, bitcoinAddressObj);
|
||||
return Ok(new
|
||||
{
|
||||
SuccessMessage = "Mined "+request.BlockCount+" blocks for " + bitcoinAddressObj
|
||||
});
|
||||
}
|
||||
return BadRequest(new
|
||||
{
|
||||
ErrorMessage = "Number of blocks should be > 0"
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return BadRequest(new
|
||||
{
|
||||
ErrorMessage = e.Message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("i/{invoiceId}/expire")]
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<hr class="my-3" />
|
||||
<p class="alert alert-danger" style="display: none; word-break: break-all;"></p>
|
||||
<p class="alert alert-success" style="display: none; word-break: break-all;"></p>
|
||||
<form id="test-payment" action="/i/@Model.InvoiceId/test-payment" method="post" class="form-inline my-2">
|
||||
<form id="test-payment" action="/i/@Model.InvoiceId/test-payment" method="post" class="cheat-mode form-inline my-2">
|
||||
<input name="CryptoCode" type="hidden" value="@Model.CryptoCode">
|
||||
<div class="form-group mb-1">
|
||||
<label for="test-payment-amount" class="control-label">{{$t("Fake a @Model.CryptoCode payment for testing")}}</label>
|
||||
|
@ -24,35 +24,39 @@
|
|||
<!-- TODO only show when expired -->
|
||||
<button class="btn btn-secondary" type="submit">{{$t("Expire Monitoring Now")}} (TODO)</button>
|
||||
</form>
|
||||
<form id="mine-block" action="/i/@Model.InvoiceId/mine-block" method="post">
|
||||
<!-- TODO only show when BTC On-chain -->
|
||||
<!-- TODO to make it work use Bitcoin RPC calls getnewaddress + generatetoaddress -->
|
||||
<button class="btn btn-secondary" type="submit">{{$t("Mine a block now")}} (TODO)</button>
|
||||
</form>
|
||||
*@
|
||||
<form id="mine-block" action="/i/@Model.InvoiceId/mine-blocks" method="post" class="cheat-mode form-inline my-2">
|
||||
<!-- TODO only show when BTC On-chain -->
|
||||
<div class="form-group mb-1">
|
||||
<label for="block-count" class="control-label">{{$t("Mine a few blocks to test processing and settlement.")}}</label>
|
||||
<div class="input-group">
|
||||
<input id="block-count" name="BlockCount" type="number" step="1" min="1" class="form-control" value="1" />
|
||||
<div class="input-group-addon">{{$t("Blocks")}}</div>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-primary" type="submit">{{$t("Mine")}}</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
const payForm = $('form#test-payment');
|
||||
const loader = $('form#test-payment-loading');
|
||||
const inputField = $('#test-payment-amount');
|
||||
const cheatForms = $('form.cheat-mode');
|
||||
const expireForm = $('form#expire-invoice');
|
||||
|
||||
const successAlert = $('#testing p.alert-success');
|
||||
const errorAlert = $('#testing p.alert-danger');
|
||||
const submitButton = payForm.find('button[type=submit]');
|
||||
const expireForm = $('form#expire-invoice');
|
||||
const expireButton = expireForm.find('[type=submit]');
|
||||
|
||||
payForm.submit(e => {
|
||||
cheatForms.submit(e => {
|
||||
e.preventDefault();
|
||||
|
||||
const form = $(e.target);
|
||||
const url = form.attr('action');
|
||||
const data = form.serialize();
|
||||
const inputField = form.find('input[type=number]');
|
||||
const submitButton = form.find('button[type=submit]');
|
||||
|
||||
successAlert.hide();
|
||||
errorAlert.hide();
|
||||
loader.show();
|
||||
inputField.prop('disabled', true);
|
||||
submitButton.prop('disabled', true);
|
||||
|
||||
|
@ -63,11 +67,13 @@
|
|||
const { successMessage, amountRemaining } = data;
|
||||
successAlert.html(successMessage);
|
||||
successAlert.show();
|
||||
if (amountRemaining > 0) {
|
||||
inputField.val(amountRemaining);
|
||||
} else {
|
||||
form.hide();
|
||||
expireForm.hide();
|
||||
if (amountRemaining){
|
||||
if(amountRemaining > 0) {
|
||||
inputField.val(amountRemaining);
|
||||
} else {
|
||||
form.hide();
|
||||
expireForm.hide();
|
||||
}
|
||||
}
|
||||
},
|
||||
error(xhr) {
|
||||
|
@ -75,7 +81,6 @@
|
|||
errorAlert.html(errorMessage).show();
|
||||
},
|
||||
complete() {
|
||||
loader.hide();
|
||||
inputField.prop('disabled', false);
|
||||
submitButton.prop('disabled', false);
|
||||
}
|
||||
|
@ -85,6 +90,10 @@
|
|||
// Expire invoice form
|
||||
expireForm.submit(e => {
|
||||
e.preventDefault();
|
||||
|
||||
const form = $(e.target);
|
||||
const submitButton = form.find('button[type=submit]');
|
||||
const expireButton = form.find('[type=submit]');
|
||||
successAlert.hide();
|
||||
errorAlert.hide();
|
||||
|
||||
|
@ -96,7 +105,6 @@
|
|||
expireButton.hide();
|
||||
},
|
||||
complete() {
|
||||
loader.hide();
|
||||
submitButton.prop('disabled', false);
|
||||
},
|
||||
error(xhr) {
|
||||
|
|
Loading…
Add table
Reference in a new issue