Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generated example entity id uniqueness #1835

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading