Browse Source

backend: fix processEvents

Joystream Stats 4 years ago
parent
commit
893e5c64bb
4 changed files with 28 additions and 9 deletions
  1. 21 0
      server/db/models/block.ts
  2. 1 0
      server/db/models/event.ts
  3. 1 1
      server/index.ts
  4. 5 8
      server/joystream/index.ts

+ 21 - 0
server/db/models/block.ts

@@ -10,4 +10,25 @@ const Block = db.define('block', {
   blocktime: DataTypes.INTEGER,
 })
 
+Block.findAllWithIncludes = function () {
+  return this.findAll({
+    include: [
+      { model: db.models.era },
+      { model: db.models.event },
+      { association: 'author' },
+    ],
+  })
+}
+
+Block.findWithIncludes = function (args: { where: any }) {
+  return this.findAll({
+    ...args,
+    include: [
+      { model: db.models.era },
+      { model: db.models.event },
+      { association: 'author' },
+    ],
+  })
+}
+
 export default Block

+ 1 - 0
server/db/models/event.ts

@@ -4,6 +4,7 @@ import { DataTypes } from 'sequelize'
 const Event = db.define('event', {
   id: {
     type: DataTypes.INTEGER,
+    autoIncrement: true,
     primaryKey: true,
   },
   section: DataTypes.STRING,

+ 1 - 1
server/index.ts

@@ -10,7 +10,7 @@ delete pg.native
 const PORT: number = process.env.PORT ? +process.env.PORT : 3500
 
 const app = express()
-const server = app.listen(() => {
+const server = app.listen(PORT, () => {
   console.log(`[Express] Listening on port ${PORT}`)
 })
 const io: any = socketio(server)

+ 5 - 8
server/joystream/index.ts

@@ -27,6 +27,7 @@ import axios from 'axios'
 import moment from 'moment'
 
 import { VoteKind } from '@joystream/types/proposals'
+import { EventRecord } from '@polkadot/types/interfaces'
 import {
   Api,
   Handles,
@@ -107,21 +108,17 @@ const addBlock = async (
 const getBlockHash = (api: Api, blockId: number) =>
   api.rpc.chain.getBlockHash(blockId)
 
-interface BlockEvent {
-  section: string
-  method: string
-  data: string
-}
-
 const processEvents = async (api: Api, blockId: number) => {
   const blockHash = await getBlockHash(api, blockId)
   const blockEvents = await api.query.system.events.at(blockHash)
-  blockEvents.forEach(({ section, method, data }: BlockEvent) => {
-    console.log(`event`, section, method, data)
+  blockEvents.forEach(({ event }: EventRecord) => {
+    let { section, method, data } = event
     Event.create({ blockId, section, method, data: JSON.stringify(data) })
   })
 }
 
+const updateAccount = async (api: Api, account: string) => {}
+
 const updateEra = async (api: Api, io: any, status: any, era: number) => {
   const now: number = moment().valueOf()
   if (lastUpdate + 60000 > now) return status