-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c11946
commit 42b9256
Showing
8 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { getEventsByCreationNumber } from "../internal/event"; | ||
import { AnyNumber, GetEventsResponse } 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<GetEventsResponse> { | ||
const data = await getEventsByCreationNumber({ aptosConfig: this.config, ...args }); | ||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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 | ||
* event namespace and without having a dependency cycle error. | ||
*/ | ||
|
||
import { AptosConfig } from "../api/aptos_config"; | ||
import { AccountAddress } from "../core"; | ||
import { AnyNumber, GetEventsResponse, HexInput } from "../types"; | ||
import { GetEventsQuery } from "../types/generated/operations"; | ||
import { GetEvents } 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<GetEventsResponse> { | ||
const { aptosConfig, creationNumber } = args; | ||
const address = AccountAddress.fromHexInput({ input: args.address }).toString(); | ||
|
||
const whereCondition: any = { | ||
account_address: { _eq: address }, | ||
creation_number: { _eq: creationNumber }, | ||
}; | ||
|
||
const graphqlQuery = { | ||
query: GetEvents, | ||
variables: { where_condition: whereCondition }, | ||
}; | ||
|
||
const data = await queryIndexer<GetEventsQuery>({ | ||
aptosConfig, | ||
query: graphqlQuery, | ||
originMethod: "getEventsByCreationNumber", | ||
}); | ||
|
||
return data.events; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
query getEvents($where_condition: events_bool_exp, $offset: Int, $limit: Int) { | ||
events(where: $where_condition, offset: $offset, limit: $limit) { | ||
sequence_number | ||
type | ||
transaction_version | ||
transaction_block_height | ||
event_index | ||
data | ||
creation_number | ||
account_address | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Account, Aptos, AptosConfig, Network } from "../../../src"; | ||
import { FUND_AMOUNT, INDEXER_WAIT_TIME } from "../../unit/helper"; | ||
import { sleep } from "../../../src/utils/helpers"; | ||
|
||
describe("Event", () => { | ||
test("it should get fund events by creation number and address", async () => { | ||
const config = new AptosConfig({ network: Network.LOCAL }); | ||
const aptos = new Aptos(config); | ||
|
||
// Fund the account | ||
const testAccount = Account.generate(); | ||
await aptos.fundAccount({ accountAddress: testAccount.accountAddress.toString(), amount: FUND_AMOUNT }); | ||
|
||
await sleep(INDEXER_WAIT_TIME); | ||
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"); | ||
}); | ||
}); |