Skip to content

Commit

Permalink
Add API to get module event (#304)
Browse files Browse the repository at this point in the history
* add get module event api

* nit

* add eventType length check

* update changelog
  • Loading branch information
0xaptosj authored Feb 23, 2024
1 parent 593a31a commit a9b634c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
28 changes: 27 additions & 1 deletion src/api/event.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<GetEventsResponse>
*/
async getModuleEventsByEventType(args: {
eventType: MoveStructId;
minimumLedgerVersion?: AnyNumber;
options?: PaginationArgs & OrderByArg<GetEventsResponse[0]>;
}): Promise<GetEventsResponse> {
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
*
Expand Down
34 changes: 33 additions & 1 deletion src/internal/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>) => {
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<GetEventsResponse[0]>;
}): Promise<GetEventsResponse> {
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;
Expand Down Expand Up @@ -67,6 +97,8 @@ export async function getEvents(args: {
options?: PaginationArgs & OrderByArg<GetEventsResponse[0]> & WhereArg<EventsBoolExp>;
}): Promise<GetEventsResponse> {
const { aptosConfig, options } = args;
// eslint-disable-next-line no-underscore-dangle
checkEventTypeLength(options?.where?.indexed_type?._eq);

const graphqlQuery = {
query: GetEvents,
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/api/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a9b634c

Please sign in to comment.