-
Notifications
You must be signed in to change notification settings - Fork 118
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
feat: ft holder indexing #2030
Merged
Merged
feat: ft holder indexing #2030
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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,20 @@ | ||
{ | ||
"limit": 100, | ||
"offset": 0, | ||
"total": 3, | ||
"total_supply": "11700", | ||
"results": [ | ||
{ | ||
"address": "SP3Y2ZSH8P7D50B0VBTSX11S7XSG24M1VB9YFQA2K", | ||
"balance": "10000" | ||
}, | ||
{ | ||
"address": "SP3WJYXJZ4QK2V5V9VX2VXVZ6VXVZ6V2V5V2V2V2V", | ||
"balance": "900" | ||
}, | ||
{ | ||
"address": "SP3WJYXJZ4QK2V5V9VX2VXVZ6VXVZ6V2V5V2V2V2V", | ||
"balance": "800" | ||
} | ||
] | ||
} |
This file contains 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,38 @@ | ||
{ | ||
"description": "List of Fungible Token holders", | ||
"title": "FungibleTokenHolderList", | ||
"type": "object", | ||
"required": [ | ||
"total_supply", | ||
"results", | ||
"limit", | ||
"offset", | ||
"total" | ||
], | ||
"additionalProperties": false, | ||
"properties": { | ||
"limit": { | ||
"type": "integer", | ||
"maximum": 200, | ||
"description": "The number of holders to return" | ||
}, | ||
"offset": { | ||
"type": "integer", | ||
"description": "The number to holders to skip (starting at `0`)" | ||
}, | ||
"total": { | ||
"type": "integer", | ||
"description": "The number of holders available" | ||
}, | ||
"total_supply": { | ||
"type": "string", | ||
"description": "The total supply of the token (the sum of all balances)" | ||
}, | ||
"results": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "../../entities/tokens/ft-holder-entry.schema.json" | ||
} | ||
} | ||
} | ||
} |
This file contains 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,14 @@ | ||
{ | ||
"type": "object", | ||
"title": "FtHolderEntry", | ||
"required": ["address", "balance"], | ||
"additionalProperties": false, | ||
"properties": { | ||
"address": { | ||
"type": "string" | ||
}, | ||
"balance": { | ||
"type": "string" | ||
} | ||
} | ||
} |
This file contains 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 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 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,125 @@ | ||
/** @param { import("node-pg-migrate").MigrationBuilder } pgm */ | ||
exports.up = pgm => { | ||
pgm.createTable('ft_balances', { | ||
id: { | ||
type: 'bigserial', | ||
primaryKey: true, | ||
}, | ||
address: { | ||
type: 'text', | ||
notNull: true, | ||
}, | ||
token: { | ||
type: 'text', | ||
notNull: true, | ||
}, | ||
balance: { | ||
type: 'numeric', | ||
notNull: true, | ||
} | ||
}); | ||
|
||
pgm.addConstraint('ft_balances', 'unique_address_token', `UNIQUE(address, token)`); | ||
|
||
// Speeds up "grab the addresses with the highest balance for a given token" queries | ||
pgm.createIndex('ft_balances', [{ name: 'token' }, { name: 'balance', sort: 'DESC' }]); | ||
|
||
// Speeds up "get the total supply of a given token" queries | ||
pgm.createIndex('ft_balances', 'token'); | ||
|
||
// Populate the table with the current 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 | ||
`); | ||
|
||
// Populate the table with the current 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 | ||
`); | ||
|
||
}; | ||
|
||
/** @param { import("node-pg-migrate").MigrationBuilder } pgm */ | ||
exports.down = pgm => { | ||
pgm.dropTable('ft_balances'); | ||
}; |
This file contains 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 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 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming this is the raw number without the metadata decimals for each token, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep that is correct