Skip to content

Commit

Permalink
fix(hydra-cli): update scaffold templates (Joystream#383)
Browse files Browse the repository at this point in the history
affects: @dzlzv/hydra-cli
  • Loading branch information
dzhelezov committed May 7, 2021
1 parent 7348f03 commit 427625a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions packages/hydra-cli/src/templates/scaffold/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ mappings:
- mappings/lib/mappings/generated/types
eventHandlers:
- event: balances.Transfer
handler: balancesTransfer(DatabaseManager, Balances.TransferEvent)
handler: balancesTransfer
extrinsicHandlers:
# infer defaults
- extrinsic: timestamp.set
handler: timestampCall(DatabaseManager, Timestamp.SetCall)
handler: timestampCall
preBlockHooks:
postBlockHooks:

66 changes: 32 additions & 34 deletions packages/hydra-cli/src/templates/scaffold/mappings/mappings.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
import { DatabaseManager } from '@dzlzv/hydra-db-utils'
import { Transfer, BlockTimestamp } from '../generated/graphql-server/model'

// run 'NODE_URL=<RPC_ENDPOINT> EVENTS=<comma separated list of events> yarn codegen:mappings-types'
// to genenerate typescript classes for events, such as Balances.TransferEvent
import { Balances, Timestamp } from './generated/types'
import BN from 'bn.js'
import {
ExtrinsicContext,
EventContext,
StoreContext,
} from '@dzlzv/hydra-common'

const start = Date.now()
let total = 0

export async function balancesTransfer(
db: DatabaseManager,
event: Balances.TransferEvent
) {
export async function balancesTransfer({
store,
event,
block,
extrinsic,
}: EventContext & StoreContext) {
const transfer = new Transfer()
transfer.from = Buffer.from(event.params[0].toHex())
transfer.to = Buffer.from(event.params[1].toHex())
transfer.value = event.params[2].toBn()
transfer.block = event.ctx.blockNumber
transfer.comment = `Transferred ${transfer.value} from ${transfer.from} to ${transfer.to}`
transfer.insertedAt = new Date()
await db.save<Transfer>(transfer)
}
const [from, to, value] = new Balances.TransferEvent(event).params
transfer.from = Buffer.from(from.toHex())
transfer.to = Buffer.from(to.toHex())
transfer.value = value.toBn()
transfer.tip = extrinsic ? new BN(extrinsic.tip.toString(10)) : new BN(0)
transfer.insertedAt = new Date(block.timestamp)

export async function timestampCall(
db: DatabaseManager,
call: Timestamp.SetCall
) {
benchmarkExtrinsics()
const block = new BlockTimestamp()
block.timestamp = call.args.now.toBn()
block.blockNumber = new BN(call.ctx.blockNumber)
console.log(`New block ${block.blockNumber} at ${block.timestamp}`)

await db.save<BlockTimestamp>(block)
transfer.block = block.height
transfer.comment = `Transferred ${transfer.value} from ${transfer.from} to ${transfer.to}`
transfer.timestamp = new BN(block.timestamp)
console.log(`Saving transfer: ${JSON.stringify(transfer, null, 2)}`)
await store.save<Transfer>(transfer)
}

function benchmarkExtrinsics() {
const millis = Date.now() - start
total = total + 1
if (total % 10 === 0) {
console.log(`seconds elapsed = ${Math.floor(millis / 1000)}`)
console.log(`Everage time ms: ${millis / total}, total events: ${total}`)
}
export async function timestampCall({
store,
event,
block,
}: ExtrinsicContext & StoreContext) {
const call = new Timestamp.SetCall(event)
const blockT = new BlockTimestamp()
blockT.timestamp = call.args.now.toBn()
blockT.blockNumber = block.height
await store.save<BlockTimestamp>(blockT)
}
6 changes: 4 additions & 2 deletions packages/hydra-cli/src/templates/scaffold/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ type Transfer @entity {
value: BigInt!
comment: String @fulltext(query: "commentSearch")
block: Int!
tip: BigInt!
timestamp: BigInt!
insertedAt: DateTime!
}

" Tracks block timestamps "
type BlockTimestamp @entity {
blockNumber: BigInt!
blockNumber: Int!
timestamp: BigInt!
}
}

0 comments on commit 427625a

Please sign in to comment.