Skip to content

Commit 34aee49

Browse files
committed
Implement Bytes as ID, block/tx data and @immutable
1 parent 8c24d85 commit 34aee49

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

src/protocols/ethereum/scaffold/mapping.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ const generatePlaceholderHandlers = ({ abi, events, contractName }) =>
1414
export function handle${event._alias}(event: ${event._alias}): void {
1515
// Entities can be loaded from the store using a string ID; this ID
1616
// needs to be unique across all entities of the same type
17-
let entity = ExampleEntity.load(event.transaction.from.toHex())
17+
let entity = ExampleEntity.load(event.transaction.from)
1818
1919
// Entities only exist after they have been saved to the store;
2020
// \`null\` checks allow to create entities on demand
2121
if (!entity) {
22-
entity = new ExampleEntity(event.transaction.from.toHex())
22+
entity = new ExampleEntity(event.transaction.from)
2323
2424
// Entity fields can be set using simple assignments
2525
entity.count = BigInt.fromI32(0)

src/scaffold/ethereum.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ type ExampleEvent @entity(immutable: true) {
145145
c_c3_value1: String! # string
146146
c_c3_value2: Bytes! # bytes32
147147
d: String! # string
148-
149148
# Block & transaction info
150149
blockNumber: BigInt!
151150
blockHash: Bytes!
@@ -159,7 +158,6 @@ type ExampleEvent1 @entity(immutable: true) {
159158
160159
# Event params
161160
a: Bytes! # bytes32
162-
163161
# Block & transaction info
164162
blockNumber: BigInt!
165163
blockHash: Bytes!
@@ -237,7 +235,7 @@ import { ExampleEvent, ExampleEvent1 } from "../generated/schema"
237235
238236
export function handleExampleEvent(event: ExampleEventEvent): void {
239237
let entity = new ExampleEvent(
240-
event.transaction.hash.concatI32(event.logIndex.toI32()),
238+
event.transaction.hash.concatI32(event.logIndex.toI32())
241239
)
242240
entity.a = event.params.a
243241
entity.b = event.params.b
@@ -261,7 +259,7 @@ export function handleExampleEvent(event: ExampleEventEvent): void {
261259
262260
export function handleExampleEvent1(event: ExampleEvent1Event): void {
263261
let entity = new ExampleEvent1(
264-
event.transaction.hash.concatI32(event.logIndex.toI32()),
262+
event.transaction.hash.concatI32(event.logIndex.toI32())
265263
)
266264
entity.a = event.params.a
267265

src/scaffold/mapping.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ const generateEventIndexingHandlers = (events, contractName) =>
3232
export function handle${event._alias}(event: ${event._alias}Event): void {
3333
let entity = new ${
3434
event._alias
35-
}(event.transaction.hash.toHex() + '-' + event.logIndex.toString())
35+
}(event.transaction.hash.concatI32(event.logIndex.toI32()))
3636
${generateEventFieldAssignments(event).join('\n')}
37+
38+
entity.blockNumber = event.block.number
39+
entity.blockHash = event.block.hash
40+
entity.blockTimestamp = event.block.timestamp
41+
entity.transactionHash = event.transaction.hash
42+
entity.logIndex = event.logIndex
43+
3744
entity.save()
3845
}
3946
`,

src/scaffold/schema.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,59 @@ const generateEventFields = ({ index, input, protocolName }) =>
2020
input.type == 'tuple'
2121
? util
2222
.unrollTuple({ value: input, path: [input.name || `param${index}`], index })
23-
.map(({ path, type }) => generateField({ name: path.join('_'), type, protocolName }))
24-
: [generateField({ name: input.name || `param${index}`, type: input.type, protocolName })]
23+
.map(({ path, type }) =>
24+
generateField({ name: path.join('_'), type, protocolName }),
25+
)
26+
: [
27+
generateField({
28+
name: input.name || `param${index}`,
29+
type: input.type,
30+
protocolName,
31+
}),
32+
]
33+
34+
const generateEventType = (event, protocolName) => `type ${
35+
event._alias
36+
} @entity(immutable: true) {
37+
id: Bytes!
2538
26-
const generateEventType = (event, protocolName) => `type ${event._alias} @entity {
27-
id: ID!
39+
# Event params
2840
${event.inputs
2941
.reduce(
30-
(acc, input, index) => acc.concat(generateEventFields({ input, index, protocolName })),
42+
(acc, input, index) =>
43+
acc.concat(generateEventFields({ input, index, protocolName })),
3144
[],
3245
)
33-
.join('\n')}
46+
.join('\n')}
47+
# Block & transaction info
48+
blockNumber: BigInt!
49+
blockHash: Bytes!
50+
blockTimestamp: BigInt!
51+
transactionHash: Bytes!
52+
logIndex: BigInt!
3453
}`
3554

3655
const generateExampleEntityType = (protocol, events) => {
37-
if (protocol.hasABIs() && events.length > 0) {
38-
return `type ExampleEntity @entity {
39-
id: ID!
56+
if (protocol.hasABIs() && events.length > 0) {
57+
return `type ExampleEntity @entity {
58+
id: Bytes!
4059
count: BigInt!
4160
${events[0].inputs
42-
.reduce((acc, input, index) => acc.concat(generateEventFields({ input, index, protocolName: protocol.name })), [])
61+
.reduce(
62+
(acc, input, index) =>
63+
acc.concat(generateEventFields({ input, index, protocolName: protocol.name })),
64+
[],
65+
)
4366
.slice(0, 2)
4467
.join('\n')}
4568
}`
46-
} else {
47-
return `type ExampleEntity @entity {
69+
} else {
70+
return `type ExampleEntity @entity {
4871
id: ID!
4972
block: Bytes!
5073
count: BigInt!
5174
}`
52-
}
75+
}
5376
}
5477

5578
module.exports = {

0 commit comments

Comments
 (0)