Skip to content

Commit

Permalink
feat: add sdk methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss committed Sep 6, 2024
1 parent 22eae1f commit 9676844
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 22 deletions.
23 changes: 23 additions & 0 deletions packages/sdk/src/core/methods/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ConnectionManager } from '../connection';
import { ConnectOptions, ConnectResponse } from '../types/methods';
import { isTM2WalletProvider } from '../utils/provider.utils';

export const connect = async (
connectionManager: ConnectionManager,
connectionOptions: ConnectOptions = {}
): Promise<ConnectResponse> => {
try {
await connectionManager.connectWallet();
} catch (e) {
const openWalletLink =
!isTM2WalletProvider(connectionManager.getWalletProvider()) && !!connectionOptions.walletDownloadUrl;
if (openWalletLink) {
openLink(connectionOptions.walletDownloadUrl!);
}
console.error(e);
}
};

const openLink = (url: string): void => {
window?.open(url, '_blank');
};
6 changes: 6 additions & 0 deletions packages/sdk/src/core/methods/disconnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ConnectionManager } from '../connection';
import { DisconnectResponse } from '../types/methods';

export const disconnect = (connectionManager: ConnectionManager): DisconnectResponse => {
connectionManager.disconnectWallet();
};
6 changes: 6 additions & 0 deletions packages/sdk/src/core/methods/get-connection-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ConnectionManager } from '../connection';
import { GetConnectionState } from '../types/methods/get-connection-state.types';

export const getConnectionState = (connectionManager: ConnectionManager): GetConnectionState => {
return connectionManager.getConnectionState();
};
5 changes: 5 additions & 0 deletions packages/sdk/src/core/methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export * from './add-establish';
export * from './add-network';
export * from './broadcast-transaction';
export * from './connect';
export * from './disconnect';
export * from './get-account';
export * from './get-connection-state';
export * from './is-connected';
export * from './off-connection-change';
export * from './on-change-account';
export * from './on-change-network';
export * from './on-connection-change';
export * from './sign-transaction';
export * from './switch-network';
9 changes: 9 additions & 0 deletions packages/sdk/src/core/methods/off-connection-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ConnectionManager } from '../connection';
import { OffConnectionChangeOptions, OffConnectionChangeResponse } from '../types/methods/off-connection-change';

export const offConnectionChange = (
connectionManager: ConnectionManager,
options: OffConnectionChangeOptions
): OffConnectionChangeResponse => {
return connectionManager.off(options.callback);
};
9 changes: 9 additions & 0 deletions packages/sdk/src/core/methods/on-connection-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ConnectionManager } from '../connection';
import { OnConnectionChangeOptions, OnConnectionChangeResponse } from '../types/methods/on-connection-change';

export const onConnectionChange = (
connectionManager: ConnectionManager,
options: OnConnectionChangeOptions
): OnConnectionChangeResponse => {
return connectionManager.on(options.callback);
};
38 changes: 16 additions & 22 deletions packages/sdk/src/core/sdk/adena-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ import {
addEstablish,
addNetwork,
broadcastTransaction,
connect,
disconnect,
getAccount,
getConnectionState,
isConnected,
offConnectionChange,
onChangeAccount,
onChangeNetwork,
onConnectionChange,
signTransaction,
switchNetwork,
} from '../methods';
import { WalletProvider } from '../providers';
import { SDKConfigure } from '../types/config.types';
import { SDKConfigure } from '../types';
import {
AddEstablishOptions,
AddEstablishResponse,
Expand All @@ -21,18 +26,19 @@ import {
BroadcastTransactionResponse,
GetAccountResponse,
IsConnectedResponse,
OffConnectionChangeOptions,
OffConnectionChangeResponse,
OnChangeAccountOptions,
OnChangeAccountResponse,
OnChangeNetworkOptions,
OnChangeNetworkResponse,
OnConnectionChangeOptions,
OnConnectionChangeResponse,
SignTransactionOptions,
SignTransactionResponse,
SwitchNetworkOptions,
SwitchNetworkResponse,
} from '../types/methods';
import { OffConnectionChangeOptions, OffConnectionChangeResponse } from '../types/methods/off-connection-change';
import { OnConnectionChangeOptions, OnConnectionChangeResponse } from '../types/methods/on-connection-change';
import { isTM2WalletProvider } from '../utils/provider.utils';

const DEFAULT_ADENA_URL = 'https://www.adena.app';

Expand Down Expand Up @@ -61,32 +67,24 @@ export class AdenaSDK {
return this.connectionManager.getWalletProvider();
}

async connectWallet(): Promise<void> {
try {
await this.connectionManager.connectWallet();
} catch (e) {
const openWalletLink = !isTM2WalletProvider(this.walletProvider) && !!this.config.walletDownloadUrl;
if (openWalletLink) {
this.openLink(this.config.walletDownloadUrl!);
}
console.error(e);
}
connectWallet(): Promise<void> {
return connect(this.connectionManager, this.config);
}

disconnectWallet(): void {
this.connectionManager.disconnectWallet();
return disconnect(this.connectionManager);
}

getConnectionState(): ConnectionState {
return this.connectionManager.getConnectionState();
return getConnectionState(this.connectionManager);
}

onConnectionChange(options: OnConnectionChangeOptions): OnConnectionChangeResponse {
return this.connectionManager.on(options.callback);
return onConnectionChange(this.connectionManager, options);
}

offConnectionChange(options: OffConnectionChangeOptions): OffConnectionChangeResponse {
return this.connectionManager.off(options.callback);
return offConnectionChange(this.connectionManager, options);
}

isConnected(): Promise<IsConnectedResponse> {
Expand Down Expand Up @@ -124,8 +122,4 @@ export class AdenaSDK {
onChangeNetwork(options: OnChangeNetworkOptions): OnChangeNetworkResponse {
return onChangeNetwork(this.walletProvider, options);
}

private openLink(url: string): void {
window?.open(url, '_blank');
}
}
1 change: 1 addition & 0 deletions packages/sdk/src/core/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './account.types';
export * from './config.types';
export * from './transaction.types';
export * from './wallet.types';
5 changes: 5 additions & 0 deletions packages/sdk/src/core/types/methods/connect.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface ConnectOptions {
walletDownloadUrl?: string;
}

export type ConnectResponse = void;
1 change: 1 addition & 0 deletions packages/sdk/src/core/types/methods/disconnect.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type DisconnectResponse = void;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ConnectionState } from '../../connection';

export type GetConnectionState = ConnectionState;
4 changes: 4 additions & 0 deletions packages/sdk/src/core/types/methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
export * from './add-establish.types';
export * from './add-network.types';
export * from './broadcast-transaction.types';
export * from './connect.types';
export * from './disconnect.types';
export * from './get-account.types';
export * from './is-connected.types';
export * from './off-connection-change';
export * from './on-change-account.types';
export * from './on-change-network.types';
export * from './on-connection-change';
export * from './sign-transaction.types';
export * from './switch-network.types';

0 comments on commit 9676844

Please sign in to comment.