@dynamic-labs/solana-core 3.0.0-alpha.51

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 (35) hide show
  1. package/CHANGELOG.md +3918 -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 +38 -0
  7. package/src/index.cjs +26 -0
  8. package/src/index.d.ts +2 -0
  9. package/src/index.js +8 -0
  10. package/src/rpc/RpcProviderSolana/RpcProviderSolana.cjs +29 -0
  11. package/src/rpc/RpcProviderSolana/RpcProviderSolana.d.ts +18 -0
  12. package/src/rpc/RpcProviderSolana/RpcProviderSolana.js +27 -0
  13. package/src/rpc/RpcProviderSolana/index.d.ts +1 -0
  14. package/src/rpc/index.d.ts +3 -0
  15. package/src/rpc/solanaProvidersSelector/index.d.ts +1 -0
  16. package/src/rpc/solanaProvidersSelector/solanaProvidersSelector.cjs +24 -0
  17. package/src/rpc/solanaProvidersSelector/solanaProvidersSelector.d.ts +7 -0
  18. package/src/rpc/solanaProvidersSelector/solanaProvidersSelector.js +20 -0
  19. package/src/utils/SolanaUiTransaction/SolanaUiTransaction.cjs +153 -0
  20. package/src/utils/SolanaUiTransaction/SolanaUiTransaction.d.ts +40 -0
  21. package/src/utils/SolanaUiTransaction/SolanaUiTransaction.js +148 -0
  22. package/src/utils/SolanaUiTransaction/index.d.ts +1 -0
  23. package/src/utils/decodeTransactionFromBase64/decodeTransactionFromBase64.cjs +16 -0
  24. package/src/utils/decodeTransactionFromBase64/decodeTransactionFromBase64.d.ts +3 -0
  25. package/src/utils/decodeTransactionFromBase64/decodeTransactionFromBase64.js +12 -0
  26. package/src/utils/decodeTransactionFromBase64/index.d.ts +1 -0
  27. package/src/utils/encodeTransactionToBase64/encodeTransactionToBase64.cjs +15 -0
  28. package/src/utils/encodeTransactionToBase64/encodeTransactionToBase64.d.ts +6 -0
  29. package/src/utils/encodeTransactionToBase64/encodeTransactionToBase64.js +11 -0
  30. package/src/utils/encodeTransactionToBase64/index.d.ts +1 -0
  31. package/src/utils/index.d.ts +4 -0
  32. package/src/utils/isVersionedTransaction/index.d.ts +1 -0
  33. package/src/utils/isVersionedTransaction/isVersionedTransaction.cjs +8 -0
  34. package/src/utils/isVersionedTransaction/isVersionedTransaction.d.ts +2 -0
  35. package/src/utils/isVersionedTransaction/isVersionedTransaction.js +4 -0
@@ -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,4 @@
1
+ export { SolanaUiTransaction } from './SolanaUiTransaction';
2
+ export { decodeTransactionFromBase64 } from './decodeTransactionFromBase64';
3
+ export { isVersionedTransaction } from './isVersionedTransaction';
4
+ export { encodeTransactionToBase64, type EncodedTransaction, } from './encodeTransactionToBase64';
@@ -0,0 +1 @@
1
+ export { isVersionedTransaction } from './isVersionedTransaction';
@@ -0,0 +1,8 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ const isVersionedTransaction = (transaction) => 'version' in transaction;
7
+
8
+ exports.isVersionedTransaction = isVersionedTransaction;
@@ -0,0 +1,2 @@
1
+ import { Transaction, VersionedTransaction } from '@solana/web3.js';
2
+ export declare const isVersionedTransaction: (transaction: Transaction | VersionedTransaction) => transaction is VersionedTransaction;
@@ -0,0 +1,4 @@
1
+ 'use client'
2
+ const isVersionedTransaction = (transaction) => 'version' in transaction;
3
+
4
+ export { isVersionedTransaction };