Skip to content

Commit

Permalink
feat(backend): create materialized views for staking pools (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrybarry authored Nov 8, 2024
1 parent 70c7201 commit a172c21
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
4 changes: 1 addition & 3 deletions apps/api/src/services/kitwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ const pools = catchAsync(async (_req: Request, res: Response) => {
SELECT
account_id
FROM
accounts
WHERE
account_id LIKE ANY (${POOLS})
staking_pools
`;

return res.status(200).json(pools.map(({ account_id }) => account_id));
Expand Down
7 changes: 7 additions & 0 deletions apps/backend/migrations/20241108151848_staking_pools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { upSQL } from 'knex-migrate-sql-file';

export async function up(knex) {
await upSQL(knex);
}

export function down() {}
17 changes: 17 additions & 0 deletions apps/backend/migrations/20241108151848_staking_pools.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE MATERIALIZED VIEW staking_pools AS
SELECT
account_id
FROM
accounts
WHERE
account_id LIKE ANY (
ARRAY[
'%.poolv1.near',
'%.pool.near',
'%.pool.%.m0',
'%.factory01.littlefarm.testnet',
'%.factory.colorpalette.testnet'
]
);

CREATE UNIQUE INDEX ON staking_pools (account_id);
8 changes: 8 additions & 0 deletions apps/backend/src/jobs/refresh-views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ import sentry from '#libs/sentry';
logger.error(error);
await sleep(1000);
}

try {
await knex.raw('REFRESH MATERIALIZED VIEW CONCURRENTLY staking_pools');
} catch (error) {
sentry.captureException(error);
logger.error(error);
await sleep(1000);
}
logger.info({ action: 'job ended', job: 'refresh-views' });

if (parentPort) {
Expand Down

0 comments on commit a172c21

Please sign in to comment.