Skip to content

Commit

Permalink
feat: track oracle transactions (#170)
Browse files Browse the repository at this point in the history
* feat: set up oracle feed tracker

* feat: set up oracle service and handler

* fix: schema

* fix: typo

* fix: redundant to big int

* fix: handler
  • Loading branch information
hieronx authored May 30, 2024
1 parent 15b78d1 commit 81b2736
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
6 changes: 5 additions & 1 deletion chains-cfg/_root.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,8 @@ dataSources:
kind: substrate/EventHandler
filter:
module: poolFees
method: Paid
method: Paid
- kind: substrate/EventHandler
filter:
module: oracleFeed
method: Fed
8 changes: 8 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ type AssetTransaction @entity {
toAsset: Asset
}

type OracleTransaction @entity {
id: ID! # extrinsic hash - timestamp - oracle key
timestamp: Date!
# feeder: Account! @index
key: String!
value: BigInt!
}

type Account @entity {
id: ID!
chain: Blockchain!
Expand Down
6 changes: 6 additions & 0 deletions src/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ export interface PoolFee extends Struct {
amounts: PoolFeeAmounts
}

export interface OracleKey extends Enum {
isin: string
}

export type LoanAsset = ITuple<[collectionId: u64, itemId: u128]>
export type LoanCreatedEvent = ITuple<[poolId: u64, loanId: u64, loanInfo: LoanInfoCreated]>
export type LoanClosedEvent = ITuple<[poolId: u64, loanId: u64, collateralInfo: LoanAsset]>
Expand Down Expand Up @@ -454,6 +458,8 @@ export type PoolFeesUnchargedEvent = PoolFeesChargedEvent
export type PoolFeesPaidEvent = ITuple<[poolId: u64, feeId: u64, amount: u128, destination: AccountId32]>
export type PoolFeesList = Vec<PoolFeesOfBucket>

export type OracleFedEvent = ITuple<[feeder: AccountId32, key: OracleKey, value: u128]>

export type ExtendedRpc = typeof api.rpc & {
pools: {
trancheTokenPrice: PromiseRpcResult<
Expand Down
19 changes: 19 additions & 0 deletions src/mappings/handlers/oracleHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { SubstrateEvent } from '@subql/types'
import { OracleFedEvent } from '../../helpers/types'
import { errorHandler } from '../../helpers/errorHandler'
import { OracleTransactionData, OracleTransactionService } from '../services/oracleTransactionService'

export const handleOracleFed = errorHandler(_handleOracleFed)
async function _handleOracleFed(event: SubstrateEvent<OracleFedEvent>) {
const [feeder, key, value] = event.event.data
logger.info(`Oracle feed: ${feeder.toString()} key: ${key.toString()} value: ${value.toString()}`)

const oracleTxData: OracleTransactionData = {
hash: event.extrinsic.extrinsic.hash.toString(),
timestamp: event.block.timestamp,
key: key.toString(),
value: value.toBigInt(),
}

OracleTransactionService.init(oracleTxData)
}
25 changes: 25 additions & 0 deletions src/mappings/services/oracleTransactionService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { OracleTransaction } from '../../types'

export interface OracleTransactionData {
readonly hash: string
readonly timestamp: Date
readonly key: string
readonly value?: bigint
}

export class OracleTransactionService extends OracleTransaction {
static init = (data: OracleTransactionData) => {
const tx = new this(
`${data.hash}-${data.timestamp.toString()}-${data.key.toString()}`,
data.timestamp,
data.key.toString(),
data.value
)

tx.timestamp = data.timestamp ?? null
tx.key = data.key ?? null
tx.value = data.value ?? null

return tx
}
}

0 comments on commit 81b2736

Please sign in to comment.