Skip to content

Commit

Permalink
fix(webapp): save incomes first
Browse files Browse the repository at this point in the history
  • Loading branch information
warnerHurtado committed Feb 16, 2023
1 parent 0227bc2 commit ab3dc02
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 44 deletions.
6 changes: 2 additions & 4 deletions hapi/src/gql/eden-delegates.gql.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const get = async (where, getMany = false) => {
return getMany ? edenDelegates : edenDelegates[0]
}

const update = async (id, lastSyncedAt) => {
const update = async (id, data) => {
const mutation = `
mutation ($id: uuid!, $payload: eden_delegates_set_input) {
update_eden_delegates_by_pk(pk_columns: {id: $id}, _set: $payload) {
Expand All @@ -48,9 +48,7 @@ const update = async (id, lastSyncedAt) => {

await hasuraUtil.instance.request(mutation, {
id,
payload: {
last_synced_at: lastSyncedAt
}
payload: data
})
}

Expand Down
27 changes: 25 additions & 2 deletions hapi/src/gql/eden-transaction.gql.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,32 @@ const update = async ({ where, _set }) => {
return data.update_eden_transaction
}

const getHistoricIncome = async (where, getMany = false) => {
const query = `
query ($where: historic_incomes_bool_exp) {
historic_incomes(where: $where) {
recipient
eos_claimed
usd_claimed
eos_unclaimed
usd_unclaimed
election
delegate_level
}
}`

const { historic_incomes: historicIncomes } =
await hasuraUtil.instance.request(query, {
where
})

return getMany ? historicIncomes : historicIncomes[0]
}

const getAggregate = async where => {
const query = `
query ($where: eden_transaction_bool_exp) {
eden_transaction_aggregate(where: $where) {
eden_transaction_aggregate(where: $where, distinct_on: txid) {
aggregate {
sum {
amount
Expand All @@ -100,5 +122,6 @@ module.exports = {
get,
deleteTx,
update,
getAggregate
getAggregate,
getHistoricIncome
}
30 changes: 27 additions & 3 deletions hapi/src/services/dfuse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,40 @@ const getDelegateData = async () => {
const delegate = delegatesList[index]
let hasMore = true
let actions = []
let blockNumber = delegate.last_synced_at
let blockNumber = delegate.last_synced_income_at
try {
console.log(delegate.account)
while (hasMore) {
;({ hasMore, actions, blockNumber } = await getActions({
query: `account:${edenConfig.edenContract} data.from:${delegate.account} data.to:${delegate.account} OR account:eosio.token data.from:${delegate.account} receiver:eosio.token OR data.account:${delegate.account} receiver:edenexplorer`,
query: `account:${edenConfig.edenContract} data.from:${delegate.account} data.to:${delegate.account}`,
lowBlockNum: blockNumber
}))

await runUpdaters(actions)
await edenDelegatesGql.update(delegate.id, blockNumber)

console.log(blockNumber)

await edenDelegatesGql.update(delegate.id, {
last_synced_income_at: blockNumber
})
await sleepUtil(10)
}

hasMore = true
blockNumber = delegate.last_synced_at

while (hasMore) {
await runUpdaters(actions)
;({ hasMore, actions, blockNumber } = await getActions({
query: `account:eosio.token data.from:${delegate.account} receiver:eosio.token OR data.account:${delegate.account} receiver:edenexplorer`,
lowBlockNum: blockNumber
}))

await runUpdaters(actions)

await edenDelegatesGql.update(delegate.id, {
last_synced_at: blockNumber
})
await sleepUtil(10)
}
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
account,
transactionToEdit.amount,
transactionToEdit.eos_exchange,
transactionToEdit.category,
category,
edenElectionGql,
edenTransactionGql,
edenTotalExpenseByDelegateAndElection,
Expand Down
9 changes: 8 additions & 1 deletion hapi/src/services/eden-unclaimed-funds.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ const updateEdenUncleimedFundsWorker = () => {
action: updateUnclaimedFunds
}
}
const updateEdenUncleimedFundsWorker2 = () => {
return {
name: servicesConstant.MESSAGES.uncleimedFunds,
action: updateUnclaimedFunds
}
}

module.exports = {
updateEdenUncleimedFundsWorker
updateEdenUncleimedFundsWorker,
updateEdenUncleimedFundsWorker2
}
1 change: 1 addition & 0 deletions hapi/src/services/worker.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const run = async ({ name, action, interval }) => {
const init = async () => {
await hasuraUtil.hasuraAssembled()
await run(edenHistoricDelegateService.updateHistoricDelegatesWorker())
await run(edenUnclaimedFundsService.updateEdenUncleimedFundsWorker2())
run(edenDelegatesService.updateEdenTableWorker())
run(edenUnclaimedFundsService.updateEdenUncleimedFundsWorker())
run(dfuseService.syncWorker())
Expand Down
39 changes: 17 additions & 22 deletions hapi/src/utils/updater.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,46 +52,39 @@ const saveTotalByDelegateAndElection = async (
const elections = await edenElectionGql.get(electionsQuery, true)

for (const { id, election } of elections) {
const incomeQuery = {
eden_election: {
eden_delegate: { account: { _eq: delegateAccount } },
election: { _eq: election }
},
type: { _eq: 'income' }
}

const totalByDelegateAndElectionQuery = {
id_election: { _eq: id }
}

const globalAmountQuery = {
election: { _eq: election },
account: { _eq: delegateAccount }
}

const { amount, usd_total } =
(await edenTransactionGql.getAggregate(incomeQuery)) || 0
const { eos_claimed, eos_unclaimed, usd_claimed, usd_unclaimed } =
await edenTransactionGql.getHistoricIncome({
election: { _eq: election },
recipient: { _eq: delegateAccount }
})

const totalByDelegateAndElection =
(await edenTotalExpenseByDelegateAndElection.getAggregate(
totalByDelegateAndElectionQuery
)) || 0
const amount = eos_claimed + eos_unclaimed || 0
const usd_total = usd_claimed + usd_unclaimed || 0

let globalAmount = await edenGlobalAmountGql.get(globalAmountQuery)

if (!globalAmount) {
globalAmount = await edenGlobalAmountGql.save({
account: delegateAccount,
election,
eos_income: amount,
usd_income: usd_total,
eos_income: Number(amount).toFixed(2),
usd_income: Number(usd_total).toFixed(2),
eos_expense: 0,
usd_expense: 0
})
}

if (totalByDelegateAndElection + tempAmount > amount) {
const totalToSave = amount - totalByDelegateAndElection
if (globalAmount.eos_expense >= globalAmount.eos_income) continue

if (tempAmount < 0) console.log(tempAmount, 'tempAmount')

if (globalAmount.eos_expense + tempAmount > amount) {
const totalToSave = globalAmount.eos_income - globalAmount.eos_expense

const totalByDelegateAndElectionData = {
eos_amount: totalToSave,
Expand Down Expand Up @@ -120,6 +113,8 @@ const saveTotalByDelegateAndElection = async (
)

tempAmount = tempAmount - totalToSave

if (totalToSave < 0) console.log(totalToSave, 'Total To Save')
} else {
const totalByDelegateAndElectionData = {
eos_amount: tempAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ select_permissions:
- created_at
- id
- last_synced_at
- last_synced_income_at
- updated_at
filter: {}
allow_aggregations: true
Expand All @@ -25,5 +26,6 @@ update_permissions:
permission:
columns:
- last_synced_at
- last_synced_income_at
filter: {}
check: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "public"."eden_delegates" add column "last_synced_income_at" int8
-- null default '209274902';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "public"."eden_delegates" add column "last_synced_income_at" int8
null default '209274902';
14 changes: 7 additions & 7 deletions webapp/src/gql/eden-expense.gql.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const GET_TOTAL_EXPENSE_BY_DELEGATE = gql`
`

export const GET_DELEGATES_EXPENSE_BY_ELECTION = gql`
query getDelegatesByElection($election: numeric) {
query getDelegatesByElection($election: Int) {
global_amount(where: { election: { _eq: $election } }) {
account
election
Expand All @@ -41,6 +41,7 @@ export const GET_DELEGATES_EXPENSE_BY_ELECTION = gql`
}
`

// TODO:
export const GET_TOTAL_EXPENSE_BY_CATEGORY_AND_ELECTION = gql`
query getExpensesByCategoryAndElection($election: Int) {
expenses_by_category_and_election(where: { election: { _eq: $election } }) {
Expand Down Expand Up @@ -89,16 +90,15 @@ export const GET_EXPENSES_BY_ACCOUNT = gql`
`

export const GET_EXPENSE_BY_CATEGORY = gql`
query getExpenseByCategory($id_election: uuid) {
total_expense_by_delegate_and_election(
query getExpensesByCategoryAndElection($id_election: uuid) {
expenses_by_category_and_election(
where: { id_election: { _eq: $id_election } }
) {
total_usd_amount
category
eos_amount
usd_amount
id
total_eos_amount
election
id_election
tx_id
}
}
`
2 changes: 1 addition & 1 deletion webapp/src/gql/eden-transaction.gql.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gql from 'graphql-tag'

export const GET_TOTAL_EXPENSE_BY_ELECTION = gql`
query getTotalExpenseByDelegateView($election: numeric, $account: String) {
query getTotalExpenseByDelegateView($election: Int, $account: String) {
global_amount(
where: { election: { _eq: $election }, account: { _eq: $account } }
) {
Expand Down
4 changes: 3 additions & 1 deletion webapp/src/hooks/customHooks/useDelegateReportState.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ const useDelegateReportState = () => {
id_election: electionId?.eden_election[0].id
})

console.log(responseCategory)

const responseTransaction = await loadTransactions({
election: Number(electionRoundSelect),
delegate: delegateSelect
Expand All @@ -116,7 +118,7 @@ const useDelegateReportState = () => {
)

const categories = newDataFormatByCategoryDelegate(
responseCategory?.total_expense_by_delegate_and_election || []
responseCategory?.expenses_by_category_and_election || []
)

setCategoryList(categories)
Expand Down
4 changes: 2 additions & 2 deletions webapp/src/utils/new-format-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export const newDataFormatByTypeDelegate = (incomeList, expenseList) => {
export const newDataFormatByCategoryDelegate = categoryList =>
categoryList.map(data => ({
category: data.category,
EOS: Number(data.eos_amount),
USD: Number(data.usd_amount),
EOS: Number(data.total_eos_amount),
USD: Number(data.total_usd_amount),
color: generateColor()
}))

0 comments on commit ab3dc02

Please sign in to comment.