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..230e58593 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 @@ -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|}} @@ -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..8cb8f1cec 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 @@ -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 |}} @@ -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,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)), } 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..cf43fae59 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 @@ -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|}} @@ -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 = { 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..b7fae6e63 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 @@ -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; @@ -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 |}} @@ -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 ); @@ -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,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}} 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..eda2849f9 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"); +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, @@ -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); +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, }; @@ -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..c8082ba8d 100644 --- a/codegenerator/cli/templates/static/erc20_template/rescript/src/EventHandlers.res +++ b/codegenerator/cli/templates/static/erc20_template/rescript/src/EventHandlers.res @@ -1,28 +1,24 @@ 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.ERC20.Approval.handler(async ({event, context}) => { + let ownerAccount = await context.account.get(event.params.owner->Ethers.ethAddressToString) if(ownerAccount->Belt.Option.isNone) { - // setting accountEntity object - let accountObject: accountEntity = { + // setting Entities.Account.t object + let accountObject: Entities.Account.t = { id: event.params.owner->Ethers.ethAddressToString, balance: Ethers.BigInt.fromInt(0), } - // setting the accountEntity with the new transfer field value + // setting the account-entity with the new transfer field value context.account.set(accountObject) } let approvalId = event.params.owner->Ethers.ethAddressToString ++ "-" ++ event.params.spender->Ethers.ethAddressToString; - let approvalObject: approvalEntity = { + let approvalObject: Entities.Approval.t = { id: approvalId, amount: event.params.value, owner_id: event.params.owner->Ethers.ethAddressToString, @@ -34,18 +30,13 @@ 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.ERC20.Transfer.handler(async ({event, context}) => { + let senderAccount = await context.account.get(event.params.from->Ethers.ethAddressToString) switch senderAccount { | Some(existingSenderAccount) => { // subtract the balance from the existing users balance - let accountObject: accountEntity = { + let accountObject: Entities.Account.t = { id: existingSenderAccount.id, balance: existingSenderAccount.balance->Ethers.BigInt.sub(event.params.value), } @@ -55,22 +46,22 @@ Handlers.ERC20Contract.Transfer.handler(({event, context}) => { | None => { // create the account // This is likely only ever going to be the zero address in the case of the first mint - let accountObject: accountEntity = { + let accountObject: Entities.Account.t = { id: event.params.from->Ethers.ethAddressToString, balance: Ethers.BigInt.fromInt(0)->Ethers.BigInt.sub(event.params.value), } - // setting the accountEntity with the new transfer field value + // setting the account-entity with the new transfer field value context.account.set(accountObject) } } - 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) => { // update existing account's added balance - let accountObject: accountEntity = { + let accountObject: Entities.Account.t = { id: existingReceiverAccount.id, balance: existingReceiverAccount.balance->Ethers.BigInt.add(event.params.value), } @@ -80,7 +71,7 @@ Handlers.ERC20Contract.Transfer.handler(({event, context}) => { | None => { // create new account - let accountObject: accountEntity = { + let accountObject: Entities.Account.t = { id: event.params.to->Ethers.ethAddressToString, balance: event.params.value, } 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..ec080d9d5 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() @@ -13,7 +13,7 @@ describe("Transfers", () => { let userAddress2 = Ethers.Addresses.mockAddresses[1]->Option.getUnsafe //Make a mock entity to set the initial state of the mock db - let mockAccountEntity: Types.accountEntity = { + let mockAccountEntity: Entities.Account.t = { id: userAddress1->Ethers.ethAddressToString, balance: Ethers.BigInt.fromInt(5), } @@ -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..2c9f833ac 100644 --- a/codegenerator/cli/templates/static/erc20_template/typescript/src/EventHandlers.ts +++ b/codegenerator/cli/templates/static/erc20_template/typescript/src/EventHandlers.ts @@ -1,19 +1,14 @@ -import { ERC20Contract, AccountEntity, ApprovalEntity } from "generated"; +import { ERC20, Account, Approval } from "generated"; -ERC20Contract.Approval.loader(({ event, context }) => { - // loading the required Account entity - context.Account.load(event.params.owner.toString()); -}); - -ERC20Contract.Approval.handler(({ event, context }) => { +ERC20.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. // create the account - let accountObject: AccountEntity = { + let accountObject: Account = { id: event.params.owner.toString(), balance: 0n, }; @@ -23,7 +18,7 @@ ERC20Contract.Approval.handler(({ event, context }) => { let approvalId = event.params.owner.toString() + "-" + event.params.spender.toString(); - let approvalObject: ApprovalEntity = { + let approvalObject: Approval = { id: approvalId, amount: event.params.value, owner_id: event.params.owner.toString(), @@ -34,18 +29,13 @@ 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()); +ERC20.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 = { + let accountObject: Account = { id: event.params.from.toString(), balance: 0n - event.params.value, }; @@ -53,25 +43,25 @@ ERC20Contract.Transfer.handler(({ event, context }) => { context.Account.set(accountObject); } else { // subtract the balance from the existing users balance - let accountObject: AccountEntity = { + let accountObject: Account = { id: senderAccount.id, balance: senderAccount.balance - event.params.value, }; 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 = { + let accountObject: Account = { id: event.params.to.toString(), balance: event.params.value, }; context.Account.set(accountObject); } else { // update existing account - let accountObject: AccountEntity = { + let accountObject: Account = { id: receiverAccount.id, balance: receiverAccount.balance + event.params.value, }; 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..bb81f73d8 100644 --- a/codegenerator/cli/templates/static/erc20_template/typescript/test/test.ts +++ b/codegenerator/cli/templates/static/erc20_template/typescript/test/test.ts @@ -1,9 +1,9 @@ import assert from "assert"; -import { TestHelpers, AccountEntity } from "generated"; +import { TestHelpers, Account } 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(); @@ -12,7 +12,7 @@ describe("Transfers", () => { const userAddress2 = Addresses.mockAddresses[1]; //Make a mock entity to set the initial state of the mock db - const mockAccountEntity: AccountEntity = { + const mockAccountEntity: Account = { id: userAddress1, balance: 5n, }; @@ -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..75b615a3f 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); -}); +const { Greeter } = require("generated"); /** 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 }) => { +Greeter.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 }) => { +Greeter.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..7a109dd59 100644 --- a/codegenerator/cli/templates/static/greeter_template/rescript/src/EventHandlers.res +++ b/codegenerator/cli/templates/static/greeter_template/rescript/src/EventHandlers.res @@ -1,35 +1,22 @@ 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.Greeter.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 { + let userEntity: Entities.User.t = switch maybeCurrentUserEntity { //In the case there is an existing "User" entity, update its //latestGreeting value, increment the numberOfGreetings and append latestGreeting //to the array of greetings @@ -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.Greeter.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..79b13ec88 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,13 +18,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, }) // Expected entity that should be created - let expectedUserEntity: Types.userEntity = { + let expectedUserEntity: Entities.User.t = { id: userAddress->Ethers.ethAddressToString, latestGreeting: greeting, numberOfGreetings: 1, @@ -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..0b6765626 100644 --- a/codegenerator/cli/templates/static/greeter_template/typescript/src/EventHandlers.ts +++ b/codegenerator/cli/templates/static/greeter_template/typescript/src/EventHandlers.ts @@ -1,35 +1,22 @@ -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); -}); +import { Greeter, User } from "generated"; /** 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 }) => { +Greeter.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 + const userEntity: User = currentUserEntity ? //In the case there is an existing "User" entity, update its //latestGreeting value, increment the numberOfGreetings and append latestGreeting //to the array of greetings @@ -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 }) => { +Greeter.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..6dee830dc 100644 --- a/codegenerator/cli/templates/static/greeter_template/typescript/test/test.ts +++ b/codegenerator/cli/templates/static/greeter_template/typescript/test/test.ts @@ -1,9 +1,9 @@ import assert from "assert"; -import { TestHelpers, UserEntity } from "generated"; +import { TestHelpers, User } 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,13 +18,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, }); // Expected entity that should be created - const expectedUserEntity: UserEntity = { + const expectedUserEntity: User = { id: userAddress, latestGreeting: greeting, numberOfGreetings: 1, @@ -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/integration_tests/tests/test_indexers/dynamic_contracts/package.json b/codegenerator/integration_tests/tests/test_indexers/dynamic_contracts/package.json index cc388553e..3b2fd15d6 100644 --- a/codegenerator/integration_tests/tests/test_indexers/dynamic_contracts/package.json +++ b/codegenerator/integration_tests/tests/test_indexers/dynamic_contracts/package.json @@ -12,7 +12,7 @@ "start": "ts-node generated/src/Index.bs.js" }, "devDependencies": { - "@types/chai": "^4.3.11", + "@types/chai": "^4.3.11", "@types/mocha": "10.0.6", "@types/node": "20.8.8", "ts-mocha": "^10.0.0", @@ -24,5 +24,8 @@ "chai": "4.3.10", "envio": "0.0.38", "ethers": "6.8.0" + }, + "optionalDependencies": { + "generated": "./generated" } } diff --git a/codegenerator/integration_tests/tests/test_indexers/dynamic_contracts/src/DynamicContractHandlers.ts b/codegenerator/integration_tests/tests/test_indexers/dynamic_contracts/src/DynamicContractHandlers.ts index 7b5eb006f..37c747529 100644 --- a/codegenerator/integration_tests/tests/test_indexers/dynamic_contracts/src/DynamicContractHandlers.ts +++ b/codegenerator/integration_tests/tests/test_indexers/dynamic_contracts/src/DynamicContractHandlers.ts @@ -2,26 +2,23 @@ *Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features* */ import { - FuturesMarketContract, - FuturesMarketManagerContract, -} from "../generated/src/Handlers.gen"; - -import { - FuturesMarket_CacheUpdatedEntity, - FuturesMarket_FundingRecomputedEntity, - FuturesMarket_FuturesTrackingEntity, - FuturesMarket_MarginTransferredEntity, - FuturesMarket_NextPriceOrderRemovedEntity, - FuturesMarket_NextPriceOrderSubmittedEntity, - FuturesMarket_PositionLiquidatedEntity, - FuturesMarket_PositionModifiedEntity, - FuturesMarketManager_MarketAddedEntity, - EventsSummaryEntity, -} from "../generated/src/Types.gen"; + FuturesMarket, + FuturesMarketManager, + FuturesMarket_CacheUpdated, + FuturesMarket_FundingRecomputed, + FuturesMarket_FuturesTracking, + FuturesMarket_MarginTransferred, + FuturesMarket_NextPriceOrderRemoved, + FuturesMarket_NextPriceOrderSubmitted, + FuturesMarket_PositionLiquidated, + FuturesMarket_PositionModified, + FuturesMarketManager_MarketAdded, + 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, futuresMarket_CacheUpdatedCount: BigInt(0), futuresMarket_FundingRecomputedCount: BigInt(0), @@ -34,15 +31,10 @@ const INITIAL_EVENTS_SUMMARY: EventsSummaryEntity = { futuresMarketManager_MarketAddedCount: BigInt(0), }; -FuturesMarketContract.CacheUpdated.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); +FuturesMarket.CacheUpdated.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); -FuturesMarketContract.CacheUpdated.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -50,7 +42,7 @@ FuturesMarketContract.CacheUpdated.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_CacheUpdatedCount + BigInt(1), }; - const futuresMarket_CacheUpdatedEntity: FuturesMarket_CacheUpdatedEntity = { + const futuresMarket_CacheUpdatedEntity: FuturesMarket_CacheUpdated = { id: event.transactionHash + event.logIndex.toString(), name: event.params.name, destination: event.params.destination, @@ -60,15 +52,11 @@ FuturesMarketContract.CacheUpdated.handler(({ event, context }) => { context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_CacheUpdated.set(futuresMarket_CacheUpdatedEntity); }); -FuturesMarketContract.FundingRecomputed.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.FundingRecomputed.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.FundingRecomputed.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -76,29 +64,25 @@ FuturesMarketContract.FundingRecomputed.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_FundingRecomputedCount + BigInt(1), }; - const futuresMarket_FundingRecomputedEntity: FuturesMarket_FundingRecomputedEntity = - { - id: event.transactionHash + event.logIndex.toString(), - funding: event.params.funding, - index: event.params.index, - timestamp: event.params.timestamp, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_FundingRecomputedEntity: FuturesMarket_FundingRecomputed = + { + id: event.transactionHash + event.logIndex.toString(), + funding: event.params.funding, + index: event.params.index, + timestamp: event.params.timestamp, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_FundingRecomputed.set( - futuresMarket_FundingRecomputedEntity + futuresMarket_FundingRecomputedEntity, ); }); -FuturesMarketContract.FuturesTracking.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.FuturesTracking.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.FuturesTracking.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -106,31 +90,26 @@ FuturesMarketContract.FuturesTracking.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_FuturesTrackingCount + BigInt(1), }; - const futuresMarket_FuturesTrackingEntity: FuturesMarket_FuturesTrackingEntity = - { - id: event.transactionHash + event.logIndex.toString(), - trackingCode: event.params.trackingCode, - baseAsset: event.params.baseAsset, - marketKey: event.params.marketKey, - sizeDelta: event.params.sizeDelta, - fee: event.params.fee, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_FuturesTrackingEntity: FuturesMarket_FuturesTracking = { + id: event.transactionHash + event.logIndex.toString(), + trackingCode: event.params.trackingCode, + baseAsset: event.params.baseAsset, + marketKey: event.params.marketKey, + sizeDelta: event.params.sizeDelta, + fee: event.params.fee, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_FuturesTracking.set( - futuresMarket_FuturesTrackingEntity + futuresMarket_FuturesTrackingEntity, ); }); -FuturesMarketContract.MarginTransferred.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.MarginTransferred.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.MarginTransferred.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -138,28 +117,24 @@ FuturesMarketContract.MarginTransferred.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_MarginTransferredCount + BigInt(1), }; - const futuresMarket_MarginTransferredEntity: FuturesMarket_MarginTransferredEntity = - { - id: event.transactionHash + event.logIndex.toString(), - account: event.params.account, - marginDelta: event.params.marginDelta, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_MarginTransferredEntity: FuturesMarket_MarginTransferred = + { + id: event.transactionHash + event.logIndex.toString(), + account: event.params.account, + marginDelta: event.params.marginDelta, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_MarginTransferred.set( - futuresMarket_MarginTransferredEntity + futuresMarket_MarginTransferredEntity, ); }); -FuturesMarketContract.NextPriceOrderRemoved.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.NextPriceOrderRemoved.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.NextPriceOrderRemoved.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -167,33 +142,29 @@ FuturesMarketContract.NextPriceOrderRemoved.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_NextPriceOrderRemovedCount + BigInt(1), }; - const futuresMarket_NextPriceOrderRemovedEntity: FuturesMarket_NextPriceOrderRemovedEntity = - { - id: event.transactionHash + event.logIndex.toString(), - account: event.params.account, - currentRoundId: event.params.currentRoundId, - sizeDelta: event.params.sizeDelta, - targetRoundId: event.params.targetRoundId, - commitDeposit: event.params.commitDeposit, - keeperDeposit: event.params.keeperDeposit, - trackingCode: event.params.trackingCode, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_NextPriceOrderRemovedEntity: FuturesMarket_NextPriceOrderRemoved = + { + id: event.transactionHash + event.logIndex.toString(), + account: event.params.account, + currentRoundId: event.params.currentRoundId, + sizeDelta: event.params.sizeDelta, + targetRoundId: event.params.targetRoundId, + commitDeposit: event.params.commitDeposit, + keeperDeposit: event.params.keeperDeposit, + trackingCode: event.params.trackingCode, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_NextPriceOrderRemoved.set( - futuresMarket_NextPriceOrderRemovedEntity + futuresMarket_NextPriceOrderRemovedEntity, ); }); -FuturesMarketContract.NextPriceOrderSubmitted.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.NextPriceOrderSubmitted.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.NextPriceOrderSubmitted.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -202,32 +173,28 @@ FuturesMarketContract.NextPriceOrderSubmitted.handler(({ event, context }) => { BigInt(1), }; - const futuresMarket_NextPriceOrderSubmittedEntity: FuturesMarket_NextPriceOrderSubmittedEntity = - { - id: event.transactionHash + event.logIndex.toString(), - account: event.params.account, - sizeDelta: event.params.sizeDelta, - targetRoundId: event.params.targetRoundId, - commitDeposit: event.params.commitDeposit, - keeperDeposit: event.params.keeperDeposit, - trackingCode: event.params.trackingCode, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_NextPriceOrderSubmittedEntity: FuturesMarket_NextPriceOrderSubmitted = + { + id: event.transactionHash + event.logIndex.toString(), + account: event.params.account, + sizeDelta: event.params.sizeDelta, + targetRoundId: event.params.targetRoundId, + commitDeposit: event.params.commitDeposit, + keeperDeposit: event.params.keeperDeposit, + trackingCode: event.params.trackingCode, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_NextPriceOrderSubmitted.set( - futuresMarket_NextPriceOrderSubmittedEntity + futuresMarket_NextPriceOrderSubmittedEntity, ); }); -FuturesMarketContract.PositionLiquidated.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.PositionLiquidated.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.PositionLiquidated.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -235,32 +202,28 @@ FuturesMarketContract.PositionLiquidated.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_PositionLiquidatedCount + BigInt(1), }; - const futuresMarket_PositionLiquidatedEntity: FuturesMarket_PositionLiquidatedEntity = - { - id: event.transactionHash + event.logIndex.toString(), - event_id: event.params.id, - account: event.params.account, - liquidator: event.params.liquidator, - size: event.params.size, - price: event.params.price, - fee: event.params.fee, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_PositionLiquidatedEntity: FuturesMarket_PositionLiquidated = + { + id: event.transactionHash + event.logIndex.toString(), + event_id: event.params.id, + account: event.params.account, + liquidator: event.params.liquidator, + size: event.params.size, + price: event.params.price, + fee: event.params.fee, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_PositionLiquidated.set( - futuresMarket_PositionLiquidatedEntity + futuresMarket_PositionLiquidatedEntity, ); }); -FuturesMarketContract.PositionModified.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.PositionModified.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.PositionModified.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -268,36 +231,33 @@ FuturesMarketContract.PositionModified.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_PositionModifiedCount + BigInt(1), }; - const futuresMarket_PositionModifiedEntity: FuturesMarket_PositionModifiedEntity = - { - id: event.transactionHash + event.logIndex.toString(), - event_id: event.params.id, - account: event.params.account, - margin: event.params.margin, - size: event.params.size, - tradeSize: event.params.tradeSize, - lastPrice: event.params.lastPrice, - fundingIndex: event.params.fundingIndex, - fee: event.params.fee, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_PositionModifiedEntity: FuturesMarket_PositionModified = { + id: event.transactionHash + event.logIndex.toString(), + event_id: event.params.id, + account: event.params.account, + margin: event.params.margin, + size: event.params.size, + tradeSize: event.params.tradeSize, + lastPrice: event.params.lastPrice, + fundingIndex: event.params.fundingIndex, + fee: event.params.fee, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_PositionModified.set( - futuresMarket_PositionModifiedEntity + futuresMarket_PositionModifiedEntity, ); }); -FuturesMarketManagerContract.MarketAdded.loader(({ event, context }) => { - context.contractRegistration.addFuturesMarket(event.params.market); - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarketManager.MarketAdded.contractRegister(({ event, context }) => { + context.addFuturesMarket(event.params.market); }); -FuturesMarketManagerContract.MarketAdded.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarketManager.MarketAdded.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -305,17 +265,17 @@ FuturesMarketManagerContract.MarketAdded.handler(({ event, context }) => { currentSummaryEntity.futuresMarketManager_MarketAddedCount + BigInt(1), }; - const futuresMarketManager_MarketAddedEntity: FuturesMarketManager_MarketAddedEntity = - { - id: event.transactionHash + event.logIndex.toString(), - market: event.params.market, - asset: event.params.asset, - marketKey: event.params.marketKey, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarketManager_MarketAddedEntity: FuturesMarketManager_MarketAdded = + { + id: event.transactionHash + event.logIndex.toString(), + market: event.params.market, + asset: event.params.asset, + marketKey: event.params.marketKey, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarketManager_MarketAdded.set( - futuresMarketManager_MarketAddedEntity + futuresMarketManager_MarketAddedEntity, ); }); diff --git a/codegenerator/integration_tests/tests/test_indexers/test_exits/package.json b/codegenerator/integration_tests/tests/test_indexers/test_exits/package.json index 088aa8e34..1926977bf 100644 --- a/codegenerator/integration_tests/tests/test_indexers/test_exits/package.json +++ b/codegenerator/integration_tests/tests/test_indexers/test_exits/package.json @@ -15,5 +15,8 @@ "dependencies": { "ts-node": "^10.9.1", "envio": "latest" + }, + "optionalDependencies": { + "generated": "./generated" } } diff --git a/codegenerator/integration_tests/tests/test_indexers/test_exits/src/DynamicContractHandlers.ts b/codegenerator/integration_tests/tests/test_indexers/test_exits/src/DynamicContractHandlers.ts index 7b5eb006f..ff6cfc6ac 100644 --- a/codegenerator/integration_tests/tests/test_indexers/test_exits/src/DynamicContractHandlers.ts +++ b/codegenerator/integration_tests/tests/test_indexers/test_exits/src/DynamicContractHandlers.ts @@ -2,26 +2,25 @@ *Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features* */ import { - FuturesMarketContract, - FuturesMarketManagerContract, -} from "../generated/src/Handlers.gen"; - -import { - FuturesMarket_CacheUpdatedEntity, - FuturesMarket_FundingRecomputedEntity, - FuturesMarket_FuturesTrackingEntity, - FuturesMarket_MarginTransferredEntity, - FuturesMarket_NextPriceOrderRemovedEntity, - FuturesMarket_NextPriceOrderSubmittedEntity, - FuturesMarket_PositionLiquidatedEntity, - FuturesMarket_PositionModifiedEntity, - FuturesMarketManager_MarketAddedEntity, - EventsSummaryEntity, -} from "../generated/src/Types.gen"; + //contracts + FuturesMarket, FuturesMarketManager + //entities + FuturesMarket_CacheUpdated, + FuturesMarket_FundingRecomputed, + FuturesMarket_FuturesTracking, + FuturesMarket_MarginTransferred, + FuturesMarket_NextPriceOrderRemoved, + FuturesMarket_NextPriceOrderSubmitted, + FuturesMarket_PositionLiquidated, + FuturesMarket_PositionModified, + FuturesMarketManager_MarketAdded, + 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, futuresMarket_CacheUpdatedCount: BigInt(0), futuresMarket_FundingRecomputedCount: BigInt(0), @@ -34,15 +33,11 @@ const INITIAL_EVENTS_SUMMARY: EventsSummaryEntity = { futuresMarketManager_MarketAddedCount: BigInt(0), }; -FuturesMarketContract.CacheUpdated.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.CacheUpdated.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.CacheUpdated.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -50,7 +45,7 @@ FuturesMarketContract.CacheUpdated.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_CacheUpdatedCount + BigInt(1), }; - const futuresMarket_CacheUpdatedEntity: FuturesMarket_CacheUpdatedEntity = { + const futuresMarket_CacheUpdatedEntity: FuturesMarket_CacheUpdated = { id: event.transactionHash + event.logIndex.toString(), name: event.params.name, destination: event.params.destination, @@ -60,15 +55,11 @@ FuturesMarketContract.CacheUpdated.handler(({ event, context }) => { context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_CacheUpdated.set(futuresMarket_CacheUpdatedEntity); }); -FuturesMarketContract.FundingRecomputed.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.FundingRecomputed.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.FundingRecomputed.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -76,29 +67,25 @@ FuturesMarketContract.FundingRecomputed.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_FundingRecomputedCount + BigInt(1), }; - const futuresMarket_FundingRecomputedEntity: FuturesMarket_FundingRecomputedEntity = - { - id: event.transactionHash + event.logIndex.toString(), - funding: event.params.funding, - index: event.params.index, - timestamp: event.params.timestamp, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_FundingRecomputedEntity: FuturesMarket_FundingRecomputed = + { + id: event.transactionHash + event.logIndex.toString(), + funding: event.params.funding, + index: event.params.index, + timestamp: event.params.timestamp, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_FundingRecomputed.set( - futuresMarket_FundingRecomputedEntity + futuresMarket_FundingRecomputedEntity, ); }); -FuturesMarketContract.FuturesTracking.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.FuturesTracking.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.FuturesTracking.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -106,31 +93,26 @@ FuturesMarketContract.FuturesTracking.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_FuturesTrackingCount + BigInt(1), }; - const futuresMarket_FuturesTrackingEntity: FuturesMarket_FuturesTrackingEntity = - { - id: event.transactionHash + event.logIndex.toString(), - trackingCode: event.params.trackingCode, - baseAsset: event.params.baseAsset, - marketKey: event.params.marketKey, - sizeDelta: event.params.sizeDelta, - fee: event.params.fee, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_FuturesTrackingEntity: FuturesMarket_FuturesTracking = { + id: event.transactionHash + event.logIndex.toString(), + trackingCode: event.params.trackingCode, + baseAsset: event.params.baseAsset, + marketKey: event.params.marketKey, + sizeDelta: event.params.sizeDelta, + fee: event.params.fee, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_FuturesTracking.set( - futuresMarket_FuturesTrackingEntity + futuresMarket_FuturesTrackingEntity, ); }); -FuturesMarketContract.MarginTransferred.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.MarginTransferred.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.MarginTransferred.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -138,28 +120,24 @@ FuturesMarketContract.MarginTransferred.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_MarginTransferredCount + BigInt(1), }; - const futuresMarket_MarginTransferredEntity: FuturesMarket_MarginTransferredEntity = - { - id: event.transactionHash + event.logIndex.toString(), - account: event.params.account, - marginDelta: event.params.marginDelta, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_MarginTransferredEntity: FuturesMarket_MarginTransferred = + { + id: event.transactionHash + event.logIndex.toString(), + account: event.params.account, + marginDelta: event.params.marginDelta, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_MarginTransferred.set( - futuresMarket_MarginTransferredEntity + futuresMarket_MarginTransferredEntity, ); }); -FuturesMarketContract.NextPriceOrderRemoved.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.NextPriceOrderRemoved.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.NextPriceOrderRemoved.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -167,33 +145,29 @@ FuturesMarketContract.NextPriceOrderRemoved.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_NextPriceOrderRemovedCount + BigInt(1), }; - const futuresMarket_NextPriceOrderRemovedEntity: FuturesMarket_NextPriceOrderRemovedEntity = - { - id: event.transactionHash + event.logIndex.toString(), - account: event.params.account, - currentRoundId: event.params.currentRoundId, - sizeDelta: event.params.sizeDelta, - targetRoundId: event.params.targetRoundId, - commitDeposit: event.params.commitDeposit, - keeperDeposit: event.params.keeperDeposit, - trackingCode: event.params.trackingCode, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_NextPriceOrderRemovedEntity: FuturesMarket_NextPriceOrderRemoved = + { + id: event.transactionHash + event.logIndex.toString(), + account: event.params.account, + currentRoundId: event.params.currentRoundId, + sizeDelta: event.params.sizeDelta, + targetRoundId: event.params.targetRoundId, + commitDeposit: event.params.commitDeposit, + keeperDeposit: event.params.keeperDeposit, + trackingCode: event.params.trackingCode, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_NextPriceOrderRemoved.set( - futuresMarket_NextPriceOrderRemovedEntity + futuresMarket_NextPriceOrderRemovedEntity, ); }); -FuturesMarketContract.NextPriceOrderSubmitted.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.NextPriceOrderSubmitted.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.NextPriceOrderSubmitted.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -202,32 +176,28 @@ FuturesMarketContract.NextPriceOrderSubmitted.handler(({ event, context }) => { BigInt(1), }; - const futuresMarket_NextPriceOrderSubmittedEntity: FuturesMarket_NextPriceOrderSubmittedEntity = - { - id: event.transactionHash + event.logIndex.toString(), - account: event.params.account, - sizeDelta: event.params.sizeDelta, - targetRoundId: event.params.targetRoundId, - commitDeposit: event.params.commitDeposit, - keeperDeposit: event.params.keeperDeposit, - trackingCode: event.params.trackingCode, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_NextPriceOrderSubmittedEntity: FuturesMarket_NextPriceOrderSubmitted = + { + id: event.transactionHash + event.logIndex.toString(), + account: event.params.account, + sizeDelta: event.params.sizeDelta, + targetRoundId: event.params.targetRoundId, + commitDeposit: event.params.commitDeposit, + keeperDeposit: event.params.keeperDeposit, + trackingCode: event.params.trackingCode, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_NextPriceOrderSubmitted.set( - futuresMarket_NextPriceOrderSubmittedEntity + futuresMarket_NextPriceOrderSubmittedEntity, ); }); -FuturesMarketContract.PositionLiquidated.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.PositionLiquidated.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.PositionLiquidated.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -235,32 +205,28 @@ FuturesMarketContract.PositionLiquidated.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_PositionLiquidatedCount + BigInt(1), }; - const futuresMarket_PositionLiquidatedEntity: FuturesMarket_PositionLiquidatedEntity = - { - id: event.transactionHash + event.logIndex.toString(), - event_id: event.params.id, - account: event.params.account, - liquidator: event.params.liquidator, - size: event.params.size, - price: event.params.price, - fee: event.params.fee, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_PositionLiquidatedEntity: FuturesMarket_PositionLiquidated = + { + id: event.transactionHash + event.logIndex.toString(), + event_id: event.params.id, + account: event.params.account, + liquidator: event.params.liquidator, + size: event.params.size, + price: event.params.price, + fee: event.params.fee, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_PositionLiquidated.set( - futuresMarket_PositionLiquidatedEntity + futuresMarket_PositionLiquidatedEntity, ); }); -FuturesMarketContract.PositionModified.loader(({ event, context }) => { - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); -}); -FuturesMarketContract.PositionModified.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarket.PositionModified.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -268,36 +234,33 @@ FuturesMarketContract.PositionModified.handler(({ event, context }) => { currentSummaryEntity.futuresMarket_PositionModifiedCount + BigInt(1), }; - const futuresMarket_PositionModifiedEntity: FuturesMarket_PositionModifiedEntity = - { - id: event.transactionHash + event.logIndex.toString(), - event_id: event.params.id, - account: event.params.account, - margin: event.params.margin, - size: event.params.size, - tradeSize: event.params.tradeSize, - lastPrice: event.params.lastPrice, - fundingIndex: event.params.fundingIndex, - fee: event.params.fee, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarket_PositionModifiedEntity: FuturesMarket_PositionModified = { + id: event.transactionHash + event.logIndex.toString(), + event_id: event.params.id, + account: event.params.account, + margin: event.params.margin, + size: event.params.size, + tradeSize: event.params.tradeSize, + lastPrice: event.params.lastPrice, + fundingIndex: event.params.fundingIndex, + fee: event.params.fee, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarket_PositionModified.set( - futuresMarket_PositionModifiedEntity + futuresMarket_PositionModifiedEntity, ); }); -FuturesMarketManagerContract.MarketAdded.loader(({ event, context }) => { - context.contractRegistration.addFuturesMarket(event.params.market); - context.EventsSummary.load(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarketManager.MarketAdded.contractRegistration(({ event, context }) => { + context.addFuturesMarket(event.params.market); }); -FuturesMarketManagerContract.MarketAdded.handler(({ event, context }) => { - const summary = context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); +FuturesMarketManager.MarketAdded.handler(async ({ event, context }) => { + const summary = await context.EventsSummary.get(GLOBAL_EVENTS_SUMMARY_KEY); - const currentSummaryEntity: EventsSummaryEntity = - summary ?? INITIAL_EVENTS_SUMMARY; + const currentSummaryEntity: EventsSummary = summary ?? INITIAL_EVENTS_SUMMARY; const nextSummaryEntity = { ...currentSummaryEntity, @@ -305,17 +268,17 @@ FuturesMarketManagerContract.MarketAdded.handler(({ event, context }) => { currentSummaryEntity.futuresMarketManager_MarketAddedCount + BigInt(1), }; - const futuresMarketManager_MarketAddedEntity: FuturesMarketManager_MarketAddedEntity = - { - id: event.transactionHash + event.logIndex.toString(), - market: event.params.market, - asset: event.params.asset, - marketKey: event.params.marketKey, - eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, - }; + const futuresMarketManager_MarketAddedEntity: FuturesMarketManager_MarketAdded = + { + id: event.transactionHash + event.logIndex.toString(), + market: event.params.market, + asset: event.params.asset, + marketKey: event.params.marketKey, + eventsSummary: GLOBAL_EVENTS_SUMMARY_KEY, + }; context.EventsSummary.set(nextSummaryEntity); context.FuturesMarketManager_MarketAdded.set( - futuresMarketManager_MarketAddedEntity + futuresMarketManager_MarketAddedEntity, ); }); diff --git a/codegenerator/integration_tests/tests/test_indexers/test_exits/src/EventHandler.ts b/codegenerator/integration_tests/tests/test_indexers/test_exits/src/EventHandler.ts index 1c15cfaf4..e5dd6e6f3 100644 --- a/codegenerator/integration_tests/tests/test_indexers/test_exits/src/EventHandler.ts +++ b/codegenerator/integration_tests/tests/test_indexers/test_exits/src/EventHandler.ts @@ -2,19 +2,13 @@ *Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features* */ -import { - SwapContractContract_Swap_loader, - SwapContractContract_Swap_handler, -} from "../generated/src/Handlers.gen"; - -SwapContractContract_Swap_loader(({ event, context }) => { }); - -SwapContractContract_Swap_handler(({ event, context }) => { +import { SwapContract } from "generated"; +SwapContract.Swap.handler(async ({ event, context }) => { context.EventTracker.set({ id: "eventTracker", - count: 1 - }) + count: 1, + }); context.Swap.set({ id: event.transactionHash + event.logIndex,