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

Update all evm templates for V2 #32

Merged
merged 4 commits into from
Jun 14, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,8 @@ const INITIAL_EVENTS_SUMMARY = {
{{/each}}
};

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

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

const currentSummaryEntity = summary ?? INITIAL_EVENTS_SUMMARY;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
});

// Processing the event
const mockDbUpdated = {{contract.name.capitalized}}.{{event.name.capitalized}}.processEvent({
const mockDbUpdatedPromise = {{contract.name.capitalized}}.{{event.name.capitalized}}.processEvent({
event: mock{{contract.name.capitalized}}{{event.name.capitalized}},
mockDb: mockDbFinal,
});

it("{{contract.name.capitalized}}_{{event.name.capitalized}} is created correctly", () => {
it("{{contract.name.capitalized}}_{{event.name.capitalized}} is created correctly", async () => {
const mockDbUpdated = await mockDbUpdatedPromise;
// Getting the actual entity from the mock database
let actual{{contract.name.capitalized}}{{event.name.capitalized}} = mockDbUpdated.entities.{{contract.name.capitalized}}_{{event.name.capitalized}}.get(
mock{{contract.name.capitalized}}{{event.name.capitalized}}.transactionHash +
Expand All @@ -75,7 +76,8 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
);
});

it("EventsSummaryEntity is updated correctly", () => {
it("EventsSummaryEntity is updated correctly", async () => {
const mockDbUpdated = await mockDbUpdatedPromise;
// Getting the actual entity from the mock database
let actualEventsSummaryEntity = mockDbUpdated.entities.EventsSummary.get(
GLOBAL_EVENTS_SUMMARY_KEY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let globalEventsSummaryKey = "GlobalEventsSummary"

module BigInt = Ethers.BigInt

let initialEventsSummary: Types.eventsSummaryEntity = {
let initialEventsSummary: Entities.EventsSummary.t = {
id: globalEventsSummaryKey,
{{#each imported_contracts as |contract|}}
{{#each contract.imported_events as |event|}}
Expand All @@ -14,15 +14,10 @@ let initialEventsSummary: Types.eventsSummaryEntity = {

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let globalEventsSummaryKey = "GlobalEventsSummary"

module BigInt = Ethers.BigInt

let mockEventsSummaryEntity: Types.eventsSummaryEntity = {
let mockEventsSummaryEntity: Entities.EventsSummary.t = {
id: globalEventsSummaryKey,
{{#each imported_contracts as | contract |}}
{{#each contract.codegen_events as | event |}}
Expand Down Expand Up @@ -43,12 +43,13 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
})

// Processing the event
let mockDbUpdated = {{contract.name.capitalized}}.{{event.name.capitalized}}.processEvent({
let mockDbUpdatedPromise = {{contract.name.capitalized}}.{{event.name.capitalized}}.processEvent({
event: mock{{contract.name.capitalized}}{{event.name.capitalized}},
mockDb: mockDbFinal,
})

it("{{contract.name.capitalized}}_{{event.name.capitalized}} is created correctly", () => {
RescriptMocha.Promise.it("{{contract.name.capitalized}}_{{event.name.capitalized}} is created correctly", () => {
let mockDbUpdatedPromise = await mockDbUpdatedPromise
// Getting the actual entity from the mock database
let actual{{contract.name.capitalized}}{{event.name.capitalized}} =
mockDbUpdated.entities.{{contract.name.uncapitalized}}_{{event.name.capitalized}}.get(
Expand All @@ -71,13 +72,14 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
)
})

it("EventsSummaryEntity is updated correctly", () => {
RescriptMocha.Promise.it("EventsSummary Entity is updated correctly", () => {
let mockDbUpdatedPromise = await mockDbUpdatedPromise
// 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 = {
let expectedEventsSummaryEntity: Entities.EventsSummary.t = {
...mockEventsSummaryEntity,
{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count: mockEventsSummaryEntity.{{contract.name.uncapitalized}}_{{event.name.capitalized}}Count->BigInt.add(BigInt.fromInt(1)),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
{{contract.name.capitalized}}_{{event.name.capitalized}},
{{/each}}
{{/each}}
EventsSummaryEntity
EventsSummary
} from "generated";

export const GLOBAL_EVENTS_SUMMARY_KEY = "GlobalEventsSummary";

const INITIAL_EVENTS_SUMMARY: EventsSummaryEntity = {
const INITIAL_EVENTS_SUMMARY: EventsSummary = {
id: GLOBAL_EVENTS_SUMMARY_KEY,
{{#each imported_contracts as |contract|}}
{{#each contract.imported_events as |event|}}
Expand All @@ -26,15 +26,10 @@ const INITIAL_EVENTS_SUMMARY: EventsSummaryEntity = {
{{#each imported_contracts as |contract|}}
{{#each contract.imported_events as |event|}}
{{contract.name.capitalized}}.{{event.name.capitalized
}}.loader(({ event, context }) => {
context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY);
});

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

const currentSummaryEntity: EventsSummaryEntity =
const currentSummaryEntity: EventsSummary =
summary ?? INITIAL_EVENTS_SUMMARY;

const nextSummaryEntity = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import assert from "assert";
import {
TestHelpers,
EventsSummaryEntity,
EventsSummary,
{{contract.name.capitalized}}_{{event.name.capitalized}}
} from "generated";
const { MockDb, {{contract.name.capitalized}}, Addresses } = TestHelpers;
Expand All @@ -13,7 +13,7 @@ import { GLOBAL_EVENTS_SUMMARY_KEY } from "../src/EventHandlers";
{{/with}}


const MOCK_EVENTS_SUMMARY_ENTITY: EventsSummaryEntity = {
const MOCK_EVENTS_SUMMARY_ENTITY: EventsSummary = {
id: GLOBAL_EVENTS_SUMMARY_KEY,
{{#each imported_contracts as | contract |}}
{{#each contract.codegen_events as | event |}}
Expand All @@ -28,7 +28,7 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
// Create mock db
const mockDbInitial = MockDb.createMockDb();

// Add mock EventsSummaryEntity to mock db
// Add mock EventsSummary to mock db
const mockDbFinal = mockDbInitial.entities.EventsSummary.set(
MOCK_EVENTS_SUMMARY_ENTITY
);
Expand All @@ -51,12 +51,13 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
});

// Processing the event
const mockDbUpdated = {{contract.name.capitalized}}.{{event.name.capitalized}}.processEvent({
const mockDbUpdatedPromise = {{contract.name.capitalized}}.{{event.name.capitalized}}.processEvent({
event: mock{{contract.name.capitalized}}{{event.name.capitalized}},
mockDb: mockDbFinal,
});

it("{{contract.name.capitalized}}_{{event.name.capitalized}} is created correctly", () => {
it("{{contract.name.capitalized}}_{{event.name.capitalized}} is created correctly", async () => {
const mockDbUpdated = await mockDbUpdatedPromise;
// Getting the actual entity from the mock database
let actual{{contract.name.capitalized}}{{event.name.capitalized}} = mockDbUpdated.entities.{{contract.name.capitalized}}_{{event.name.capitalized}}.get(
mock{{contract.name.capitalized}}{{event.name.capitalized}}.transactionHash +
Expand All @@ -77,19 +78,20 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
assert.deepEqual(actual{{contract.name.capitalized}}{{event.name.capitalized}}, expected{{contract.name.capitalized}}{{event.name.capitalized}}, "Actual {{contract.name.capitalized}}{{event.name.capitalized}} should be the same as the expected{{contract.name.capitalized}}{{event.name.capitalized}}");
});

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

// Creating the expected entity
const expectedEventsSummaryEntity: EventsSummaryEntity = {
const expectedEventsSummary: EventsSummary = {
...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 {{contract.name.capitalized}}{{event.name.capitalized}} should be the same as the expected{{contract.name.capitalized}}{{event.name.capitalized}}");
assert.deepEqual(actualEventsSummary, expectedEventsSummary, "Actual {{contract.name.capitalized}}{{event.name.capitalized}} should be the same as the expected{{contract.name.capitalized}}{{event.name.capitalized}}");
});
});
{{/with}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
const { ERC20Contract } = require("generated");
const { ERC20 } = require("generated");

ERC20Contract.Approval.loader(({ event, context }) => {
// loading the required Account entity
context.Account.load(event.params.owner);
});

ERC20Contract.Approval.handler(({ event, context }) => {
ERC20.Approval.handler(async ({ event, context }) => {
// getting the owner Account entity
let ownerAccount = context.Account.get(event.params.owner);
const ownerAccount = await context.Account.get(event.params.owner);

if (ownerAccount === undefined) {
// It's possible to call the approve function without having a balance of the token and hence the account doesn't exist yet

// create the account
let accountObject = {
const accountObject = {
id: event.params.owner,
balance: BigInt(0),
};

context.Account.set(accountObject);
}
let approvalId = event.params.owner + "-" + event.params.spender;
const approvalId = event.params.owner + "-" + event.params.spender;

let approvalObject = {
const approvalObject = {
id: approvalId,
amount: event.params.value,
owner_id: event.params.owner,
Expand All @@ -33,45 +28,41 @@ ERC20Contract.Approval.handler(({ event, context }) => {
context.Approval.set(approvalObject);
});

ERC20Contract.Transfer.loader(({ event, context }) => {
context.Account.load(event.params.from);
context.Account.load(event.params.to);
});

ERC20Contract.Transfer.handler(({ event, context }) => {
let senderAccount = context.Account.get(event.params.from);
ERC20.Transfer.handler(async ({ event, context }) => {
const senderAccount = await context.Account.get(event.params.from);

if (senderAccount === undefined) {
// create the account
// This is likely only ever going to be the zero address in the case of the first mint
let accountObject = {
const accountObject = {
id: event.params.from,
balance: BigInt(0) - event.params.value,
};

context.Account.set(accountObject);
} else {
// subtract the balance from the existing users balance
let accountObject = {
const accountObject = {
id: senderAccount.id,
balance: senderAccount.balance - event.params.value,
};
context.Account.set(accountObject);
}

// getting the sender Account entity
let receiverAccount = context.Account.get(event.params.to);
const receiverAccount = await context.Account.get(event.params.to);

if (receiverAccount === undefined) {
// create new account
let accountObject = {
const accountObject = {
id: event.params.to,
balance: event.params.value,
};

context.Account.set(accountObject);
} else {
// update existing account
let accountObject = {
const accountObject = {
id: receiverAccount.id,
balance: receiverAccount.balance + event.params.value,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { TestHelpers } = require("generated");
const { MockDb, ERC20, Addresses } = TestHelpers;

describe("Transfers", () => {
it("Transfer subtracts the from account balance and adds to the to account balance", () => {
it("Transfer subtracts the from account balance and adds to the to account balance", async () => {
//Instantiate a mock DB
const mockDbEmpty = MockDb.createMockDb();

Expand Down Expand Up @@ -32,7 +32,7 @@ describe("Transfers", () => {
//Process the mockEvent
//This takes in the mockDb and returns a new updated mockDb.
//The initial mockDb is not mutated with processEvent
const mockDbAfterTransfer = ERC20.Transfer.processEvent({
const mockDbAfterTransfer = await ERC20.Transfer.processEvent({
event: mockTransfer,
mockDb,
});
Expand Down
Loading
Loading