From 6060c6e425107cef5c795003a5d1bcc3120d747d Mon Sep 17 00:00:00 2001 From: jinoosss <112360739+jinoosss@users.noreply.github.com> Date: Tue, 10 Sep 2024 12:20:52 +0900 Subject: [PATCH] chore: add comments to sdk modules (#9) --- .../src/core/connection/connection-manager.ts | 50 +++++++++++++- .../src/core/connection/connection-state.ts | 16 +++++ packages/sdk/src/core/sdk/adena-sdk.ts | 68 +++++++++++++++++++ 3 files changed, 132 insertions(+), 2 deletions(-) diff --git a/packages/sdk/src/core/connection/connection-manager.ts b/packages/sdk/src/core/connection/connection-manager.ts index 1862937..1705942 100644 --- a/packages/sdk/src/core/connection/connection-manager.ts +++ b/packages/sdk/src/core/connection/connection-manager.ts @@ -21,6 +21,11 @@ export class ConnectionManager { } } + /** + * Initiates the connection process with the wallet provider. + * Determines if the provider is TM2 or Adena and calls the respective connection method. + * @returns A promise that resolves when the connection process is complete. + */ async connectWallet(): Promise { if (isTM2WalletProvider(this.provider)) { return this.connectTM2Wallet(); @@ -28,6 +33,9 @@ export class ConnectionManager { return this.connectAdenaWallet(); } + /** + * Disconnects from the wallet provider and updates the connection state. + */ disconnectWallet(): void { if (this.provider instanceof GnoWalletProvider) { this.provider.disconnect(); @@ -35,10 +43,20 @@ export class ConnectionManager { this.disconnect(); } + /** + * Retrieves the current connection state from the state manager. + * @returns The current connection state. + */ getConnectionState(): ConnectionState { return this.stateManager.getState(); } + /** + * Retrieves the wallet provider if the connection state is connected. + * Throws an error if the wallet is not connected. + * @returns The current wallet provider. + * @throws Error if the wallet is not connected. + */ getWalletProvider(): WalletProvider { if (this.stateManager.getState() !== ConnectionState.CONNECTED) { throw new Error('not connect wallet'); @@ -46,28 +64,40 @@ export class ConnectionManager { return this.provider; } + /** + * Registers a callback function to be called when the connection state changes. + * @param listener - The callback function to be invoked on connection state changes. + */ on(listener: (connection: WalletConnectionEvent) => void): void { this.listeners.push(listener); } + /** + * Unregisters a previously registered callback function from being called on connection state changes. + * @param listener - The callback function to be removed. + */ off(listener: (connection: WalletConnectionEvent) => void): void { this.listeners = this.listeners.filter((l) => l !== listener); } + /** + * Handles the connection process for Adena wallet providers. + * Updates the connection state based on the success or failure of connection attempts. + * @returns A promise that resolves when the connection process is complete. + * @throws Error if an error occurs during the connection process. + */ private async connectAdenaWallet(): Promise { if (this.getConnectionState() !== ConnectionState.CONNECTED) { this.stateManager.setState(ConnectionState.CONNECTING); } try { - // If your wallet is already connected, change to connected. const isConnectedResponse = await this.provider.isConnected(); if (isConnectedResponse.status === WalletResponseStatus.SUCCESS) { this.connect(); return; } - // If the app is registered in your wallet, change the status to connected. const addEstablishResponse = await this.provider.addEstablish({}); if (addEstablishResponse.status === WalletResponseStatus.SUCCESS) { this.connect(); @@ -81,6 +111,12 @@ export class ConnectionManager { this.stateManager.setState(ConnectionState.DISCONNECTED); } + /** + * Handles the connection process for TM2 wallet providers. + * Updates the connection state based on the success or failure of the connection attempt. + * @returns A promise that resolves when the connection process is complete. + * @throws Error if an error occurs during the connection process. + */ private async connectTM2Wallet(): Promise { if (this.getConnectionState() !== ConnectionState.CONNECTED) { this.stateManager.setState(ConnectionState.CONNECTING); @@ -100,16 +136,26 @@ export class ConnectionManager { this.stateManager.setState(ConnectionState.DISCONNECTED); } + /** + * Updates the connection state to connected and triggers the connection event. + */ private connect() { this.stateManager.setState(ConnectionState.CONNECTED); this.triggerConnectionEvent('connect'); } + /** + * Updates the connection state to disconnected and triggers the disconnection event. + */ private disconnect() { this.stateManager.setState(ConnectionState.DISCONNECTED); this.triggerConnectionEvent('disconnect'); } + /** + * Invokes all registered listeners with the specified connection event. + * @param event - The connection event to trigger. + */ private triggerConnectionEvent(event: WalletConnectionEvent): void { this.listeners?.forEach((listener) => listener(event)); } diff --git a/packages/sdk/src/core/connection/connection-state.ts b/packages/sdk/src/core/connection/connection-state.ts index 7735195..6a37e42 100644 --- a/packages/sdk/src/core/connection/connection-state.ts +++ b/packages/sdk/src/core/connection/connection-state.ts @@ -12,15 +12,27 @@ export class ConnectionStateManager { private state: ConnectionState = ConnectionState.DISCONNECTED; + /** + * Retrieves the current connection state. + * @returns The current connection state. + */ getState(): ConnectionState { return this.state; } + /** + * Sets a new connection state and saves it to storage. + * @param state - The new connection state to set. + */ setState(state: ConnectionState): void { this.state = state; this.saveState(); } + /** + * Loads the connection state from storage and updates the internal state. + * If the stored state is 'CONNECTED', it sets the state to CONNECTED. + */ loadState(): void { const savedState = getSessionStorageItem(ConnectionStateManager.STORAGE_KEY); if (savedState === ConnectionState.CONNECTED.toString()) { @@ -28,6 +40,10 @@ export class ConnectionStateManager { } } + /** + * Saves the current connection state to storage. + * The state is saved under the key defined by STORAGE_KEY. + */ private saveState(): void { setSessionStorageItem(ConnectionStateManager.STORAGE_KEY, this.state.toString()); } diff --git a/packages/sdk/src/core/sdk/adena-sdk.ts b/packages/sdk/src/core/sdk/adena-sdk.ts index 5ff515e..d6123d0 100644 --- a/packages/sdk/src/core/sdk/adena-sdk.ts +++ b/packages/sdk/src/core/sdk/adena-sdk.ts @@ -65,62 +65,130 @@ export class AdenaSDK { this.connectionManager = new ConnectionManager(provider, this.config); } + /** + * Retrieves the current wallet provider from the connection manager. + * @returns The current wallet provider. + */ private get walletProvider() { return this.connectionManager.getWalletProvider(); } + /** + * Connects to the wallet using the connection manager and configuration. + * @returns A promise that resolves when the connection is successful. + */ connectWallet(): Promise { return connect(this.connectionManager, this.config); } + /** + * Disconnects from the wallet using the connection manager. + */ disconnectWallet(): void { return disconnect(this.connectionManager); } + /** + * Retrieves the current connection state from the connection manager. + * @returns The current connection state. + */ getConnectionState(): ConnectionState { return getConnectionState(this.connectionManager); } + /** + * Registers a callback to be invoked when the connection state changes. + * @param options - Configuration options including the callback function. + * @returns The response from the `onConnectionChange` method of the connection manager. + */ onConnectionChange(options: OnConnectionChangeOptions): OnConnectionChangeResponse { return onConnectionChange(this.connectionManager, options); } + /** + * Unregisters a callback previously registered for connection state changes. + * @param options - Configuration options including the callback function. + * @returns The response from the `offConnectionChange` method of the connection manager. + */ offConnectionChange(options: OffConnectionChangeOptions): OffConnectionChangeResponse { return offConnectionChange(this.connectionManager, options); } + /** + * Checks if the wallet provider is currently connected. + * @returns A promise that resolves to the response from the wallet provider's `isConnected` method. + */ isConnected(): Promise { return isConnected(this.walletProvider); } + /** + * Adds an establishment request to the wallet provider. + * @param options - Options for the establishment request. + * @returns A promise that resolves to the response from the wallet provider's `addEstablish` method. + */ addEstablish(options: AddEstablishOptions): Promise { return addEstablish(this.walletProvider, options); } + /** + * Retrieves account information from the wallet provider. + * @returns A promise that resolves to the account information from the wallet provider. + */ getAccount(): Promise { return getAccount(this.walletProvider); } + /** + * Switches the network in the wallet provider. + * @param options - Options for switching the network. + * @returns A promise that resolves to the response from the wallet provider's `switchNetwork` method. + */ switchNetwork(options: SwitchNetworkOptions): Promise { return switchNetwork(this.walletProvider, options); } + /** + * Adds a network to the wallet provider. + * @param options - Options for adding the network. + * @returns A promise that resolves to the response from the wallet provider's `addNetwork` method. + */ addNetwork(options: AddNetworkOptions): Promise { return addNetwork(this.walletProvider, options); } + /** + * Signs a transaction using the wallet provider. + * @param options - Options for signing the transaction, including the transaction details. + * @returns A promise that resolves to the response from the wallet provider's `signTransaction` method. + */ signTransaction(options: SignTransactionOptions): Promise { return signTransaction(this.walletProvider, options); } + /** + * Broadcasts a transaction using the wallet provider. + * @param options - Options for broadcasting the transaction. + * @returns A promise that resolves to the response from the wallet provider's `broadcastTransaction` method. + */ broadcastTransaction(options: BroadcastTransactionOptions): Promise { return broadcastTransaction(this.walletProvider, options); } + /** + * Registers a callback to be invoked when the account changes. + * @param options - Configuration options including the callback function. + * @returns The response from the wallet provider's `onChangeAccount` method. + */ onChangeAccount(options: OnChangeAccountOptions): OnChangeAccountResponse { return onChangeAccount(this.walletProvider, options); } + /** + * Registers a callback to be invoked when the network changes. + * @param options - Configuration options including the callback function. + * @returns The response from the wallet provider's `onChangeNetwork` method. + */ onChangeNetwork(options: OnChangeNetworkOptions): OnChangeNetworkResponse { return onChangeNetwork(this.walletProvider, options); }