2023-07-23 15:08:43 +00:00
|
|
|
import { GraphQLError } from 'graphql'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { amountSchema, ssValidate } from '@/lib/validate'
|
2023-09-26 20:15:09 +00:00
|
|
|
import { getItem } from './item'
|
2024-03-01 16:28:55 +00:00
|
|
|
import { topUsers } from './user'
|
2024-07-01 17:02:29 +00:00
|
|
|
import performPaidAction from '../paidAction'
|
2022-12-08 00:04:02 +00:00
|
|
|
|
2024-03-01 16:28:55 +00:00
|
|
|
let rewardCache
|
2023-10-24 00:46:51 +00:00
|
|
|
|
2024-03-01 16:28:55 +00:00
|
|
|
async function updateCachedRewards (models) {
|
|
|
|
const rewards = await getActiveRewards(models)
|
|
|
|
rewardCache = { rewards, createdAt: Date.now() }
|
2023-10-24 00:46:51 +00:00
|
|
|
return rewards
|
|
|
|
}
|
|
|
|
|
2024-03-01 16:28:55 +00:00
|
|
|
async function getCachedActiveRewards (staleIn, models) {
|
|
|
|
if (rewardCache) {
|
|
|
|
const { rewards, createdAt } = rewardCache
|
2023-10-24 00:46:51 +00:00
|
|
|
const expired = createdAt + staleIn < Date.now()
|
2024-03-01 16:28:55 +00:00
|
|
|
if (expired) updateCachedRewards(models).catch(console.error)
|
2023-10-24 00:46:51 +00:00
|
|
|
return rewards // serve stale rewards
|
|
|
|
}
|
2024-03-01 16:28:55 +00:00
|
|
|
return await updateCachedRewards(models)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getActiveRewards (models) {
|
|
|
|
return await models.$queryRaw`
|
|
|
|
SELECT
|
|
|
|
(sum(total) / 1000)::INT as total,
|
2024-05-01 14:30:36 +00:00
|
|
|
date_trunc('day', (now() AT TIME ZONE 'America/Chicago') + interval '1 day') AT TIME ZONE 'America/Chicago' as time,
|
2024-03-01 16:28:55 +00:00
|
|
|
json_build_array(
|
|
|
|
json_build_object('name', 'donations', 'value', (sum(donations) / 1000)::INT),
|
|
|
|
json_build_object('name', 'fees', 'value', (sum(fees) / 1000)::INT),
|
|
|
|
json_build_object('name', 'boost', 'value', (sum(boost) / 1000)::INT),
|
|
|
|
json_build_object('name', 'jobs', 'value', (sum(jobs) / 1000)::INT),
|
|
|
|
json_build_object('name', 'anon''s stack', 'value', (sum(anons_stack) / 1000)::INT)
|
|
|
|
) AS sources
|
|
|
|
FROM (
|
|
|
|
(SELECT * FROM rewards_today)
|
|
|
|
UNION ALL
|
|
|
|
(SELECT * FROM
|
|
|
|
rewards(
|
|
|
|
date_trunc('hour', timezone('America/Chicago', now())),
|
|
|
|
date_trunc('hour', timezone('America/Chicago', now())), '1 hour'::INTERVAL, 'hour'))
|
|
|
|
) u`
|
2023-10-24 00:46:51 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 01:53:07 +00:00
|
|
|
async function getMonthlyRewards (when, models) {
|
|
|
|
return await models.$queryRaw`
|
|
|
|
SELECT
|
|
|
|
(sum(total) / 1000)::INT as total,
|
|
|
|
date_trunc('month', ${when?.[0]}::text::timestamp) AT TIME ZONE 'America/Chicago' as time,
|
|
|
|
json_build_array(
|
|
|
|
json_build_object('name', 'donations', 'value', (sum(donations) / 1000)::INT),
|
|
|
|
json_build_object('name', 'fees', 'value', (sum(fees) / 1000)::INT),
|
|
|
|
json_build_object('name', 'boost', 'value', (sum(boost) / 1000)::INT),
|
|
|
|
json_build_object('name', 'jobs', 'value', (sum(jobs) / 1000)::INT),
|
|
|
|
json_build_object('name', 'anon''s stack', 'value', (sum(anons_stack) / 1000)::INT)
|
|
|
|
) AS sources
|
|
|
|
FROM rewards_days
|
|
|
|
WHERE date_trunc('month', rewards_days.t) = date_trunc('month', ${when?.[0]}::text::timestamp - interval '1 month')`
|
|
|
|
}
|
|
|
|
|
2023-10-24 00:46:51 +00:00
|
|
|
async function getRewards (when, models) {
|
|
|
|
if (when) {
|
2024-03-30 01:53:07 +00:00
|
|
|
if (when.length > 1) {
|
2023-10-24 00:46:51 +00:00
|
|
|
throw new GraphQLError('too many dates', { extensions: { code: 'BAD_USER_INPUT' } })
|
|
|
|
}
|
|
|
|
when.forEach(w => {
|
|
|
|
if (isNaN(new Date(w))) {
|
|
|
|
throw new GraphQLError('invalid date', { extensions: { code: 'BAD_USER_INPUT' } })
|
2023-08-15 17:41:51 +00:00
|
|
|
}
|
2023-10-24 00:46:51 +00:00
|
|
|
})
|
|
|
|
if (new Date(when[0]) > new Date(when[when.length - 1])) {
|
|
|
|
throw new GraphQLError('bad date range', { extensions: { code: 'BAD_USER_INPUT' } })
|
|
|
|
}
|
2024-03-30 01:53:07 +00:00
|
|
|
|
2024-05-01 14:30:36 +00:00
|
|
|
if (new Date(when[0]).getTime() > new Date('2024-03-01').getTime() && new Date(when[0]).getTime() < new Date('2024-05-02').getTime()) {
|
|
|
|
// after 3/1/2024 and until 5/1/2024, we reward monthly on the 1st
|
2024-03-31 19:21:08 +00:00
|
|
|
if (new Date(when[0]).getUTCDate() !== 1) {
|
|
|
|
throw new GraphQLError('invalid reward date', { extensions: { code: 'BAD_USER_INPUT' } })
|
|
|
|
}
|
|
|
|
|
2024-03-30 01:53:07 +00:00
|
|
|
return await getMonthlyRewards(when, models)
|
|
|
|
}
|
2023-10-24 00:46:51 +00:00
|
|
|
}
|
2023-08-15 17:41:51 +00:00
|
|
|
|
2023-10-24 00:46:51 +00:00
|
|
|
const results = await models.$queryRaw`
|
|
|
|
WITH days_cte (day) AS (
|
|
|
|
SELECT date_trunc('day', t)
|
|
|
|
FROM generate_series(
|
|
|
|
COALESCE(${when?.[0]}::text::timestamp - interval '1 day', now() AT TIME ZONE 'America/Chicago'),
|
|
|
|
COALESCE(${when?.[when.length - 1]}::text::timestamp - interval '1 day', now() AT TIME ZONE 'America/Chicago'),
|
|
|
|
interval '1 day') AS t
|
|
|
|
)
|
2024-03-01 16:28:55 +00:00
|
|
|
SELECT (total / 1000)::INT as total,
|
2023-10-24 00:46:51 +00:00
|
|
|
days_cte.day + interval '1 day' as time,
|
|
|
|
json_build_array(
|
2024-03-01 16:28:55 +00:00
|
|
|
json_build_object('name', 'donations', 'value', donations / 1000),
|
|
|
|
json_build_object('name', 'fees', 'value', fees / 1000),
|
|
|
|
json_build_object('name', 'boost', 'value', boost / 1000),
|
|
|
|
json_build_object('name', 'jobs', 'value', jobs / 1000),
|
|
|
|
json_build_object('name', 'anon''s stack', 'value', anons_stack / 1000)
|
2023-10-24 00:46:51 +00:00
|
|
|
) AS sources
|
|
|
|
FROM days_cte
|
2024-03-01 16:28:55 +00:00
|
|
|
JOIN rewards_days ON rewards_days.t = days_cte.day
|
|
|
|
GROUP BY days_cte.day, total, donations, fees, boost, jobs, anons_stack
|
2023-10-24 00:46:51 +00:00
|
|
|
ORDER BY days_cte.day ASC`
|
|
|
|
|
|
|
|
return results.length ? results : [{ total: 0, time: '0', sources: [] }]
|
|
|
|
}
|
2022-12-08 00:04:02 +00:00
|
|
|
|
2023-10-24 00:46:51 +00:00
|
|
|
export default {
|
|
|
|
Query: {
|
|
|
|
rewards: async (parent, { when }, { models }) =>
|
2024-03-01 16:28:55 +00:00
|
|
|
when ? await getRewards(when, models) : await getCachedActiveRewards(5000, models),
|
2023-08-15 17:41:51 +00:00
|
|
|
meRewards: async (parent, { when }, { me, models }) => {
|
|
|
|
if (!me) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2023-08-30 02:05:10 +00:00
|
|
|
if (!when || when.length > 2) {
|
2023-08-30 00:13:21 +00:00
|
|
|
throw new GraphQLError('invalid date range', { extensions: { code: 'BAD_USER_INPUT' } })
|
|
|
|
}
|
|
|
|
for (const w of when) {
|
|
|
|
if (isNaN(new Date(w))) {
|
|
|
|
throw new GraphQLError('invalid date', { extensions: { code: 'BAD_USER_INPUT' } })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const results = await models.$queryRaw`
|
|
|
|
WITH days_cte (day) AS (
|
2023-08-30 02:05:10 +00:00
|
|
|
SELECT date_trunc('day', t)
|
|
|
|
FROM generate_series(
|
2023-09-18 22:48:05 +00:00
|
|
|
${when[0]}::text::timestamp,
|
|
|
|
${when[when.length - 1]}::text::timestamp,
|
2023-08-30 02:05:10 +00:00
|
|
|
interval '1 day') AS t
|
2023-08-15 17:41:51 +00:00
|
|
|
)
|
|
|
|
SELECT coalesce(sum(sats), 0) as total, json_agg("Earn".*) as rewards
|
2023-08-30 00:13:21 +00:00
|
|
|
FROM days_cte
|
2023-08-15 17:41:51 +00:00
|
|
|
CROSS JOIN LATERAL (
|
2023-08-29 21:05:09 +00:00
|
|
|
(SELECT FLOOR("Earn".msats / 1000.0) as sats, type, rank, "typeId"
|
2023-08-15 17:41:51 +00:00
|
|
|
FROM "Earn"
|
|
|
|
WHERE "Earn"."userId" = ${me.id}
|
2024-07-19 17:38:49 +00:00
|
|
|
AND (type IS NULL OR type NOT IN ('FOREVER_REFERRAL', 'ONE_DAY_REFERRAL'))
|
2023-08-30 00:13:21 +00:00
|
|
|
AND date_trunc('day', "Earn".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = days_cte.day
|
2023-08-15 17:41:51 +00:00
|
|
|
ORDER BY "Earn".msats DESC)
|
2023-08-30 00:13:21 +00:00
|
|
|
) "Earn"
|
|
|
|
GROUP BY days_cte.day
|
|
|
|
ORDER BY days_cte.day ASC`
|
2023-08-15 17:41:51 +00:00
|
|
|
|
2023-08-30 00:13:21 +00:00
|
|
|
return results
|
2022-12-08 00:04:02 +00:00
|
|
|
}
|
|
|
|
},
|
2024-03-01 16:28:55 +00:00
|
|
|
Rewards: {
|
|
|
|
leaderboard: async (parent, args, { models, ...context }) => {
|
|
|
|
// get to and from using postgres because it's easier to do there
|
|
|
|
const [{ to, from }] = await models.$queryRaw`
|
2024-05-01 14:30:36 +00:00
|
|
|
SELECT date_trunc('day', (now() AT TIME ZONE 'America/Chicago')) AT TIME ZONE 'America/Chicago' as from,
|
|
|
|
(date_trunc('day', (now() AT TIME ZONE 'America/Chicago')) AT TIME ZONE 'America/Chicago') + interval '1 day - 1 second' as to`
|
2024-03-31 21:53:57 +00:00
|
|
|
return await topUsers(parent, { when: 'custom', to: new Date(to).getTime().toString(), from: new Date(from).getTime().toString(), limit: 100 }, { models, ...context })
|
2024-07-07 16:12:02 +00:00
|
|
|
},
|
|
|
|
total: async (parent, args, { models }) => {
|
|
|
|
if (!parent.total) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return parent.total
|
2024-03-01 16:28:55 +00:00
|
|
|
}
|
|
|
|
},
|
2022-12-08 00:04:02 +00:00
|
|
|
Mutation: {
|
2024-07-04 17:30:42 +00:00
|
|
|
donateToRewards: async (parent, { sats }, { me, models, lnd }) => {
|
2023-02-08 19:38:04 +00:00
|
|
|
await ssValidate(amountSchema, { amount: sats })
|
|
|
|
|
2024-07-04 17:30:42 +00:00
|
|
|
return await performPaidAction('DONATE', { sats }, { me, models, lnd })
|
2022-12-08 00:04:02 +00:00
|
|
|
}
|
2023-08-29 21:05:09 +00:00
|
|
|
},
|
|
|
|
Reward: {
|
|
|
|
item: async (reward, args, { me, models }) => {
|
|
|
|
if (!reward.typeId) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return getItem(reward, { id: reward.typeId }, { me, models })
|
|
|
|
}
|
2022-12-08 00:04:02 +00:00
|
|
|
}
|
|
|
|
}
|