From 7a78210cb6b7314afd59a330f8a28c32b77dde8a Mon Sep 17 00:00:00 2001 From: keyan Date: Fri, 18 Mar 2022 07:29:02 -0500 Subject: [PATCH] improvements to earning --- api/resolvers/user.js | 14 +++++++++++++- worker/earn.js | 45 ++++++++++++++++++++++++++++++++++++++++++- worker/index.js | 3 ++- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/api/resolvers/user.js b/api/resolvers/user.js index 9cc8fb61..9e566a52 100644 --- a/api/resolvers/user.js +++ b/api/resolvers/user.js @@ -161,7 +161,16 @@ export default { WHERE "ItemAct"."userId" <> ${user.id} AND "ItemAct".act <> 'BOOST' AND "Item"."userId" = ${user.id}` - return sum || 0 + const { sum: { msats } } = await models.earn.aggregate({ + sum: { + msats: true + }, + where: { + userId: Number(user.id) + } + }) + + return (sum || 0) + Math.floor((msats || 0) / 1000) }, sats: async (user, args, { models, me }) => { if (me?.id !== user.id) { @@ -241,6 +250,9 @@ export default { userId: user.id, createdAt: { gt: user.checkedNotesAt || new Date(0) + }, + msats: { + gte: 1000 } } }) diff --git a/worker/earn.js b/worker/earn.js index 3ac5d01a..e6b3da17 100644 --- a/worker/earn.js +++ b/worker/earn.js @@ -41,4 +41,47 @@ function earn ({ models }) { } } -module.exports = { earn } +// earn historical ... TODO: delete after announcement +function earnHistorical ({ models }) { + return async function ({ name }) { + console.log('running', name) + + // compute how much sn earned today + const [{ sum }] = await models.$queryRaw` + SELECT sum("ItemAct".sats) + FROM "ItemAct" + JOIN "Item" on "ItemAct"."itemId" = "Item".id + WHERE ("ItemAct".act in ('BOOST', 'STREAM') + OR ("ItemAct".act = 'VOTE' AND "Item"."userId" = "ItemAct"."userId"))` + + // add in the job sats that weren't recorded from jobs + const fullSum = 200000 + sum + + // calculate the total trust + const { sum: { trust } } = await models.user.aggregate({ + sum: { + trust: true + } + }) + + // get earners { id, earnings } + const earners = await models.$queryRaw(` + SELECT id, FLOOR(${fullSum} * (trust/${trust}) * 1000) as earnings + FROM users + WHERE trust > 0`) + + // for each earner, serialize earnings + // we do this for each earner because we don't need to serialize + // all earner updates together + earners.forEach(async earner => { + if (earner.earnings > 0) { + await serialize(models, + models.$executeRaw`SELECT earn(${earner.id}, ${earner.earnings})`) + } + }) + + console.log('done', name) + } +} + +module.exports = { earn, earnHistorical } diff --git a/worker/index.js b/worker/index.js index 3399acd5..efd4daef 100644 --- a/worker/index.js +++ b/worker/index.js @@ -6,7 +6,7 @@ const { checkInvoice, checkWithdrawal } = require('./wallet') const { repin } = require('./repin') const { trust } = require('./trust') const { auction } = require('./auction') -const { earn } = require('./earn') +const { earn, earnHistorical } = require('./earn') const { ApolloClient, HttpLink, InMemoryCache } = require('@apollo/client') const { indexItem, indexAllItems } = require('./search') const fetch = require('cross-fetch') @@ -45,6 +45,7 @@ async function work () { await boss.work('indexAllItems', indexAllItems(args)) await boss.work('auction', auction(args)) await boss.work('earn', earn(args)) + await boss.work('earnHistorical', earnHistorical(args)) console.log('working jobs') }