diff --git a/CHANGELOG.md b/CHANGELOG.md index fa99ef1e5..341cee37a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T # Unreleased - Add `decimals` field to token data queries -- Add support for `validator_transaction` type introduced in 1.10. +- Add support for `validator_transaction` type introduced in 1.10 +- Add `getModuleEventsByEventType` API # 1.7.0 (2024-02-13) diff --git a/src/api/event.ts b/src/api/event.ts index d60584da7..0ea42ca17 100644 --- a/src/api/event.ts +++ b/src/api/event.ts @@ -1,7 +1,12 @@ // Copyright © Aptos Foundation // SPDX-License-Identifier: Apache-2.0 -import { getAccountEventsByCreationNumber, getAccountEventsByEventType, getEvents } from "../internal/event"; +import { + getAccountEventsByCreationNumber, + getAccountEventsByEventType, + getModuleEventsByEventType, + getEvents, +} from "../internal/event"; import { AnyNumber, GetEventsResponse, MoveStructId, OrderByArg, PaginationArgs, WhereArg } from "../types"; import { EventsBoolExp } from "../types/generated/types"; import { AccountAddressInput } from "../core"; @@ -15,6 +20,27 @@ import { waitForIndexerOnVersion } from "./utils"; export class Event { constructor(readonly config: AptosConfig) {} + /** + * Get module events by event type + * + * @param args.eventType - The event type + * @param args.minimumLedgerVersion Optional ledger version to sync up to, before querying + * + * @returns Promise + */ + async getModuleEventsByEventType(args: { + eventType: MoveStructId; + minimumLedgerVersion?: AnyNumber; + options?: PaginationArgs & OrderByArg; + }): Promise { + await waitForIndexerOnVersion({ + config: this.config, + minimumLedgerVersion: args.minimumLedgerVersion, + processorType: ProcessorType.EVENTS_PROCESSOR, + }); + return getModuleEventsByEventType({ aptosConfig: this.config, ...args }); + } + /** * Get events by creation number and an account address * diff --git a/src/internal/event.ts b/src/internal/event.ts index 32ceef2d9..66af1b3cf 100644 --- a/src/internal/event.ts +++ b/src/internal/event.ts @@ -13,9 +13,39 @@ import { AccountAddress, AccountAddressInput } from "../core"; import { AnyNumber, GetEventsResponse, PaginationArgs, MoveStructId, OrderByArg, WhereArg } from "../types"; import { GetEventsQuery } from "../types/generated/operations"; import { GetEvents } from "../types/generated/queries"; -import { EventsBoolExp } from "../types/generated/types"; +import { EventsBoolExp, InputMaybe } from "../types/generated/types"; import { queryIndexer } from "./general"; +const MAX_EVENT_TYPE_LENGTH = 300; +const checkEventTypeLength = (eventType?: InputMaybe) => { + if (eventType && eventType.length > MAX_EVENT_TYPE_LENGTH) { + throw new Error(`Event type length exceeds the maximum length of ${MAX_EVENT_TYPE_LENGTH}`); + } +}; + +export async function getModuleEventsByEventType(args: { + aptosConfig: AptosConfig; + eventType: MoveStructId; + options?: PaginationArgs & OrderByArg; +}): Promise { + const { aptosConfig, eventType, options } = args; + + const whereCondition: EventsBoolExp = { + account_address: { _eq: "0x0000000000000000000000000000000000000000000000000000000000000000" }, + creation_number: { _eq: "0" }, + sequence_number: { _eq: "0" }, + indexed_type: { _eq: eventType }, + }; + + const customOptions = { + where: whereCondition, + pagination: options, + orderBy: options?.orderBy, + }; + + return getEvents({ aptosConfig, options: customOptions }); +} + export async function getAccountEventsByCreationNumber(args: { aptosConfig: AptosConfig; accountAddress: AccountAddressInput; @@ -67,6 +97,8 @@ export async function getEvents(args: { options?: PaginationArgs & OrderByArg & WhereArg; }): Promise { const { aptosConfig, options } = args; + // eslint-disable-next-line no-underscore-dangle + checkEventTypeLength(options?.where?.indexed_type?._eq); const graphqlQuery = { query: GetEvents, diff --git a/tests/e2e/api/event.test.ts b/tests/e2e/api/event.test.ts index 479f37837..821a4305d 100644 --- a/tests/e2e/api/event.test.ts +++ b/tests/e2e/api/event.test.ts @@ -5,6 +5,20 @@ import { Account, Aptos, AptosConfig, Network } from "../../../src"; import { FUND_AMOUNT, longTestTimeout } from "../../unit/helper"; describe("Event", () => { + test("it should get transaction fee statement module event by event type", async () => { + const config = new AptosConfig({ network: Network.LOCAL }); + const aptos = new Aptos(config); + + const testAccount = Account.generate(); + await aptos.fundAccount({ accountAddress: testAccount.accountAddress, amount: FUND_AMOUNT }); + + const events = await aptos.getModuleEventsByEventType({ + eventType: "0x1::transaction_fee::FeeStatement", + }); + + expect(events[0].type).toEqual("0x1::transaction_fee::FeeStatement"); + }); + test("it should get fund event by creation number and address", async () => { const config = new AptosConfig({ network: Network.LOCAL }); const aptos = new Aptos(config);