Skip to content

Commit

Permalink
Fix generated example entity id uniqueness (#1835)
Browse files Browse the repository at this point in the history
* fix generated example entity id uniqueness

* exclude renovate.json from prettier
  • Loading branch information
YaroShkvorets authored Dec 13, 2024
1 parent e2e5139 commit 299c75b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-jobs-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-cli': patch
---

fix generated example entity id uniqueness
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ packages/cli/tests/cli/add
packages/cli/config/TheGraphNetworksRegistry.json
pnpm-lock.yaml
website/src/graphql-env.d.ts
renovate.json
9 changes: 5 additions & 4 deletions packages/cli/src/protocols/ethereum/scaffold/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const generatePlaceholderHandlers = ({
contractName: string;
}) =>
`
import { BigInt } from '@graphprotocol/graph-ts'
import { BigInt, Bytes } from '@graphprotocol/graph-ts'
import { ${contractName}, ${events.map(event => event._alias)} }
from '../generated/${contractName}/${contractName}'
import { ExampleEntity } from '../generated/schema'
Expand All @@ -21,14 +21,15 @@ export const generatePlaceholderHandlers = ({
index === 0
? `
export function handle${event._alias}(event: ${event._alias}): void {
// Entities can be loaded from the store using a string ID; this ID
// Entities can be loaded from the store using an ID; this ID
// needs to be unique across all entities of the same type
let entity = ExampleEntity.load(event.transaction.from)
const id = event.transaction.hash.concat(Bytes.fromByteArray(Bytes.fromBigInt(event.logIndex)));
let entity = ExampleEntity.load(id)
// Entities only exist after they have been saved to the store;
// \`null\` checks allow to create entities on demand
if (!entity) {
entity = new ExampleEntity(event.transaction.from)
entity = new ExampleEntity(id)
// Entity fields can be set using simple assignments
entity.count = BigInt.fromI32(0)
Expand Down
11 changes: 7 additions & 4 deletions packages/cli/src/scaffold/__snapshots__/ethereum.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dataSources:
`;

exports[`Ethereum subgraph scaffolding > Mapping (default) 1`] = `
"import { BigInt } from "@graphprotocol/graph-ts"
"import { BigInt, Bytes } from "@graphprotocol/graph-ts"
import {
Contract,
ExampleEvent,
Expand All @@ -43,14 +43,17 @@ import {
import { ExampleEntity } from "../generated/schema"
export function handleExampleEvent(event: ExampleEvent): void {
// Entities can be loaded from the store using a string ID; this ID
// Entities can be loaded from the store using an ID; this ID
// needs to be unique across all entities of the same type
let entity = ExampleEntity.load(event.transaction.from)
const id = event.transaction.hash.concat(
Bytes.fromByteArray(Bytes.fromBigInt(event.logIndex))
)
let entity = ExampleEntity.load(id)
// Entities only exist after they have been saved to the store;
// \`null\` checks allow to create entities on demand
if (!entity) {
entity = new ExampleEntity(event.transaction.from)
entity = new ExampleEntity(id)
// Entity fields can be set using simple assignments
entity.count = BigInt.fromI32(0)
Expand Down

0 comments on commit 299c75b

Please sign in to comment.