fix balance for cross group transactions

This commit is contained in:
einhornimmond 2026-02-18 15:23:24 +01:00
parent fcb701efb8
commit b023fd98b6
2 changed files with 12 additions and 3 deletions

View File

@ -51,7 +51,7 @@ export abstract class AbstractSyncRole<ItemType> {
throw new Error('publicKey is empty')
}
const f = Filter.lastBalanceFor(publicKey)
f.coinCommunityId = communityId
f.setCommunityId(communityId)
const lastSenderTransaction = blockchain.findOne(f)
if (!lastSenderTransaction) {
return new Balance(publicKey, communityId)
@ -59,6 +59,7 @@ export abstract class AbstractSyncRole<ItemType> {
const lastConfirmedTransaction = lastSenderTransaction.getConfirmedTransaction()
if (!lastConfirmedTransaction) {
throw new Error(`invalid transaction, getConfirmedTransaction call failed for transaction nr: ${lastSenderTransaction.getTransactionNr()}`)
}
const senderLastAccountBalance = lastConfirmedTransaction.getAccountBalance(publicKey, communityId)
if (!senderLastAccountBalance) {

View File

@ -114,13 +114,19 @@ export class RemoteTransactionsSyncRole extends AbstractSyncRole<TransactionDb>
this.accountBalances.add(new AccountBalance(publicKey, GradidoUnit.zero(), coinCommunityId))
return this.accountBalances
} else {
const lastBalance = this.getLastBalanceForUser(publicKey, communityContext.blockchain, coinCommunityId)
// try to use same coins from this community
let lastBalance = this.getLastBalanceForUser(publicKey, communityContext.blockchain, coinCommunityId)
if (lastBalance.getBalance().equal(GradidoUnit.zero()) || lastBalance.getBalance().calculateDecay(lastBalance.getDate(), item.balanceDate).lt(amount)) {
// don't work, so we use or own coins
lastBalance = this.getLastBalanceForUser(publicKey, communityContext.blockchain, communityContext.communityId)
}
try {
lastBalance.updateLegacyDecay(amount, item.balanceDate)
} catch(e) {
if (e instanceof NegativeBalanceError) {
this.logLastBalanceChangingTransactions(publicKey, communityContext.blockchain, 10)
console.log(`coin community id: ${coinCommunityId}, context community id: ${communityContext.communityId}`)
this.logLastBalanceChangingTransactions(publicKey, communityContext.blockchain, 1)
throw e
}
}
@ -195,5 +201,7 @@ export class RemoteTransactionsSyncRole extends AbstractSyncRole<TransactionDb>
}
throw new BlockchainError(`Error adding ${this.itemTypeName()}`, item, e as Error)
}
// this.logLastBalanceChangingTransactions(senderPublicKey, senderCommunityContext.blockchain, 1)
// this.logLastBalanceChangingTransactions(recipientPublicKey, recipientCommunityContext.blockchain, 1)
}
}