From 02156f8f3e9e70f7d53e7781fab58eb0a787cf74 Mon Sep 17 00:00:00 2001 From: YaroShkvorets Date: Fri, 13 Dec 2024 15:04:34 -0500 Subject: [PATCH 1/2] fix generated example entity id uniqueness --- .changeset/swift-jobs-count.md | 5 +++++ .../cli/src/protocols/ethereum/scaffold/mapping.ts | 9 +++++---- .../src/scaffold/__snapshots__/ethereum.test.ts.snap | 11 +++++++---- 3 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 .changeset/swift-jobs-count.md diff --git a/.changeset/swift-jobs-count.md b/.changeset/swift-jobs-count.md new file mode 100644 index 000000000..d5e790c82 --- /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/packages/cli/src/protocols/ethereum/scaffold/mapping.ts b/packages/cli/src/protocols/ethereum/scaffold/mapping.ts index fcf03f5cb..113996a72 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) diff --git a/packages/cli/src/scaffold/__snapshots__/ethereum.test.ts.snap b/packages/cli/src/scaffold/__snapshots__/ethereum.test.ts.snap index 8c778bd16..ad94388fe 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) From 1c37a09aa64f76c65ef0270c68e4eda7e97951d7 Mon Sep 17 00:00:00 2001 From: YaroShkvorets Date: Fri, 13 Dec 2024 15:13:36 -0500 Subject: [PATCH 2/2] exclude renovate.json from prettier --- .prettierignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.prettierignore b/.prettierignore index b613e565b..037a242b4 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