Skip to content

Commit

Permalink
chore: add comments to sdk modules (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss authored Sep 10, 2024
1 parent 2f1bde0 commit 6060c6e
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 2 deletions.
50 changes: 48 additions & 2 deletions packages/sdk/src/core/connection/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,83 @@ 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<void> {
if (isTM2WalletProvider(this.provider)) {
return this.connectTM2Wallet();
}
return this.connectAdenaWallet();
}

/**
* Disconnects from the wallet provider and updates the connection state.
*/
disconnectWallet(): void {
if (this.provider instanceof GnoWalletProvider) {
this.provider.disconnect();
}
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');
}
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<void> {
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();
Expand All @@ -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<void> {
if (this.getConnectionState() !== ConnectionState.CONNECTED) {
this.stateManager.setState(ConnectionState.CONNECTING);
Expand All @@ -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));
}
Expand Down
16 changes: 16 additions & 0 deletions packages/sdk/src/core/connection/connection-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,38 @@ 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()) {
this.setState(ConnectionState.CONNECTED);
}
}

/**
* 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());
}
Expand Down
68 changes: 68 additions & 0 deletions packages/sdk/src/core/sdk/adena-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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<IsConnectedResponse> {
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<AddEstablishResponse> {
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<GetAccountResponse> {
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<SwitchNetworkResponse> {
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<AddNetworkResponse> {
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<SignTransactionResponse> {
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<BroadcastTransactionResponse> {
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);
}
Expand Down

0 comments on commit 6060c6e

Please sign in to comment.