Skip to content

Commit

Permalink
feat: Add secure endpoint to refresh leaderboard stats (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
chef-huan authored Dec 13, 2021
1 parent 54c7755 commit 4eee698
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 4 deletions.
112 changes: 112 additions & 0 deletions api/cron/refreshLeaderboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { VercelRequest, VercelResponse } from "@vercel/node";
import { gql, request } from "graphql-request";
import { getModel } from "../../utils/mongo";
import { TRADING_COMPETITION_SUBGRAPH } from "../../utils";

const refreshTradingCompLeaderboard = async () => {
console.log("TradingCompLeaderboard refresh begin");

const { users } = await request(
TRADING_COMPETITION_SUBGRAPH,
gql`
{
users(orderBy: volumeUSD, orderDirection: desc) {
id
volumeUSD
team {
id
}
}
}
`
);

console.log("Fetched users count: {}", users.length);

const teamUser = new Map();
for (let i = 0; i < users.length; i++) {
users[i].globalRank = i + 1;
const userList = teamUser.get(users[i].team.id);
if (userList == undefined) {
teamUser.set(users[i].team.id, [users[i]]);
} else {
userList.push(users[i]);
}
}

for (const [, value] of teamUser.entries()) {
let lastTierVolume = 0;
for (let i = 0; i < value.length; i++) {
value[i].teamRank = i + 1;

if (lastTierVolume === 0) {
value[i].next_rank = 0;
} else {
value[i].next_rank = lastTierVolume - value[i].volumeUSD;
}

if (i === 0) {
lastTierVolume = value[i].volumeUSD;
}
if (i === 9) {
lastTierVolume = value[i].volumeUSD;
}
if (i === 99) {
lastTierVolume = value[i].volumeUSD;
}
if (i === 499) {
lastTierVolume = value[i].volumeUSD;
}
}
}

const userModel = await getModel("User");
for (const [key, value] of teamUser.entries()) {
for (let i = 0; i < value.length; i++) {
const query = { address: value[i].id.toLowerCase() };
try {
await userModel
.updateOne(
query,
{
leaderboard_fantoken: {
global: value[i].globalRank,
team: value[i].teamRank,
volume: value[i].volumeUSD,
next_rank: value[i].next_rank,
},
team: key,
updated_at: new Date(),
},
{ strict: false, upsert: true }
)
.exec();
} catch (e) {
console.log(e);
}
}
}

console.log("TradingCompLeaderboard refresh ended");
};

export default async (req: VercelRequest, res: VercelResponse): Promise<VercelResponse | void> => {
if (req.method === "POST") {
try {
const { authorization } = req.headers;

console.log(process.env.CRON_API_SECRET_KEY);
if (authorization === `${process.env.CRON_API_SECRET_KEY}`) {
await refreshTradingCompLeaderboard();
res.status(200).json({ success: true });
} else {
res.status(401).json({ success: false });
}
} catch (err) {
res.status(500).json({ statusCode: 500 });
}
} else {
res.setHeader("Allow", "POST");
res.status(405).end("Method Not Allowed");
}
};
4 changes: 2 additions & 2 deletions api/leaderboard/global.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { VercelRequest, VercelResponse } from "@vercel/node";
import { gql, request } from "graphql-request";
import { getAddress } from "ethers/lib/utils";
import { TRADING_COMPETITION_V2_SUBGRAPH_TEST } from "../../utils";
import { TRADING_COMPETITION_SUBGRAPH } from "../../utils";

interface User {
id: string;
Expand All @@ -17,7 +17,7 @@ export default async (req: VercelRequest, res: VercelResponse): Promise<VercelRe
}

const { competition, users } = await request(
TRADING_COMPETITION_V2_SUBGRAPH_TEST,
TRADING_COMPETITION_SUBGRAPH,
gql`
{
competition(id: "2") {
Expand Down
4 changes: 2 additions & 2 deletions api/leaderboard/team.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { VercelRequest, VercelResponse } from "@vercel/node";
import { gql, request } from "graphql-request";
import { getAddress } from "ethers/lib/utils";
import { TRADING_COMPETITION_V2_SUBGRAPH_TEST } from "../../utils";
import { TRADING_COMPETITION_SUBGRAPH } from "../../utils";

interface User {
id: string;
Expand All @@ -18,7 +18,7 @@ export default async (req: VercelRequest, res: VercelResponse): Promise<VercelRe

if (teamId) {
const { team } = await request(
TRADING_COMPETITION_V2_SUBGRAPH_TEST,
TRADING_COMPETITION_SUBGRAPH,
gql`
query getUsersByTeamOrderedByVolumeDesc($teamId: ID!) {
team(id: $teamId) {
Expand Down
6 changes: 6 additions & 0 deletions api/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export default async (req: VercelRequest, res: VercelResponse): Promise<VercelRe
volume: user.leaderboard?.volume ?? 0,
next_rank: user.leaderboard?.next_rank ?? "???",
},
leaderboard_fantoken: {
global: user.leaderboard_fantoken?.global ?? "???",
team: user.leaderboard_fantoken?.team ?? "???",
volume: user.leaderboard_fantoken?.volume ?? 0,
next_rank: user.leaderboard_fantoken?.next_rank ?? "???",
},
created_at: user.created_at,
updated_at: user.updated_at,
});
Expand Down
2 changes: 2 additions & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const TRADING_COMPETITION_V1_SUBGRAPH =
export const TRADING_COMPETITION_V2_SUBGRAPH_TEST =
"https://api.thegraph.com/subgraphs/name/chef-huan/tradingcompv2";

export const TRADING_COMPETITION_SUBGRAPH = TRADING_COMPETITION_V2_SUBGRAPH_TEST;

/**
* Check for the validity of a username based on rules (see documentation).
*
Expand Down
4 changes: 4 additions & 0 deletions utils/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export const userSchema: Schema = new Schema(
type: leaderboardSchema,
required: false,
},
leaderboard_fantoken: {
type: leaderboardSchema,
required: false,
},
created_at: {
type: Date,
default: Date.now,
Expand Down
1 change: 1 addition & 0 deletions utils/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface User extends Document {
slug: string;
team: string;
leaderboard?: Leaderboard;
leaderboard_fantoken?: Leaderboard;
created_at: Date;
updated_at: Date;
}
Expand Down

1 comment on commit 4eee698

@vercel
Copy link

@vercel vercel bot commented on 4eee698 Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.