Repository: graphprotocol/graph-tooling
Pull request #1835 (2024-12-13 20:08:11)

Fix generated example entity id uniqueness
Generated example entity id should be unique. See old PR #595

changeset-bot[bot]: ###  🦋  Changeset detected

Latest commit: 02156f8f3e9e70f7d53e7781fab58eb0a787cf74

**The changes in this PR will be included in the next version bump.**

<details><summary>This PR includes changesets to release 1 package</summary>

  | Name                     | Type  |
| ------------------------ | ----- |
| @graphprotocol/graph-cli | Patch |

</details>

Not sure what this means? [Click here  to learn what changesets are](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add another changeset to this PR](https://github.com/YaroShkvorets/graph-tooling/new/fix/make-example-entities-unique?filename=.changeset/ninety-stingrays-knock.md&value=---%0A%22%40graphprotocol%2Fgraph-cli%22%3A%20patch%0A---%0A%0AFix%20generated%20example%20entity%20id%20uniqueness%0A)



Patch:
diff --git a/.changeset/swift-jobs-count.md b/.changeset/swift-jobs-count.md
new file mode 100644
index 00000000..d5e790c8
--- /dev/null
+++ b/.changeset/swift-jobs-count.md
@@ -0,0 +1,5 @@
+---
+'@graphprotocol/graph-cli': patch
+---
+
+fix generated example entity id uniqueness
diff --git a/.prettierignore b/.prettierignore
index b613e565..037a242b 100644
--- a/.prettierignore
+++ b/.prettierignore
@@ -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
diff --git a/packages/cli/src/protocols/ethereum/scaffold/mapping.ts b/packages/cli/src/protocols/ethereum/scaffold/mapping.ts
index fcf03f5c..113996a7 100644
--- a/packages/cli/src/protocols/ethereum/scaffold/mapping.ts
+++ b/packages/cli/src/protocols/ethereum/scaffold/mapping.ts
@@ -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'
@@ -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)


Test patch:
diff --git a/packages/cli/src/scaffold/__snapshots__/ethereum.test.ts.snap b/packages/cli/src/scaffold/__snapshots__/ethereum.test.ts.snap
index 8c778bd1..ad94388f 100644
--- a/packages/cli/src/scaffold/__snapshots__/ethereum.test.ts.snap
+++ b/packages/cli/src/scaffold/__snapshots__/ethereum.test.ts.snap
@@ -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,
@@ -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)