@reown/appkit-solana-react-native 0.0.1 → 2.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/lib/commonjs/adapter.js +28 -22
  2. package/lib/commonjs/adapter.js.map +1 -1
  3. package/lib/commonjs/connectors/PhantomConnector.js +247 -0
  4. package/lib/commonjs/connectors/PhantomConnector.js.map +1 -0
  5. package/lib/commonjs/helpers.js +103 -0
  6. package/lib/commonjs/helpers.js.map +1 -0
  7. package/lib/commonjs/index.js +7 -0
  8. package/lib/commonjs/index.js.map +1 -1
  9. package/lib/commonjs/providers/PhantomProvider.js +391 -0
  10. package/lib/commonjs/providers/PhantomProvider.js.map +1 -0
  11. package/lib/commonjs/types.js +6 -0
  12. package/lib/commonjs/types.js.map +1 -0
  13. package/lib/module/adapter.js +28 -22
  14. package/lib/module/adapter.js.map +1 -1
  15. package/lib/module/connectors/PhantomConnector.js +239 -0
  16. package/lib/module/connectors/PhantomConnector.js.map +1 -0
  17. package/lib/module/helpers.js +94 -0
  18. package/lib/module/helpers.js.map +1 -0
  19. package/lib/module/index.js +7 -2
  20. package/lib/module/index.js.map +1 -1
  21. package/lib/module/providers/PhantomProvider.js +383 -0
  22. package/lib/module/providers/PhantomProvider.js.map +1 -0
  23. package/lib/module/types.js +2 -0
  24. package/lib/module/types.js.map +1 -0
  25. package/lib/typescript/adapter.d.ts +0 -1
  26. package/lib/typescript/adapter.d.ts.map +1 -1
  27. package/lib/typescript/connectors/PhantomConnector.d.ts +26 -0
  28. package/lib/typescript/connectors/PhantomConnector.d.ts.map +1 -0
  29. package/lib/typescript/helpers.d.ts +31 -0
  30. package/lib/typescript/helpers.d.ts.map +1 -0
  31. package/lib/typescript/index.d.ts +3 -2
  32. package/lib/typescript/index.d.ts.map +1 -1
  33. package/lib/typescript/providers/PhantomProvider.d.ts +37 -0
  34. package/lib/typescript/providers/PhantomProvider.d.ts.map +1 -0
  35. package/lib/typescript/types.d.ts +96 -0
  36. package/lib/typescript/types.d.ts.map +1 -0
  37. package/package.json +8 -14
  38. package/src/adapter.ts +29 -27
  39. package/src/connectors/PhantomConnector.ts +328 -0
  40. package/src/helpers.ts +102 -0
  41. package/src/index.ts +8 -0
  42. package/src/providers/PhantomProvider.ts +530 -0
  43. package/src/types.ts +131 -0
  44. package/src/index.tsx +0 -2
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"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"}
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@reown/appkit-solana-react-native",
3
- "version": "0.0.1",
3
+ "version": "2.0.0-alpha.1",
4
4
  "main": "lib/commonjs/index.js",
5
5
  "types": "lib/typescript/index.d.ts",
6
6
  "module": "lib/module/index.js",
7
- "source": "src/index.tsx",
7
+ "react-native": "src/index.ts",
8
+ "source": "src/index.ts",
8
9
  "scripts": {
9
10
  "build": "bob build",
10
11
  "clean": "rm -rf lib",
@@ -38,15 +39,8 @@
38
39
  "access": "public"
39
40
  },
40
41
  "dependencies": {
41
- "@reown/appkit-common-react-native": "1.2.3",
42
- "@solana/web3.js": "1.98.2"
43
- },
44
- "peerDependencies": {
45
- "@solana/web3.js": ">=1.90.0",
46
- "bs58": ">=6.0.0"
47
- },
48
- "devDependencies": {
49
- "@solana/web3.js": "1.98.2"
50
- },
51
- "react-native": "src/index.tsx"
52
- }
42
+ "@reown/appkit-common-react-native": "2.0.0-alpha.1",
43
+ "bs58": "6.0.0",
44
+ "tweetnacl": "1.0.3"
45
+ }
46
+ }
package/src/adapter.ts CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  type GetBalanceParams,
7
7
  type GetBalanceResponse
8
8
  } from '@reown/appkit-common-react-native';
9
- import { Connection, PublicKey } from '@solana/web3.js';
9
+ import { getSolanaNativeBalance, getSolanaTokenBalance } from './helpers';
10
10
 
11
11
  export class SolanaAdapter extends SolanaBaseAdapter {
12
12
  private static supportedNamespace: ChainNamespace = 'solana';
@@ -19,7 +19,7 @@ export class SolanaAdapter extends SolanaBaseAdapter {
19
19
  }
20
20
 
21
21
  async getBalance(params: GetBalanceParams): Promise<GetBalanceResponse> {
22
- const { network, address } = params;
22
+ const { network, address, tokens } = params;
23
23
 
24
24
  if (!this.connector) throw new Error('No active connector');
25
25
  if (!network) throw new Error('No network provided');
@@ -28,26 +28,35 @@ export class SolanaAdapter extends SolanaBaseAdapter {
28
28
  address || this.getAccounts()?.find(account => account.includes(network.id.toString()));
29
29
 
30
30
  if (!balanceAddress) {
31
- return Promise.resolve({ amount: '0.00', symbol: 'SOL' });
31
+ return { amount: '0.00', symbol: 'SOL' };
32
32
  }
33
33
 
34
34
  try {
35
- const connection = new Connection(network?.rpcUrls?.default?.http?.[0] as string); //TODO: check connection settings
36
- const balanceAmount = await connection.getBalance(
37
- new PublicKey(balanceAddress.split(':')[2] as string)
38
- );
39
- const formattedBalance = (balanceAmount / 1000000000).toString(); //TODO: add util with LAMPORTS_PER_SOL
40
-
41
- const balance = {
42
- amount: formattedBalance,
43
- symbol: network?.nativeCurrency.symbol || 'SOL'
44
- };
45
-
46
- this.emit('balanceChanged', {
47
- namespace: this.getSupportedNamespace(),
48
- address: balanceAddress,
49
- balance
50
- });
35
+ const rpcUrl = network.rpcUrls?.default?.http?.[0];
36
+ if (!rpcUrl) throw new Error('No RPC URL available');
37
+
38
+ const base58Address = balanceAddress.split(':')[2];
39
+
40
+ if (!base58Address) throw new Error('Invalid balance address');
41
+
42
+ const token = network?.caipNetworkId && tokens?.[network.caipNetworkId]?.address;
43
+ let balance;
44
+
45
+ if (token) {
46
+ const { amount, symbol } = await getSolanaTokenBalance(rpcUrl, base58Address, token);
47
+ balance = {
48
+ amount,
49
+ symbol
50
+ };
51
+ } else {
52
+ const amount = await getSolanaNativeBalance(rpcUrl, base58Address);
53
+ balance = {
54
+ amount: amount.toString(),
55
+ symbol: 'SOL'
56
+ };
57
+ }
58
+
59
+ this.emit('balanceChanged', { address: balanceAddress, balance });
51
60
 
52
61
  return balance;
53
62
  } catch (error) {
@@ -78,16 +87,9 @@ export class SolanaAdapter extends SolanaBaseAdapter {
78
87
  }
79
88
 
80
89
  disconnect(): Promise<void> {
81
- if (!this.connector) throw new Error('SolanaAdapter:disconnect - No active connector');
82
-
83
- return this.connector.disconnect();
84
- }
85
-
86
- async request(method: string, params?: any[]) {
87
90
  if (!this.connector) throw new Error('No active connector');
88
- const provider = this.connector.getProvider();
89
91
 
90
- return provider.request({ method, params });
92
+ return this.connector.disconnect();
91
93
  }
92
94
 
93
95
  getSupportedNamespace(): ChainNamespace {
@@ -0,0 +1,328 @@
1
+ import {
2
+ WalletConnector,
3
+ type AppKitNetwork,
4
+ type CaipNetworkId,
5
+ type ChainNamespace,
6
+ type ConnectOptions,
7
+ type Namespaces,
8
+ type WalletInfo,
9
+ type CaipAddress,
10
+ type ConnectorInitOptions,
11
+ type Storage,
12
+ solana,
13
+ solanaDevnet,
14
+ solanaTestnet
15
+ } from '@reown/appkit-common-react-native';
16
+ import nacl from 'tweetnacl';
17
+ import bs58 from 'bs58';
18
+
19
+ import { PhantomProvider, SOLANA_SIGNING_METHODS } from '../providers/PhantomProvider';
20
+ import type {
21
+ PhantomCluster,
22
+ PhantomConnectorConfig,
23
+ PhantomConnectorSessionData,
24
+ PhantomProviderConfig
25
+ } from '../types';
26
+
27
+ const SOLANA_CLUSTER_TO_CHAIN_ID_PART: Record<PhantomCluster, string> = {
28
+ 'mainnet-beta': solana.id as string,
29
+ 'testnet': solanaTestnet.id as string,
30
+ 'devnet': solanaDevnet.id as string
31
+ };
32
+
33
+ const PHANTOM_CONNECTOR_STORAGE_KEY = '@appkit/phantom-connector-data';
34
+ const DAPP_KEYPAIR_STORAGE_KEY = '@appkit/phantom-dapp-secret-key';
35
+
36
+ export class PhantomConnector extends WalletConnector {
37
+ private readonly config: PhantomConnectorConfig;
38
+
39
+ private currentCaipNetworkId: CaipNetworkId | null = null;
40
+ private dappEncryptionKeyPair?: nacl.BoxKeyPair;
41
+
42
+ private static readonly SUPPORTED_NAMESPACE: ChainNamespace = 'solana';
43
+
44
+ constructor(config?: PhantomConnectorConfig) {
45
+ super({ type: 'phantom' });
46
+ this.config = config ?? { cluster: 'mainnet-beta' };
47
+ }
48
+
49
+ override async init(ops: ConnectorInitOptions) {
50
+ super.init(ops);
51
+ this.storage = ops.storage;
52
+ await this.initializeKeyPair();
53
+
54
+ const appScheme = ops.metadata.redirect?.universal;
55
+ if (!appScheme) {
56
+ throw new Error(
57
+ 'Phantom Connector: No universal link found in metadata. Please add redirect.universal to the metadata.'
58
+ );
59
+ }
60
+
61
+ const providerConfig: PhantomProviderConfig = {
62
+ appScheme,
63
+ dappUrl: ops.metadata.url,
64
+ storage: ops.storage,
65
+ dappEncryptionKeyPair: this.dappEncryptionKeyPair!
66
+ };
67
+
68
+ this.provider = new PhantomProvider(providerConfig);
69
+ await this.restoreSession();
70
+ }
71
+
72
+ private async initializeKeyPair(): Promise<void> {
73
+ try {
74
+ const secretKeyB58 = await this.getStorage().getItem(DAPP_KEYPAIR_STORAGE_KEY);
75
+ if (secretKeyB58) {
76
+ const secretKey = bs58.decode(secretKeyB58);
77
+ this.dappEncryptionKeyPair = nacl.box.keyPair.fromSecretKey(secretKey);
78
+ } else {
79
+ const newKeyPair = nacl.box.keyPair();
80
+ this.dappEncryptionKeyPair = newKeyPair;
81
+ await this.getStorage().setItem(
82
+ DAPP_KEYPAIR_STORAGE_KEY,
83
+ bs58.encode(newKeyPair.secretKey)
84
+ );
85
+ }
86
+ } catch (error) {
87
+ // disconnect and clear session
88
+ await this.disconnect();
89
+ throw error;
90
+ }
91
+ }
92
+
93
+ override async connect(opts?: ConnectOptions): Promise<Namespaces | undefined> {
94
+ if (this.isConnected()) {
95
+ return this.namespaces;
96
+ }
97
+
98
+ const defaultChain =
99
+ opts?.defaultChain?.split(':')?.[0] === 'solana'
100
+ ? opts?.defaultChain?.split(':')[1]
101
+ : opts?.namespaces?.['solana']?.chains?.[0]?.split(':')[1];
102
+
103
+ const requestedCluster =
104
+ this.config.cluster ??
105
+ (Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID_PART).find(
106
+ key =>
107
+ SOLANA_CLUSTER_TO_CHAIN_ID_PART[key as keyof typeof SOLANA_CLUSTER_TO_CHAIN_ID_PART] ===
108
+ defaultChain
109
+ ) as PhantomCluster | undefined);
110
+
111
+ try {
112
+ const connectResult = await this.getProvider().connect({ cluster: requestedCluster });
113
+
114
+ const solanaChainIdPart = SOLANA_CLUSTER_TO_CHAIN_ID_PART[connectResult.cluster];
115
+ if (!solanaChainIdPart) {
116
+ throw new Error(
117
+ `Phantom Connect: Internal - Unknown cluster mapping for ${connectResult.cluster}`
118
+ );
119
+ }
120
+ this.currentCaipNetworkId = `solana:${solanaChainIdPart}` as CaipNetworkId;
121
+
122
+ this.wallet = {
123
+ name: 'Phantom Wallet',
124
+ id: 'phantom-wallet'
125
+ };
126
+
127
+ const userPublicKey = this.getProvider().getUserPublicKey();
128
+ if (!userPublicKey) {
129
+ throw new Error('Phantom Connect: Provider failed to return a user public key.');
130
+ }
131
+
132
+ const caipAddress = `${this.currentCaipNetworkId}:${userPublicKey}` as CaipAddress;
133
+ this.namespaces = {
134
+ [PhantomConnector.SUPPORTED_NAMESPACE]: {
135
+ accounts: [caipAddress],
136
+ methods: Object.values(SOLANA_SIGNING_METHODS),
137
+ events: [],
138
+ chains: [this.currentCaipNetworkId]
139
+ }
140
+ };
141
+
142
+ await this.saveSession(); // Save connector-specific session on successful connect
143
+
144
+ return this.namespaces;
145
+ } catch (error: any) {
146
+ this.clearSession();
147
+ throw error;
148
+ }
149
+ }
150
+
151
+ override async disconnect(): Promise<void> {
152
+ if (!this.isConnected()) {
153
+ return Promise.resolve();
154
+ }
155
+ try {
156
+ await this.getProvider().disconnect();
157
+ } catch (error: any) {
158
+ // console.warn(`PhantomConnector: Error during provider disconnect: ${error.message}. Proceeding with local clear.`);
159
+ }
160
+ await this.clearSession();
161
+ }
162
+
163
+ private async clearSession(): Promise<void> {
164
+ this.namespaces = undefined;
165
+ this.wallet = undefined;
166
+ this.currentCaipNetworkId = null;
167
+ await this.clearSessionStorage();
168
+ }
169
+
170
+ override getProvider(): PhantomProvider {
171
+ if (!this.provider) {
172
+ throw new Error('Phantom Connector: Provider not initialized. Call init() first.');
173
+ }
174
+
175
+ return this.provider as PhantomProvider;
176
+ }
177
+
178
+ private getStorage(): Storage {
179
+ if (!this.storage) {
180
+ throw new Error('Phantom Connector: Storage not initialized. Call init() first.');
181
+ }
182
+
183
+ return this.storage;
184
+ }
185
+
186
+ override getNamespaces(): Namespaces {
187
+ if (!this.namespaces) {
188
+ throw new Error('Phantom Connector: Not connected. Call connect() first.');
189
+ }
190
+
191
+ return this.namespaces;
192
+ }
193
+
194
+ override getChainId(namespace: ChainNamespace): CaipNetworkId | undefined {
195
+ if (namespace === PhantomConnector.SUPPORTED_NAMESPACE) {
196
+ return this.currentCaipNetworkId ?? undefined;
197
+ }
198
+
199
+ return undefined;
200
+ }
201
+
202
+ override getWalletInfo(): WalletInfo | undefined {
203
+ if (!this.isConnected()) {
204
+ return undefined;
205
+ }
206
+
207
+ return this.wallet;
208
+ }
209
+
210
+ isConnected(): boolean {
211
+ // Rely solely on the provider as the source of truth for connection status.
212
+ return this.getProvider().isConnected() && !!this.getProvider().getUserPublicKey();
213
+ }
214
+
215
+ override async switchNetwork(network: AppKitNetwork): Promise<void> {
216
+ const targetClusterName = Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID_PART).find(
217
+ key =>
218
+ SOLANA_CLUSTER_TO_CHAIN_ID_PART[key as keyof typeof SOLANA_CLUSTER_TO_CHAIN_ID_PART] ===
219
+ network.id
220
+ ) as PhantomCluster | undefined;
221
+
222
+ if (!targetClusterName) {
223
+ throw new Error(`Cannot switch to unsupported network ID: ${network.id}`);
224
+ }
225
+
226
+ const currentClusterName = Object.keys(SOLANA_CLUSTER_TO_CHAIN_ID_PART).find(
227
+ key =>
228
+ `solana:${
229
+ SOLANA_CLUSTER_TO_CHAIN_ID_PART[key as keyof typeof SOLANA_CLUSTER_TO_CHAIN_ID_PART]
230
+ }` === this.currentCaipNetworkId
231
+ ) as PhantomCluster | undefined;
232
+
233
+ if (targetClusterName === currentClusterName && this.isConnected()) {
234
+ return Promise.resolve();
235
+ }
236
+
237
+ // For deeplink wallets, switching network effectively means re-connecting to the new cluster.
238
+ // We can try to disconnect the current session and then initiate a new connection.
239
+ // console.log(`Attempting to switch network to: ${targetClusterName}`);
240
+ await this.disconnect(); // Clear current session
241
+
242
+ // Create a temporary options object to guide the new connection
243
+ const tempConnectOpts: ConnectOptions = {
244
+ defaultChain: `solana:${SOLANA_CLUSTER_TO_CHAIN_ID_PART[targetClusterName]}` as CaipNetworkId
245
+ };
246
+
247
+ // Attempt to connect to the new cluster
248
+ // The connect method will use the defaultChain from opts to determine the cluster.
249
+ await this.connect(tempConnectOpts);
250
+
251
+ // Verify if the connection was successful and to the correct new network
252
+ if (
253
+ !this.isConnected() ||
254
+ this.getChainId(PhantomConnector.SUPPORTED_NAMESPACE) !== tempConnectOpts.defaultChain
255
+ ) {
256
+ throw new Error(
257
+ `Failed to switch network to ${targetClusterName}. Please try connecting manually.`
258
+ );
259
+ }
260
+ }
261
+
262
+ // Orchestrates session restoration
263
+ public async restoreSession(): Promise<boolean> {
264
+ try {
265
+ const providerSession = await this.getProvider().restoreSession();
266
+ if (!providerSession) {
267
+ return false;
268
+ }
269
+
270
+ // If provider session is restored, try to restore connector data
271
+ const connectorData = await this.getStorage().getItem<PhantomConnectorSessionData>(
272
+ PHANTOM_CONNECTOR_STORAGE_KEY
273
+ );
274
+ if (!connectorData) {
275
+ return false; // Provider session exists but connector data is missing
276
+ }
277
+
278
+ this.namespaces = connectorData.namespaces;
279
+ this.wallet = connectorData.wallet;
280
+ this.currentCaipNetworkId = connectorData.currentCaipNetworkId;
281
+
282
+ // await this.initializeKeyPair();
283
+
284
+ // Final validation
285
+ if (this.isConnected()) {
286
+ return true;
287
+ }
288
+
289
+ // If validation fails, something is out of sync. Clear everything.
290
+ await this.disconnect();
291
+
292
+ return false;
293
+ } catch (error) {
294
+ // On any error, disconnect to ensure a clean state
295
+ await this.disconnect();
296
+
297
+ return false;
298
+ }
299
+ }
300
+
301
+ // Saves only connector-specific data
302
+ private async saveSession(): Promise<void> {
303
+ if (!this.namespaces || !this.wallet || !this.currentCaipNetworkId) {
304
+ return;
305
+ }
306
+
307
+ const connectorData: PhantomConnectorSessionData = {
308
+ namespaces: this.namespaces,
309
+ wallet: this.wallet,
310
+ currentCaipNetworkId: this.currentCaipNetworkId
311
+ };
312
+
313
+ try {
314
+ await this.getStorage().setItem(PHANTOM_CONNECTOR_STORAGE_KEY, connectorData);
315
+ } catch (error) {
316
+ // console.error('PhantomConnector: Failed to save session.', error);
317
+ }
318
+ }
319
+
320
+ // Clears only connector-specific data from storage
321
+ private async clearSessionStorage(): Promise<void> {
322
+ try {
323
+ await this.getStorage().removeItem(PHANTOM_CONNECTOR_STORAGE_KEY);
324
+ } catch (error) {
325
+ // console.error('PhantomConnector: Failed to clear session from storage.', error);
326
+ }
327
+ }
328
+ }
package/src/helpers.ts ADDED
@@ -0,0 +1,102 @@
1
+ import type { TokenInfo } from './types';
2
+
3
+ /**
4
+ * Validates if the given string is a Solana address.
5
+ * @param address The string to validate.
6
+ * @returns True if the address is valid, false otherwise.
7
+ */
8
+ export function isSolanaAddress(address: string): boolean {
9
+ const solanaAddressRegex = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
10
+
11
+ return solanaAddressRegex.test(address);
12
+ }
13
+
14
+ /**
15
+ * Helper to fetch SOL balance using JSON-RPC
16
+ * @param rpcUrl Solana RPC endpoint
17
+ * @param address Solana public address (base58)
18
+ */
19
+ export async function getSolanaNativeBalance(rpcUrl: string, address: string): Promise<number> {
20
+ if (!isSolanaAddress(address)) {
21
+ throw new Error('Invalid Solana address format');
22
+ }
23
+
24
+ const response = await fetch(rpcUrl, {
25
+ method: 'POST',
26
+ headers: { 'Content-Type': 'application/json' },
27
+ body: JSON.stringify({
28
+ jsonrpc: '2.0',
29
+ id: 1,
30
+ method: 'getBalance',
31
+ params: [address]
32
+ })
33
+ });
34
+
35
+ const json = (await response.json()) as {
36
+ result: { value: number };
37
+ error?: { message: string };
38
+ };
39
+ if (json.error) throw new Error(json.error.message);
40
+
41
+ return json.result.value / 1000000000; // Convert lamports to SOL
42
+ }
43
+
44
+ let tokenCache: Record<string, TokenInfo> = {};
45
+
46
+ /**
47
+ * Fetch metadata for a Solana SPL token using the Jupiter token list.
48
+ * @param mint - The token's mint address
49
+ * @returns TokenInfo if found, or undefined
50
+ */
51
+ export async function getSolanaTokenMetadata(mint: string): Promise<TokenInfo | undefined> {
52
+ // Return from cache if available
53
+ if (tokenCache[mint]) return tokenCache[mint];
54
+
55
+ try {
56
+ const res = await fetch('https://token.jup.ag/all');
57
+ const list: TokenInfo[] = await res.json();
58
+
59
+ for (const token of list) {
60
+ tokenCache[token.address] = token;
61
+ }
62
+
63
+ return tokenCache[mint];
64
+ } catch (error) {
65
+ return undefined;
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Get the balance of a token for a given address
71
+ * @param rpcUrl - The RPC URL to use
72
+ * @param address - The address to get the balance for
73
+ * @param tokenAddress - The address of the token to get the balance for
74
+ * @returns The balance of the token for the given address
75
+ */
76
+ export async function getSolanaTokenBalance(
77
+ rpcUrl: string,
78
+ address: string,
79
+ tokenAddress: string
80
+ ): Promise<{ amount: string; symbol: string }> {
81
+ if (!isSolanaAddress(address)) {
82
+ throw new Error('Invalid Solana address format');
83
+ }
84
+
85
+ const token = await getSolanaTokenMetadata(tokenAddress);
86
+
87
+ const response = await fetch(rpcUrl, {
88
+ method: 'POST',
89
+ headers: { 'Content-Type': 'application/json' },
90
+ body: JSON.stringify({
91
+ jsonrpc: '2.0',
92
+ id: 1,
93
+ method: 'getTokenAccountsByOwner',
94
+ params: [address, { mint: tokenAddress }, { encoding: 'jsonParsed' }]
95
+ })
96
+ });
97
+
98
+ const result = await response.json();
99
+ const balance = result.result.value[0]?.account?.data?.parsed?.info?.tokenAmount?.uiAmount;
100
+
101
+ return { amount: balance?.toString() ?? '0', symbol: token?.symbol ?? 'SOL' };
102
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ // Connectors
2
+ export { PhantomConnector } from './connectors/PhantomConnector';
3
+
4
+ // Types
5
+ export type { PhantomConnectorConfig } from './types';
6
+
7
+ // Adapter
8
+ export { SolanaAdapter } from './adapter';