Last active
February 7, 2019 16:02
-
-
Save donma/da94fe7a6a57e088aae9be25d5e50a6b to your computer and use it in GitHub Desktop.
Get confim and unConfirm Balance By NBitcoin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// 取的該錢包有多少餘額 | |
/// </summary> | |
/// <param name="ssAddress">錢包地址</param> | |
/// <param name="confirmBalance">已確認的</param> | |
/// <param name="unConfirmBalance">未確認的</param> | |
public static void GetWalletBalance(string ssAddress, out decimal confirmBalance, out decimal unConfirmBalance) | |
{ | |
//幾個確認判斷為已經確認完成 | |
//預設你可以設定為 5 | |
//但是因為測試所以>0 , 我就判斷已經當作確認過了 | |
var confirmThres = 5; | |
QBitNinja.Client.QBitNinjaClient client = new QBitNinja.Client.QBitNinjaClient(NBitcoin.Network.TestNet); | |
var balance = client.GetBalance(new NBitcoin.BitcoinPubKeyAddress(ssAddress), false).Result; | |
confirmBalance = 0; | |
unConfirmBalance = 0; | |
if (balance.Operations.Count > 0) | |
{ | |
var unspentCoins = new System.Collections.Generic.List<NBitcoin.Coin>(); | |
var unspentCoinsConfirmed = new System.Collections.Generic.List<NBitcoin.Coin>(); | |
foreach (var operation in balance.Operations) | |
{ | |
unspentCoins.AddRange(operation.ReceivedCoins.Select(coin => coin as NBitcoin.Coin)); | |
if (operation.Confirmations > confirmThres) | |
{ | |
unspentCoinsConfirmed.AddRange(operation.ReceivedCoins.Select(coin => coin as NBitcoin.Coin)); | |
} | |
unConfirmBalance = unspentCoins.Sum(x => x.Amount.ToDecimal(NBitcoin.MoneyUnit.BTC)); | |
confirmBalance = unspentCoinsConfirmed.Sum(x => x.Amount.ToDecimal(NBitcoin.MoneyUnit.BTC)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment