Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt Indexer to Mempool Endpoints #223

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions src/Blockcore.Indexer.Angor/Controllers/MempoolSpaceController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Microsoft.AspNetCore.Mvc;
using Blockcore.Indexer.Core.Storage;
using Blockcore.Indexer.Core.Models;
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using Blockcore.Indexer.Core.Storage.Types;
using Blockcore.Indexer.Core.Handlers;



namespace Blockcore.Indexer.Angor.Controllers
{
[ApiController]
[Route("api/mempoolspace")]
public class MempoolSpaceController : Controller
{
private readonly IStorage storage;
private readonly StatsHandler statsHandler;

private readonly JsonSerializerOptions serializeOption = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
WriteIndented = true
};

public MempoolSpaceController(IStorage storage, StatsHandler statsHandler)
{
this.storage = storage;
this.statsHandler = statsHandler;
}

[HttpGet]
[Route("address/{address}")]
public IActionResult GetAddress([MinLength(4)][MaxLength(100)] string address)
{
AddressResponse addressResponse = storage.AddressResponseBalance(address);
return Ok(JsonSerializer.Serialize(addressResponse, serializeOption));
}

[HttpGet]
[Route("address/{address}/txs")]
public async Task<IActionResult> GetAddressTransactions(string address)
{
var transactions = storage.AddressHistory(address, null, 50).Items.Select(t => t.TransactionHash).ToList();
List<MempoolTransaction> txns = await storage.GetMempoolTransactionListAsync(transactions);
return Ok(JsonSerializer.Serialize(txns, serializeOption));
}

[HttpGet]
[Route("tx/{txid}/outspends")]
public async Task<IActionResult> GetTransactionOutspends(string txid)
{
List<OutspentResponse> responses = await storage.GetTransactionOutspendsAsync(txid);
return Ok(JsonSerializer.Serialize(responses, serializeOption));
}

[HttpGet]
[Route("fees/recommended")]
public IActionResult GetRecommendedFees()
{
RecommendedFees recommendedFees = new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is done in another branch so I'll ignore it here

var statsFees = statsHandler.GetFeeEstimation([1, 3, 6, 12, 48]);
statsFees.Wait();
var Fees = statsFees.Result.Fees.Select(fee => ConvertToSatsPerVByte(fee.FeeRate)).ToList();
recommendedFees.FastestFee = (int)Fees[0];
recommendedFees.HalfHourFee = (int)Fees[1];
recommendedFees.HourFee = (int)Fees[2];
recommendedFees.EconomyFee = (int)Fees[3];
recommendedFees.MinimumFee = (int)Fees[4];

return Ok(JsonSerializer.Serialize(recommendedFees, new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
}));
}

private double ConvertToSatsPerVByte(double fee)
{
return fee / 1_000;
}

[HttpGet]
[Route("tx/{txid}/hex")]
public IActionResult GetTransactionHex(string txid)
{
var txn = storage.GetRawTransaction(txid);
if (txn == null)
{
return NotFound();
}
return Ok(txn);
}

[HttpGet]
[Route("block-height/{height}")]
public IActionResult GetBlockHeight(int height)
{
return Ok(storage.BlockByIndex(height).BlockHash);
}
}
}
97 changes: 97 additions & 0 deletions src/Blockcore.Indexer.Core/Models/MempoolSpaceModels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http.Json;


namespace Blockcore.Indexer.Core.Models
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer having each model in a seperate file under the same namespace

{

public class AddressStats
{
public int FundedTxoCount { get; set; }
public long FundedTxoSum { get; set; }
public int SpentTxoCount { get; set; }
public long SpentTxoSum { get; set; }
public int TxCount { get; set; }
}
public class AddressResponse
{
public string Address { get; set; }
public AddressStats ChainStats { get; set; }
public AddressStats MempoolStats { get; set; }
}

public class OutspentResponse{
public bool spent { get; set; }
public string txid { get; set; }
public int vin { get; set; }
public UtxoStatus status { get; set; }
}

public class AddressUtxo
{
public string Txid { get; set; }
public int Vout { get; set; }
public UtxoStatus Status { get; set; }
public long Value { get; set; }
}

public class UtxoStatus
{
public bool Confirmed { get; set; }
public int BlockHeight { get; set; }
public string BlockHash { get; set; }
public long BlockTime { get; set; }
}

public class RecommendedFees
{
public int FastestFee { get; set; }
public int HalfHourFee { get; set; }
public int HourFee { get; set; }
public int EconomyFee { get; set; }
public int MinimumFee { get; set; }
}

public class Vin
{
public bool IsCoinbase { get; set; }
public PrevOut Prevout { get; set; }
public string Scriptsig { get; set; }
public string Asm { get; set; }
public long Sequence { get; set; }
public string Txid { get; set; }
public int Vout { get; set; }
public List<string> Witness { get; set; }
public string InnserRedeemscriptAsm { get; set; }
public string InnerWitnessscriptAsm { get; set; }
}
public class PrevOut
{
public long Value { get; set; }
public string Scriptpubkey { get; set; }
public string ScriptpubkeyAddress { get; set; }
public string ScriptpubkeyAsm { get; set; }
public string ScriptpubkeyType { get; set; }
}

public class MempoolTransaction
{
public string Txid { get; set; }

public int Version { get; set; }

public int Locktime { get; set; }
public int Size { get; set; }
public int Weight { get; set; }
public int Fee { get; set; }
public List<Vin> Vin { get; set; }
public List<PrevOut> Vout { get; set; }
public UtxoStatus Status { get; set; }
}

public class Outspent
{
public bool Spent { get; set; }
}
}
10 changes: 9 additions & 1 deletion src/Blockcore.Indexer.Core/Storage/IStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ public interface IStorage

QueryAddress AddressBalance(string address);

AddressResponse AddressResponseBalance(string address);

Task<List<QueryAddressBalance>> QuickBalancesLookupForAddressesWithHistoryCheckAsync(
IEnumerable<string> addresses, bool includePending = false);

QueryResult<QueryAddressItem> AddressHistory(string address, int? offset, int limit);

Task<List<MempoolTransaction>> GetMempoolTransactionListAsync(List<string> txids);

QueryResult<QueryMempoolTransactionHashes> GetMemoryTransactionsSlim(int offset, int limit);

QueryResult<QueryTransaction> GetMemoryTransactions(int offset, int limit);
Expand Down Expand Up @@ -50,7 +54,7 @@ Task<List<QueryAddressBalance>> QuickBalancesLookupForAddressesWithHistoryCheckA

long TotalBalance();

Task<QueryResult<Output>> GetUnspentTransactionsByAddressAsync(string address,long confirmations, int offset, int limit);
Task<QueryResult<Output>> GetUnspentTransactionsByAddressAsync(string address, long confirmations, int offset, int limit);

Task DeleteBlockAsync(string blockHash);

Expand All @@ -62,5 +66,9 @@ Task<List<QueryAddressBalance>> QuickBalancesLookupForAddressesWithHistoryCheckA

List<PeerDetails> GetPeerFromDate(DateTime date);
Task<long> InsertPeer(PeerDetails info);

public Task<Output> GetOutputFromOutpointAsync(string txid, int index);

public Task<List<OutspentResponse>> GetTransactionOutspendsAsync(string txid);
}
}
Loading
Loading