-
Notifications
You must be signed in to change notification settings - Fork 122
fix: consider microblock transactions in balance calculations #2277
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* eslint-disable camelcase */ | ||
|
|
||
| exports.shorthands = undefined; | ||
|
|
||
| exports.up = pgm => { | ||
| // Remove old balances. | ||
| pgm.sql(`TRUNCATE TABLE ft_balances`); | ||
|
|
||
| // Recalculate STX balances | ||
| pgm.sql(` | ||
| WITH all_balances AS ( | ||
| SELECT sender AS address, -SUM(amount) AS balance_change | ||
| FROM stx_events | ||
| WHERE asset_event_type_id IN (1, 3) -- Transfers and Burns affect the sender's balance | ||
| AND canonical = true AND microblock_canonical = true | ||
| GROUP BY sender | ||
| UNION ALL | ||
| SELECT recipient AS address, SUM(amount) AS balance_change | ||
| FROM stx_events | ||
| WHERE asset_event_type_id IN (1, 2) -- Transfers and Mints affect the recipient's balance | ||
| AND canonical = true AND microblock_canonical = true | ||
| GROUP BY recipient | ||
| ), | ||
| net_balances AS ( | ||
| SELECT address, SUM(balance_change) AS balance | ||
| FROM all_balances | ||
| GROUP BY address | ||
| ), | ||
| fees AS ( | ||
| SELECT address, SUM(total_fees) AS total_fees | ||
| FROM ( | ||
| SELECT sender_address AS address, SUM(fee_rate) AS total_fees | ||
| FROM txs | ||
| WHERE canonical = true AND microblock_canonical = true AND sponsored = false | ||
| GROUP BY sender_address | ||
| UNION ALL | ||
| SELECT sponsor_address AS address, SUM(fee_rate) AS total_fees | ||
| FROM txs | ||
| WHERE canonical = true AND microblock_canonical = true AND sponsored = true | ||
| GROUP BY sponsor_address | ||
| ) AS subquery | ||
| GROUP BY address | ||
| ), | ||
| rewards AS ( | ||
| SELECT | ||
| recipient AS address, | ||
| SUM( | ||
| coinbase_amount + tx_fees_anchored + tx_fees_streamed_confirmed + tx_fees_streamed_produced | ||
| ) AS total_rewards | ||
| FROM miner_rewards | ||
| WHERE canonical = true | ||
| GROUP BY recipient | ||
| ), | ||
| all_addresses AS ( | ||
| SELECT address FROM net_balances | ||
| UNION | ||
| SELECT address FROM fees | ||
| UNION | ||
| SELECT address FROM rewards | ||
| ) | ||
| INSERT INTO ft_balances (address, balance, token) | ||
| SELECT | ||
| aa.address, | ||
| COALESCE(nb.balance, 0) - COALESCE(f.total_fees, 0) + COALESCE(r.total_rewards, 0) AS balance, | ||
| 'stx' AS token | ||
| FROM all_addresses aa | ||
| LEFT JOIN net_balances nb ON aa.address = nb.address | ||
| LEFT JOIN fees f ON aa.address = f.address | ||
| LEFT JOIN rewards r ON aa.address = r.address | ||
| `); | ||
|
|
||
| // Recalculate FT balances | ||
| pgm.sql(` | ||
| WITH all_balances AS ( | ||
| SELECT sender AS address, asset_identifier, -SUM(amount) AS balance_change | ||
| FROM ft_events | ||
| WHERE asset_event_type_id IN (1, 3) -- Transfers and Burns affect the sender's balance | ||
| AND canonical = true | ||
| AND microblock_canonical = true | ||
| GROUP BY sender, asset_identifier | ||
| UNION ALL | ||
| SELECT recipient AS address, asset_identifier, SUM(amount) AS balance_change | ||
| FROM ft_events | ||
| WHERE asset_event_type_id IN (1, 2) -- Transfers and Mints affect the recipient's balance | ||
| AND canonical = true | ||
| AND microblock_canonical = true | ||
| GROUP BY recipient, asset_identifier | ||
| ), | ||
| net_balances AS ( | ||
| SELECT address, asset_identifier, SUM(balance_change) AS balance | ||
| FROM all_balances | ||
| GROUP BY address, asset_identifier | ||
| ) | ||
| INSERT INTO ft_balances (address, balance, token) | ||
| SELECT address, balance, asset_identifier AS token | ||
| FROM net_balances | ||
| `); | ||
| }; | ||
|
|
||
| exports.down = pgm => {}; | ||
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.