-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-sync): restore validator wallet attributes (#732)
* Add getAllValidators * Move method * Update abi * Add all validators values * Get all validators * style: resolve style guide violations * write missing wallet attributes * update validator ranking * style: resolve style guide violations * ensure all validators are included * api fixes * add index, include resigned in ranking order * resolve TODO, add votersCount * attribute casts --------- Co-authored-by: sebastijankuzner <[email protected]> Co-authored-by: sebastijankuzner <[email protected]> Co-authored-by: oXtxNt9U <[email protected]>
- Loading branch information
1 parent
42de4ad
commit da99ddc
Showing
6 changed files
with
188 additions
and
37 deletions.
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
47 changes: 47 additions & 0 deletions
47
...ages/api-database/source/migrations/1729064427168-CreateUpdateValidatorRankingFunction.ts
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,47 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class CreateUpdateValidatorRankingFunction1729064427168 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
// language=postgresql | ||
await queryRunner.query(` | ||
CREATE OR REPLACE FUNCTION update_validator_ranks() | ||
RETURNS VOID AS $$ | ||
BEGIN | ||
WITH all_validators AS ( | ||
SELECT | ||
address, | ||
(attributes->>'validatorVoteBalance')::numeric AS vote_balance, | ||
COALESCE((attributes->>'validatorResigned')::boolean, FALSE) AS is_resigned | ||
FROM wallets | ||
WHERE attributes ? 'validatorPublicKey' | ||
), | ||
ranking AS ( | ||
SELECT | ||
address, | ||
vote_balance, | ||
ROW_NUMBER() OVER (ORDER BY is_resigned ASC, vote_balance DESC, address ASC) AS rank | ||
FROM all_validators | ||
ORDER BY is_resigned ASC, vote_balance DESC, address ASC | ||
) | ||
UPDATE wallets | ||
SET | ||
attributes = attributes || jsonb_build_object( | ||
'validatorRank', ranking.rank, | ||
'validatorApproval', ROUND(COALESCE(ranking.vote_balance::numeric / NULLIF(state.supply::numeric, 0), 0), 4) | ||
) | ||
FROM ranking | ||
CROSS JOIN state | ||
WHERE wallets.address = ranking.address; | ||
END; | ||
$$ | ||
LANGUAGE plpgsql; | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
// language=postgresql | ||
await queryRunner.query(` | ||
DROP FUNCTION IF EXISTS update_validator_ranks(); | ||
`); | ||
} | ||
} |
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
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