Skip to content

Commit 0ff8dcc

Browse files
committed
feat: track additional evm transaction properties (gasUsed, gasPrice, gasLimit, value, nonce, type)
1 parent 5403744 commit 0ff8dcc

File tree

9 files changed

+199
-165
lines changed

9 files changed

+199
-165
lines changed

packages/fasset-indexer-core/src/indexer/eventlib/event-parser.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,10 @@ import { AgentVault } from "../../orm/entities/agent"
22
import { Context } from "../../context/context"
33
import type { EntityManager } from "@mikro-orm/knex"
44
import type { Log } from "ethers"
5-
import type { Event } from "./event-scraper"
65
import type { FAssetIface } from "../../shared"
6+
import type { Block, Transaction, Event } from "./types"
77

88

9-
interface Block {
10-
index: number
11-
timestamp: number
12-
}
13-
14-
interface Transaction {
15-
hash: string
16-
source: string
17-
target: string | null
18-
}
199

2010
export class EventParser {
2111
private topicToIface = new Map<string, FAssetIface>()
@@ -27,7 +17,8 @@ export class EventParser {
2717
}
2818

2919
async logToEvent(log: Log): Promise<Event | null> {
30-
const iface = this.topicToIface.get(log.topics[0])
20+
const topic = log.topics[0]
21+
const iface = this.topicToIface.get(topic)
3122
if (iface == null) return null
3223
const valid = await this.validLogSource(iface, log.address)
3324
if (!valid) return null
@@ -37,16 +28,12 @@ export class EventParser {
3728
const transaction = await this.getTransaction(log.transactionHash)
3829
return {
3930
name: parsed.name,
31+
topic,
4032
args: parsed.args,
41-
blockNumber: log.blockNumber,
42-
transactionIndex: log.transactionIndex,
43-
logIndex: log.index,
4433
source: log.address,
45-
blockTimestamp: block.timestamp,
46-
transactionHash: log.transactionHash,
47-
transactionSource: transaction.source,
48-
transactionTarget: transaction.target,
49-
topic: log.topics[0]
34+
index: log.index,
35+
transaction: transaction,
36+
block: block,
5037
}
5138
}
5239

@@ -107,7 +94,7 @@ export class EventParser {
10794
throw new Error(`Failed to fetch block ${blockNumber}`)
10895
}
10996
this.blockCache = {
110-
index: blockNumber,
97+
index: block.number,
11198
timestamp: block.timestamp
11299
}
113100
}
@@ -117,13 +104,21 @@ export class EventParser {
117104
protected async getTransaction(transactionHash: string): Promise<Transaction> {
118105
if (this.transactionCache === null || this.transactionCache.hash !== transactionHash) {
119106
const transaction = await this.context.provider.getTransaction(transactionHash)
120-
if (transaction === null) {
107+
const receipt = await this.context.provider.getTransactionReceipt(transactionHash)
108+
if (transaction == null || receipt == null) {
121109
throw new Error(`Failed to fetch transaction ${transactionHash}`)
122110
}
123111
this.transactionCache = {
124112
hash: transactionHash,
125113
source: transaction.from,
126-
target: transaction.to
114+
target: transaction.to ?? receipt.to,
115+
gasUsed: receipt.gasUsed,
116+
gasPrice: transaction.gasPrice,
117+
gasLimit: transaction.gasLimit,
118+
type: transaction.type,
119+
value: transaction.value,
120+
nonce: transaction.nonce,
121+
index: transaction.index
127122
}
128123
}
129124
return this.transactionCache

packages/fasset-indexer-core/src/indexer/eventlib/event-scraper.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,6 @@ import type { Context } from "../../context/context"
22
import type { Log } from "ethers"
33

44

5-
export type EventArgs = any
6-
7-
export interface Event {
8-
topic: string
9-
name: string
10-
args: EventArgs
11-
blockNumber: number
12-
transactionIndex: number
13-
logIndex: number
14-
source: string
15-
blockTimestamp: number
16-
transactionHash: string
17-
transactionSource: string
18-
transactionTarget: string | null
19-
}
20-
215
interface LogUri {
226
blockNumber: number
237
transactionIndex: number

packages/fasset-indexer-core/src/indexer/eventlib/event-storer.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { AssetManagerEventMigration } from "./migrations/asset-manager-migration
77
import { EVENTS } from '../../config/constants'
88
import type { EntityManager } from "@mikro-orm/knex"
99
import type { ORM } from "../../orm/interface"
10-
import type { Event } from "./event-scraper"
10+
import type { Event } from "./types"
1111
import type * as AssetManager from "../../../chain/typechain/assetManager/IAssetManager__latest"
1212
import type * as CollateralPool from "../../../chain/typechain/collateralPool/ICollateralPool__latest"
1313
import type * as CollateralPoolPreUpgrade from "../../../chain/typechain/collateralPool/ICollateralPool__initial"
@@ -313,8 +313,8 @@ export class EventStorer {
313313
}
314314

315315
protected async logExists(em: EntityManager, log: Event): Promise<boolean> {
316-
const { blockNumber, logIndex } = log
317-
const evmLog = await em.findOne(Entities.EvmLog, { index: logIndex, block: { index: blockNumber }})
316+
const { block: { index: blockIndex }, index } = log
317+
const evmLog = await em.findOne(Entities.EvmLog, { index, block: { index: blockIndex }})
318318
return evmLog !== null
319319
}
320320

@@ -1520,19 +1520,16 @@ export class EventStorer {
15201520
// will not persist new transactions, blocks, and events if the latter will not be successfully processed
15211521
// we do have to persist addresses as they can be refereced during event processing
15221522
private async createLogEntity(em: EntityManager, log: Event): Promise<Entities.EvmLog> {
1523-
const transactionSource = await findOrCreateEntity(em, Entities.EvmAddress, { hex: log.transactionSource })
1524-
const transactionTarget = log.transactionTarget === null ? undefined
1525-
: await findOrCreateEntity(em, Entities.EvmAddress, { hex: log.transactionTarget })
1523+
const source = await findOrCreateEntity(em, Entities.EvmAddress, { hex: log.transaction.source })
1524+
const target = log.transaction.target === null ? undefined
1525+
: await findOrCreateEntity(em, Entities.EvmAddress, { hex: log.transaction.target })
15261526
const block = await findOrCreateEntity(em, Entities.EvmBlock,
1527-
{ index: log.blockNumber }, { persist: false }, { timestamp: log.blockTimestamp })
1527+
{ index: log.block.index }, { persist: false }, { timestamp: log.block.timestamp })
15281528
const transaction = await findOrCreateEntity(em, Entities.EvmTransaction,
1529-
{ hash: log.transactionHash }, { persist: false },
1530-
{ block, index: log.transactionIndex, source: transactionSource, target: transactionTarget }
1531-
)
1532-
const eventSource = await findOrCreateEntity(em, Entities.EvmAddress, { hex: log.source })
1533-
return em.create(Entities.EvmLog, {
1534-
index: log.logIndex, name: log.name, address: eventSource, transaction, block
1535-
}, { persist: false })
1529+
{ hash: log.transaction.hash }, { persist: false }, { block, ...log.transaction, source, target })
1530+
const address = await findOrCreateEntity(em, Entities.EvmAddress, { hex: log.source })
1531+
return em.create(Entities.EvmLog,
1532+
{ index: log.index, name: log.name, address, transaction, block }, { persist: false })
15361533
}
15371534

15381535
private async changeTokenBalance(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export interface Block {
2+
index: number
3+
timestamp: number
4+
}
5+
6+
export interface Transaction {
7+
hash: string
8+
index: number
9+
source: string
10+
nonce: number
11+
target: string | null
12+
gasUsed: bigint
13+
gasPrice: bigint
14+
gasLimit: bigint
15+
value: bigint
16+
type: number
17+
}
18+
19+
export type EventArgs = any
20+
21+
export interface Event {
22+
topic: string
23+
name: string
24+
args: EventArgs
25+
source: string
26+
index: number
27+
transaction: Transaction
28+
block: Block
29+
}

packages/fasset-indexer-core/src/indexer/indexer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ export class EventIndexer {
6565
for (const log of logs) {
6666
const fullLog = await this.eventParser.logToEvent(log)
6767
if (fullLog != null) {
68-
logger.info(`event indexer is processing event ${fullLog.name} (block: ${fullLog.blockNumber}, log: ${fullLog.logIndex})`)
68+
logger.info(`event indexer is processing event ${fullLog.name} (block: ${fullLog.block.index}, log: ${fullLog.index})`)
6969
const processed = await this.stateUpdater.processEvent(fullLog)
70-
logger.info(`event indexer ${processed ? 'stored' : 'skipped storing'} event (${fullLog.blockNumber}, ${fullLog.logIndex})`)
70+
logger.info(`event indexer ${processed ? 'stored' : 'skipped storing'} event (${fullLog.block.index}, ${fullLog.index})`)
7171
}
7272
}
7373
}

packages/fasset-indexer-core/src/orm/entities/evm/transaction.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Entity, PrimaryKey, Property, Unique, ManyToOne } from "@mikro-orm/core
22
import { EvmAddress } from "./address"
33
import { EvmBlock } from "./block"
44
import { BYTES32_LENGTH } from "../../../config/constants"
5+
import { uint256 } from "../../custom/uint"
56

67

78
@Entity()
@@ -25,4 +26,22 @@ export class EvmTransaction {
2526

2627
@ManyToOne({ entity: () => EvmAddress, nullable: true })
2728
target?: EvmAddress
29+
30+
@Property({ type: new uint256(), nullable: true })
31+
value!: bigint
32+
33+
@Property({ type: new uint256(), nullable: true })
34+
gasUsed!: bigint
35+
36+
@Property({ type: new uint256(), nullable: true })
37+
gasPrice!: bigint
38+
39+
@Property({ type: new uint256(), nullable: true })
40+
gasLimit!: bigint
41+
42+
@Property({ type: 'number', nullable: true })
43+
type!: number
44+
45+
@Property({ type: 'number', nullable: true })
46+
nonce!: number
2847
}

0 commit comments

Comments
 (0)