Skip to content

Commit

Permalink
- fixed balance not updating for pending transactions
Browse files Browse the repository at this point in the history
- fixed multi-transactions within same block issue
  • Loading branch information
firestorm40 committed Sep 22, 2024
1 parent 4a36d6f commit ac032d7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
9 changes: 9 additions & 0 deletions Spixi/Meta/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,15 @@ public override IxiNumber getWalletBalance(Address id)
return 0;
}

// Returns the current wallet's usable balance
public static IxiNumber getAvailableBalance()
{
IxiNumber currentBalance = Node.balance.balance;
currentBalance -= TransactionCache.getPendingSentTransactionsAmount();

return currentBalance;
}

public override void parseProtocolMessage(ProtocolMessageCode code, byte[] data, RemoteEndpoint endpoint)
{
ProtocolMessage.parseProtocolMessage(code, data, endpoint);
Expand Down
5 changes: 3 additions & 2 deletions Spixi/Pages/Home/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,8 +1045,9 @@ public override void updateScreen()
{
Logging.error("Exception occurred in HomePage.UpdateScreen: " + e);
}
string balance = Utils.amountToHumanFormatString(Node.balance.balance);
string fiatBalance = Utils.amountToHumanFormatString(Node.fiatPrice * Node.balance.balance);
IxiNumber availableBalance = Node.getAvailableBalance();
string balance = Utils.amountToHumanFormatString(availableBalance);
string fiatBalance = Utils.amountToHumanFormatString(Node.fiatPrice * availableBalance);
Utils.sendUiCommand(this, "setBalance", balance, fiatBalance, Node.localStorage.nickname);

// Check if we should reload certain elements
Expand Down
5 changes: 3 additions & 2 deletions Spixi/Pages/Wallet/WalletContactRequestPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ private void onSend()
return;
}

if (_amount + fee > Node.balance.balance)
IxiNumber availableBalance = Node.getAvailableBalance();
if (_amount + fee > availableBalance)
{
string alert_body = String.Format(SpixiLocalization._SL("wallet-error-balance-text"), _amount + fee, Node.balance.balance);
string alert_body = String.Format(SpixiLocalization._SL("wallet-error-balance-text"), _amount + fee, availableBalance);
displaySpixiAlert(SpixiLocalization._SL("wallet-error-balance-title"), alert_body, SpixiLocalization._SL("global-dialog-ok"));
return;
}
Expand Down
6 changes: 3 additions & 3 deletions Spixi/Pages/Wallet/WalletSend2Page.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void onLoad()
Transaction tx = new Transaction((int)Transaction.Type.Normal, fee, tx_list, from, pubKey, IxianHandler.getHighestKnownNetworkBlockHeight());

Utils.sendUiCommand(this, "setRecipient", toAddress, toAddress, "img/spixiavatar.png");
Utils.sendUiCommand(this, "setBalance", Node.balance.balance.ToString(), Node.fiatPrice.ToString());
Utils.sendUiCommand(this, "setBalance", Node.getAvailableBalance().ToString(), Node.fiatPrice.ToString());
Utils.sendUiCommand(this, "setFees", tx.fee.ToString());
}

Expand Down Expand Up @@ -129,10 +129,10 @@ private void onNavigating(object sender, WebNavigatingEventArgs e)
}
else if (current_url.Contains("ixian:getMaxAmount"))
{
if (Node.balance.balance > ConsensusConfig.forceTransactionPrice * 2)
if (Node.getAvailableBalance() > ConsensusConfig.forceTransactionPrice * 2)
{
// TODO needs to be improved and pubKey length needs to be taken into account
Utils.sendUiCommand(this, "setMaxAmount", (Node.balance.balance - (ConsensusConfig.forceTransactionPrice * 2)).ToString());
Utils.sendUiCommand(this, "setMaxAmount", (Node.getAvailableBalance() - (ConsensusConfig.forceTransactionPrice * 2)).ToString());
}
}
else
Expand Down
6 changes: 3 additions & 3 deletions Spixi/Pages/Wallet/WalletSendPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private void onNavigated(object sender, WebNavigatedEventArgs e)

private void onLoad()
{
Utils.sendUiCommand(this, "setBalance", Node.balance.balance.ToString());
Utils.sendUiCommand(this, "setBalance", Node.getAvailableBalance().ToString());

// If we have a pre-set recipient, fill out the recipient wallet address and nickname
if (recipient != null)
Expand Down Expand Up @@ -165,10 +165,10 @@ private void onNavigating(object sender, WebNavigatingEventArgs e)
}
else if (current_url.Contains("ixian:getMaxAmount"))
{
if (Node.balance.balance > ConsensusConfig.forceTransactionPrice * 2)
if (Node.getAvailableBalance() > ConsensusConfig.forceTransactionPrice * 2)
{
// TODO needs to be improved and pubKey length needs to be taken into account
Utils.sendUiCommand(this, "setAmount", (Node.balance.balance - (ConsensusConfig.forceTransactionPrice * 2)).ToString());
Utils.sendUiCommand(this, "setAmount", (Node.getAvailableBalance() - (ConsensusConfig.forceTransactionPrice * 2)).ToString());
}
}
else if (current_url.Contains("ixian:addrecipient"))
Expand Down
21 changes: 21 additions & 0 deletions Spixi/Storage/TransactionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ public static Transaction getUnconfirmedTransaction(byte[] txid)
return null;
}

// Returns the amount of funds in pending state for the current blockheight
public static IxiNumber getPendingSentTransactionsAmount()
{
IxiNumber pendingAmount = 0;
lock (unconfirmedTransactions)
{
foreach (StorageTransaction tx in unconfirmedTransactions)
{
if (tx.transaction.blockHeight >= Node.balance.blockHeight)
{
Address addr = tx.transaction.pubKey;
if (addr.SequenceEqual(IxianHandler.getWalletStorage().getPrimaryAddress()))
{
pendingAmount += tx.transaction.amount;
}
}
}
}
return pendingAmount;
}

// Add a storage transaction to local storage
public static bool addTransaction(StorageTransaction t, bool writeToFile = true)
{
Expand Down

0 comments on commit ac032d7

Please sign in to comment.