Skip to content

Commit 299c75b

Browse files
Fix generated example entity id uniqueness (#1835)
* fix generated example entity id uniqueness * exclude renovate.json from prettier
1 parent e2e5139 commit 299c75b

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

.changeset/swift-jobs-count.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': patch
3+
---
4+
5+
fix generated example entity id uniqueness

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ packages/cli/tests/cli/add
88
packages/cli/config/TheGraphNetworksRegistry.json
99
pnpm-lock.yaml
1010
website/src/graphql-env.d.ts
11+
renovate.json

packages/cli/src/protocols/ethereum/scaffold/mapping.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const generatePlaceholderHandlers = ({
1111
contractName: string;
1212
}) =>
1313
`
14-
import { BigInt } from '@graphprotocol/graph-ts'
14+
import { BigInt, Bytes } from '@graphprotocol/graph-ts'
1515
import { ${contractName}, ${events.map(event => event._alias)} }
1616
from '../generated/${contractName}/${contractName}'
1717
import { ExampleEntity } from '../generated/schema'
@@ -21,14 +21,15 @@ export const generatePlaceholderHandlers = ({
2121
index === 0
2222
? `
2323
export function handle${event._alias}(event: ${event._alias}): void {
24-
// Entities can be loaded from the store using a string ID; this ID
24+
// Entities can be loaded from the store using an ID; this ID
2525
// needs to be unique across all entities of the same type
26-
let entity = ExampleEntity.load(event.transaction.from)
26+
const id = event.transaction.hash.concat(Bytes.fromByteArray(Bytes.fromBigInt(event.logIndex)));
27+
let entity = ExampleEntity.load(id)
2728
2829
// Entities only exist after they have been saved to the store;
2930
// \`null\` checks allow to create entities on demand
3031
if (!entity) {
31-
entity = new ExampleEntity(event.transaction.from)
32+
entity = new ExampleEntity(id)
3233
3334
// Entity fields can be set using simple assignments
3435
entity.count = BigInt.fromI32(0)

packages/cli/src/scaffold/__snapshots__/ethereum.test.ts.snap

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dataSources:
3434
`;
3535

3636
exports[`Ethereum subgraph scaffolding > Mapping (default) 1`] = `
37-
"import { BigInt } from "@graphprotocol/graph-ts"
37+
"import { BigInt, Bytes } from "@graphprotocol/graph-ts"
3838
import {
3939
Contract,
4040
ExampleEvent,
@@ -43,14 +43,17 @@ import {
4343
import { ExampleEntity } from "../generated/schema"
4444
4545
export function handleExampleEvent(event: ExampleEvent): void {
46-
// Entities can be loaded from the store using a string ID; this ID
46+
// Entities can be loaded from the store using an ID; this ID
4747
// needs to be unique across all entities of the same type
48-
let entity = ExampleEntity.load(event.transaction.from)
48+
const id = event.transaction.hash.concat(
49+
Bytes.fromByteArray(Bytes.fromBigInt(event.logIndex))
50+
)
51+
let entity = ExampleEntity.load(id)
4952
5053
// Entities only exist after they have been saved to the store;
5154
// \`null\` checks allow to create entities on demand
5255
if (!entity) {
53-
entity = new ExampleEntity(event.transaction.from)
56+
entity = new ExampleEntity(id)
5457
5558
// Entity fields can be set using simple assignments
5659
entity.count = BigInt.fromI32(0)

0 commit comments

Comments
 (0)