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

Event subscription (TS sdk) #4249

Merged
merged 97 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 92 commits
Commits
Show all changes
97 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
988e309
handle case of currently-connecting when calling connect
Sep 8, 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
11 changes: 9 additions & 2 deletions explorer/client/src/utils/api/DefaultRpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ import { getEndpoint, Network } from './rpcSetting';

export { Network, getEndpoint };

export const DefaultRpcClient = (network: Network | string) =>
new JsonRpcProvider(getEndpoint(network));
const defaultRpcMap: Map<Network | string, JsonRpcProvider> = new Map();
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 change solved a problem with clients being recreated - that works fine when the client has no state, but a websocket connection adds some state

export const DefaultRpcClient = (network: Network | string) => {
stella3d marked this conversation as resolved.
Show resolved Hide resolved
const existingClient = defaultRpcMap.get(network);
if (existingClient) return existingClient;

const provider = new JsonRpcProvider(getEndpoint(network));
defaultRpcMap.set(network, provider);
return provider;
};
81 changes: 79 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sdk/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"buffer": "^6.0.3",
"cross-fetch": "^3.1.5",
"jayson": "^3.6.6",
"rpc-websockets": "^7.5.0",
"js-sha3": "^0.8.0",
"lossless-json": "^1.0.5",
"tweetnacl": "^1.0.3"
Expand Down
22 changes: 21 additions & 1 deletion sdk/typescript/src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@ import {
SuiObjectRef,
getObjectReference,
Coin,
SuiEventFilter,
SuiEventEnvelope,
SubscriptionId,
ExecuteTransactionRequestType,
SuiExecuteTransactionResponse,
} from '../types';
import { SignatureScheme } from '../cryptography/publickey';
import { DEFAULT_CLIENT_OPTIONS, WebsocketClient, WebsocketClientOptions } from '../rpc/websocket-client';

const isNumber = (val: any): val is number => typeof val === 'number';
const isAny = (_val: any): _val is any => true;


export class JsonRpcProvider extends Provider {
protected client: JsonRpcClient;
protected wsClient: WebsocketClient;
/**
* Establish a connection to a Sui RPC endpoint
*
Expand All @@ -54,10 +60,13 @@ export class JsonRpcProvider extends Provider {
*/
constructor(
public endpoint: string,
public skipDataValidation: boolean = false
public skipDataValidation: boolean = false,
public socketOptions: WebsocketClientOptions = DEFAULT_CLIENT_OPTIONS
) {
super();

this.client = new JsonRpcClient(endpoint);
this.wsClient = new WebsocketClient(endpoint, skipDataValidation, socketOptions);
}

// Move info
Expand Down Expand Up @@ -421,4 +430,15 @@ export class JsonRpcProvider extends Provider {
);
}
}

async subscribeEvent(
filter: SuiEventFilter,
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the desired behavior if filter == {All: []}, will it get subscribed all the events in the Network? cc @longbowlu

onMessage: (event: SuiEventEnvelope) => void
): Promise<SubscriptionId> {
return this.wsClient.subscribeEvent(filter, onMessage);
}

async unsubscribeEvent(id: SubscriptionId): Promise<boolean> {
return this.wsClient.unsubscribeEvent(id);
}
}
19 changes: 19 additions & 0 deletions sdk/typescript/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
SuiMoveNormalizedStruct,
SuiMoveNormalizedModule,
SuiMoveNormalizedModules,
SuiEventFilter,
SuiEventEnvelope,
SubscriptionId,
ExecuteTransactionRequestType,
SuiExecuteTransactionResponse,
} from '../types';
Expand Down Expand Up @@ -136,5 +139,21 @@ export abstract class Provider {
): Promise<SuiMoveNormalizedStruct>;

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

/**
* Subscribe to get notifications whenever an event matching the filter occurs
* @param filter - filter describing the subset of events to follow
* @param onMessage - function to run when we receive a notification of a new event matching the filter
*/
abstract subscribeEvent(
filter: SuiEventFilter,
onMessage: (event: SuiEventEnvelope) => void
): Promise<SubscriptionId>;

/**
* Unsubscribe from an event subscription
* @param id - subscription id to unsubscribe from (previously received from subscribeEvent)
*/
abstract unsubscribeEvent(id: SubscriptionId): Promise<boolean>;
// TODO: add more interface methods
}
14 changes: 14 additions & 0 deletions sdk/typescript/src/providers/void-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import {
SuiMoveNormalizedStruct,
SuiMoveNormalizedModule,
SuiMoveNormalizedModules,
SuiEventFilter,
SuiEventEnvelope,
SubscriptionId,
ExecuteTransactionRequestType,
SuiExecuteTransactionResponse,
} from '../types';
Expand Down Expand Up @@ -123,6 +126,17 @@ export class VoidProvider extends Provider {
throw this.newError('syncAccountState');
}

async subscribeEvent(
_filter: SuiEventFilter,
_onMessage: (event: SuiEventEnvelope) => void
): Promise<SubscriptionId> {
throw this.newError('subscribeEvent');
}

async unsubscribeEvent(_id: SubscriptionId): Promise<boolean> {
throw this.newError('unsubscribeEvent');
}

private newError(operation: string): Error {
return new Error(`Please use a valid provider for ${operation}`);
}
Expand Down
Loading