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

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