diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/src/EventHandlers.js.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/src/EventHandlers.js.hbs index 52716d8ce..10d2a7ee2 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/src/EventHandlers.js.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/src/EventHandlers.js.hbs @@ -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; diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/test/Test.js.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/test/Test.js.hbs index 1e94bc32a..dba08efe3 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/test/Test.js.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/javascript/test/Test.js.hbs @@ -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 + @@ -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 diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/src/EventHandlers.res.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/src/EventHandlers.res.hbs index 0a7861965..50864aef6 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/src/EventHandlers.res.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/src/EventHandlers.res.hbs @@ -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) diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/test/Test.res.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/test/Test.res.hbs index af040ab37..0d7b9a756 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/test/Test.res.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/rescript/test/Test.res.hbs @@ -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( @@ -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 diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/src/EventHandlers.ts.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/src/EventHandlers.ts.hbs index d4449e018..dc11c16fc 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/src/EventHandlers.ts.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/src/EventHandlers.ts.hbs @@ -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; diff --git a/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/test/Test.ts.hbs b/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/test/Test.ts.hbs index 21672b066..4d0fdea97 100644 --- a/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/test/Test.ts.hbs +++ b/codegenerator/cli/templates/dynamic/contract_import_templates/typescript/test/Test.ts.hbs @@ -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 + @@ -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 diff --git a/codegenerator/cli/templates/static/erc20_template/javascript/src/EventHandlers.js b/codegenerator/cli/templates/static/erc20_template/javascript/src/EventHandlers.js index 7ae54632c..7501493c2 100644 --- a/codegenerator/cli/templates/static/erc20_template/javascript/src/EventHandlers.js +++ b/codegenerator/cli/templates/static/erc20_template/javascript/src/EventHandlers.js @@ -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, @@ -33,18 +28,13 @@ 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, }; @@ -52,7 +42,7 @@ ERC20Contract.Transfer.handler(({ event, context }) => { 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, }; @@ -60,18 +50,19 @@ ERC20Contract.Transfer.handler(({ event, context }) => { } // 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, }; diff --git a/codegenerator/cli/templates/static/erc20_template/javascript/test/test.js b/codegenerator/cli/templates/static/erc20_template/javascript/test/test.js index 5898e48ab..d6806f513 100644 --- a/codegenerator/cli/templates/static/erc20_template/javascript/test/test.js +++ b/codegenerator/cli/templates/static/erc20_template/javascript/test/test.js @@ -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(); @@ -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, }); diff --git a/codegenerator/cli/templates/static/erc20_template/rescript/src/EventHandlers.res b/codegenerator/cli/templates/static/erc20_template/rescript/src/EventHandlers.res index 32ab7b270..079384375 100644 --- a/codegenerator/cli/templates/static/erc20_template/rescript/src/EventHandlers.res +++ b/codegenerator/cli/templates/static/erc20_template/rescript/src/EventHandlers.res @@ -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) { @@ -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) => { @@ -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) => { diff --git a/codegenerator/cli/templates/static/erc20_template/rescript/test/Test.res b/codegenerator/cli/templates/static/erc20_template/rescript/test/Test.res index 6ab2f2ffe..7d6096c55 100644 --- a/codegenerator/cli/templates/static/erc20_template/rescript/test/Test.res +++ b/codegenerator/cli/templates/static/erc20_template/rescript/test/Test.res @@ -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() @@ -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, }) diff --git a/codegenerator/cli/templates/static/erc20_template/typescript/src/EventHandlers.ts b/codegenerator/cli/templates/static/erc20_template/typescript/src/EventHandlers.ts index 7e93dd09a..92053f804 100644 --- a/codegenerator/cli/templates/static/erc20_template/typescript/src/EventHandlers.ts +++ b/codegenerator/cli/templates/static/erc20_template/typescript/src/EventHandlers.ts @@ -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. @@ -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 = { @@ -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(), diff --git a/codegenerator/cli/templates/static/erc20_template/typescript/test/test.ts b/codegenerator/cli/templates/static/erc20_template/typescript/test/test.ts index 7cda70dca..d4ba2a130 100644 --- a/codegenerator/cli/templates/static/erc20_template/typescript/test/test.ts +++ b/codegenerator/cli/templates/static/erc20_template/typescript/test/test.ts @@ -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(); @@ -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, }); diff --git a/codegenerator/cli/templates/static/greeter_template/javascript/src/EventHandlers.js b/codegenerator/cli/templates/static/greeter_template/javascript/src/EventHandlers.js index 84f812ceb..809316043 100644 --- a/codegenerator/cli/templates/static/greeter_template/javascript/src/EventHandlers.js +++ b/codegenerator/cli/templates/static/greeter_template/javascript/src/EventHandlers.js @@ -1,32 +1,19 @@ const { GreeterContract } = require("generated"); -/** -Registers a loader that loads any values from your database that your -NewGreeting event handler might need on the Greeter contract. -*/ -GreeterContract.NewGreeting.loader(({ event, context }) => { - //The id for the User entity derived from params of the NewGreeting event - const userId = event.params.user; - //Try load in in a User entity with id of the user param on the - //NewGreeting event - context.User.load(userId); -}); - /** Registers a handler that handles any values from the NewGreeting event on the Greeter contract and index these values into the DB. */ -GreeterContract.NewGreeting.handler(({ event, context }) => { +GreeterContract.NewGreeting.handler(async ({ event, context }) => { //The id for the User entity const userId = event.params.user; //The greeting string that was added. const latestGreeting = event.params.greeting; //The optional user entity that may exist already at "userId" - //This value would be undefined in the case that it was not loaded in the - //loader function above OR in the case where it never existed in the db - const currentUserEntity = context.User.get(userId); + //This value would be undefined in the case that it never existed in the db + const currentUserEntity = await context.User.get(userId); //Construct the userEntity that is to be set in the DB const userEntity = currentUserEntity @@ -53,30 +40,17 @@ GreeterContract.NewGreeting.handler(({ event, context }) => { context.User.set(userEntity); }); -/** -Registers a loader that loads any values from your database that your -ClearGreeting event handler might need on the Greeter contract. -*/ -GreeterContract.ClearGreeting.loader(({ event, context }) => { - //The id for the "User" entity derived from params of the ClearGreeting event - const userId = event.params.user; - //Try load in in a "User" entity with id of the user param on the - //ClearGreeting event - context.User.load(userId); -}); - /** Registers a handler that handles any values from the ClearGreeting event on the Greeter contract and index these values into the DB. */ -GreeterContract.ClearGreeting.handler(({ event, context }) => { +GreeterContract.ClearGreeting.handler(async ({ event, context }) => { //The id for the "User" entity derived from params of the ClearGreeting event const userId = event.params.user; //The optional user entity that may exist already at "userId" - //This value would be "undefined" in the case that it was not loaded in the - //loader function above OR in the case where it never existed in the db - const currentUserEntity = context.User.get(userId); + //This value would be "undefined" in the case that it never existed in the db + const currentUserEntity = await context.User.get(userId); if (currentUserEntity) { //Only make any changes in the case that there is an existing User diff --git a/codegenerator/cli/templates/static/greeter_template/javascript/test/test.js b/codegenerator/cli/templates/static/greeter_template/javascript/test/test.js index a8a097622..3d640bef3 100644 --- a/codegenerator/cli/templates/static/greeter_template/javascript/test/test.js +++ b/codegenerator/cli/templates/static/greeter_template/javascript/test/test.js @@ -3,7 +3,7 @@ const { TestHelpers } = require("generated"); const { MockDb, Greeter, Addresses } = TestHelpers; describe("Greeter template tests", () => { - it("A NewGreeting event creates a User entity", () => { + it("A NewGreeting event creates a User entity", async () => { // Initializing the mock database const mockDbInitial = MockDb.createMockDb(); @@ -18,7 +18,7 @@ describe("Greeter template tests", () => { }); // Processing the mock event on the mock database - const updatedMockDb = Greeter.NewGreeting.processEvent({ + const updatedMockDb = await Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent, mockDb: mockDbInitial, }); @@ -38,7 +38,7 @@ describe("Greeter template tests", () => { assert.deepEqual(expectedUserEntity, actualUserEntity); }); - it("2 Greetings from the same users results in that user having a greeter count of 2", () => { + it("2 Greetings from the same users results in that user having a greeter count of 2", async () => { // Initializing the mock database const mockDbInitial = MockDb.createMockDb(); // Initializing values for mock event @@ -59,13 +59,13 @@ describe("Greeter template tests", () => { }); // Processing the mock event on the mock database - const updatedMockDb = Greeter.NewGreeting.processEvent({ + const updatedMockDb = await Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent, mockDb: mockDbInitial, }); // Processing the mock event on the updated mock database - const updatedMockDb2 = Greeter.NewGreeting.processEvent({ + const updatedMockDb2 = await Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent2, mockDb: updatedMockDb, }); @@ -77,7 +77,7 @@ describe("Greeter template tests", () => { assert.equal(2, actualUserEntity?.numberOfGreetings); }); - it("2 Greetings from the same users results in the latest greeting being the greeting from the second event", () => { + it("2 Greetings from the same users results in the latest greeting being the greeting from the second event", async () => { // Initializing the mock database const mockDbInitial = MockDb.createMockDb(); // Initializing values for mock event @@ -98,13 +98,13 @@ describe("Greeter template tests", () => { }); // Processing the mock event on the mock database - const updatedMockDb = Greeter.NewGreeting.processEvent({ + const updatedMockDb = await Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent, mockDb: mockDbInitial, }); // Processing the mock event on the updated mock database - const updatedMockDb2 = Greeter.NewGreeting.processEvent({ + const updatedMockDb2 = await Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent2, mockDb: updatedMockDb, }); diff --git a/codegenerator/cli/templates/static/greeter_template/rescript/src/EventHandlers.res b/codegenerator/cli/templates/static/greeter_template/rescript/src/EventHandlers.res index f3c6e43fb..240395078 100644 --- a/codegenerator/cli/templates/static/greeter_template/rescript/src/EventHandlers.res +++ b/codegenerator/cli/templates/static/greeter_template/rescript/src/EventHandlers.res @@ -1,32 +1,19 @@ open Types -/** -Registers a loader that loads any values from your database that your -NewGreeting event handler might need on the Greeter contract. -*/ -Handlers.GreeterContract.NewGreeting.loader(({event, context}) => { - //The id for the "User" entity derived from params of the NewGreeting event - let userId = event.params.user->Ethers.ethAddressToString - //Try load in in a "User" entity with id of the user param on the - //NewGreeting event - context.user.load(userId) -}) - /** Registers a handler that handles any values from the NewGreeting event on the Greeter contract and index these values into the DB. */ -Handlers.GreeterContract.NewGreeting.handler(({event, context}) => { +Handlers.GreeterContract.NewGreeting.handler(async ({event, context}) => { //The id for the "User" entity let userId = event.params.user->Ethers.ethAddressToString //The greeting string that was added. let latestGreeting = event.params.greeting //The optional User entity that may exist already at "userId" - //This value would be None in the case that it was not loaded in the - //loader function above OR in the case where it never existed in the db - let maybeCurrentUserEntity = context.user.get(userId) + //This value would be None in the case that it never existed in the db + let maybeCurrentUserEntity = await context.user.get(userId) //Construct the userEntity that is to be set in the DB let userEntity: userEntity = switch maybeCurrentUserEntity { @@ -55,28 +42,17 @@ Handlers.GreeterContract.NewGreeting.handler(({event, context}) => { context.user.set(userEntity) }) -/** -Registers a loader that loads any values from your database that your -ClearGreeting event handler might need on the Greeter contract. -*/ -Handlers.GreeterContract.ClearGreeting.loader(({event, context}) => { - //Try load in in a "User" entity with id of the user param on the - //ClearGreeting event - context.user.load(event.params.user->Ethers.ethAddressToString) -}) - /** Registers a handler that handles any values from the ClearGreeting event on the Greeter contract and index these values into the DB. */ -Handlers.GreeterContract.ClearGreeting.handler(({event, context}) => { +Handlers.GreeterContract.ClearGreeting.handler(async ({event, context}) => { //The id for the "User" entity let userId = event.params.user->Ethers.ethAddressToString //The optional User entity that may exist already at "userId" - //This value would be None in the case that it was not loaded in the - //loader function above OR in the case where it never existed in the db - let maybeCurrentUserEntity = context.user.get(userId) + //This value would be None in the case that it never existed in the db + let maybeCurrentUserEntity = await context.user.get(userId) switch maybeCurrentUserEntity { //Only make any changes in the case that there is an existing User diff --git a/codegenerator/cli/templates/static/greeter_template/rescript/test/Test.res b/codegenerator/cli/templates/static/greeter_template/rescript/test/Test.res index 016829991..36364f1e7 100644 --- a/codegenerator/cli/templates/static/greeter_template/rescript/test/Test.res +++ b/codegenerator/cli/templates/static/greeter_template/rescript/test/Test.res @@ -3,7 +3,7 @@ open Mocha open Belt describe("Greeter template tests", () => { - it("A NewGreeting event creates a User entity", () => { + RescriptMocha.Promise.it("A NewGreeting event creates a User entity", async () => { // Initializing the mock database let mockDbInitial = TestHelpers.MockDb.createMockDb() @@ -18,7 +18,7 @@ describe("Greeter template tests", () => { }) // Processing the mock event on the mock database - let updatedMockDb = TestHelpers.Greeter.NewGreeting.processEvent({ + let updatedMockDb = await TestHelpers.Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent, mockDb: mockDbInitial, }) @@ -39,7 +39,7 @@ describe("Greeter template tests", () => { Assert.deep_equal(expectedUserEntity, actualUserEntity) }) - it("2 Greetings from the same users results in that user having a greeter count of 2", () => { + RescriptMocha.Promise.it("2 Greetings from the same users results in that user having a greeter count of 2", async () => { // Initializing the mock database let mockDbInitial = TestHelpers.MockDb.createMockDb() @@ -61,13 +61,13 @@ describe("Greeter template tests", () => { }) // Processing the mock event on the mock database - let updatedMockDb = TestHelpers.Greeter.NewGreeting.processEvent({ + let updatedMockDb = await TestHelpers.Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent, mockDb: mockDbInitial, }) // Processing the mock event on the updated mock database - let updatedMockDb2 = TestHelpers.Greeter.NewGreeting.processEvent({ + let updatedMockDb2 = await TestHelpers.Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent2, mockDb: updatedMockDb, }) @@ -82,9 +82,9 @@ describe("Greeter template tests", () => { Assert.equal(actualUserEntity.numberOfGreetings, expectedGreetingCount) }) - it( + RescriptMocha.Promise.it( "2 Greetings from the same users results in the latest greeting being the greeting from the second event", - () => { + async () => { // Initializing the mock database let mockDbInitial = TestHelpers.MockDb.createMockDb() @@ -106,13 +106,13 @@ describe("Greeter template tests", () => { }) // Processing the mock event on the mock database - let updatedMockDb = TestHelpers.Greeter.NewGreeting.processEvent({ + let updatedMockDb = await TestHelpers.Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent, mockDb: mockDbInitial, }) // Processing the mock event on the updated mock database - let updatedMockDb2 = TestHelpers.Greeter.NewGreeting.processEvent({ + let updatedMockDb2 = await TestHelpers.Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent2, mockDb: updatedMockDb, }) diff --git a/codegenerator/cli/templates/static/greeter_template/typescript/src/EventHandlers.ts b/codegenerator/cli/templates/static/greeter_template/typescript/src/EventHandlers.ts index bc52af53a..5f68a2792 100644 --- a/codegenerator/cli/templates/static/greeter_template/typescript/src/EventHandlers.ts +++ b/codegenerator/cli/templates/static/greeter_template/typescript/src/EventHandlers.ts @@ -1,32 +1,19 @@ import { GreeterContract, UserEntity } from "generated"; -/** -Registers a loader that loads any values from your database that your -NewGreeting event handler might need on the Greeter contract. -*/ -GreeterContract.NewGreeting.loader(({ event, context }) => { - //The id for the "User" entity derived from params of the NewGreeting event - const userId = event.params.user; - //Try load in in a "User" entity with id of the user param on the - //NewGreeting event - context.User.load(userId); -}); - /** Registers a handler that handles any values from the NewGreeting event on the Greeter contract and index these values into the DB. */ -GreeterContract.NewGreeting.handler(({ event, context }) => { +GreeterContract.NewGreeting.handler(async ({ event, context }) => { //The id for the "User" entity const userId = event.params.user; //The greeting string that was added. const latestGreeting = event.params.greeting; //The optional User entity that may exist already at "userId" - //This value would be undefined in the case that it was not loaded in the - //loader function above OR in the case where it never existed in the db - const currentUserEntity = context.User.get(userId); + //This value would be undefined in the case that it never existed in the db + const currentUserEntity = await context.User.get(userId); //Construct the userEntity that is to be set in the DB const userEntity: UserEntity = currentUserEntity @@ -53,30 +40,17 @@ GreeterContract.NewGreeting.handler(({ event, context }) => { context.User.set(userEntity); }); -/** -Registers a loader that loads any values from your database that your -ClearGreeting event handler might need on the Greeter contract. -*/ -GreeterContract.ClearGreeting.loader(({ event, context }) => { - //The id for the "User" entity derived from params of the ClearGreeting event - const userId = event.params.user; - //Try load in in a "User" entity with id of the user param on the - //ClearGreeting event - context.User.load(userId); -}); - /** Registers a handler that handles any values from the ClearGreeting event on the Greeter contract and index these values into the DB. */ -GreeterContract.ClearGreeting.handler(({ event, context }) => { +GreeterContract.ClearGreeting.handler(async ({ event, context }) => { //The id for the "User" entity derived from params of the ClearGreeting event const userId = event.params.user; //The optional User entity that may exist already at "userId" - //This value would be "undefined" in the case that it was not loaded in the - //loader function above OR in the case where it never existed in the db - const currentUserEntity = context.User.get(userId); + //This value would be "undefined" in the case that it never existed in the db + const currentUserEntity = await context.User.get(userId); if (currentUserEntity) { //Only make any changes in the case that there is an existing User diff --git a/codegenerator/cli/templates/static/greeter_template/typescript/test/test.ts b/codegenerator/cli/templates/static/greeter_template/typescript/test/test.ts index d568f4f3f..fc839553e 100644 --- a/codegenerator/cli/templates/static/greeter_template/typescript/test/test.ts +++ b/codegenerator/cli/templates/static/greeter_template/typescript/test/test.ts @@ -3,7 +3,7 @@ import { TestHelpers, UserEntity } from "generated"; const { MockDb, Greeter, Addresses } = TestHelpers; describe("Greeter template tests", () => { - it("A NewGreeting event creates a User entity", () => { + it("A NewGreeting event creates a User entity", async () => { // Initializing the mock database const mockDbInitial = MockDb.createMockDb(); @@ -18,7 +18,7 @@ describe("Greeter template tests", () => { }); // Processing the mock event on the mock database - const updatedMockDb = Greeter.NewGreeting.processEvent({ + const updatedMockDb = await Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent, mockDb: mockDbInitial, }); @@ -38,7 +38,7 @@ describe("Greeter template tests", () => { assert.deepEqual(expectedUserEntity, actualUserEntity); }); - it("2 Greetings from the same users results in that user having a greeter count of 2", () => { + it("2 Greetings from the same users results in that user having a greeter count of 2", async () => { // Initializing the mock database const mockDbInitial = MockDb.createMockDb(); // Initializing values for mock event @@ -59,13 +59,13 @@ describe("Greeter template tests", () => { }); // Processing the mock event on the mock database - const updatedMockDb = Greeter.NewGreeting.processEvent({ + const updatedMockDb = await Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent, mockDb: mockDbInitial, }); // Processing the mock event on the updated mock database - const updatedMockDb2 = Greeter.NewGreeting.processEvent({ + const updatedMockDb2 = await Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent2, mockDb: updatedMockDb, }); @@ -77,7 +77,7 @@ describe("Greeter template tests", () => { assert.equal(2, actualUserEntity?.numberOfGreetings); }); - it("2 Greetings from the same users results in the latest greeting being the greeting from the second event", () => { + it("2 Greetings from the same users results in the latest greeting being the greeting from the second event", async () => { // Initializing the mock database const mockDbInitial = MockDb.createMockDb(); // Initializing values for mock event @@ -98,13 +98,13 @@ describe("Greeter template tests", () => { }); // Processing the mock event on the mock database - const updatedMockDb = Greeter.NewGreeting.processEvent({ + const updatedMockDb = await Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent, mockDb: mockDbInitial, }); // Processing the mock event on the updated mock database - const updatedMockDb2 = Greeter.NewGreeting.processEvent({ + const updatedMockDb2 = await Greeter.NewGreeting.processEvent({ event: mockNewGreetingEvent2, mockDb: updatedMockDb, });