@dynamic-labs/embedded-wallet-solana 4.0.0-alpha.4 → 4.0.0-alpha.41

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.
@@ -1,162 +0,0 @@
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 walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
10
- var utils = require('@dynamic-labs/utils');
11
-
12
- const getTotalSolanaSpend = (transaction, connection, thisAddress) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
13
- var _a, _b, _c, _d, _e, _f;
14
- let simulation;
15
- if ('version' in transaction) {
16
- simulation = yield connection.simulateTransaction(transaction, {
17
- accounts: { addresses: [thisAddress], encoding: 'base64' },
18
- replaceRecentBlockhash: true,
19
- });
20
- }
21
- else {
22
- simulation = yield connection.simulateTransaction(transaction, undefined, [new web3_js.PublicKey(thisAddress)]);
23
- }
24
- const previousBalance = yield connection.getBalance(new web3_js.PublicKey(thisAddress));
25
- if (!((_c = (_b = (_a = simulation === null || simulation === void 0 ? void 0 : simulation.value) === null || _a === void 0 ? void 0 : _a.accounts) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.lamports)) {
26
- walletConnectorCore.logger.debug('Transaction simulation failed', simulation);
27
- const instructionError = JSON.stringify({
28
- InstructionError: [
29
- 0,
30
- {
31
- Custom: 1,
32
- },
33
- ],
34
- });
35
- const insufficientFundsForRent = JSON.stringify({
36
- InsufficientFundsForRent: {
37
- account_index: 0,
38
- },
39
- });
40
- if (JSON.stringify(simulation === null || simulation === void 0 ? void 0 : simulation.value.err) === instructionError ||
41
- JSON.stringify(simulation === null || simulation === void 0 ? void 0 : simulation.value.err) === insufficientFundsForRent) {
42
- throw new Error('Insufficient funds');
43
- }
44
- return undefined;
45
- }
46
- const totalSolTransfer = previousBalance - ((_f = (_e = (_d = simulation === null || simulation === void 0 ? void 0 : simulation.value) === null || _d === void 0 ? void 0 : _d.accounts) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.lamports);
47
- return BigInt(totalSolTransfer);
48
- });
49
- const decodeTransaction = (transaction, connection, thisAddress) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
50
- var _g;
51
- if (!transaction) {
52
- throw new utils.DynamicError('Transaction is required');
53
- }
54
- try {
55
- let decodedInstructions = [];
56
- if ('version' in transaction) {
57
- const lookupTableAddresses = transaction.message.addressTableLookups.map((lookup) => new web3_js.PublicKey(lookup.accountKey));
58
- // For non-simple SOL transfers, we need to fetch the lookup table accounts
59
- if (lookupTableAddresses.length > 0) {
60
- const lookupTables = yield Promise.all(lookupTableAddresses.map((address) => connection.getAddressLookupTable(address)));
61
- const lookupTableAccounts = lookupTables
62
- .filter((result) => result !== null)
63
- .map((result) => result.value);
64
- if (lookupTableAccounts.length > 0) {
65
- decodedInstructions = web3_js.TransactionMessage.decompile(transaction.message, {
66
- addressLookupTableAccounts: lookupTableAccounts,
67
- }).instructions;
68
- }
69
- }
70
- else {
71
- decodedInstructions = web3_js.TransactionMessage.decompile(transaction.message).instructions;
72
- }
73
- }
74
- else if (!transaction.instructions) {
75
- decodedInstructions = (_g = web3_js.Transaction.from(Buffer.from(transaction.serialize()))) === null || _g === void 0 ? void 0 : _g.instructions;
76
- }
77
- else {
78
- decodedInstructions = transaction.instructions;
79
- }
80
- if (!(decodedInstructions === null || decodedInstructions === void 0 ? void 0 : decodedInstructions.length)) {
81
- throw new utils.DynamicError('Bad formatted instruction');
82
- }
83
- const solTransfers = decodedInstructions.filter((instruction) => instruction.programId.equals(web3_js.SystemProgram.programId));
84
- // Non-SPL transfers, just SOL
85
- if (solTransfers.length > 0) {
86
- return solTransfers.map((decodedInstruction) => {
87
- let decodedTransferInstruction;
88
- try {
89
- decodedTransferInstruction =
90
- web3_js.SystemInstruction.decodeTransfer(decodedInstruction);
91
- }
92
- catch (e) {
93
- return { from: thisAddress, to: 'Unknown' };
94
- }
95
- return {
96
- from: decodedTransferInstruction === null || decodedTransferInstruction === void 0 ? void 0 : decodedTransferInstruction.fromPubkey.toBase58(),
97
- to: decodedTransferInstruction === null || decodedTransferInstruction === void 0 ? void 0 : decodedTransferInstruction.toPubkey.toBase58(),
98
- };
99
- });
100
- }
101
- // SPL transfers
102
- let splTransfers = yield Promise.all(decodedInstructions.map((instruction) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
103
- var _h, _j, _k, _l, _m, _o, _p;
104
- if (instruction.programId.equals(splToken.TOKEN_PROGRAM_ID)) {
105
- const decodedTokenInstruction = splToken.decodeTransferInstructionUnchecked(instruction);
106
- const { source, destination } = decodedTokenInstruction.keys;
107
- if (destination) {
108
- // For contract interactions, e.g., swaps, the destination is the turnkey address, flip sender and receiver
109
- if ((destination === null || destination === void 0 ? void 0 : destination.pubkey.toBase58()) === thisAddress) {
110
- return {
111
- from: thisAddress,
112
- to: source === null || source === void 0 ? void 0 : source.pubkey.toBase58(),
113
- };
114
- }
115
- // Pure SPL transfers, get the address from the destination token account to display to user
116
- const destinationAccountInfo = yield connection.getParsedAccountInfo(destination.pubkey);
117
- const isTokenAccount = ((_k = (_j = (_h = destinationAccountInfo.value) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j.parsed) === null || _k === void 0 ? void 0 : _k.type) === 'account';
118
- const destinationOwner = isTokenAccount
119
- ? (_p = (_o = (_m = (_l = destinationAccountInfo.value) === null || _l === void 0 ? void 0 : _l.data) === null || _m === void 0 ? void 0 : _m.parsed) === null || _o === void 0 ? void 0 : _o.info) === null || _p === void 0 ? void 0 : _p.owner
120
- : null;
121
- const toAddress = destinationOwner
122
- ? destinationOwner
123
- : destination === null || destination === void 0 ? void 0 : destination.pubkey.toBase58();
124
- return {
125
- from: thisAddress,
126
- to: toAddress,
127
- };
128
- }
129
- }
130
- return null;
131
- })));
132
- splTransfers = splTransfers.filter((transfer) => transfer !== null);
133
- if (splTransfers === null || splTransfers === void 0 ? void 0 : splTransfers.length)
134
- return splTransfers;
135
- // Return program IDs for non-SOL and non-SPL transfers
136
- const noTransferDestinations = decodedInstructions.map((instruction) => ({
137
- from: thisAddress,
138
- to: instruction.programId.toBase58(),
139
- }));
140
- if (noTransferDestinations.length)
141
- return noTransferDestinations;
142
- return [{ from: thisAddress, to: 'Unknown' }];
143
- }
144
- catch (error) {
145
- walletConnectorCore.logger.warn('[decodeTransaction]', error);
146
- return [{ from: thisAddress, to: 'Unknown' }];
147
- }
148
- });
149
- const summarizeTransactionDecodedData = (transactionsData) => {
150
- const recipients = new Set();
151
- transactionsData.forEach((transactionData) => {
152
- if (transactionData && transactionData.to) {
153
- recipients.add(transactionData.to);
154
- }
155
- });
156
- const to = recipients.size === 1 ? recipients.values().next().value : 'Multiple';
157
- return to;
158
- };
159
-
160
- exports.decodeTransaction = decodeTransaction;
161
- exports.getTotalSolanaSpend = getTotalSolanaSpend;
162
- exports.summarizeTransactionDecodedData = summarizeTransactionDecodedData;
@@ -1,12 +0,0 @@
1
- import { Transaction, VersionedTransaction, Connection } from '@solana/web3.js';
2
- type DestinationAndFrom = {
3
- to: string;
4
- from: string;
5
- };
6
- export declare const getTotalSolanaSpend: (transaction: Transaction | VersionedTransaction, connection: Connection, thisAddress: string) => Promise<bigint | undefined>;
7
- export declare const decodeTransaction: (transaction: Transaction | VersionedTransaction, connection: Connection, thisAddress: string) => Promise<Array<DestinationAndFrom>>;
8
- export declare const summarizeTransactionDecodedData: (transactionsData: {
9
- to: string;
10
- from: string;
11
- }[]) => any;
12
- export {};
@@ -1,156 +0,0 @@
1
- 'use client'
2
- import { __awaiter } from '../../../../_virtual/_tslib.js';
3
- import { PublicKey, TransactionMessage, Transaction, SystemProgram, SystemInstruction } from '@solana/web3.js';
4
- import { TOKEN_PROGRAM_ID, decodeTransferInstructionUnchecked } from '@solana/spl-token';
5
- import { logger } from '@dynamic-labs/wallet-connector-core';
6
- import { DynamicError } from '@dynamic-labs/utils';
7
-
8
- const getTotalSolanaSpend = (transaction, connection, thisAddress) => __awaiter(void 0, void 0, void 0, function* () {
9
- var _a, _b, _c, _d, _e, _f;
10
- let simulation;
11
- if ('version' in transaction) {
12
- simulation = yield connection.simulateTransaction(transaction, {
13
- accounts: { addresses: [thisAddress], encoding: 'base64' },
14
- replaceRecentBlockhash: true,
15
- });
16
- }
17
- else {
18
- simulation = yield connection.simulateTransaction(transaction, undefined, [new PublicKey(thisAddress)]);
19
- }
20
- const previousBalance = yield connection.getBalance(new PublicKey(thisAddress));
21
- if (!((_c = (_b = (_a = simulation === null || simulation === void 0 ? void 0 : simulation.value) === null || _a === void 0 ? void 0 : _a.accounts) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.lamports)) {
22
- logger.debug('Transaction simulation failed', simulation);
23
- const instructionError = JSON.stringify({
24
- InstructionError: [
25
- 0,
26
- {
27
- Custom: 1,
28
- },
29
- ],
30
- });
31
- const insufficientFundsForRent = JSON.stringify({
32
- InsufficientFundsForRent: {
33
- account_index: 0,
34
- },
35
- });
36
- if (JSON.stringify(simulation === null || simulation === void 0 ? void 0 : simulation.value.err) === instructionError ||
37
- JSON.stringify(simulation === null || simulation === void 0 ? void 0 : simulation.value.err) === insufficientFundsForRent) {
38
- throw new Error('Insufficient funds');
39
- }
40
- return undefined;
41
- }
42
- const totalSolTransfer = previousBalance - ((_f = (_e = (_d = simulation === null || simulation === void 0 ? void 0 : simulation.value) === null || _d === void 0 ? void 0 : _d.accounts) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.lamports);
43
- return BigInt(totalSolTransfer);
44
- });
45
- const decodeTransaction = (transaction, connection, thisAddress) => __awaiter(void 0, void 0, void 0, function* () {
46
- var _g;
47
- if (!transaction) {
48
- throw new DynamicError('Transaction is required');
49
- }
50
- try {
51
- let decodedInstructions = [];
52
- if ('version' in transaction) {
53
- const lookupTableAddresses = transaction.message.addressTableLookups.map((lookup) => new PublicKey(lookup.accountKey));
54
- // For non-simple SOL transfers, we need to fetch the lookup table accounts
55
- if (lookupTableAddresses.length > 0) {
56
- const lookupTables = yield Promise.all(lookupTableAddresses.map((address) => connection.getAddressLookupTable(address)));
57
- const lookupTableAccounts = lookupTables
58
- .filter((result) => result !== null)
59
- .map((result) => result.value);
60
- if (lookupTableAccounts.length > 0) {
61
- decodedInstructions = TransactionMessage.decompile(transaction.message, {
62
- addressLookupTableAccounts: lookupTableAccounts,
63
- }).instructions;
64
- }
65
- }
66
- else {
67
- decodedInstructions = TransactionMessage.decompile(transaction.message).instructions;
68
- }
69
- }
70
- else if (!transaction.instructions) {
71
- decodedInstructions = (_g = Transaction.from(Buffer.from(transaction.serialize()))) === null || _g === void 0 ? void 0 : _g.instructions;
72
- }
73
- else {
74
- decodedInstructions = transaction.instructions;
75
- }
76
- if (!(decodedInstructions === null || decodedInstructions === void 0 ? void 0 : decodedInstructions.length)) {
77
- throw new DynamicError('Bad formatted instruction');
78
- }
79
- const solTransfers = decodedInstructions.filter((instruction) => instruction.programId.equals(SystemProgram.programId));
80
- // Non-SPL transfers, just SOL
81
- if (solTransfers.length > 0) {
82
- return solTransfers.map((decodedInstruction) => {
83
- let decodedTransferInstruction;
84
- try {
85
- decodedTransferInstruction =
86
- SystemInstruction.decodeTransfer(decodedInstruction);
87
- }
88
- catch (e) {
89
- return { from: thisAddress, to: 'Unknown' };
90
- }
91
- return {
92
- from: decodedTransferInstruction === null || decodedTransferInstruction === void 0 ? void 0 : decodedTransferInstruction.fromPubkey.toBase58(),
93
- to: decodedTransferInstruction === null || decodedTransferInstruction === void 0 ? void 0 : decodedTransferInstruction.toPubkey.toBase58(),
94
- };
95
- });
96
- }
97
- // SPL transfers
98
- let splTransfers = yield Promise.all(decodedInstructions.map((instruction) => __awaiter(void 0, void 0, void 0, function* () {
99
- var _h, _j, _k, _l, _m, _o, _p;
100
- if (instruction.programId.equals(TOKEN_PROGRAM_ID)) {
101
- const decodedTokenInstruction = decodeTransferInstructionUnchecked(instruction);
102
- const { source, destination } = decodedTokenInstruction.keys;
103
- if (destination) {
104
- // For contract interactions, e.g., swaps, the destination is the turnkey address, flip sender and receiver
105
- if ((destination === null || destination === void 0 ? void 0 : destination.pubkey.toBase58()) === thisAddress) {
106
- return {
107
- from: thisAddress,
108
- to: source === null || source === void 0 ? void 0 : source.pubkey.toBase58(),
109
- };
110
- }
111
- // Pure SPL transfers, get the address from the destination token account to display to user
112
- const destinationAccountInfo = yield connection.getParsedAccountInfo(destination.pubkey);
113
- const isTokenAccount = ((_k = (_j = (_h = destinationAccountInfo.value) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j.parsed) === null || _k === void 0 ? void 0 : _k.type) === 'account';
114
- const destinationOwner = isTokenAccount
115
- ? (_p = (_o = (_m = (_l = destinationAccountInfo.value) === null || _l === void 0 ? void 0 : _l.data) === null || _m === void 0 ? void 0 : _m.parsed) === null || _o === void 0 ? void 0 : _o.info) === null || _p === void 0 ? void 0 : _p.owner
116
- : null;
117
- const toAddress = destinationOwner
118
- ? destinationOwner
119
- : destination === null || destination === void 0 ? void 0 : destination.pubkey.toBase58();
120
- return {
121
- from: thisAddress,
122
- to: toAddress,
123
- };
124
- }
125
- }
126
- return null;
127
- })));
128
- splTransfers = splTransfers.filter((transfer) => transfer !== null);
129
- if (splTransfers === null || splTransfers === void 0 ? void 0 : splTransfers.length)
130
- return splTransfers;
131
- // Return program IDs for non-SOL and non-SPL transfers
132
- const noTransferDestinations = decodedInstructions.map((instruction) => ({
133
- from: thisAddress,
134
- to: instruction.programId.toBase58(),
135
- }));
136
- if (noTransferDestinations.length)
137
- return noTransferDestinations;
138
- return [{ from: thisAddress, to: 'Unknown' }];
139
- }
140
- catch (error) {
141
- logger.warn('[decodeTransaction]', error);
142
- return [{ from: thisAddress, to: 'Unknown' }];
143
- }
144
- });
145
- const summarizeTransactionDecodedData = (transactionsData) => {
146
- const recipients = new Set();
147
- transactionsData.forEach((transactionData) => {
148
- if (transactionData && transactionData.to) {
149
- recipients.add(transactionData.to);
150
- }
151
- });
152
- const to = recipients.size === 1 ? recipients.values().next().value : 'Multiple';
153
- return to;
154
- };
155
-
156
- export { decodeTransaction, getTotalSolanaSpend, summarizeTransactionDecodedData };