Skip to content

Commit 216d9d5

Browse files
committed
gnd: Update/modernize the templates for codegen
1 parent ff529f8 commit 216d9d5

2 files changed

Lines changed: 12 additions & 15 deletions

File tree

gnd/src/scaffold/mapping.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,18 @@ fn generate_first_placeholder_handler(
115115
format!(
116116
r#"
117117
export function handle{event_name}(event: {event_name}Event): void {{
118-
// Entities can be loaded from the store using a string ID; this ID
119-
// needs to be unique across all entities of the same type
120-
let entity = ExampleEntity.load(
121-
event.transaction.hash.concat(Bytes.fromByteArray(Bytes.fromBigInt(event.logIndex)))
118+
// Entities can be loaded from the store using their id; using 'Bytes' as
119+
// the id type is more efficient than 'ID' or 'String' and should be used
120+
// whenever possible.
121+
const id = event.transaction.hash.concat(
122+
Bytes.fromByteArray(Bytes.fromBigInt(event.logIndex))
122123
)
124+
let entity = ExampleEntity.load(id)
123125
124126
// Entities only exist after they have been saved to the store;
125127
// `null` checks allow to create entities on demand
126128
if (!entity) {{
127-
entity = new ExampleEntity(
128-
event.transaction.hash.concat(Bytes.fromByteArray(Bytes.fromBigInt(event.logIndex)))
129-
)
129+
entity = new ExampleEntity(id)
130130
131131
// Entity fields can be set using simple assignments
132132
entity.count = BigInt.fromI32(0)
@@ -139,12 +139,6 @@ export function handle{event_name}(event: {event_name}Event): void {{
139139
{field_assignments}
140140
entity.save()
141141
142-
// Note: If a handler doesn't require existing field values, it is faster
143-
// _not_ to load the entity from the store. Instead, create it only once with
144-
// `new Entity(...)`, set the fields that should be updated and save the
145-
// entity back to the store. Fields that were not set or unset remain
146-
// unchanged, allowing for partial updates to be applied.
147-
148142
// It is also possible to access smart contracts from mappings. For
149143
// example, the contract that has emitted the event can be connected to
150144
// with:

gnd/src/scaffold/schema.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub fn generate_schema(options: &ScaffoldOptions) -> String {
3333
/// Uses first 2 event params if available, with type comments.
3434
fn generate_example_entity(inputs: &[EventInput]) -> String {
3535
let mut fields = String::new();
36+
fields.push_str(" # Use Bytes when possible for better performance\n");
3637
fields.push_str(" id: Bytes!\n");
3738
fields.push_str(" count: BigInt!\n");
3839

@@ -47,7 +48,8 @@ fn generate_example_entity(inputs: &[EventInput]) -> String {
4748
}
4849

4950
format!(
50-
"type ExampleEntity @entity(immutable: true) {{\n{}}}\n",
51+
"# Declare entity types as immutable when possible for better performance\n\
52+
type ExampleEntity @entity(immutable: true) {{\n{}}}\n",
5153
fields
5254
)
5355
}
@@ -72,7 +74,8 @@ fn generate_event_entity(event_name: &str, inputs: &[EventInput]) -> String {
7274
fields.push_str(" transactionHash: Bytes!");
7375

7476
format!(
75-
"type {} @entity(immutable: true) {{\n{}\n}}",
77+
"# Declare entity types as immutable when possible for better performance\n\
78+
type {} @entity(immutable: true) {{\n{}\n}}",
7679
event_name, fields
7780
)
7881
}

0 commit comments

Comments
 (0)