Skip to content

Commit

Permalink
Merge pull request #10 from cosmos-gaminghub/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
EG-easy authored Oct 7, 2021
2 parents 4ab16a2 + 18e6e3a commit e3f1cc5
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 11 deletions.
6 changes: 6 additions & 0 deletions graphql/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const express = require('express')
const { graphqlHTTP } = require('express-graphql')
const cors = require('cors')

// database
const schema = require('./schema')
Expand All @@ -9,6 +10,11 @@ const resolver = new Root(new Service()).init()

// Create an express server and a GraphQL endpoint
const app = express()
const corsOpts = {
origin: '*'
}

app.use(cors(corsOpts))
app.use('/graphql',
graphqlHTTP({
schema: schema,
Expand Down
2 changes: 2 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Validator {
moniker: String
operator_address: String
address: String
totalTxs: Int
totalPoints: Int
}

type TxCount {
Expand Down
12 changes: 10 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@cosmjs/stargate": "^0.26.0",
"@graphql-tools/graphql-file-loader": "^7.1.0",
"@graphql-tools/load": "^7.3.2",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"express-graphql": "^0.12.0",
Expand Down
32 changes: 29 additions & 3 deletions service/Service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { sequelize, Mission, Validator } = require('../models')
const { QueryTypes } = require('sequelize')
const ValidatorResponse = require('./types/validatorResponse')

class Service {
async fetchMissions () {
Expand All @@ -23,12 +24,37 @@ class Service {
const validator = await Validator.findOne(
{ where: { id: args.validatorID } }
)
return validator
// get total_txs data
const txResult = await sequelize.query(
'SELECT COUNT(*) as totalTxs FROM `TXS` WHERE ( SELECT address FROM `validators` WHERE id = ?)',
{
replacements: [args.validatorID],
type: QueryTypes.SELECT
}
)

// get mission clear point data
const pointResult = await sequelize.query(
'SELECT sum(point) as totalPoints FROM missions WHERE id in (SELECT mission_id FROM point_histories WHERE validator_id = ?)',
{
replacements: [args.validatorID],
type: QueryTypes.SELECT
}
)

const res = new ValidatorResponse()
.setOperatorAddress(validator.operator_address)
.setAddress(validator.address)
.setMoniker(validator.moniker)
.setTotalTxs(txResult[0].totalTxs)
.setTotalPoints(pointResult[0].totalPoints)

return res
}

async fetchTxCount (args) {
const result = await sequelize.query(
'SELECT COUNT(*) as total_txs FROM `TXS` WHERE ( SELECT address FROM `validators` WHERE id = ?)',
'SELECT COUNT(*) as totalTxs FROM `TXS` WHERE ( SELECT address FROM `validators` WHERE id = ?)',
{
replacements: [args.validatorID],
type: QueryTypes.SELECT
Expand All @@ -52,7 +78,7 @@ class Service {

async fetchMyPoint (args) {
const result = await sequelize.query(
'SELECT sum(point) as total_point FROM missions WHERE id in (SELECT mission_id FROM point_histories WHERE validator_id = ?)',
'SELECT sum(point) as totalPoints FROM missions WHERE id in (SELECT mission_id FROM point_histories WHERE validator_id = ?)',
{
replacements: [args.validatorID],
type: QueryTypes.SELECT
Expand Down
28 changes: 28 additions & 0 deletions service/types/validatorResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class ValidatorResponse {
setMoniker (a) {
this.moniker = a
return this
}

setOperatorAddress (a) {
this.operator_address = a
return this
}

setAddress (a) {
this.address = a
return this
}

setTotalTxs (a) {
this.totalTxs = a
return this
}

setTotalPoints (a) {
this.totalPoints = a
return this
}
}

module.exports = ValidatorResponse
2 changes: 1 addition & 1 deletion tests/__tests__/services/fetchMissionResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ describe('Mission Histories', () => {
test('Total of point is 30', async () => {
const q = { validatorID: 1 }
const res = await service.fetchMyPoint(q)
expect(res.total_point).toBe(30)
expect(res.totalPoints).toBe(30)
})
})
10 changes: 5 additions & 5 deletions tests/__tests__/services/fetchValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ describe('Validators Model', () => {
describe('Validators Model', () => {
test('Check validators columns', async () => {
const q = { validatorID: 1 }
const validator = await service.fetchValidator(q)
const res = await service.fetchValidator(q)

const columns = Object.keys(validator.rawAttributes)
expect(columns.indexOf('operator_address')).toBe(1)
expect(columns.indexOf('moniker')).toBe(2)
expect(columns.indexOf('address')).toBe(3)
expect(res.operator_address).toBe('cosmosvaloper156gqf9837u7d4c4678yt3rl4ls9c5vuursrrzf')
expect(res.moniker).toBe('Binance Staking')
expect(res.address).toBe('cosmos156gqf9837u7d4c4678yt3rl4ls9c5vuuxyhkw6')
expect(res.totalPoints).toBe(30)
})
})

0 comments on commit e3f1cc5

Please sign in to comment.