Skip to content

fix: voided token addr balance#291

Merged
andreabadesso merged 52 commits intomasterfrom
fix/voided-token-addr-balance
Sep 18, 2025
Merged

fix: voided token addr balance#291
andreabadesso merged 52 commits intomasterfrom
fix/voided-token-addr-balance

Conversation

@andreabadesso
Copy link
Copy Markdown
Collaborator

@andreabadesso andreabadesso commented Sep 17, 2025

Motivation

When an address is involved in a token creation transaction that is later on voided and this transaction is the only one this address has received so far, the initWalletBalance method (that is called when initializing a new wallet) throws because of a mismatch in the rows from address_balance and address_tx_history

This happens because when a transaction is voided, we remove it from the address_tx_history but the address_balance is set to 0 and never removed again.

Acceptance Criteria

  • We should include a test that will fail if the number of rows in both tables for the voided token is different
  • We should delete from the address_balance table in case the only row for that token is zero
  • We should delete from the wallet_balance in case the only row for that token is zero

Checklist

  • If you are requesting a merge into master, confirm this code is production-ready and can be included in future releases as soon as it gets merged
  • Make sure either the unit tests and/or the QA tests are capable of testing the new features
  • Make sure you do not include new dependencies in the project unless strictly necessary and do not include dev-dependencies as production ones. More dependencies increase the possibility of one of them being hijacked and affecting us.

…and removed locked authorities recalculation
@andreabadesso andreabadesso force-pushed the fix/voided-token-addr-balance branch from 5b69658 to e4efac8 Compare September 17, 2025 17:14
Comment on lines -402 to -417
const entry = {
address,
token_id: token,
// totalAmountSent is the sum of the value of all outputs of this token on the tx being sent to this address
// which means it is the "total_received" for this address
total_received: tokenBalance.totalAmountSent,
// if it's < 0, there must be an entry already, so it will execute "ON DUPLICATE KEY UPDATE" instead of setting it to 0
unlocked_balance: (tokenBalance.unlockedAmount < 0 ? 0 : tokenBalance.unlockedAmount),
// this is never less than 0, as locked balance only changes when a tx is unlocked
locked_balance: tokenBalance.lockedAmount,
unlocked_authorities: tokenBalance.unlockedAuthorities.toUnsignedInteger(),
locked_authorities: tokenBalance.lockedAuthorities.toUnsignedInteger(),
timelock_expires: tokenBalance.lockExpires,
transactions: 1,
};

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This should have been removed in the last PR

Comment thread packages/daemon/src/services/index.ts Outdated
outputs: EventTxOutput[],
tokens: string[],
headers: EventTxHeader[],
version?: number,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is it optional?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

Comment thread packages/daemon/src/db/index.ts Outdated

// If this is a token creation transaction, we must remove the address_balance
// row for that token as it has ceased to exist
if (isCreateTokenTx) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As we discussed, we need to handle this case even for non token creation tx. The tx might be the only one that added balance to the address and must also be deleted.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment thread packages/daemon/src/db/index.ts Outdated
}

// If this is a token creation transaction, clean up zeroed entries
if (isCreateTokenTx) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same as the other

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done

@pedroferreira1 pedroferreira1 moved this from In Progress (WIP) to In Review (WIP) in Hathor Network Sep 17, 2025
@andreabadesso andreabadesso merged commit 32ff6a1 into master Sep 18, 2025
1 check passed
@github-project-automation github-project-automation Bot moved this from In Review (WIP) to Waiting to be deployed in Hathor Network Sep 18, 2025
@andreabadesso andreabadesso deleted the fix/voided-token-addr-balance branch September 18, 2025 19:45
This was referenced Nov 17, 2025
@andreabadesso andreabadesso moved this from Waiting to be deployed to Done in Hathor Network Nov 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants