Skip to content

Commit

Permalink
Update all evm templates for V2
Browse files Browse the repository at this point in the history
  • Loading branch information
JonoPrest committed Jun 13, 2024
1 parent 306dbf9 commit 7752a7c
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 211 deletions.
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 @@ -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 @@ -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,7 +72,8 @@ describe("{{contract.name.capitalized}} contract {{event.name.capitalized}} even
)
})

it("EventsSummaryEntity is updated correctly", () => {
RescriptMocha.Promise.it("EventsSummaryEntity is updated correctly", () => {
let mockDbUpdatedPromise = await mockDbUpdatedPromise
// Getting the actual entity from the mock database
let actualEventsSummaryEntity =
mockDbUpdated.entities.eventsSummary.get(globalEventsSummaryKey)->Option.getExn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ 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 =
summary ?? INITIAL_EVENTS_SUMMARY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,7 +78,8 @@ 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("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
@@ -1,28 +1,23 @@
const { ERC20Contract } = require("generated");

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

ERC20Contract.Approval.handler(({ event, context }) => {
ERC20Contract.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);
ERC20Contract.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
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
open Types

Handlers.ERC20Contract.Approval.loader(({event, context}) => {
context.account.load(event.params.owner->Ethers.ethAddressToString)
})

Handlers.ERC20Contract.Approval.handler(({event, context}) => {
let ownerAccount = context.account.get(event.params.owner->Ethers.ethAddressToString)
Handlers.ERC20Contract.Approval.handler(async ({event, context}) => {
let ownerAccount = await context.account.get(event.params.owner->Ethers.ethAddressToString)

if(ownerAccount->Belt.Option.isNone)
{
Expand Down Expand Up @@ -34,13 +30,8 @@ Handlers.ERC20Contract.Approval.handler(({event, context}) => {

})

Handlers.ERC20Contract.Transfer.loader(({event, context}) => {
context.account.load(event.params.from->Ethers.ethAddressToString)
context.account.load(event.params.to->Ethers.ethAddressToString)
})

Handlers.ERC20Contract.Transfer.handler(({event, context}) => {
let senderAccount = context.account.get(event.params.from->Ethers.ethAddressToString)
Handlers.ERC20Contract.Transfer.handler(async ({event, context}) => {
let senderAccount = await context.account.get(event.params.from->Ethers.ethAddressToString)

switch senderAccount {
| Some(existingSenderAccount) => {
Expand All @@ -65,7 +56,7 @@ Handlers.ERC20Contract.Transfer.handler(({event, context}) => {
}
}

let receiverAccount = context.account.get(event.params.to->Ethers.ethAddressToString)
let receiverAccount = await context.account.get(event.params.to->Ethers.ethAddressToString)

switch receiverAccount {
| Some(existingReceiverAccount) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ open Belt
open TestHelpers

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

Expand Down Expand Up @@ -33,7 +33,7 @@ describe("Transfers", () => {
//Process the mockEvent
//Note: processEvent functions do not mutate the mockDb, they return a new
//mockDb with with modified state
let mockDbAfterTransfer = ERC20.Transfer.processEvent({
let mockDbAfterTransfer = await ERC20.Transfer.processEvent({
event: mockTransfer,
mockDb,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { ERC20Contract, AccountEntity, ApprovalEntity } from "generated";

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

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

if (ownerAccount === undefined) {
// Usually an account that is being approved already has/has had a balance, but it is possible they haven't.
Expand All @@ -34,15 +29,10 @@ ERC20Contract.Approval.handler(({ event, context }) => {
context.Approval.set(approvalObject);
});

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

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

if (senderAccount === undefined || senderAccount === null) {
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: AccountEntity = {
Expand All @@ -60,9 +50,9 @@ ERC20Contract.Transfer.handler(({ event, context }) => {
context.Account.set(accountObject);
}

let receiverAccount = context.Account.get(event.params.to.toString());
let receiverAccount = await context.Account.get(event.params.to.toString());

if (receiverAccount === undefined || receiverAccount === null) {
if (receiverAccount === undefined) {
// create new account
let accountObject: AccountEntity = {
id: event.params.to.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TestHelpers, AccountEntity } from "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
//Note: processEvent functions do not mutate the mockDb, they return a new
//mockDb with with modified state
const mockDbAfterTransfer = ERC20.Transfer.processEvent({
const mockDbAfterTransfer = await ERC20.Transfer.processEvent({
event: mockTransfer,
mockDb,
});
Expand Down
Loading

0 comments on commit 7752a7c

Please sign in to comment.