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

Add challenge DF tables, routes, parsers #57

Merged
merged 13 commits into from
Jun 27, 2023
27 changes: 23 additions & 4 deletions db/structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ const passiveRewardsInfo = `CREATE TABLE passive_rewards_info(

const rewardsSummary = `CREATE TABLE rewards_summary(
LP_addr VARCHAR(94) NOT NULL,
passive_amt FLOAT(94, 10) NOT NULL,
curating_amt FLOAT(94, 10) NOT NULL,
passive_amt FLOAT(94, 10) NOT NULL,
curating_amt FLOAT(94, 10) NOT NULL,
predictoor_amt FLOAT(94, 10) NOT NULL,
round INT NOT NULL,
challenge_amt FLOAT(94, 10) NOT NULL,
round INT NOT NULL,
PRIMARY KEY(LP_addr, round))`

const ownersInfo = `CREATE TABLE owners_info(
Expand Down Expand Up @@ -99,6 +100,22 @@ const predictoorRewards = `CREATE TABLE predictoor_rewards(
PRIMARY KEY(predictoor_addr, round)
)`

const challengeData = `CREATE TABLE challenge_data(
from_addr VARCHAR(42) NOT NULL,
nft_addr VARCHAR(42) NOT NULL,
nmse FLOAT(94, 10) NOT NULL,
round INT NOT NULL,
PRIMARY KEY(from_addr, nft_addr, round)
)`

const challengeRewards = `CREATE TABLE challenge_rewards(
chainID INT NOT NULL,
winner_addr VARCHAR(42) NOT NULL,
OCEAN_amt FLOAT(94, 10) NOT NULL,
round INT NOT NULL,
PRIMARY KEY(winner_addr, round)
)`

Copy link
Member

Choose a reason for hiding this comment

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

Looks great!

module.exports = {
allocationsTable,
nftVolsTable,
Expand All @@ -109,5 +126,7 @@ module.exports = {
rewardsSummary,
ownersInfo,
predictoorData,
predictoorRewards
predictoorRewards,
challengeData,
challengeRewards
}
13 changes: 12 additions & 1 deletion src/comps/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ function readDataDir(dataDir) {
let predictoor_data = []
let predictoor_rewards = []

let challenge_data = []
let challenge_rewards = []

let files = fs.readdirSync(dataDir)
let hashsum = ""
for (let file of files) {
Expand Down Expand Up @@ -48,6 +51,10 @@ function readDataDir(dataDir) {
predictoor_data.push(...parseCsv(`${dataDir}${file}`))
} else if (file.includes("predictoor_rewards")) {
predictoor_rewards.push(...parseCsv(`${dataDir}${file}`))
} else if (file.includes("challenge_data")) {
challenge_data.push(...parseCsv(`${dataDir}${file}`))
} else if (file.includes("challenge_rewards")) {
challenge_rewards.push(...parseCsv(`${dataDir}${file}`))
} else continue

let hash = crypto.createHash("sha256")
Expand All @@ -67,7 +74,11 @@ function readDataDir(dataDir) {
nftinfo,
rates,
symbols,
hashsum
hashsum,
predictoor_data,
predictoor_rewards,
challenge_data,
challenge_rewards
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/comps/update/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const batchUpdateRound = async ({
ownerInfo,
roundNumber,
predictoor_data,
predictoor_rewards
predictoor_rewards,
challenge_data,
challenge_rewards
}) => {
await dropTable("allocations", roundNumber)
await updateDb(allocations, "allocations", roundNumber)
Expand Down Expand Up @@ -50,6 +52,16 @@ const batchUpdateRound = async ({
await dropTable("predictoor_rewards", roundNumber)
await updateDb(predictoor_rewards, "predictoor_rewards", roundNumber)
}

if (challenge_data) {
await dropTable("challenge_data", roundNumber)
await updateDb(challenge_data, "challenge_data", roundNumber)
}

if (challenge_rewards) {
await dropTable("challenge_rewards", roundNumber)
await updateDb(challenge_rewards, "challenge_rewards", roundNumber)
}
}

module.exports = { batchUpdateRound }
26 changes: 14 additions & 12 deletions src/comps/update/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,27 @@ async function updateDb(data, dbname, round) {
}
}
}

async function updateRewardsSummary(round) {
// Aggregates the sum of "passive," "curating," and "predictoor" rewards for each "LP_addr" for a given "round" by getting the sum of `amt` from `rewards_info`, `passive_rewards_info`, and `predictoor_rewards` tables
// Aggregates the sum of "passive," "curating," "predictoor" and "challenge" rewards for each "LP_addr" for a given "round" by getting the sum of `amt` from `rewards_info`, `passive_rewards_info`, `predictoor_rewards` and `challenge_rewards` tables
await db.promise().query(
`
INSERT INTO rewards_summary(LP_addr, passive_amt, curating_amt, predictoor_amt, round)
SELECT LP_addr, SUM(passive) AS passive_amt, SUM(curating) AS curating_amt, SUM(OCEAN_amt) AS predictoor_amt, ? FROM
(
SELECT LP_addr, SUM(reward) AS passive, 0 AS curating, 0 AS OCEAN_amt FROM passive_rewards_info WHERE round = ? GROUP BY LP_addr
UNION
SELECT LP_addr, 0 AS passive, SUM(amt) AS curating, 0 AS OCEAN_amt FROM rewards_info WHERE round = ? GROUP BY LP_addr
UNION
SELECT predictoor_addr AS LP_addr, 0 AS passive, 0 AS curating, OCEAN_amt FROM predictoor_rewards WHERE round = ?
) AS foo GROUP BY LP_addr`,
[round, round, round, round]
INSERT INTO rewards_summary(LP_addr, passive_amt, curating_amt, predictoor_amt, challenge_amt, round)
Copy link
Member

Choose a reason for hiding this comment

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

Thank you!

SELECT LP_addr, SUM(passive) AS passive_amt, SUM(curating) AS curating_amt, SUM(predictoor) AS predictoor_amt, SUM(challenge) AS challenge_amt, ? FROM
(
SELECT LP_addr, SUM(reward) AS passive, 0 AS curating, 0 AS predictoor, 0 AS challenge FROM passive_rewards_info WHERE round = ? GROUP BY LP_addr
UNION
SELECT LP_addr, 0 AS passive, SUM(amt) AS curating, 0 AS predictoor, 0 AS challenge FROM rewards_info WHERE round = ? GROUP BY LP_addr
UNION
SELECT predictoor_addr AS LP_addr, 0 AS passive, 0 AS curating, OCEAN_amt AS predictoor, 0 AS challenge FROM predictoor_rewards WHERE round = ?
UNION
SELECT winner_addr AS LP_addr, 0 AS passive, 0 AS curating, 0 AS predictoor, SUM(OCEAN_amt) AS challenge FROM challenge_rewards WHERE round = ? GROUP BY winner_addr
) AS foo GROUP BY LP_addr`,
[round, round, round, round, round]
);
}



module.exports = {
updateDb,
dropTable,
Expand Down
6 changes: 5 additions & 1 deletion src/comps/update/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ async function sync(dataDir, roundNumber) {
symbols,
hashsum,
predictoor_data,
predictoor_rewards
predictoor_rewards,
challenge_data,
challenge_rewards
} = readDataDir(dataDir)

if (round_hash_map[roundNumber] == hashsum) {
Expand Down Expand Up @@ -91,6 +93,8 @@ async function sync(dataDir, roundNumber) {
nftinfo,
predictoor_data,
predictoor_rewards,
challenge_data,
challenge_rewards,
roundNumber: roundNumber
})
}
Expand Down
15 changes: 15 additions & 0 deletions src/routes/challenge/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const express = require("express")
const { getChallengeData, getChallengeRewards } = require("../../services/challenge")
const router = express.Router()

router.post("/data", async (req, res) => {
let data = await getChallengeData(req.body)
res.json(data)
})

router.post("/rewards", async (req, res) => {
let data = await getChallengeRewards(req.body)
res.json(data)
})

module.exports = router
2 changes: 2 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const rewardsSummaryRouter = require("./rewards_summary")
const nftInfoRouter = require("./nft_info")
const apyRouter = require("./apy")
const predictoorRouter = require("./predictoor")
const challengeRouter = require("./challenge")

app.use(cors())
app.use(express.json({ limit: "100mb" }))
Expand All @@ -21,6 +22,7 @@ app.use(rewardsInfoRouter)
app.use(rewardsSummaryRouter)
app.use(nftInfoRouter)
app.use("/predictoor", predictoorRouter)
app.use("/challenge", challengeRouter)
app.use("/apy", apyRouter)

app.listen(6234)
2 changes: 1 addition & 1 deletion src/routes/predictoor/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require("express")
const { getPredictoorData, getPredictoorRewards } = require("../../services/rewards_info")
const { getPredictoorData, getPredictoorRewards } = require("../../services/predictoor")
const router = express.Router()

router.post("/data", async (req, res) => {
Expand Down
32 changes: 32 additions & 0 deletions src/services/challenge/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { selectQuery } = require("../querier")

const getChallengeData = ({ query, sort, limit, offset, group, fields, join }) => {
return selectQuery(
query,
sort,
limit,
offset,
"challenge_data",
group,
fields,
join
)
}

const getChallengeRewards = ({ query, sort, limit, offset, group, fields, join }) => {
return selectQuery(
query,
sort,
limit,
offset,
"challenge_rewards",
group,
fields,
join
)
}

module.exports = {
getChallengeData,
getChallengeRewards
}
22 changes: 12 additions & 10 deletions test/db_operations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,18 @@ describe("Test db operations", () => {
await updateRewardsSummary(round)
expect(db.promise().query).toHaveBeenCalledWith(
`
INSERT INTO rewards_summary(LP_addr, passive_amt, curating_amt, predictoor_amt, round)
SELECT LP_addr, SUM(passive) AS passive_amt, SUM(curating) AS curating_amt, SUM(OCEAN_amt) AS predictoor_amt, ? FROM
(
SELECT LP_addr, SUM(reward) AS passive, 0 AS curating, 0 AS OCEAN_amt FROM passive_rewards_info WHERE round = ? GROUP BY LP_addr
UNION
SELECT LP_addr, 0 AS passive, SUM(amt) AS curating, 0 AS OCEAN_amt FROM rewards_info WHERE round = ? GROUP BY LP_addr
UNION
SELECT predictoor_addr AS LP_addr, 0 AS passive, 0 AS curating, OCEAN_amt FROM predictoor_rewards WHERE round = ?
) AS foo GROUP BY LP_addr`,
[round, round, round, round]
INSERT INTO rewards_summary(LP_addr, passive_amt, curating_amt, predictoor_amt, challenge_amt, round)
SELECT LP_addr, SUM(passive) AS passive_amt, SUM(curating) AS curating_amt, SUM(predictoor) AS predictoor_amt, SUM(challenge) AS challenge_amt, ? FROM
(
SELECT LP_addr, SUM(reward) AS passive, 0 AS curating, 0 AS predictoor, 0 AS challenge FROM passive_rewards_info WHERE round = ? GROUP BY LP_addr
UNION
SELECT LP_addr, 0 AS passive, SUM(amt) AS curating, 0 AS predictoor, 0 AS challenge FROM rewards_info WHERE round = ? GROUP BY LP_addr
UNION
SELECT predictoor_addr AS LP_addr, 0 AS passive, 0 AS curating, OCEAN_amt AS predictoor, 0 AS challenge FROM predictoor_rewards WHERE round = ?
UNION
SELECT winner_addr AS LP_addr, 0 AS passive, 0 AS curating, 0 AS predictoor, SUM(OCEAN_amt) AS challenge FROM challenge_rewards WHERE round = ? GROUP BY winner_addr
) AS foo GROUP BY LP_addr`,
[round, round, round, round, round]
)
})

Expand Down
22 changes: 15 additions & 7 deletions test/fs_dir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ describe("Testing readDataDir function", () => {
"passive.csv",
"nftinfo.csv",
"rate-sample.csv",
"symbols-sample.csv"
"symbols-sample.csv",
"predictoor_rewards.csv",
"predictoor_data.csv",
"challenge_rewards.csv",
"challenge_data.csv"
])

// Mock readFileSync to return a predefined string
Expand All @@ -47,11 +51,11 @@ describe("Testing readDataDir function", () => {
const result = readDataDir(dataDir)

expect(fs.readdirSync).toHaveBeenCalledWith(dataDir)
expect(fs.readFileSync).toHaveBeenCalledTimes(10)
expect(parseCsv).toHaveBeenCalledTimes(10)
expect(crypto.createHash).toHaveBeenCalledTimes(10)
expect(mockHash.update).toHaveBeenCalledTimes(10)
expect(mockHash.digest).toHaveBeenCalledTimes(10)
expect(fs.readFileSync).toHaveBeenCalledTimes(14)
expect(parseCsv).toHaveBeenCalledTimes(14)
expect(crypto.createHash).toHaveBeenCalledTimes(14)
expect(mockHash.update).toHaveBeenCalledTimes(14)
expect(mockHash.digest).toHaveBeenCalledTimes(14)
Copy link
Member

Choose a reason for hiding this comment

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

Great!


expect(result).toEqual({
allocations: parseCsv(),
Expand All @@ -64,8 +68,12 @@ describe("Testing readDataDir function", () => {
nftinfo: parseCsv(),
rates: parseCsv(),
symbols: parseCsv(),
predictoor_rewards: parseCsv(),
predictoor_data: parseCsv(),
challenge_rewards: parseCsv(),
challenge_data: parseCsv(),
hashsum:
"123456123456123456123456123456123456123456123456123456123456"
"123456123456123456123456123456123456123456123456123456123456123456123456123456123456"
})
})
})