Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bridge #167

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = class Data1714035126926 {
name = 'Data1714035126926'
module.exports = class Data1712051797737 {
name = 'Data1712051797737'

async up(db) {
await db.query(`DROP INDEX "public"."IDX_1ba28b188c45fa806f1cb9c039"`)
Expand Down
8 changes: 8 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ export const eventNames = [
'Kensetsu.DebtPayment',
'Kensetsu.Liquidated',
'Kensetsu.CDPClosed',
'XcmPallet.Attempted',
'SubstrateBridgeOutboundChannel.MessageAccepted',
'SubstrateDispatch.MessageDispatched',
'BridgeProxy.RequestStatusUpdate',
'ParachainBridgeApp.Minted',
'ParachainBridgeApp.Burned',
'system.ExtrinsicSuccess',
'system.ExtrinsicFailed'
] as const

export const versionsWithStringAssetId = ['1', '1Stage', '1Test', '2', '2Stage', '2Test', '3', '3Stage', '3Test', '4', '4Stage', '4Test', '5', '5Stage', '5Test', '6', '6Stage', '6Test', '7', '7Stage', '7Test', '8', '8Stage', '8Test', '9', '9Stage', '9Test', '10', '10Stage', '10Test', '11', '11Stage', '11Test', '12', '12Stage', '12Test', '13', '13Stage', '13Test', '14', '14Stage', '14Test', '15', '15Stage', '15Test', '16', '16Stage', '16Test', '17', '17Stage', '17Test', '18', '18Stage', '18Test', '19', '19Stage', '19Test', '20', '20Stage', '20Test', '21', '21Stage', '21Test', '22', '22Stage', '22Test', '23', '23Stage', '23Test', '24', '24Stage', '24Test', '25', '25Stage', '25Test', '26', '26Stage', '26Test', '27', '27Stage', '27Test', '28', '28Stage', '28Test', '29', '29Stage', '29Test', '30', '30Stage', '30Test', '31', '31Stage', '31Test', '32', '32Stage', '32Test', '33', '33Stage', '33Test'] as const
163 changes: 163 additions & 0 deletions src/handlers/events/bridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { BlockContext, Event } from '../../types'
import { getEventData } from '../../utils/entities'
import { events } from '../../types/generated/merged'
import { createEventHistoryElement } from '../../utils/history'
import { logStartProcessingEvent } from '../../utils/logs'


export async function xcmPalletAttemptedHandler(
ctx: BlockContext,
event: Event<'XcmPallet.Attempted'>
): Promise<void> {
ctx.log.info('start indexing XcmPallet.Attempted')
await logStartProcessingEvent(ctx, event)

// const type = events.xcmPallet.attempted
// const data = getEventData(ctx, type, event)
}

export async function messageAcceptedHandler(
ctx: BlockContext,
event: Event<'SubstrateBridgeOutboundChannel.MessageAccepted'>
): Promise<void> {
await logStartProcessingEvent(ctx, event)

const type = events.substrateBridgeOutboundChannel.messageAccepted
const data = getEventData(ctx, type, event)

const networkId = 'networkId' in data ? data.networkId : data[0]
const batchNonce = 'batchNonce' in data ? data.batchNonce.toString() : null
const messageNonce = 'messageNonce' in data ? data.messageNonce.toString() : null

const historyData = {
networkId: networkId.__kind,
batchNonce,
messageNonce
}

await createEventHistoryElement(ctx, event, undefined, historyData)
}



export async function systemExtrinsicFailedHandler(
ctx: BlockContext,
event: Event<'system.ExtrinsicFailed'>
): Promise<void> {
await logStartProcessingEvent(ctx, event)

const type = events.system.extrinsicFailed
const data = getEventData(ctx, type, event)

const dispatchError = 'dispatchError' in data ? data.dispatchError : data[0]
const dispatchInfo = 'dispatchInfo' in data ? data.dispatchInfo : data[1]

const classInfo = dispatchInfo?.class
const paysFee = dispatchInfo?.paysFee
const weight = dispatchInfo?.weight

const proofSize = typeof weight === 'object' ? weight.proofSize.toString() : null
const refTime = typeof weight === 'object' ? weight.refTime.toString() : null

const historyData: Record<string, any> = {
dispatchError,
classInfo,
paysFee,
proofSize,
refTime
}

if (typeof weight !== 'object') historyData.weight = weight.toString()

await createEventHistoryElement(ctx, event, undefined, historyData)
}

export async function systemExtrinsicSuccessHandler(
ctx: BlockContext,
event: Event<'system.ExtrinsicSuccess'>
): Promise<void> {
await logStartProcessingEvent(ctx, event)

const type = events.system.extrinsicSuccess
const data = getEventData(ctx, type, event)

const dispatchInfo = 'dispatchInfo' in data ? data.dispatchInfo : data

const classInfo = dispatchInfo?.class
const paysFee = dispatchInfo?.paysFee
const weight = dispatchInfo?.weight

const proofSize = typeof weight === 'object' ? weight.proofSize.toString() : null
const refTime = typeof weight === 'object' ? weight.refTime.toString() : null

const historyData: Record<string, any> = {
classInfo,
paysFee,
proofSize,
refTime
}

if (typeof weight !== 'object') historyData.weight = weight.toString()

await createEventHistoryElement(ctx, event, undefined, historyData)
}

export async function messageDispatchedHandler(
ctx: BlockContext,
event: Event<'SubstrateDispatch.MessageDispatched'>
): Promise<void> {
await logStartProcessingEvent(ctx, event)

const type = events.substrateDispatch.messageDispatched
const data = getEventData(ctx, type, event)

const messageId = data[0]
const result = data[1]

const sender = 'sender' in messageId ? messageId.sender.value : null
const receiver = 'receiver' in messageId ? messageId.receiver.value : null
const batchNonce = 'batchNonce' in messageId ? messageId.batchNonce?.toString() ?? null : null
const messageNonce = 'messageNonce' in messageId ? messageId.messageNonce?.toString() : null
const direction = 'direction' in messageId ? messageId.direction?.toString() : null

const historyData: Record<string, any> = {
sender,
receiver,
batchNonce,
messageNonce,
result,
direction
}

await createEventHistoryElement(ctx, event, undefined, historyData)
}

export async function requestStatusUpdateHandler(
ctx: BlockContext,
event: Event<'BridgeProxy.RequestStatusUpdate'>
): Promise<void> {
logStartProcessingEvent(ctx, event)

// const type = events.bridgeProxy.requestStatusUpdate
// const data = getEventData(ctx, type, event)
}

export async function mintedHandler(
ctx: BlockContext,
event: Event<'ParachainBridgeApp.Minted'>
): Promise<void> {
await logStartProcessingEvent(ctx, event)

// const type = events.parachainBridgeApp.minted
// const data = getEventData(ctx, type, event)
}

export async function burnedHandler(
ctx: BlockContext,
event: Event<'ParachainBridgeApp.Burned'>
): Promise<void> {
await logStartProcessingEvent(ctx, event)

// const type = events.parachainBridgeApp.burned
// const data = getEventData(ctx, type, event)
}
14 changes: 14 additions & 0 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ import {
orderBookMarketOrderExecutedEventHandler,
orderBookStatusChangedEventHandler
} from './handlers/events/orderBook'
import {
xcmPalletAttemptedHandler,
messageAcceptedHandler,
messageDispatchedHandler,
requestStatusUpdateHandler,
mintedHandler,
burnedHandler
} from './handlers/events/bridge'
import { orderBookCancelLimitOrderCallHandler } from './handlers/calls/orderBook/cancelLimitOrder'
import { getSortedItems } from './utils/processor'
import { bandRelayCallHandler } from './handlers/calls/band/relay'
Expand Down Expand Up @@ -268,6 +276,12 @@ processor.run(new TypeormDatabase({ supportHotBlocks: false }), async (ctx) => {
if (event.name === 'Kensetsu.DebtPayment') await vaultDebtPaymentEvent(blockContext, event)
if (event.name === 'Kensetsu.Liquidated') await vaultLiquidatedEvent(blockContext, event)
if (event.name === 'Kensetsu.CDPClosed') await vaultClosedEvent(blockContext, event)
if (event.name === 'XcmPallet.Attempted') await xcmPalletAttemptedHandler(blockContext, event)
if (event.name === 'SubstrateBridgeOutboundChannel.MessageAccepted') await messageAcceptedHandler(blockContext, event)
if (event.name === 'SubstrateDispatch.MessageDispatched') await messageDispatchedHandler(blockContext, event)
if (event.name === 'BridgeProxy.RequestStatusUpdate') await requestStatusUpdateHandler(blockContext, event)
if (event.name === 'ParachainBridgeApp.Minted') await mintedHandler(blockContext, event)
if (event.name === 'ParachainBridgeApp.Burned') await burnedHandler(blockContext, event)
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/utils/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ export const createHistoryElement = async (
success,
error,
})
} else {
historyElement.execution = new ExecutionResult({
success: true,
})
}

if (data) {
Expand Down Expand Up @@ -235,7 +239,7 @@ export const createCallHistoryElement = async (
export const createEventHistoryElement = async (
ctx: BlockContext,
event: Event<any>,
address: Address,
address?: Address,
data?: {},
calls?: HistoryElementCall[]
) => {
Expand Down