Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS SDK - Event Queries #4503

Merged
merged 122 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
122 commits
Select commit Hold shift + click to select a range
375e989
add subscribeEvent stub
Aug 12, 2022
c46292a
add onMessage callback to subscribeEvent
Aug 12, 2022
6ad6655
add json rpc version of subscribeEvent
Aug 12, 2022
5cbe7fc
add subscribeEvent to void provider
Aug 12, 2022
13cd12f
refine types for event sub, add websocket client instance
Aug 12, 2022
fdd1491
support insecure websocket
Aug 12, 2022
db9daf5
rename subscribeEvent parameter
Aug 12, 2022
98ccb96
update generated type guards
Aug 12, 2022
43fada4
add debug logs to getWebsocketUrl
Aug 12, 2022
b8430d3
add SuiEventEnvelope typescript type
Aug 12, 2022
131c057
remove unused import, update type guards
Aug 12, 2022
a3c94b2
{} to void return for subscribeevent
Aug 12, 2022
1fd4a34
add basic event subscription test box
Aug 12, 2022
d77f785
TEMPORARILY add eventsubscription to home page
Aug 12, 2022
067ec69
remove broken checkbox select
Aug 12, 2022
940d14a
add basic Event Filter Type element
Aug 12, 2022
5d36cb5
lint changes
Aug 12, 2022
007030d
working objectId filter
Aug 12, 2022
c338f77
lint changes
Aug 12, 2022
11be84b
remove select form from event tester
Aug 12, 2022
f47c243
remove jayson WebsocketClient
Aug 12, 2022
9a6c314
remove excess whitespace
Aug 12, 2022
add156f
add Subscription tracking
Aug 12, 2022
0ddeeea
Update index.guard.ts
Aug 12, 2022
b9ccd23
add unsubscribeEvent
Aug 16, 2022
7077204
remove console logs, add comments
Aug 16, 2022
2c89e05
style / organization changes in ts sdk
Aug 17, 2022
3e1f33a
don't re-create websocket client
Aug 22, 2022
513115f
only create one default rpc client per network
Aug 22, 2022
1581667
lint changes
Aug 22, 2022
66bf813
add on socket error
Aug 23, 2022
62682fa
fix message subscription
Aug 23, 2022
4b2d3b3
add unsubscribe rpc call
Aug 23, 2022
59e5002
allow timestamp as number in SuiEventEnvelope
Aug 23, 2022
18ed01d
remove LossLessJSON from event parsing
Aug 23, 2022
4d22654
Update index.guard.ts
Aug 23, 2022
037c1da
move socket event setup to method
Aug 23, 2022
78e80d7
add check for method id
Aug 23, 2022
5af06ba
remove explorer eventSubscription
Aug 23, 2022
b27cda0
make websocket rpc client not auto connection
Aug 23, 2022
3686c6e
bring back explorer test page for event sub
Aug 23, 2022
84405fc
remove heartbeat
Aug 23, 2022
0d2929f
improve comment for socket type cast
Aug 24, 2022
484b3fa
improve timeout handling for websocket connection
Aug 24, 2022
c72c9ea
add FilterSubHandler type
Aug 24, 2022
e1f428a
delete from active subs on remove
Aug 24, 2022
52a4e00
shorten lazy connect comment
Aug 24, 2022
732f537
use one single map to track subscription data
Aug 25, 2022
75b3804
use .values() instead of entries() in sub refresh
Aug 25, 2022
77df956
improve comment about subscription refresh
Aug 25, 2022
6035584
Delete yarn.lock
Aug 25, 2022
a071c94
remove explorer home changes for testing
Aug 25, 2022
17d4ebf
Merge branch 'main' into tx-subscribe
Aug 25, 2022
9a1cc51
use shorter console error
Aug 25, 2022
a4b6c7a
fix provider subscribe return value
Aug 25, 2022
adb4730
fix doc comment on unsubscribe event
Aug 25, 2022
173a95e
remove console.log
Aug 25, 2022
c930f25
use jsdoc comment style
Aug 30, 2022
d283c66
change EventType discriminator enum
Aug 31, 2022
ddb4eea
make timestamp only a number
Aug 31, 2022
039ab01
replace string manipulation with url object
Aug 31, 2022
c126061
update onMessage return types
Aug 31, 2022
b1d2209
change to doing socket setup in connect
Aug 31, 2022
264ea32
add timeout error
Aug 31, 2022
3c869b4
shorten usage of reject
Aug 31, 2022
8ad806d
const instead of let for two vars
Aug 31, 2022
5698806
if spacing change
Aug 31, 2022
51ae889
remove calbackwithType
Aug 31, 2022
136ac7f
Update client.ts
Sep 2, 2022
9b587d8
Update client.ts
Sep 2, 2022
dafd8bd
move websocket client into its own class
Sep 2, 2022
ad1f441
Revert "remove explorer home changes for testing"
Sep 2, 2022
7d2d48e
rename most websocket client symbols
Sep 2, 2022
cce81e3
Revert "Revert "remove explorer home changes for testing""
Sep 2, 2022
79954c2
validate minimum data with validation off
Sep 2, 2022
9bd75ae
Update websocket-client.ts
Sep 2, 2022
33e1d94
add else if in socket message handler
Sep 2, 2022
8fe8fdd
activeSubs -> eventSubs
Sep 2, 2022
bcf1347
wsProvider -> wsClient
Sep 2, 2022
82ca988
move websocket client into rpc folder
Sep 2, 2022
58a34d2
better styling for default websocket options
Sep 2, 2022
766c48a
add maxReconnects option to websockets
Sep 2, 2022
d828f19
Revert "Revert "Revert "remove explorer home changes for testing"""
Sep 2, 2022
4acf1cd
remove old todo
Sep 7, 2022
b8f5f78
Update sdk/typescript/src/providers/void-provider.ts
Sep 7, 2022
007f5be
add better usage comments for event subscription
Sep 7, 2022
b48ad38
Revert "Revert "Revert "Revert "remove explorer home changes for test…
Sep 7, 2022
feba665
minor formatting change in provider
Sep 7, 2022
9334a31
Merge branch 'main' into tx-subscribe
Sep 7, 2022
2ffccec
fix type guard import
Sep 7, 2022
4980844
Update pnpm-lock.yaml
Sep 7, 2022
dd16350
fix capitalization on event filters
Sep 7, 2022
bac0794
Update sdk/typescript/src/rpc/websocket-client.ts
Sep 7, 2022
ab6738e
add websocket config jsdoc
Sep 7, 2022
02b07ee
add jsdoc for websocket client
Sep 7, 2022
97df83f
update type guard
Sep 8, 2022
be5b614
add event query api to TS sdk
Sep 7, 2022
e114868
fix package variable naming format
Sep 7, 2022
be48bc8
use SuiEvents type, add JSDoc for event queries
Sep 7, 2022
55785c6
add JSDoc for getEventsByTransaction
Sep 7, 2022
ea5592e
add JSDoc for options nested params
Sep 7, 2022
7d42761
add missing JSDoc for events options
Sep 7, 2022
1e4ac68
remove extra whitespace
Sep 7, 2022
fb5b9dc
add comment for TimeRangeQueryOptions
Sep 7, 2022
4988d0c
refactor to remove options struct in ts event queries
Sep 7, 2022
2933492
remove TimeRangeQueryOptions
Sep 7, 2022
3f09e50
update JSDoc for Provider
Sep 7, 2022
36aef1d
add defaults to count, startTime, endtime
Sep 7, 2022
ed6034f
remove console.log
Sep 7, 2022
e46990f
fix jsdoc comment for getEventsBySender
Sep 7, 2022
9fcb1cb
remove last options.startTime
Sep 7, 2022
e88ecaf
Merge branch 'main' into event-query
Sep 8, 2022
5fbd4bf
make MoveEvent.fields optional
Sep 8, 2022
29a366a
Merge branch 'main' into event-query
Sep 8, 2022
c8000bd
update eventdisplay to allow for undefined event fields
Sep 8, 2022
95936c5
unwrap connection promise
Sep 9, 2022
16462f4
change to getEventsByModule
Sep 9, 2022
aca2b05
Update sdk/typescript/src/types/events.ts
Sep 9, 2022
6f3f011
Update sdk/typescript/src/providers/provider.ts
Sep 9, 2022
fdabfa3
Update sdk/typescript/src/providers/provider.ts
Sep 9, 2022
dc98c2a
DEFAULT_RESULT_COUNT -> EVENT_QUERY_MAX_LIMIT
Sep 9, 2022
e3178b3
add two event query examples to ts sdk readme
Sep 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion explorer/client/src/components/events/eventDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ function objectContent(label: string, id: ObjectId) {
};
}

function fieldsContent(fields: { [key: string]: any }) {
function fieldsContent(fields: { [key: string]: any } | undefined) {
if (!fields) return [];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is related to my other comment about making MoveEvent.fields optional.

return Object.keys(fields).map((k) => {
return {
label: k,
Expand Down
20 changes: 20 additions & 0 deletions sdk/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ const txn = await provider.getTransaction(
);
```

Fetch transaction events from a transaction digest:

```typescript
import { JsonRpcProvider } from '@mysten/sui.js';
const provider = new JsonRpcProvider('https://gateway.devnet.sui.io:443');
const txEvents = await provider.getEventsByTransaction(
'6mn5W1CczLwitHCO9OIUbqirNrQ0cuKdyxaNe16SAME='
);
```

Fetch events by sender address:

```typescript
import { JsonRpcProvider } from '@mysten/sui.js';
const provider = new JsonRpcProvider('https://gateway.devnet.sui.io:443');
const senderEvents = await provider.getEventsBySender(
'0xbff6ccc8707aa517b4f1b95750a2a8c666012df3'
);
```

For any operations that involves signing or submitting transactions, you should use the `Signer` API. For example:

To transfer a `0x2::coin::Coin<SUI>`:
Expand Down
148 changes: 148 additions & 0 deletions sdk/typescript/src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isSuiMoveNormalizedFunction,
isSuiMoveNormalizedStruct,
isSuiExecuteTransactionResponse,
isSuiEvents
} from '../types/index.guard';
import {
GatewayTxSeqNumber,
Expand All @@ -35,6 +36,13 @@ import {
SubscriptionId,
ExecuteTransactionRequestType,
SuiExecuteTransactionResponse,
SuiAddress,
ObjectOwner,
ObjectId,
SuiEvents,
EVENT_QUERY_MAX_LIMIT,
DEFAULT_START_TIME,
DEFAULT_END_TIME,
} from '../types';
import { SignatureScheme } from '../cryptography/publickey';
import { DEFAULT_CLIENT_OPTIONS, WebsocketClient, WebsocketClientOptions } from '../rpc/websocket-client';
Expand Down Expand Up @@ -431,6 +439,146 @@ export class JsonRpcProvider extends Provider {
}
}

// Events

async getEventsByTransaction(
digest: TransactionDigest,
count: number = EVENT_QUERY_MAX_LIMIT
): Promise<SuiEvents> {
try {
return await this.client.requestWithType(
'sui_getEventsByTransaction',
[digest, count],
isSuiEvents,
this.skipDataValidation
);
} catch (err) {
throw new Error(
`Error getting events by transaction: ${digest}, with error: ${err}`
);
}
}

async getEventsByModule(
package_: string,
module: string,
count: number = EVENT_QUERY_MAX_LIMIT,
startTime: number = DEFAULT_START_TIME,
endTime: number = DEFAULT_END_TIME
): Promise<SuiEvents> {
try {
return await this.client.requestWithType(
'sui_getEventsByModule',
[package_, module, count, startTime, endTime],
isSuiEvents,
this.skipDataValidation
);
} catch (err) {
throw new Error(
`Error getting events by transaction module: ${package_}::${module}, with error: ${err}`
);
}
}

async getEventsByMoveEventStructName(
moveEventStructName: string,
count: number = EVENT_QUERY_MAX_LIMIT,
startTime: number = DEFAULT_START_TIME,
endTime: number = DEFAULT_END_TIME
): Promise<SuiEvents> {
try {
return await this.client.requestWithType(
'sui_getEventsByMoveEventStructName',
[moveEventStructName, count, startTime, endTime],
isSuiEvents,
this.skipDataValidation
);
} catch (err) {
throw new Error(
`Error getting events by move event struct name: ${moveEventStructName}, with error: ${err}`
);
}
}

async getEventsBySender(
sender: SuiAddress,
count: number = EVENT_QUERY_MAX_LIMIT,
startTime: number = DEFAULT_START_TIME,
endTime: number = DEFAULT_END_TIME
): Promise<SuiEvents> {
try {
return await this.client.requestWithType(
'sui_getEventsBySender',
[sender, count, startTime, endTime],
isSuiEvents,
this.skipDataValidation
);
} catch (err) {
throw new Error(
`Error getting events by sender: ${sender}, with error: ${err}`
);
}
}

async getEventsByRecipient(
recipient: ObjectOwner,
count: number = EVENT_QUERY_MAX_LIMIT,
startTime: number = DEFAULT_START_TIME,
endTime: number = DEFAULT_END_TIME
): Promise<SuiEvents> {
try {
return await this.client.requestWithType(
'sui_getEventsByRecipient',
[recipient, count, startTime, endTime],
isSuiEvents,
this.skipDataValidation
);
} catch (err) {
throw new Error(
`Error getting events by receipient: ${recipient}, with error: ${err}`
);
}
}

async getEventsByObject(
object: ObjectId,
count: number = EVENT_QUERY_MAX_LIMIT,
startTime: number = DEFAULT_START_TIME,
endTime: number = DEFAULT_END_TIME
): Promise<SuiEvents> {
try {
return await this.client.requestWithType(
'sui_getEventsByObject',
[object, count, startTime, endTime],
isSuiEvents,
this.skipDataValidation
);
} catch (err) {
throw new Error(
`Error getting events by object: ${object}, with error: ${err}`
);
}
}

async getEventsByTimeRange(
count: number = EVENT_QUERY_MAX_LIMIT,
startTime: number = DEFAULT_START_TIME,
endTime: number = DEFAULT_END_TIME
): Promise<SuiEvents> {
try {
return await this.client.requestWithType(
'sui_getEventsByTimeRange',
[count, startTime, endTime],
isSuiEvents,
this.skipDataValidation
);
} catch (err) {
throw new Error(
`Error getting events by time range: ${startTime} thru ${endTime}, with error: ${err}`
);
}
}

async subscribeEvent(
filter: SuiEventFilter,
onMessage: (event: SuiEventEnvelope) => void
Expand Down
96 changes: 96 additions & 0 deletions sdk/typescript/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import {
SubscriptionId,
ExecuteTransactionRequestType,
SuiExecuteTransactionResponse,
TransactionDigest,
ObjectId,
SuiAddress,
ObjectOwner,
SuiEvents,
} from '../types';

///////////////////////////////
Expand Down Expand Up @@ -140,6 +145,97 @@ export abstract class Provider {

abstract syncAccountState(address: string): Promise<any>;

/**
* Get events for one transaction
* @param digest transaction digest to search by
* @param count max result count
*/
abstract getEventsByTransaction(digest: TransactionDigest, count: number): Promise<SuiEvents>;

/**
* Get events emitted from within the specified Move module
* @param package_ Move package object ID
* @param module Move module name
* @param count max result count
* @param startTime start of time range
* @param endTime end of time range, exclusive
*/
abstract getEventsByModule(
packageId: ObjectId,
module: string,
count: number,
startTime: number,
endTime: number
): Promise<SuiEvents>;

/**
* Get events with a matching Move type name
* @param moveEventStructName Move struct type name
* @param count max result count
* @param startTime start of time range to search
* @param endTime end of time range
*/
abstract getEventsByMoveEventStructName(
moveEventStructName: string,
count: number,
startTime: number,
endTime: number
): Promise<SuiEvents>;

/**
* Get events from transactions sent by a specific address
* @param sender Sui address of the sender of the transaction that generated the event
* @param count max result count
* @param startTime start of time range to search
* @param endTime end of time range
*/
abstract getEventsBySender(
sender: SuiAddress,
count: number,
startTime: number,
endTime: number
): Promise<SuiEvents>;

/**
* Get events with a matching recipient
* @param recipient object owner that received the transaction that generated the event
* @param count max result count
* @param startTime start of time range to search
* @param endTime end of time range
*/
abstract getEventsByRecipient(
recipient: ObjectOwner,
count: number,
startTime: number,
endTime: number
): Promise<SuiEvents>;

/**
* Get events involving the given object
* @param object object id created, mutated, or deleted in events
* @param count max result count
* @param startTime start of time range to search
* @param endTime end of time range
*/
abstract getEventsByObject(
object: ObjectId,
count: number,
startTime: number,
endTime: number
): Promise<SuiEvents>;

/**
* Get all events within the given time span
* @param count max result count
* @param startTime start of time range to search
* @param endTime end of time range
*/
abstract getEventsByTimeRange(
count: number,
startTime: number,
endTime: number
): Promise<SuiEvents>;

/**
* Subscribe to get notifications whenever an event matching the filter occurs
* @param filter - filter describing the subset of events to follow
Expand Down
62 changes: 62 additions & 0 deletions sdk/typescript/src/providers/void-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import {
SubscriptionId,
ExecuteTransactionRequestType,
SuiExecuteTransactionResponse,
ObjectOwner,
SuiAddress,
ObjectId,
SuiEvents,
} from '../types';
import { Provider } from './provider';

Expand Down Expand Up @@ -126,6 +130,64 @@ export class VoidProvider extends Provider {
throw this.newError('syncAccountState');
}

async getEventsByTransaction(_digest: TransactionDigest, _count: number): Promise<SuiEvents> {
throw this.newError('getEventsByTransaction');
}

async getEventsByModule(
_package: string,
_module: string,
_count: number,
_startTime: number,
_endTime: number
): Promise<SuiEvents> {
throw this.newError('getEventsByTransactionModule');
}

async getEventsByMoveEventStructName(
_moveEventStructName: string,
_count: number,
_startTime: number,
_endTime: number
): Promise<SuiEvents> {
throw this.newError('getEventsByMoveEventStructName');
}

async getEventsBySender(
_sender: SuiAddress,
_count: number,
_startTime: number,
_endTime: number
): Promise<SuiEvents> {
throw this.newError('getEventsBySender');
}

async getEventsByRecipient(
_recipient: ObjectOwner,
_count: number,
_startTime: number,
_endTime: number
): Promise<SuiEvents> {
throw this.newError('getEventsByRecipient');
}

async getEventsByObject(
_object: ObjectId,
_count: number,
_startTime: number,
_endTime: number
): Promise<SuiEvents> {
throw this.newError('getEventsByObject');
}

async getEventsByTimeRange(
_count: number,
_startTime: number,
_endTime: number
): Promise<SuiEvents> {
throw this.newError('getEventsByTimeRange');
}

async subscribeEvent(
_filter: SuiEventFilter,
_onMessage: (event: SuiEventEnvelope) => void
Expand Down
Loading