From f4af6aa9bfed91c442a1c7b15d5426a2ced85a2a Mon Sep 17 00:00:00 2001 From: halaprix Date: Mon, 27 May 2024 14:42:57 +0200 Subject: [PATCH] feat: add new leaderboard position --- packages/rays-db/src/database-types.ts | 3 ++ .../src/migrations/003_leaderboard.mts | 51 +++++++++++-------- summerfi-api/get-rays-function/src/index.ts | 7 ++- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/packages/rays-db/src/database-types.ts b/packages/rays-db/src/database-types.ts index 04d20befed..4ffbe6bdeb 100644 --- a/packages/rays-db/src/database-types.ts +++ b/packages/rays-db/src/database-types.ts @@ -7,6 +7,8 @@ export type Generated = ? ColumnType : ColumnType +export type Int8 = ColumnType + export type Json = JsonValue export type JsonArray = JsonValue[] @@ -45,6 +47,7 @@ export interface EligibilityCondition { } export interface Leaderboard { + position: Int8 | null totalPoints: Numeric | null userAddress: string | null } diff --git a/packages/rays-db/src/migrations/003_leaderboard.mts b/packages/rays-db/src/migrations/003_leaderboard.mts index 4cef23a745..5f180d7a4a 100644 --- a/packages/rays-db/src/migrations/003_leaderboard.mts +++ b/packages/rays-db/src/migrations/003_leaderboard.mts @@ -2,26 +2,37 @@ import { Kysely, sql } from 'kysely' export async function up(db: Kysely) { await sql` - CREATE VIEW leaderboard AS - SELECT - ua.address AS user_address, - COALESCE(SUM(cp.total_points), 0) AS total_points - FROM - user_address ua - LEFT JOIN - ( - SELECT - COALESCE(p.user_address_id, pd.user_address_id) AS user_address_id, - SUM(pd.points) AS total_points - FROM - points_distribution pd - LEFT JOIN - position p ON pd.position_id = p.id - GROUP BY - COALESCE(p.user_address_id, pd.user_address_id) - ) AS cp ON ua.id = cp.user_address_id - GROUP BY - ua.address; + CREATE VIEW leaderboard AS + SELECT + ROW_NUMBER() OVER (ORDER BY total_points DESC) AS position, + user_address, + total_points + FROM + ( + SELECT + ua.address AS user_address, + COALESCE(SUM(cp.total_points), 0) AS total_points + FROM + user_address ua + LEFT JOIN + ( + SELECT + COALESCE(p.user_address_id, pd.user_address_id) AS user_address_id, + SUM(pd.points) AS total_points + FROM + points_distribution pd + LEFT JOIN + position p ON pd.position_id = p.id + GROUP BY + COALESCE(p.user_address_id, pd.user_address_id) + ) AS cp ON ua.id = cp.user_address_id + GROUP BY + ua.address + ) AS leaderboard + WHERE + total_points IS NOT NULL + ORDER BY + total_points DESC; `.execute(db) } diff --git a/summerfi-api/get-rays-function/src/index.ts b/summerfi-api/get-rays-function/src/index.ts index ab33729ef9..35a713384a 100644 --- a/summerfi-api/get-rays-function/src/index.ts +++ b/summerfi-api/get-rays-function/src/index.ts @@ -88,7 +88,11 @@ export const handler = async ( 'eligibilityCondition.type', ]) .execute() - + const positionInLeaderboard = await db + .selectFrom('leaderboard') + .where('userAddress', '=', address.toLowerCase()) + .select(['position']) + .execute() const points = userPoints.concat(positionsPoints) const byDueDate = groupBy( @@ -116,6 +120,7 @@ export const handler = async ( eligiblePoints, allPossiblePoints, actionRequiredPoints, + positionInLeaderboard: positionInLeaderboard[0]?.position, }, }) }