Skip to content

Commit

Permalink
[V1] Remove EventSummary from init template (#34)
Browse files Browse the repository at this point in the history
* Remove EventSummary from init template

* Revert the code which I shouldn't remove
  • Loading branch information
DZakh authored Jun 19, 2024
1 parent d5b728f commit 6edd0ee
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 308 deletions.
1 change: 1 addition & 0 deletions codegenerator/cli/src/config_parsing/human_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod evm {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ecosystem: Option<EcosystemTag>,
#[serde(skip_serializing_if = "Option::is_none")]
pub schema: Option<String>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,16 @@
/*
*Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features*
* Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features
*/
const {
const {
{{#each imported_contracts as |contract|}}
{{contract.name.capitalized}}Contract,
{{/each}}
} = require("generated");

const GLOBAL_EVENTS_SUMMARY_KEY = "GlobalEventsSummary";

module.exports = {
GLOBAL_EVENTS_SUMMARY_KEY,
};

const INITIAL_EVENTS_SUMMARY = {
id: GLOBAL_EVENTS_SUMMARY_KEY,
{{#each imported_contracts as |contract|}}
{{#each contract.imported_events as |event|}}
{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: BigInt(0),
{{/each}}
{{/each}}
};

{{#each imported_contracts as |contract|}}
{{#each contract.imported_events as |event|}}
{{contract.name.capitalized}}Contract.{{event.name.capitalized
}}.loader(({event, context}) => {
context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY);
});

{{contract.name.capitalized}}Contract.{{event.name.capitalized
}}.handler(({event, context}) => {
const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY);

const currentSummaryEntity = summary ?? INITIAL_EVENTS_SUMMARY;

const nextSummaryEntity = {
...currentSummaryEntity,
{{contract.name.uncapitalized}}_{{event.name.capitalized
}}Count: currentSummaryEntity.{{contract.name.uncapitalized
}}_{{event.name.capitalized
}}Count + BigInt(1),
};

const {{contract.name.uncapitalized
}}_{{event.name.capitalized
}}Entity = {
{{contract.name.capitalized}}Contract.{{event.name.capitalized}}.handler(({event, context}) => {
const entity = {
id: event.transactionHash + event.logIndex.toString(),
{{#each event.params as |param|}}
{{param.entity_key.uncapitalized
Expand All @@ -58,13 +23,10 @@ const INITIAL_EVENTS_SUMMARY = {
{{/if}}
,
{{/each}}
eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY,
};

context.EventsSummary.set(nextSummaryEntity);
context.{{contract.name.capitalized
}}_{{event.name.capitalized}}.set({{contract.name.uncapitalized
}}_{{event.name.capitalized}}Entity);
}}_{{event.name.capitalized}}.set(entity);
});
{{/each}}
{{/each}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,13 @@
const assert = require("assert");
const { TestHelpers } = require("generated");
const { MockDb, {{contract.name.capitalized}}, Addresses } = TestHelpers;

const { GLOBAL_EVENTS_SUMMARY_KEY } = require("../src/EventHandlers");
{{/with}}

const MOCK_EVENTS_SUMMARY_ENTITY = {
id: GLOBAL_EVENTS_SUMMARY_KEY,
{{#each imported_contracts as | contract |}}
{{#each contract.codegen_events as | event |}}
{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: BigInt(0),
{{/each}}
{{/each}}
};

{{#with imported_contracts.[0] as | contract |}}
{{#with contract.codegen_events.[0] as | event |}}
describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} event tests", () => {
// Create mock db
const mockDbInitial = MockDb.createMockDb();

// Add mock EventsSummaryEntity to mock db
const mockDbFinal = mockDbInitial.entities.EventsSummary.set(
MOCK_EVENTS_SUMMARY_ENTITY
);
const mockDb = MockDb.createMockDb();

// Creating mock {{contract.name.capitalized}} contract {{event.name.capitalized}} event
const mock{{contract.name.capitalized}}{{event.name.capitalized}}Event = {{contract.name.capitalized}}.{{event.name.capitalized}}.createMockEvent({
Expand All @@ -47,7 +31,7 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
// Processing the event
const mockDbUpdated = {{contract.name.capitalized}}.{{event.name.capitalized}}.processEvent({
event: mock{{contract.name.capitalized}}{{event.name.capitalized}}Event,
mockDb: mockDbFinal,
mockDb,
});

it("{{contract.name.capitalized}}_{{event.name.capitalized}}Entity is created correctly", () => {
Expand All @@ -65,7 +49,6 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
{{#each event.params as |param|}}
{{param.param_name.uncapitalized}}: mock{{contract.name.capitalized}}{{event.name.capitalized}}Event.params.{{param.param_name.uncapitalized}},
{{/each}}
eventsSummary: "GlobalEventsSummary",
};
// Asserting that the entity in the mock database is the same as the expected entity
assert.deepEqual(
Expand All @@ -74,25 +57,6 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
"Actual {{contract.name.capitalized}}{{event.name.capitalized}}Entity should be the same as the expected{{contract.name.capitalized}}{{event.name.capitalized}}Entity"
);
});

it("EventsSummaryEntity is updated correctly", () => {
// Getting the actual entity from the mock database
let actualEventsSummaryEntity = mockDbUpdated.entities.EventsSummary.get(
GLOBAL_EVENTS_SUMMARY_KEY
);

// Creating the expected entity
const expectedEventsSummaryEntity = {
...MOCK_EVENTS_SUMMARY_ENTITY,
{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: MOCK_EVENTS_SUMMARY_ENTITY.{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count + BigInt(1),
};
// Asserting that the entity in the mock database is the same as the expected entity
assert.deepEqual(
actualEventsSummaryEntity,
expectedEventsSummaryEntity,
"Actual EventsSummaryEntity should be the same as the expected EventsSummaryEntity"
);
});
});
{{/with}}
{{/with}}
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
let globalEventsSummaryKey = "GlobalEventsSummary"

module BigInt = Ethers.BigInt

let initialEventsSummary: Types.eventsSummaryEntity = {
id: globalEventsSummaryKey,
/*
* Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features
*/
{{#each imported_contracts as |contract|}}
{{#each contract.imported_events as |event|}}
{{contract.name.uncapitalized}}_{{event.name.capitalized
}}Count: BigInt.fromInt(0),
{{/each}}
{{/each}}
}

{{#each imported_contracts as |contract|}}
{{#each contract.imported_events as |event|}}
Handlers.{{contract.name.capitalized}}Contract.{{event.name.capitalized
}}.loader(({event, context}) => {
context.eventsSummary.load(globalEventsSummaryKey)
})

Handlers.{{contract.name.capitalized
}}Contract.{{event.name.capitalized
}}.handler(({event, context}) => {
let summary = context.eventsSummary.get(globalEventsSummaryKey)

let currentSummaryEntity = summary->Belt.Option.getWithDefault(initialEventsSummary)

let nextSummaryEntity = {
...currentSummaryEntity,
{{contract.name.uncapitalized}}_{{event.name.capitalized
}}Count: currentSummaryEntity.{{contract.name.uncapitalized
}}_{{event.name.capitalized
}}Count->BigInt.add(BigInt.fromInt(1)),
}

let {{contract.name.uncapitalized
}}_{{event.name.capitalized}}Entity: Types.{{contract.name.uncapitalized
let entity: Types.{{contract.name.uncapitalized
}}_{{event.name.capitalized
}}Entity = {
id: event.transactionHash ++ event.logIndex->Belt.Int.toString,
Expand All @@ -52,13 +24,10 @@ Handlers.{{contract.name.capitalized
{{/if}}
,
{{/each}}
eventsSummary: globalEventsSummaryKey,
}

context.eventsSummary.set(nextSummaryEntity)
context.{{contract.name.uncapitalized
}}_{{event.name.capitalized}}.set({{contract.name.uncapitalized
}}_{{event.name.capitalized}}Entity)
}}_{{event.name.capitalized}}.set(entity)
})
{{/each}}
{{/each}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,11 @@ open Mocha
open Belt
open TestHelpers

let globalEventsSummaryKey = "GlobalEventsSummary"

module BigInt = Ethers.BigInt

let mockEventsSummaryEntity: Types.eventsSummaryEntity = {
id: globalEventsSummaryKey,
{{#each imported_contracts as | contract |}}
{{#each contract.codegen_events as | event |}}
{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: BigInt.fromInt(0),
{{/each}}
{{/each}}
}

{{#with imported_contracts.[0] as | contract |}}
{{#with contract.codegen_events.[0] as | event |}}
describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} event tests", () => {
// Create mock db
let mockDbInitial = MockDb.createMockDb()

// Add mock EventsSummaryEntity to mock db
let mockDbFinal = mockDbInitial.entities.eventsSummary.set(mockEventsSummaryEntity)
let mockDb = MockDb.createMockDb()

// Creating mock {{contract.name.capitalized}} contract {{event.name.capitalized}} event
let mock{{contract.name.capitalized}}{{event.name.capitalized}}Event = {{contract.name.capitalized}}.{{event.name.capitalized}}.createMockEvent({
Expand All @@ -45,7 +29,7 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
// Processing the event
let mockDbUpdated = {{contract.name.capitalized}}.{{event.name.capitalized}}.processEvent({
event: mock{{contract.name.capitalized}}{{event.name.capitalized}}Event,
mockDb: mockDbFinal,
mockDb,
})

it("{{contract.name.capitalized}}_{{event.name.capitalized}}Entity is created correctly", () => {
Expand All @@ -61,7 +45,6 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
{{#each event.params as |param|}}
{{param.param_name.uncapitalized}}: mock{{contract.name.capitalized}}{{event.name.capitalized}}Event.params.{{param.param_name.uncapitalized}}{{#if param.is_eth_address}}->Ethers.ethAddressToString{{/if}},
{{/each}}
eventsSummary: globalEventsSummaryKey,
}
//Assert the expected {{contract.name.capitalized}} {{event.name.capitalized}} entity
Assert.deep_equal(
Expand All @@ -70,24 +53,6 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
~message="Actual {{contract.name.capitalized}}_{{event.name.capitalized}}Entity should be the same as the expected {{contract.name.capitalized}}_{{event.name.capitalized}}Entity",
)
})

it("EventsSummaryEntity is updated correctly", () => {
// Getting the actual entity from the mock database
let actualEventsSummaryEntity =
mockDbUpdated.entities.eventsSummary.get(globalEventsSummaryKey)->Option.getExn

// Creating the expected entity
let expectedEventsSummaryEntity: Types.eventsSummaryEntity = {
...mockEventsSummaryEntity,
{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: mockEventsSummaryEntity.{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count->BigInt.add(BigInt.fromInt(1)),
}
// Asserting that the entity in the mock database is the same as the expected entity
Assert.deep_equal(
actualEventsSummaryEntity,
expectedEventsSummaryEntity,
~message="Actual EventsSummaryEntity should be the same as the expected EventsSummaryEntity",
)
})
})
{{/with}}
{{/with}}
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
type EventsSummary {
id: ID!
{{#each imported_contracts as |contract|}}
{{#each contract.imported_events as |event|}}
{{contract.name.uncapitalized}}_{{event.name.capitalized
}}: [{{contract.name.capitalized}}_{{event.name.capitalized
}}!]! @derivedFrom(field: "eventsSummary")
{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: BigInt!
{{/each}}
{{/each}}
}
{{#each imported_contracts as |contract|}}
{{#each contract.imported_events as |event|}}

type {{contract.name.capitalized}}_{{event.name.capitalized}} {
id: ID!
{{#each event.params as |param|}}
{{param.entity_key.uncapitalized}}: {{param.graphql_type}}
{{/each}}
eventsSummary: String!
}

{{/each}}
{{/each}}
{{/each}}
Original file line number Diff line number Diff line change
@@ -1,72 +1,32 @@
/*
*Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features*
* Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features
*/
import {
{{#each imported_contracts as |contract|}}
{{contract.name.capitalized}}Contract,
{{#each contract.imported_events as |event|}}
{{contract.name.capitalized}}_{{event.name.capitalized}}Entity,
{{/each}}
{{#each contract.imported_events as |event|}}
{{contract.name.capitalized}}_{{event.name.capitalized}}Entity,
{{/each}}
EventsSummaryEntity
} from "generated";

export const GLOBAL_EVENTS_SUMMARY_KEY = "GlobalEventsSummary";

const INITIAL_EVENTS_SUMMARY: EventsSummaryEntity = {
id: GLOBAL_EVENTS_SUMMARY_KEY,
{{#each imported_contracts as |contract|}}
{{#each contract.imported_events as |event|}}
{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: BigInt(0),
{{/each}}
{{/each}}
};

} from "generated";
{{#each imported_contracts as |contract|}}
{{#each contract.imported_events as |event|}}
{{contract.name.capitalized}}Contract.{{event.name.capitalized
}}.loader(({ event, context }) => {
context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY);
});

{{contract.name.capitalized}}Contract.{{event.name.capitalized
}}.handler(({ event, context }) => {
const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY);

const currentSummaryEntity: EventsSummaryEntity =
summary ?? INITIAL_EVENTS_SUMMARY;

const nextSummaryEntity = {
...currentSummaryEntity,
{{contract.name.uncapitalized}}_{{event.name.capitalized
}}Count: currentSummaryEntity.{{contract.name.uncapitalized
}}_{{event.name.capitalized
}}Count + BigInt(1),
};

const {{contract.name.uncapitalized
}}_{{event.name.capitalized}}Entity: {{contract.name.capitalized
}}_{{event.name.capitalized
}}Entity = {
{{contract.name.capitalized}}Contract.{{event.name.capitalized}}.handler(({ event, context }) => {
const entity: {{contract.name.capitalized}}_{{event.name.capitalized}}Entity = {
id: event.transactionHash + event.logIndex.toString(),
{{#each event.params as |param|}}
{{param.entity_key.uncapitalized
}}: event.params.{{param.event_key.uncapitalized}}{{#if
param.tuple_param_accessor_indexes
}}
{{#each param.tuple_param_accessor_indexes as |index|}}
[{{index}}]
{{/each}}
{{/if}}
,
{{param.entity_key.uncapitalized}}: event.params.{{param.event_key.uncapitalized}}{{#if
param.tuple_param_accessor_indexes
}}
{{#each param.tuple_param_accessor_indexes as |index|}}
[{{index}}]
{{/each}}
{{/if}},
{{/each}}
eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY,
};

context.EventsSummary.set(nextSummaryEntity);
context.{{contract.name.capitalized
}}_{{event.name.capitalized}}.set({{contract.name.uncapitalized
}}_{{event.name.capitalized}}Entity);
context.{{contract.name.capitalized}}_{{event.name.capitalized}}.set(entity);
});
{{/each}}
{{/each}}
Loading

0 comments on commit 6edd0ee

Please sign in to comment.