Skip to content

Commit 8c24d85

Browse files
committed
Test for Bytes as ID and block/tx data
1 parent d0b05a3 commit 8c24d85

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

src/scaffold/ethereum.test.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ dataSources:
121121
test('Schema (default)', () => {
122122
expect(scaffold.generateSchema()).toEqual(`\
123123
type ExampleEntity @entity {
124-
id: ID!
124+
id: Bytes!
125125
count: BigInt!
126126
a: BigInt! # uint256
127127
b: [Bytes]! # bytes[4]
@@ -131,8 +131,10 @@ type ExampleEntity @entity {
131131

132132
test('Schema (for indexing events)', () => {
133133
expect(scaffoldWithIndexEvents.generateSchema()).toEqual(`\
134-
type ExampleEvent @entity {
135-
id: ID!
134+
type ExampleEvent @entity(immutable: true) {
135+
id: Bytes!
136+
137+
# Event params
136138
a: BigInt! # uint256
137139
b: [Bytes]! # bytes[4]
138140
param2: String! # string
@@ -143,11 +145,27 @@ type ExampleEvent @entity {
143145
c_c3_value1: String! # string
144146
c_c3_value2: Bytes! # bytes32
145147
d: String! # string
148+
149+
# Block & transaction info
150+
blockNumber: BigInt!
151+
blockHash: Bytes!
152+
blockTimestamp: BigInt!
153+
transactionHash: Bytes!
154+
logIndex: BigInt!
146155
}
147156
148-
type ExampleEvent1 @entity {
149-
id: ID!
157+
type ExampleEvent1 @entity(immutable: true) {
158+
id: Bytes!
159+
160+
# Event params
150161
a: Bytes! # bytes32
162+
163+
# Block & transaction info
164+
blockNumber: BigInt!
165+
blockHash: Bytes!
166+
blockTimestamp: BigInt!
167+
transactionHash: Bytes!
168+
logIndex: BigInt!
151169
}
152170
`)
153171
})
@@ -165,12 +183,12 @@ import { ExampleEntity } from "../generated/schema"
165183
export function handleExampleEvent(event: ExampleEvent): void {
166184
// Entities can be loaded from the store using a string ID; this ID
167185
// needs to be unique across all entities of the same type
168-
let entity = ExampleEntity.load(event.transaction.from.toHex())
186+
let entity = ExampleEntity.load(event.transaction.from)
169187
170188
// Entities only exist after they have been saved to the store;
171189
// \`null\` checks allow to create entities on demand
172190
if (!entity) {
173-
entity = new ExampleEntity(event.transaction.from.toHex())
191+
entity = new ExampleEntity(event.transaction.from)
174192
175193
// Entity fields can be set using simple assignments
176194
entity.count = BigInt.fromI32(0)
@@ -219,7 +237,7 @@ import { ExampleEvent, ExampleEvent1 } from "../generated/schema"
219237
220238
export function handleExampleEvent(event: ExampleEventEvent): void {
221239
let entity = new ExampleEvent(
222-
event.transaction.hash.toHex() + "-" + event.logIndex.toString()
240+
event.transaction.hash.concatI32(event.logIndex.toI32()),
223241
)
224242
entity.a = event.params.a
225243
entity.b = event.params.b
@@ -231,14 +249,28 @@ export function handleExampleEvent(event: ExampleEventEvent): void {
231249
entity.c_c3_value1 = event.params.c.c3.value1
232250
entity.c_c3_value2 = event.params.c.c3.value2
233251
entity.d = event.params.d
252+
253+
entity.blockNumber = event.block.number
254+
entity.blockHash = event.block.hash
255+
entity.blockTimestamp = event.block.timestamp
256+
entity.transactionHash = event.transaction.hash
257+
entity.logIndex = event.logIndex
258+
234259
entity.save()
235260
}
236261
237262
export function handleExampleEvent1(event: ExampleEvent1Event): void {
238263
let entity = new ExampleEvent1(
239-
event.transaction.hash.toHex() + "-" + event.logIndex.toString()
264+
event.transaction.hash.concatI32(event.logIndex.toI32()),
240265
)
241266
entity.a = event.params.a
267+
268+
entity.blockNumber = event.block.number
269+
entity.blockHash = event.block.hash
270+
entity.blockTimestamp = event.block.timestamp
271+
entity.transactionHash = event.transaction.hash
272+
entity.logIndex = event.logIndex
273+
242274
entity.save()
243275
}
244276
`)

0 commit comments

Comments
 (0)