Skip to content

Commit

Permalink
Add getEventsByCreationNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin committed Oct 13, 2023
1 parent 9fc979b commit 40ccc2e
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/api/aptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Account } from "./account";
import { AptosConfig } from "./aptos_config";
import { Coin } from "./coin";
import { Event } from "./event";
import { Faucet } from "./faucet";
import { General } from "./general";
import { Transaction } from "./transaction";
Expand All @@ -16,6 +17,8 @@ export class Aptos {

readonly coin: Coin;

readonly event: Event;

readonly faucet: Faucet;

readonly general: General;
Expand Down Expand Up @@ -45,14 +48,15 @@ export class Aptos {
this.config = new AptosConfig(settings);
this.account = new Account(this.config);
this.coin = new Coin(this.config);
this.event = new Event(this.config);
this.faucet = new Faucet(this.config);
this.general = new General(this.config);
this.transaction = new Transaction(this.config);
this.transactionSubmission = new TransactionSubmission(this.config);
}
}

export interface Aptos extends Account, Coin, Faucet, General, Transaction, TransactionSubmission {}
export interface Aptos extends Account, Coin, Event, Faucet, General, Transaction, TransactionSubmission {}

/**
In TypeScript, we can’t inherit or extend from more than one class,
Expand All @@ -77,6 +81,7 @@ function applyMixin(targetClass: any, baseClass: any, baseClassProp: string) {

applyMixin(Aptos, Account, "account");
applyMixin(Aptos, Coin, "coin");
applyMixin(Aptos, Event, "event");
applyMixin(Aptos, Faucet, "faucet");
applyMixin(Aptos, General, "general");
applyMixin(Aptos, Transaction, "transaction");
Expand Down
32 changes: 32 additions & 0 deletions src/api/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

import { getEventsByCreationNumber } from "../internal/event";
import { AnyNumber, GetEventsByCreationNumberResponse } from "../types";
import { AptosConfig } from "./aptos_config";

/**
* A class to query all `Event` Aptos related queries
*/
export class Event {
readonly config: AptosConfig;

constructor(config: AptosConfig) {
this.config = config;
}

/**
* Get events by creation number and the address
*
* @param args.address - The account address
* @param args.creationNumber - The event creation number
* @returns Promise<GetEventsByCreationNumberResponse>
*/
async getEventsByCreationNumber(args: {
address: string;
creationNumber: AnyNumber;
}): Promise<GetEventsByCreationNumberResponse> {
const data = await getEventsByCreationNumber({ aptosConfig: this.config, ...args });
return data;
}
}
43 changes: 43 additions & 0 deletions src/internal/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

/**
* This file contains the underlying implementations for exposed API surface in
* the {@link api/event}. By moving the methods out into a separate file,
* other namespaces and processes can access these methods without depending on the entire
* account namespace and without having a dependency cycle error.
*/

import { AptosConfig } from "../api/aptos_config";
import { AnyNumber, GetEventsByCreationNumberResponse, HexInput } from "../types";
import { GetEventsByCreationNumberQuery } from "../types/generated/operations";
import { GetEventsByCreationNumber } from "../types/generated/queries";
import { queryIndexer } from "./general";

/**
* Get events by creation number and the address
*
* @param args.aptosConfig - The aptos config
* @param args.address - The account address
* @param args.creationNumber - The event creation number
* @returns Promise<GetEventsByCreationNumberResponse>
*/
export async function getEventsByCreationNumber(args: {
aptosConfig: AptosConfig;
address: HexInput;
creationNumber: AnyNumber;
}): Promise<GetEventsByCreationNumberResponse> {
const { aptosConfig, address, creationNumber } = args;
const graphqlQuery = {
query: GetEventsByCreationNumber,
variables: { address, creationNumber },
};

const data = await queryIndexer<GetEventsByCreationNumberQuery>({
aptosConfig,
query: graphqlQuery,
originMethod: "getEventsByCreationNumber",
});

return data.events;
}
12 changes: 12 additions & 0 deletions src/internal/queries/getEventsByCreationNumber.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query getEventsByCreationNumber($address: String, $creationNumber: bigint) {
events(where: { creation_number: { _eq: $creationNumber }, account_address: { _eq: $address } }) {
sequence_number
type
transaction_version
transaction_block_height
event_index
data
creation_number
account_address
}
}
8 changes: 8 additions & 0 deletions src/types/generated/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,11 @@ export type GetChainTopUserTransactionsQueryVariables = Types.Exact<{


export type GetChainTopUserTransactionsQuery = { user_transactions: Array<{ version: any }> };

export type GetEventsByCreationNumberQueryVariables = Types.Exact<{
address?: Types.InputMaybe<Types.Scalars['String']>;
creationNumber?: Types.InputMaybe<Types.Scalars['bigint']>;
}>;


export type GetEventsByCreationNumberQuery = { events: Array<{ sequence_number: any, type: string, transaction_version: any, transaction_block_height: any, event_index: any, data: any, creation_number: any, account_address: string }> };
19 changes: 19 additions & 0 deletions src/types/generated/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,22 @@ export const GetChainTopUserTransactions = `
}
}
`;
export const GetEventsByCreationNumber = `
query getEventsByCreationNumber($address: String, $creationNumber: bigint) {
events(
where: {creation_number: {_eq: $creationNumber}, account_address: {_eq: $address}}
) {
sequence_number
type
transaction_version
transaction_block_height
event_index
data
creation_number
account_address
}
}
`;

export type SdkFunctionWrapper = <T>(action: (requestHeaders?:Record<string, string>) => Promise<T>, operationName: string, operationType?: string) => Promise<T>;

Expand Down Expand Up @@ -249,6 +265,9 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper =
},
getChainTopUserTransactions(variables?: Types.GetChainTopUserTransactionsQueryVariables, requestHeaders?: Dom.RequestInit["headers"]): Promise<Types.GetChainTopUserTransactionsQuery> {
return withWrapper((wrappedRequestHeaders) => client.request<Types.GetChainTopUserTransactionsQuery>(GetChainTopUserTransactions, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'getChainTopUserTransactions', 'query');
},
getEventsByCreationNumber(variables?: Types.GetEventsByCreationNumberQueryVariables, requestHeaders?: Dom.RequestInit["headers"]): Promise<Types.GetEventsByCreationNumberQuery> {
return withWrapper((wrappedRequestHeaders) => client.request<Types.GetEventsByCreationNumberQuery>(GetEventsByCreationNumber, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'getEventsByCreationNumber', 'query');
}
};
}
Expand Down
2 changes: 2 additions & 0 deletions src/types/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
GetAccountOwnedTokensFromCollectionQuery,
GetAccountCollectionsWithOwnedTokensQuery,
GetChainTopUserTransactionsQuery,
GetEventsByCreationNumberQuery,
} from "./generated/operations";

/**
Expand Down Expand Up @@ -49,6 +50,7 @@ export type GetAccountCollectionsWithOwnedTokenResponse =
GetAccountCollectionsWithOwnedTokensQuery["current_collection_ownership_v2_view"];
export type GetAccountCoinsDataResponse = GetAccountCoinsDataQuery["current_fungible_asset_balances"];
export type GetChainTopUserTransactionsResponse = GetChainTopUserTransactionsQuery["user_transactions"];
export type GetEventsByCreationNumberResponse = GetEventsByCreationNumberQuery["events"];

/**
* A generic type that being passed by each function and holds an
Expand Down
24 changes: 24 additions & 0 deletions tests/e2e/api/event.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

import { Account, Aptos, AptosConfig, Network, SigningScheme } from "../../../src";

describe("Event", () => {
test("it should get fund events by creation number and address", async () => {
const config = new AptosConfig({ network: Network.TESTNET });
const aptos = new Aptos(config);

// Fund the account
const testAccount = Account.generate({ scheme: SigningScheme.Ed25519 });
await aptos.fundAccount({ accountAddress: testAccount.accountAddress.toString(), amount: 10_000_000 });

const events = await aptos.getEventsByCreationNumber({
address: testAccount.accountAddress.toString(),
creationNumber: 0,
});

// Ensure events are returned and that the event type is CoinRegisterEvent
expect(events.length).toBeGreaterThan(0);
expect(events[0].type).toEqual("0x1::account::CoinRegisterEvent");
});
});

0 comments on commit 40ccc2e

Please sign in to comment.