Skip to content

Commit

Permalink
Merge pull request #435 from edenia/dev
Browse files Browse the repository at this point in the history
Production Release
  • Loading branch information
xavier506 authored Mar 9, 2023
2 parents 3fd94e6 + 54aef05 commit 726246a
Show file tree
Hide file tree
Showing 88 changed files with 898 additions and 168 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ HAPI_NETWORK_WALLET_URL=http://wallet:8888
HAPI_HASURA_URL=http://hasura:8080/v1/graphql
HAPI_HASURA_ADMIN_SECRET=myadminsecretkey
HAPI_EDEN_ELECTION_INTERVAL=7889400
HAPI_EDEN_HISTORIC_ELECTION= [{"election": 0, "date_election": "2022-09-18"}, {"election": 1, "date_election": "2022-09-22"}, {"election": 2, "date_election": "2022-09-23T13:00:00.000"}, {"election": 3, "date_election": "2022-09-23T20:00:00.000"}, {"election": 4, "date_election": "2022-09-27"}, {"election": 5, "date_election": "2022-09-30"}]
HAPI_EDEN_HISTORIC_ELECTION= [{"election": 0, "date_election": "2022-09-18"}, {"election": 1, "date_election": "2022-09-22"}, {"election": 2, "date_election": "2022-09-23T13:00:00.000"}, {"election": 3, "date_election": "2022-09-23T20:00:00.000"}, {"election": 4, "date_election": "2022-09-27"}, {"election": 5, "date_election": "2022-09-30"}, {"election": 6, "date_election": "2022-12-26T13:05:00.000"}, {"election": 7, "date_election": "2022-12-26T19:05:00.000"}, {"election": 8, "date_election": "2022-12-28T19:05:00.000"}, {"election": 9, "date_election": "2023-01-03T19:00:00.000"}, {"election": 10, "date_election": "2023-02-13T17:30:00.000"}, {"election": 11, "date_election": "2023-02-15T17:30:00.000"}, {"election": 12, "date_election": "2023-02-17T20:30:00.000"}, {"election": 13, "date_election": "2023-02-23T17:30:00.000"}]
HAPI_EDEN_CONTRACT=genesis.eden
HAPI_RATE_HISTORY=https://api.coingecko.com/api/v3
HAPI_DFUSE_API_KEY=<api_key>
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
- ./tmp/wallet/:/opt/application/data/
postgres:
container_name: 'eden-spending-postgres'
image: postgres:12.12-bullseye
image: postgres:12.14-bullseye
ports:
- '5432:5432'
volumes:
Expand Down
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
31 changes: 28 additions & 3 deletions hapi/src/gql/eden-transaction.gql.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const get = async (where, getMany = false) => {
txid
type
updated_at
eos_exchange
digest
}
}
Expand Down Expand Up @@ -73,13 +74,36 @@ 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
usd_total
}
}
}
Expand All @@ -90,13 +114,14 @@ const getAggregate = async where => {
where
})

return edenTransactionAggregate?.aggregate?.sum?.amount || 0
return edenTransactionAggregate?.aggregate?.sum || {}
}

module.exports = {
save,
get,
deleteTx,
update,
getAggregate
getAggregate,
getHistoricIncome
}
86 changes: 86 additions & 0 deletions hapi/src/gql/eden_global_amount.gql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const { hasuraUtil } = require('../utils')

const save = async payload => {
const mutation = `
mutation ($payload: global_amount_insert_input!) {
insert_global_amount_one(object: $payload) {
id
eos_income
usd_income
usd_expense
eos_expense
election
account
}
}
`
const data = await hasuraUtil.instance.request(mutation, { payload })

return data.insert_global_amount_one
}

const get = async (where, getMany = false) => {
const query = `
query ($where: global_amount_bool_exp) {
global_amount(where: $where) {
id
eos_income
usd_income
usd_expense
eos_expense
election
account
}
}
`
const { global_amount } = await hasuraUtil.instance.request(query, {
where
})

return getMany ? global_amount : global_amount[0]
}

const update = async ({ where, _set }) => {
const mutation = `
mutation update($where: global_amount_bool_exp!, $_set: global_amount_set_input) {
update_global_amount(where: $where, _set: $_set) {
returning {
id
}
}
}
`
const data = await hasuraUtil.instance.request(mutation, {
where,
_set
})

return data.update_eden_transaction
}

const getAggregate = async where => {
const query = `
query ($where: global_amount_bool_exp) {
global_amount_aggregate(where: $where) {
aggregate {
sum {
eos_income
usd_income
}
}
}
}
`
const { global_amount } = await hasuraUtil.instance.request(query, {
where
})

return global_amount?.aggregate?.sum || {}
}

module.exports = {
save,
get,
update,
getAggregate
}
81 changes: 81 additions & 0 deletions hapi/src/gql/eden_total_expense_by_delegate_and_election.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const { hasuraUtil } = require('../utils')

const save = async payload => {
const mutation = `
mutation ($payload: total_expense_by_delegate_and_election_insert_input!) {
insert_total_expense_by_delegate_and_election_one(object: $payload) {
id
}
}
`
const data = await hasuraUtil.instance.request(mutation, { payload })

return data.insert_total_expense_by_delegate_and_election_one
}

const get = async (where, getMany = false) => {
const query = `
query ($where: total_expense_by_delegate_and_election_bool_exp) {
total_expense_by_delegate_and_election(where: $where) {
id
eos_amount
usd_amount
id_election
category
}
}
`
const { total_expense_by_delegate_and_election } =
await hasuraUtil.instance.request(query, {
where
})

return getMany
? total_expense_by_delegate_and_election
: total_expense_by_delegate_and_election[0]
}

const update = async ({ where, _set }) => {
const mutation = `
mutation update($where: total_expense_by_delegate_and_election_bool_exp!, $_set: total_expense_by_delegate_and_election_set_input) {
update_total_expense_by_delegate_and_election(where: $where, _set: $_set) {
returning {
id
}
}
}
`
const data = await hasuraUtil.instance.request(mutation, {
where,
_set
})

return data.update_eden_transaction
}

const getAggregate = async where => {
const query = `
query ($where: total_expense_by_delegate_and_election_bool_exp) {
total_expense_by_delegate_and_election_aggregate(where: $where) {
aggregate {
sum {
eos_amount
}
}
}
}
`
const { total_expense_by_delegate_and_election } =
await hasuraUtil.instance.request(query, {
where
})

return total_expense_by_delegate_and_election?.aggregate?.sum?.eos_amount || 0
}

module.exports = {
save,
get,
update,
getAggregate
}
4 changes: 3 additions & 1 deletion hapi/src/gql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ module.exports = {
edenTreasury: require('./eden-treasury.gql'),
edenDelegatesGql: require('./eden-delegates.gql'),
edenElectionGql: require('./eden-election.gql'),
edenHistoricElectionGql: require('./eden-historic-election.gql')
edenHistoricElectionGql: require('./eden-historic-election.gql'),
edenTotalExpenseByDelegateAndElection: require('./eden_total_expense_by_delegate_and_election'),
edenGlobalAmountGql: require('./eden_global_amount.gql')
}
27 changes: 24 additions & 3 deletions hapi/src/services/dfuse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,37 @@ 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 {
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)

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
@@ -1,4 +1,9 @@
const { edenTransactionGql, edenElectionGql } = require('../../../gql')
const {
edenTransactionGql,
edenElectionGql,
edenTotalExpenseByDelegateAndElection,
edenGlobalAmountGql
} = require('../../../gql')
const { updaterUtil } = require('../../../utils')

module.exports = {
Expand All @@ -12,6 +17,7 @@ module.exports = {

const { category, description } = updaterUtil.memoSplit(memo)
const transactionToEditQuery = {
type: { _eq: 'expense' },
txid: { _eq: tx_id },
...(digest && { digest: { _eq: digest } })
}
Expand All @@ -23,12 +29,7 @@ module.exports = {
if (!transactionToEdit) return

const { idElection: id_election } =
await updaterUtil.getElectionWithoutExpense(
account,
transactionToEdit.amount,
edenElectionGql,
edenTransactionGql
)
await updaterUtil.getElectionWithoutExpense(account, edenElectionGql)

const updateQuery = {
where: {
Expand All @@ -39,6 +40,31 @@ module.exports = {
}

await edenTransactionGql.update(updateQuery)

const existExpense = await edenTotalExpenseByDelegateAndElection.get({
tx_id: { _eq: transactionToEdit.digest }
})

if (transactionToEdit.category === 'uncategorized' || !existExpense) {
await updaterUtil.saveTotalByDelegateAndElection(
transactionToEdit.digest,
account,
transactionToEdit.amount,
transactionToEdit.eos_exchange,
category,
edenElectionGql,
edenTransactionGql,
edenTotalExpenseByDelegateAndElection,
edenGlobalAmountGql
)
} else {
await edenTotalExpenseByDelegateAndElection.update({
where: {
tx_id: { _eq: transactionToEdit.digest }
},
_set: { category }
})
}
} catch (error) {
console.error(`categorize updater sync error: ${error.message}`)
}
Expand Down
Loading

0 comments on commit 726246a

Please sign in to comment.