Skip to content
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: add leaderboard position to user #306

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/rays-db/src/database-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export type Generated<T> =
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>

export type Int8 = ColumnType<string, bigint | number | string, bigint | number | string>

export type Json = JsonValue

export type JsonArray = JsonValue[]
Expand Down Expand Up @@ -45,6 +47,7 @@ export interface EligibilityCondition {
}

export interface Leaderboard {
position: Int8 | null
totalPoints: Numeric | null
userAddress: string | null
}
Expand Down
51 changes: 31 additions & 20 deletions packages/rays-db/src/migrations/003_leaderboard.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@ import { Kysely, sql } from 'kysely'

export async function up(db: Kysely<never>) {
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)
}

Expand Down
7 changes: 6 additions & 1 deletion summerfi-api/get-rays-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -116,6 +120,7 @@ export const handler = async (
eligiblePoints,
allPossiblePoints,
actionRequiredPoints,
positionInLeaderboard: positionInLeaderboard[0]?.position,
},
})
}
Expand Down
Loading