@dynamic-labs/solana-core 0.0.0-exp20240827.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 (59) hide show
  1. package/CHANGELOG.md +3952 -0
  2. package/LICENSE +21 -0
  3. package/README.md +11 -0
  4. package/_virtual/_tslib.cjs +36 -0
  5. package/_virtual/_tslib.js +32 -0
  6. package/package.json +41 -0
  7. package/src/connector/SolWalletConnector.cjs +175 -0
  8. package/src/connector/SolWalletConnector.d.ts +37 -0
  9. package/src/connector/SolWalletConnector.js +171 -0
  10. package/src/connector/index.d.ts +1 -0
  11. package/src/constants.cjs +8 -0
  12. package/src/constants.d.ts +1 -0
  13. package/src/constants.js +4 -0
  14. package/src/index.cjs +36 -0
  15. package/src/index.d.ts +6 -0
  16. package/src/index.js +13 -0
  17. package/src/rpc/RpcProviderSolana/RpcProviderSolana.cjs +29 -0
  18. package/src/rpc/RpcProviderSolana/RpcProviderSolana.d.ts +18 -0
  19. package/src/rpc/RpcProviderSolana/RpcProviderSolana.js +27 -0
  20. package/src/rpc/RpcProviderSolana/index.d.ts +1 -0
  21. package/src/rpc/index.d.ts +3 -0
  22. package/src/rpc/solanaProvidersSelector/index.d.ts +1 -0
  23. package/src/rpc/solanaProvidersSelector/solanaProvidersSelector.cjs +24 -0
  24. package/src/rpc/solanaProvidersSelector/solanaProvidersSelector.d.ts +7 -0
  25. package/src/rpc/solanaProvidersSelector/solanaProvidersSelector.js +20 -0
  26. package/src/types.d.ts +67 -0
  27. package/src/utils/SolanaUiTransaction/SolanaUiTransaction.cjs +153 -0
  28. package/src/utils/SolanaUiTransaction/SolanaUiTransaction.d.ts +40 -0
  29. package/src/utils/SolanaUiTransaction/SolanaUiTransaction.js +148 -0
  30. package/src/utils/SolanaUiTransaction/index.d.ts +1 -0
  31. package/src/utils/decodeTransactionFromBase64/decodeTransactionFromBase64.cjs +16 -0
  32. package/src/utils/decodeTransactionFromBase64/decodeTransactionFromBase64.d.ts +3 -0
  33. package/src/utils/decodeTransactionFromBase64/decodeTransactionFromBase64.js +12 -0
  34. package/src/utils/decodeTransactionFromBase64/index.d.ts +1 -0
  35. package/src/utils/encodeTransactionToBase64/encodeTransactionToBase64.cjs +15 -0
  36. package/src/utils/encodeTransactionToBase64/encodeTransactionToBase64.d.ts +6 -0
  37. package/src/utils/encodeTransactionToBase64/encodeTransactionToBase64.js +11 -0
  38. package/src/utils/encodeTransactionToBase64/index.d.ts +1 -0
  39. package/src/utils/extractNonce/extractNonce.cjs +14 -0
  40. package/src/utils/extractNonce/extractNonce.d.ts +1 -0
  41. package/src/utils/extractNonce/extractNonce.js +10 -0
  42. package/src/utils/extractNonce/index.d.ts +1 -0
  43. package/src/utils/getGenesisHashLSKey/getGenesisHashLSKey.cjs +10 -0
  44. package/src/utils/getGenesisHashLSKey/getGenesisHashLSKey.d.ts +1 -0
  45. package/src/utils/getGenesisHashLSKey/getGenesisHashLSKey.js +6 -0
  46. package/src/utils/getGenesisHashLSKey/index.d.ts +1 -0
  47. package/src/utils/index.d.ts +5 -0
  48. package/src/utils/isVersionedTransaction/index.d.ts +1 -0
  49. package/src/utils/isVersionedTransaction/isVersionedTransaction.cjs +8 -0
  50. package/src/utils/isVersionedTransaction/isVersionedTransaction.d.ts +2 -0
  51. package/src/utils/isVersionedTransaction/isVersionedTransaction.js +4 -0
  52. package/src/wallet/SolanaWallet.cjs +52 -0
  53. package/src/wallet/SolanaWallet.d.ts +17 -0
  54. package/src/wallet/SolanaWallet.js +48 -0
  55. package/src/wallet/index.d.ts +2 -0
  56. package/src/wallet/isSolanaWallet/index.d.ts +1 -0
  57. package/src/wallet/isSolanaWallet/isSolanaWallet.cjs +8 -0
  58. package/src/wallet/isSolanaWallet/isSolanaWallet.d.ts +3 -0
  59. package/src/wallet/isSolanaWallet/isSolanaWallet.js +4 -0
@@ -0,0 +1,27 @@
1
+ 'use client'
2
+ import { Connection } from '@solana/web3.js';
3
+ import { ChainRpcProviders, ProviderChain } from '@dynamic-labs/rpc-providers';
4
+
5
+ ChainRpcProviders.getSolanaProviderByChainId = (rpcProviders, chainId) => {
6
+ var _a;
7
+ const provider = (_a = rpcProviders['solana']) === null || _a === void 0 ? void 0 : _a.find((rpcProvider) => rpcProvider.chainId === chainId);
8
+ return provider;
9
+ };
10
+ ChainRpcProviders.registerSolanaProviders = () => {
11
+ ChainRpcProviders.registerChainProviders(ProviderChain.SOLANA, (config) => {
12
+ const rpcProviders = {};
13
+ if (config === null || config === void 0 ? void 0 : config.solana) {
14
+ rpcProviders.solana = config.solana.map((network) => {
15
+ var _a;
16
+ const rpcUrl = ((_a = network.privateCustomerRpcUrls) === null || _a === void 0 ? void 0 : _a[0]) || network.rpcUrls[0];
17
+ const provider = new Connection(rpcUrl);
18
+ return {
19
+ chainId: network.chainId.toString(),
20
+ chainName: network.name,
21
+ provider,
22
+ };
23
+ });
24
+ }
25
+ return rpcProviders.solana;
26
+ });
27
+ };
@@ -0,0 +1 @@
1
+ import './RpcProviderSolana';
@@ -0,0 +1,3 @@
1
+ import './RpcProviderSolana';
2
+ export * from '@dynamic-labs/rpc-providers';
3
+ export { type SolanaRpcProviderMethods, solanaProvidersSelector, } from './solanaProvidersSelector';
@@ -0,0 +1 @@
1
+ export * from './solanaProvidersSelector';
@@ -0,0 +1,24 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ require('../RpcProviderSolana/RpcProviderSolana.cjs');
7
+ var rpcProviders = require('@dynamic-labs/rpc-providers');
8
+
9
+ const solanaProvidersSelector = (providers) => {
10
+ if (!providers)
11
+ return {
12
+ defaultProvider: undefined,
13
+ getProviderByChainId: () => undefined,
14
+ providers: [],
15
+ };
16
+ const getProviderByChainId = (chainId) => { var _a; return (_a = rpcProviders.ChainRpcProviders.getSolanaProviderByChainId) === null || _a === void 0 ? void 0 : _a.call(rpcProviders.ChainRpcProviders, providers, chainId); };
17
+ return {
18
+ defaultProvider: getProviderByChainId('101'),
19
+ getProviderByChainId,
20
+ providers: providers === null || providers === void 0 ? void 0 : providers.solana,
21
+ };
22
+ };
23
+
24
+ exports.solanaProvidersSelector = solanaProvidersSelector;
@@ -0,0 +1,7 @@
1
+ import { SolanaRpcProvider, RpcProvidersSelector } from '..';
2
+ export type SolanaRpcProviderMethods = {
3
+ defaultProvider: SolanaRpcProvider | undefined;
4
+ providers: SolanaRpcProvider[] | undefined;
5
+ getProviderByChainId: (chainId: string) => SolanaRpcProvider | undefined;
6
+ };
7
+ export declare const solanaProvidersSelector: RpcProvidersSelector<SolanaRpcProviderMethods>;
@@ -0,0 +1,20 @@
1
+ 'use client'
2
+ import '../RpcProviderSolana/RpcProviderSolana.js';
3
+ import { ChainRpcProviders } from '@dynamic-labs/rpc-providers';
4
+
5
+ const solanaProvidersSelector = (providers) => {
6
+ if (!providers)
7
+ return {
8
+ defaultProvider: undefined,
9
+ getProviderByChainId: () => undefined,
10
+ providers: [],
11
+ };
12
+ const getProviderByChainId = (chainId) => { var _a; return (_a = ChainRpcProviders.getSolanaProviderByChainId) === null || _a === void 0 ? void 0 : _a.call(ChainRpcProviders, providers, chainId); };
13
+ return {
14
+ defaultProvider: getProviderByChainId('101'),
15
+ getProviderByChainId,
16
+ providers: providers === null || providers === void 0 ? void 0 : providers.solana,
17
+ };
18
+ };
19
+
20
+ export { solanaProvidersSelector };
package/src/types.d.ts ADDED
@@ -0,0 +1,67 @@
1
+ import { Connection, SendOptions, Signer, Transaction, TransactionSignature, VersionedTransaction } from '@solana/web3.js';
2
+ import EventEmitter from 'eventemitter3';
3
+ declare global {
4
+ interface IWindowPhantom {
5
+ solana: ISolana;
6
+ }
7
+ interface Window {
8
+ coinbaseSolana?: ICoinbaseSolanaSigner;
9
+ glowSolana?: ISolana;
10
+ phantom?: IWindowPhantom;
11
+ solana?: ISolana;
12
+ solflare?: ISolana;
13
+ backpack?: IBackpackSolanaSigner;
14
+ magicEden?: {
15
+ solana: ISolana;
16
+ };
17
+ }
18
+ }
19
+ type PublicKey = {
20
+ toString: () => string;
21
+ };
22
+ export type ConnectionResult = {
23
+ address?: string;
24
+ publicKey?: PublicKey;
25
+ } | undefined;
26
+ interface ISolanaEvents {
27
+ connect(...args: unknown[]): unknown;
28
+ disconnect(...args: unknown[]): unknown;
29
+ accountChanged(publicKey: string): unknown;
30
+ activeWalletDidChange(publicKey: string): unknown;
31
+ }
32
+ export type SignedMessage = {
33
+ signature: Uint8Array;
34
+ };
35
+ type ExtensionLocators = {
36
+ [key in ExtensionLocator]: boolean;
37
+ };
38
+ export type ISolanaSigner = ExtensionLocators & EventEmitter<ISolanaEvents> & {
39
+ publicKey?: {
40
+ toBytes(): Uint8Array;
41
+ };
42
+ isConnected: boolean;
43
+ providers: ISolanaSigner[];
44
+ signTransaction<T extends Transaction | VersionedTransaction>(transaction: T): Promise<T>;
45
+ signAllTransactions<T extends Transaction | VersionedTransaction>(transactions: T[]): Promise<T[]>;
46
+ signAndSendTransaction<T extends Transaction | VersionedTransaction>(transaction: T, options?: SendOptions): Promise<{
47
+ signature: TransactionSignature;
48
+ }>;
49
+ signMessage(message: Uint8Array, encoding?: string): Promise<SignedMessage>;
50
+ connect: (args?: {
51
+ onlyIfTrusted: boolean;
52
+ }) => Promise<ConnectionResult>;
53
+ disconnect(): Promise<void>;
54
+ };
55
+ type BackpackSolanaSignerResponse = {
56
+ signature: Uint8Array;
57
+ };
58
+ export type IBackpackSolanaSigner = Omit<ISolanaSigner, 'signMessage'> & {
59
+ signMessage: (message: Uint8Array, encoding?: string) => Promise<void | BackpackSolanaSignerResponse | Uint8Array>;
60
+ send: (transaction: Transaction, signers?: Signer[], options?: SendOptions, connection?: Connection, publicKey?: PublicKey) => Promise<TransactionSignature>;
61
+ };
62
+ export type ICoinbaseSolanaSigner = Omit<ISolanaSigner, 'signMessage'> & {
63
+ signMessage: (message: Uint8Array, publicKey?: string) => Promise<void | Uint8Array>;
64
+ };
65
+ export type ISolana = ISolanaSigner | IBackpackSolanaSigner | ICoinbaseSolanaSigner;
66
+ export type ExtensionLocator = 'isBraveWallet' | 'isGlow' | 'isPhantom' | 'isSolflare' | 'isExodus' | 'isBackpack' | 'isMagicEden';
67
+ export {};
@@ -0,0 +1,153 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../../_virtual/_tslib.cjs');
7
+ var web3_js = require('@solana/web3.js');
8
+ var splToken = require('@solana/spl-token');
9
+ var utils = require('@dynamic-labs/utils');
10
+
11
+ const LAMPORTS_PER_SOL = 1000000000;
12
+ class SolanaUiTransaction {
13
+ constructor({ onSubmit, from, connection, originalTransaction, }) {
14
+ this.chain = 'SOL';
15
+ this.data = undefined;
16
+ this.fee = { gas: undefined };
17
+ this.feeDeducted = false;
18
+ this.formatNonNativeToken = (value, decimals) => (Number(value) / Number(Math.pow(10, decimals))).toString();
19
+ this.from = from;
20
+ this.onSubmit = onSubmit;
21
+ this.connection = connection;
22
+ this.originalTransaction = originalTransaction;
23
+ }
24
+ parse(input) {
25
+ const floatValue = parseFloat(input);
26
+ const lamports = Math.round(floatValue * LAMPORTS_PER_SOL);
27
+ return BigInt(lamports);
28
+ }
29
+ parseNonNativeToken(input, decimals) {
30
+ return BigInt(Math.floor(Number(input) * Math.pow(10, decimals)));
31
+ }
32
+ format(value, { precision } = {}) {
33
+ const solValue = Number(value) / LAMPORTS_PER_SOL;
34
+ const decimalString = solValue.toLocaleString('fullwide', {
35
+ maximumFractionDigits: 20,
36
+ minimumFractionDigits: 0,
37
+ useGrouping: false,
38
+ });
39
+ return utils.formatNumberText(decimalString, { precision });
40
+ }
41
+ submit() {
42
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
43
+ const sendTransaction = yield this.createTransaction();
44
+ return this.onSubmit(sendTransaction);
45
+ });
46
+ }
47
+ getBalance() {
48
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
49
+ const publicKey = new web3_js.PublicKey(this.from);
50
+ const balance = yield this.connection.getBalance(publicKey);
51
+ return BigInt(balance);
52
+ });
53
+ }
54
+ validateAddressFormat(address) {
55
+ if (address === 'dyn_send_transaction.multiple_recipients') {
56
+ return true;
57
+ }
58
+ return /^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(address);
59
+ }
60
+ fetchFee() {
61
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
62
+ if (this.fee.gas)
63
+ return;
64
+ const transaction = yield this.createTransactionSafe();
65
+ if (!transaction) {
66
+ return;
67
+ }
68
+ let compiledMessage;
69
+ const { blockhash } = yield this.connection.getLatestBlockhash();
70
+ if ('version' in transaction) {
71
+ transaction.message.recentBlockhash = blockhash;
72
+ compiledMessage = transaction.message;
73
+ }
74
+ else {
75
+ transaction.recentBlockhash = blockhash;
76
+ transaction.feePayer = new web3_js.PublicKey(this.from);
77
+ compiledMessage = transaction.compileMessage();
78
+ }
79
+ if (!compiledMessage) {
80
+ throw new Error('Invalid transaction');
81
+ }
82
+ let res = yield this.connection.getFeeForMessage(compiledMessage);
83
+ let retryCount = 0;
84
+ while (res.value === null && retryCount < 5) {
85
+ res = yield this.connection.getFeeForMessage(compiledMessage);
86
+ retryCount++;
87
+ }
88
+ this.fee.gas = res.value ? BigInt(res.value) : undefined;
89
+ if (!this.feeDeducted && this.fee.gas && this.value) {
90
+ this.value = this.value - this.fee.gas;
91
+ this.feeDeducted = true;
92
+ }
93
+ });
94
+ }
95
+ createTransaction() {
96
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
97
+ var _a;
98
+ const { value, to, nonNativeAddress: splTokenMintAddress, nonNativeValue, } = this;
99
+ if (!to) {
100
+ throw new Error('Destination is required');
101
+ }
102
+ if (this.originalTransaction) {
103
+ return this.originalTransaction;
104
+ }
105
+ const sendTransaction = new web3_js.Transaction();
106
+ const fromPubkey = new web3_js.PublicKey(this.from);
107
+ const toPubkey = new web3_js.PublicKey(to);
108
+ if (splTokenMintAddress && nonNativeValue) {
109
+ const tokenMintPubkey = new web3_js.PublicKey(splTokenMintAddress);
110
+ const amount = nonNativeValue;
111
+ const fromTokenAccount = (_a = (yield this.connection.getTokenAccountsByOwner(fromPubkey, {
112
+ mint: tokenMintPubkey,
113
+ })).value[0]) === null || _a === void 0 ? void 0 : _a.pubkey;
114
+ if (!fromTokenAccount)
115
+ throw new Error('Source token account not found');
116
+ let toTokenAccountPubkey;
117
+ try {
118
+ toTokenAccountPubkey = yield splToken.getAssociatedTokenAddress(tokenMintPubkey, toPubkey);
119
+ yield this.connection.getTokenAccountBalance(toTokenAccountPubkey);
120
+ }
121
+ catch (_b) {
122
+ // If the recipient doesn't have a token account, create one
123
+ toTokenAccountPubkey = yield splToken.getAssociatedTokenAddress(tokenMintPubkey, toPubkey);
124
+ sendTransaction.add(splToken.createAssociatedTokenAccountInstruction(fromPubkey, toTokenAccountPubkey, toPubkey, tokenMintPubkey));
125
+ }
126
+ sendTransaction.add(splToken.createTransferInstruction(fromTokenAccount, toTokenAccountPubkey, fromPubkey, amount));
127
+ }
128
+ else {
129
+ // Native SOL transfer
130
+ const lamports = value !== null && value !== void 0 ? value : BigInt(0);
131
+ sendTransaction.add(web3_js.SystemProgram.transfer({
132
+ fromPubkey,
133
+ lamports,
134
+ toPubkey,
135
+ }));
136
+ }
137
+ return sendTransaction;
138
+ });
139
+ }
140
+ createTransactionSafe() {
141
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
142
+ try {
143
+ return yield this.createTransaction();
144
+ }
145
+ catch (error) {
146
+ return undefined;
147
+ }
148
+ });
149
+ }
150
+ }
151
+
152
+ exports.LAMPORTS_PER_SOL = LAMPORTS_PER_SOL;
153
+ exports.SolanaUiTransaction = SolanaUiTransaction;
@@ -0,0 +1,40 @@
1
+ import { Connection, Transaction, VersionedTransaction } from '@solana/web3.js';
2
+ import { IUITransaction, IUITransactionFormatOptions } from '@dynamic-labs/types';
3
+ export declare const LAMPORTS_PER_SOL = 1000000000;
4
+ type SolanaUiTransactionProps = {
5
+ from: string;
6
+ connection: Connection;
7
+ onSubmit: (transaction: Transaction | VersionedTransaction) => Promise<string>;
8
+ originalTransaction?: Transaction | VersionedTransaction;
9
+ };
10
+ export declare class SolanaUiTransaction implements IUITransaction {
11
+ notEnoughFundsError: boolean | undefined;
12
+ to: string | undefined;
13
+ from: string;
14
+ value: bigint | undefined;
15
+ chain: string;
16
+ receipt: string | undefined;
17
+ data: undefined;
18
+ fee: {
19
+ gas: bigint | undefined;
20
+ };
21
+ originalTransaction: Transaction | VersionedTransaction | undefined;
22
+ feeDeducted: boolean;
23
+ nonNativeAddress?: string;
24
+ nonNativeDecimal?: number;
25
+ nonNativeValue?: bigint;
26
+ private onSubmit;
27
+ private connection;
28
+ constructor({ onSubmit, from, connection, originalTransaction, }: SolanaUiTransactionProps);
29
+ parse(input: string): bigint;
30
+ parseNonNativeToken(input: string, decimals: number): bigint;
31
+ formatNonNativeToken: (value: bigint, decimals: number) => string;
32
+ format(value: bigint, { precision }?: IUITransactionFormatOptions): string;
33
+ submit(): Promise<string>;
34
+ getBalance(): Promise<bigint>;
35
+ validateAddressFormat(address: string): boolean;
36
+ fetchFee(): Promise<void>;
37
+ private createTransaction;
38
+ private createTransactionSafe;
39
+ }
40
+ export {};
@@ -0,0 +1,148 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../_virtual/_tslib.js';
3
+ import { PublicKey, Transaction, SystemProgram } from '@solana/web3.js';
4
+ import { getAssociatedTokenAddress, createAssociatedTokenAccountInstruction, createTransferInstruction } from '@solana/spl-token';
5
+ import { formatNumberText } from '@dynamic-labs/utils';
6
+
7
+ const LAMPORTS_PER_SOL = 1000000000;
8
+ class SolanaUiTransaction {
9
+ constructor({ onSubmit, from, connection, originalTransaction, }) {
10
+ this.chain = 'SOL';
11
+ this.data = undefined;
12
+ this.fee = { gas: undefined };
13
+ this.feeDeducted = false;
14
+ this.formatNonNativeToken = (value, decimals) => (Number(value) / Number(Math.pow(10, decimals))).toString();
15
+ this.from = from;
16
+ this.onSubmit = onSubmit;
17
+ this.connection = connection;
18
+ this.originalTransaction = originalTransaction;
19
+ }
20
+ parse(input) {
21
+ const floatValue = parseFloat(input);
22
+ const lamports = Math.round(floatValue * LAMPORTS_PER_SOL);
23
+ return BigInt(lamports);
24
+ }
25
+ parseNonNativeToken(input, decimals) {
26
+ return BigInt(Math.floor(Number(input) * Math.pow(10, decimals)));
27
+ }
28
+ format(value, { precision } = {}) {
29
+ const solValue = Number(value) / LAMPORTS_PER_SOL;
30
+ const decimalString = solValue.toLocaleString('fullwide', {
31
+ maximumFractionDigits: 20,
32
+ minimumFractionDigits: 0,
33
+ useGrouping: false,
34
+ });
35
+ return formatNumberText(decimalString, { precision });
36
+ }
37
+ submit() {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ const sendTransaction = yield this.createTransaction();
40
+ return this.onSubmit(sendTransaction);
41
+ });
42
+ }
43
+ getBalance() {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ const publicKey = new PublicKey(this.from);
46
+ const balance = yield this.connection.getBalance(publicKey);
47
+ return BigInt(balance);
48
+ });
49
+ }
50
+ validateAddressFormat(address) {
51
+ if (address === 'dyn_send_transaction.multiple_recipients') {
52
+ return true;
53
+ }
54
+ return /^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(address);
55
+ }
56
+ fetchFee() {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ if (this.fee.gas)
59
+ return;
60
+ const transaction = yield this.createTransactionSafe();
61
+ if (!transaction) {
62
+ return;
63
+ }
64
+ let compiledMessage;
65
+ const { blockhash } = yield this.connection.getLatestBlockhash();
66
+ if ('version' in transaction) {
67
+ transaction.message.recentBlockhash = blockhash;
68
+ compiledMessage = transaction.message;
69
+ }
70
+ else {
71
+ transaction.recentBlockhash = blockhash;
72
+ transaction.feePayer = new PublicKey(this.from);
73
+ compiledMessage = transaction.compileMessage();
74
+ }
75
+ if (!compiledMessage) {
76
+ throw new Error('Invalid transaction');
77
+ }
78
+ let res = yield this.connection.getFeeForMessage(compiledMessage);
79
+ let retryCount = 0;
80
+ while (res.value === null && retryCount < 5) {
81
+ res = yield this.connection.getFeeForMessage(compiledMessage);
82
+ retryCount++;
83
+ }
84
+ this.fee.gas = res.value ? BigInt(res.value) : undefined;
85
+ if (!this.feeDeducted && this.fee.gas && this.value) {
86
+ this.value = this.value - this.fee.gas;
87
+ this.feeDeducted = true;
88
+ }
89
+ });
90
+ }
91
+ createTransaction() {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ var _a;
94
+ const { value, to, nonNativeAddress: splTokenMintAddress, nonNativeValue, } = this;
95
+ if (!to) {
96
+ throw new Error('Destination is required');
97
+ }
98
+ if (this.originalTransaction) {
99
+ return this.originalTransaction;
100
+ }
101
+ const sendTransaction = new Transaction();
102
+ const fromPubkey = new PublicKey(this.from);
103
+ const toPubkey = new PublicKey(to);
104
+ if (splTokenMintAddress && nonNativeValue) {
105
+ const tokenMintPubkey = new PublicKey(splTokenMintAddress);
106
+ const amount = nonNativeValue;
107
+ const fromTokenAccount = (_a = (yield this.connection.getTokenAccountsByOwner(fromPubkey, {
108
+ mint: tokenMintPubkey,
109
+ })).value[0]) === null || _a === void 0 ? void 0 : _a.pubkey;
110
+ if (!fromTokenAccount)
111
+ throw new Error('Source token account not found');
112
+ let toTokenAccountPubkey;
113
+ try {
114
+ toTokenAccountPubkey = yield getAssociatedTokenAddress(tokenMintPubkey, toPubkey);
115
+ yield this.connection.getTokenAccountBalance(toTokenAccountPubkey);
116
+ }
117
+ catch (_b) {
118
+ // If the recipient doesn't have a token account, create one
119
+ toTokenAccountPubkey = yield getAssociatedTokenAddress(tokenMintPubkey, toPubkey);
120
+ sendTransaction.add(createAssociatedTokenAccountInstruction(fromPubkey, toTokenAccountPubkey, toPubkey, tokenMintPubkey));
121
+ }
122
+ sendTransaction.add(createTransferInstruction(fromTokenAccount, toTokenAccountPubkey, fromPubkey, amount));
123
+ }
124
+ else {
125
+ // Native SOL transfer
126
+ const lamports = value !== null && value !== void 0 ? value : BigInt(0);
127
+ sendTransaction.add(SystemProgram.transfer({
128
+ fromPubkey,
129
+ lamports,
130
+ toPubkey,
131
+ }));
132
+ }
133
+ return sendTransaction;
134
+ });
135
+ }
136
+ createTransactionSafe() {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ try {
139
+ return yield this.createTransaction();
140
+ }
141
+ catch (error) {
142
+ return undefined;
143
+ }
144
+ });
145
+ }
146
+ }
147
+
148
+ export { LAMPORTS_PER_SOL, SolanaUiTransaction };
@@ -0,0 +1 @@
1
+ export { SolanaUiTransaction } from './SolanaUiTransaction';
@@ -0,0 +1,16 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var web3_js = require('@solana/web3.js');
7
+
8
+ const decodeTransactionFromBase64 = (encodedTransaction) => {
9
+ const transactionBuffer = Buffer.from(encodedTransaction.transaction, 'base64');
10
+ if (encodedTransaction.type === 'legacy') {
11
+ return web3_js.Transaction.from(transactionBuffer);
12
+ }
13
+ return web3_js.VersionedTransaction.deserialize(transactionBuffer);
14
+ };
15
+
16
+ exports.decodeTransactionFromBase64 = decodeTransactionFromBase64;
@@ -0,0 +1,3 @@
1
+ import { Transaction, VersionedTransaction } from '@solana/web3.js';
2
+ import { EncodedTransaction } from '../encodeTransactionToBase64';
3
+ export declare const decodeTransactionFromBase64: (encodedTransaction: EncodedTransaction) => Transaction | VersionedTransaction;
@@ -0,0 +1,12 @@
1
+ 'use client'
2
+ import { Transaction, VersionedTransaction } from '@solana/web3.js';
3
+
4
+ const decodeTransactionFromBase64 = (encodedTransaction) => {
5
+ const transactionBuffer = Buffer.from(encodedTransaction.transaction, 'base64');
6
+ if (encodedTransaction.type === 'legacy') {
7
+ return Transaction.from(transactionBuffer);
8
+ }
9
+ return VersionedTransaction.deserialize(transactionBuffer);
10
+ };
11
+
12
+ export { decodeTransactionFromBase64 };
@@ -0,0 +1 @@
1
+ export { decodeTransactionFromBase64 } from './decodeTransactionFromBase64';
@@ -0,0 +1,15 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var isVersionedTransaction = require('../isVersionedTransaction/isVersionedTransaction.cjs');
7
+
8
+ const encodeTransactionToBase64 = (transaction) => ({
9
+ transaction: Buffer.from(transaction.serialize({
10
+ verifySignatures: false,
11
+ })).toString('base64'),
12
+ type: isVersionedTransaction.isVersionedTransaction(transaction) ? 'versioned' : 'legacy',
13
+ });
14
+
15
+ exports.encodeTransactionToBase64 = encodeTransactionToBase64;
@@ -0,0 +1,6 @@
1
+ import { Transaction, VersionedTransaction } from '@solana/web3.js';
2
+ export type EncodedTransaction = {
3
+ type: 'legacy' | 'versioned';
4
+ transaction: string;
5
+ };
6
+ export declare const encodeTransactionToBase64: (transaction: Transaction | VersionedTransaction) => EncodedTransaction;
@@ -0,0 +1,11 @@
1
+ 'use client'
2
+ import { isVersionedTransaction } from '../isVersionedTransaction/isVersionedTransaction.js';
3
+
4
+ const encodeTransactionToBase64 = (transaction) => ({
5
+ transaction: Buffer.from(transaction.serialize({
6
+ verifySignatures: false,
7
+ })).toString('base64'),
8
+ type: isVersionedTransaction(transaction) ? 'versioned' : 'legacy',
9
+ });
10
+
11
+ export { encodeTransactionToBase64 };
@@ -0,0 +1 @@
1
+ export { encodeTransactionToBase64, type EncodedTransaction, } from './encodeTransactionToBase64';
@@ -0,0 +1,14 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ const extractNonce = (messageToSign) => {
7
+ const regex = messageToSign.match(/Nonce: (.*)/);
8
+ if (regex && regex.length === 2) {
9
+ return regex[1];
10
+ }
11
+ return undefined;
12
+ };
13
+
14
+ exports.extractNonce = extractNonce;
@@ -0,0 +1 @@
1
+ export declare const extractNonce: (messageToSign: string) => string | undefined;
@@ -0,0 +1,10 @@
1
+ 'use client'
2
+ const extractNonce = (messageToSign) => {
3
+ const regex = messageToSign.match(/Nonce: (.*)/);
4
+ if (regex && regex.length === 2) {
5
+ return regex[1];
6
+ }
7
+ return undefined;
8
+ };
9
+
10
+ export { extractNonce };
@@ -0,0 +1 @@
1
+ export { extractNonce } from './extractNonce';
@@ -0,0 +1,10 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var constants = require('../../constants.cjs');
7
+
8
+ const getGenesisHashLSKey = (rpcEndpoint) => `${rpcEndpoint}_${constants.SOLANA_GENESIS_HASH}`;
9
+
10
+ exports.getGenesisHashLSKey = getGenesisHashLSKey;
@@ -0,0 +1 @@
1
+ export declare const getGenesisHashLSKey: (rpcEndpoint: string) => string;
@@ -0,0 +1,6 @@
1
+ 'use client'
2
+ import { SOLANA_GENESIS_HASH } from '../../constants.js';
3
+
4
+ const getGenesisHashLSKey = (rpcEndpoint) => `${rpcEndpoint}_${SOLANA_GENESIS_HASH}`;
5
+
6
+ export { getGenesisHashLSKey };
@@ -0,0 +1 @@
1
+ export { getGenesisHashLSKey } from './getGenesisHashLSKey';
@@ -0,0 +1,5 @@
1
+ export { SolanaUiTransaction } from './SolanaUiTransaction';
2
+ export { decodeTransactionFromBase64 } from './decodeTransactionFromBase64';
3
+ export { isVersionedTransaction } from './isVersionedTransaction';
4
+ export { encodeTransactionToBase64, type EncodedTransaction, } from './encodeTransactionToBase64';
5
+ export { getGenesisHashLSKey } from './getGenesisHashLSKey';
@@ -0,0 +1 @@
1
+ export { isVersionedTransaction } from './isVersionedTransaction';