@meshconnect/web-link-sdk 3.2.5 → 3.2.6

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshconnect/web-link-sdk",
3
- "version": "3.2.5",
3
+ "version": "3.2.6",
4
4
  "description": "A client-side JS library for integrating with Mesh Connect",
5
5
  "exports": "./index.js",
6
6
  "license": "MIT",
@@ -1,18 +1,13 @@
1
1
  import { PublicKey, VersionedTransaction, TransactionInstruction } from '@meshconnect/solana-web3.js';
2
2
  import { TransactionConfig, SolanaProvider } from './types';
3
- export declare function getAssociatedTokenAddress(mint: PublicKey, owner: PublicKey): Promise<PublicKey>;
3
+ export declare function getAssociatedTokenAddress(mint: PublicKey, owner: PublicKey, programId?: string): Promise<PublicKey>;
4
+ export declare function createTransferCheckedInstruction(fromTokenAccount: PublicKey, toTokenAccount: PublicKey, owner: PublicKey, amount: bigint, decimals: number | undefined, tokenMint: PublicKey): TransactionInstruction;
4
5
  export declare function createSPLTransferInstruction({ fromTokenAccount, toTokenAccount, owner, amount }: {
5
6
  fromTokenAccount: PublicKey;
6
7
  toTokenAccount: PublicKey;
7
8
  owner: PublicKey;
8
9
  amount: bigint;
9
10
  }): TransactionInstruction;
10
- export declare function createTransferTransaction(config: {
11
- fromAddress: string;
12
- toAddress: string;
13
- amount: bigint;
14
- tokenMint?: string;
15
- blockhash: string;
16
- }): Promise<VersionedTransaction>;
11
+ export declare function createTransferTransaction(config: TransactionConfig): Promise<VersionedTransaction>;
17
12
  export declare function handleManualSignAndSend(transaction: VersionedTransaction, provider: SolanaProvider): Promise<string>;
18
13
  export declare const sendSOLTransaction: (config: TransactionConfig) => Promise<string>;
@@ -34,9 +34,10 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
34
34
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
35
  }
36
36
  };
37
- import { PublicKey, SystemProgram, TransactionMessage, VersionedTransaction, TransactionInstruction } from '@meshconnect/solana-web3.js';
37
+ import { PublicKey, SystemProgram, TransactionMessage, VersionedTransaction, TransactionInstruction, Connection } from '@meshconnect/solana-web3.js';
38
38
  import { getSolanaProvider } from './providerDiscovery';
39
39
  var TOKEN_PROGRAM_ID = new PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA');
40
+ var TOKEN_2022_PROGRAM_ID = new PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
40
41
  var ASSOCIATED_TOKEN_PROGRAM_ID = new PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');
41
42
  var isUserRejection = function (error) {
42
43
  if (!error || typeof error !== 'object')
@@ -49,12 +50,13 @@ var isUserRejection = function (error) {
49
50
  message.includes('denied') ||
50
51
  err.code === 4001);
51
52
  };
52
- export function getAssociatedTokenAddress(mint, owner) {
53
- return __awaiter(this, void 0, void 0, function () {
53
+ export function getAssociatedTokenAddress(mint_1, owner_1) {
54
+ return __awaiter(this, arguments, void 0, function (mint, owner, programId) {
54
55
  var address;
56
+ if (programId === void 0) { programId = TOKEN_PROGRAM_ID.toBase58(); }
55
57
  return __generator(this, function (_a) {
56
58
  switch (_a.label) {
57
- case 0: return [4 /*yield*/, PublicKey.findProgramAddress([owner.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), mint.toBuffer()], ASSOCIATED_TOKEN_PROGRAM_ID)];
59
+ case 0: return [4 /*yield*/, PublicKey.findProgramAddress([owner.toBuffer(), new PublicKey(programId).toBuffer(), mint.toBuffer()], ASSOCIATED_TOKEN_PROGRAM_ID)];
58
60
  case 1:
59
61
  address = (_a.sent())[0];
60
62
  return [2 /*return*/, address];
@@ -62,6 +64,24 @@ export function getAssociatedTokenAddress(mint, owner) {
62
64
  });
63
65
  });
64
66
  }
67
+ export function createTransferCheckedInstruction(fromTokenAccount, toTokenAccount, owner, amount, decimals, tokenMint) {
68
+ if (decimals === void 0) { decimals = 9; }
69
+ var data = Buffer.alloc(10);
70
+ data[0] = 12; // TransferChecked instruction enum
71
+ data.writeBigUInt64LE(amount, 1); // 8-byte amount
72
+ data[9] = decimals; // single-byte decimal
73
+ var programId = TOKEN_2022_PROGRAM_ID;
74
+ return new TransactionInstruction({
75
+ keys: [
76
+ { pubkey: fromTokenAccount, isSigner: false, isWritable: true },
77
+ { pubkey: tokenMint, isSigner: false, isWritable: false },
78
+ { pubkey: toTokenAccount, isSigner: false, isWritable: true },
79
+ { pubkey: owner, isSigner: true, isWritable: false }
80
+ ],
81
+ programId: programId,
82
+ data: data
83
+ });
84
+ }
65
85
  export function createSPLTransferInstruction(_a) {
66
86
  var fromTokenAccount = _a.fromTokenAccount, toTokenAccount = _a.toTokenAccount, owner = _a.owner, amount = _a.amount;
67
87
  var data = Buffer.alloc(9);
@@ -86,7 +106,6 @@ export function createTransferTransaction(config) {
86
106
  fromPubkey = new PublicKey(config.fromAddress);
87
107
  toPubkey = new PublicKey(config.toAddress);
88
108
  if (!!config.tokenMint) return [3 /*break*/, 1];
89
- // Native SOL transfer
90
109
  instruction = SystemProgram.transfer({
91
110
  fromPubkey: fromPubkey,
92
111
  toPubkey: toPubkey,
@@ -95,18 +114,21 @@ export function createTransferTransaction(config) {
95
114
  return [3 /*break*/, 4];
96
115
  case 1:
97
116
  tokenMintPubkey = new PublicKey(config.tokenMint);
98
- return [4 /*yield*/, getAssociatedTokenAddress(tokenMintPubkey, fromPubkey)];
117
+ return [4 /*yield*/, getAssociatedTokenAddress(tokenMintPubkey, fromPubkey, config.tokenProgram)];
99
118
  case 2:
100
119
  fromTokenAccount = _a.sent();
101
- return [4 /*yield*/, getAssociatedTokenAddress(tokenMintPubkey, toPubkey)];
120
+ return [4 /*yield*/, getAssociatedTokenAddress(tokenMintPubkey, toPubkey, config.tokenProgram)];
102
121
  case 3:
103
122
  toTokenAccount = _a.sent();
104
- instruction = createSPLTransferInstruction({
105
- fromTokenAccount: fromTokenAccount,
106
- toTokenAccount: toTokenAccount,
107
- owner: fromPubkey,
108
- amount: BigInt(config.amount)
109
- });
123
+ instruction =
124
+ config.tokenProgram === TOKEN_2022_PROGRAM_ID.toBase58()
125
+ ? createTransferCheckedInstruction(fromTokenAccount, toTokenAccount, fromPubkey, BigInt(config.amount), config.tokenDecimals, tokenMintPubkey)
126
+ : createSPLTransferInstruction({
127
+ fromTokenAccount: fromTokenAccount,
128
+ toTokenAccount: toTokenAccount,
129
+ owner: fromPubkey,
130
+ amount: BigInt(config.amount)
131
+ });
110
132
  _a.label = 4;
111
133
  case 4:
112
134
  messageV0 = new TransactionMessage({
@@ -156,38 +178,55 @@ export function handleManualSignAndSend(transaction, provider) {
156
178
  });
157
179
  }
158
180
  export var sendSOLTransaction = function (config) { return __awaiter(void 0, void 0, void 0, function () {
159
- var provider, transaction, isManualWallet, signature, error_2, error_3;
181
+ var provider, walletPublicKey, connection, token2022Accounts, transaction, isManualWallet, signature, error_2, error_3;
160
182
  return __generator(this, function (_a) {
161
183
  switch (_a.label) {
162
184
  case 0:
163
- _a.trys.push([0, 8, , 9]);
185
+ _a.trys.push([0, 10, , 11]);
164
186
  provider = getSolanaProvider(config.walletName);
165
- return [4 /*yield*/, createTransferTransaction(config)];
187
+ walletPublicKey = new PublicKey(config.fromAddress);
188
+ if (!config.tokenMint) return [3 /*break*/, 2];
189
+ connection = void 0;
190
+ // special use case for PYUSD on solana devnet. TODO: make it generic
191
+ if (config.tokenMint === 'CXk2AMBfi3TwaEL2468s6zP8xq9NxTXjp9gjMgzeUynM') {
192
+ connection = new Connection('https://api.devnet.solana.com', 'confirmed');
193
+ }
194
+ else {
195
+ connection = new Connection('https://api.mainnet-beta.solana.com', 'confirmed');
196
+ }
197
+ return [4 /*yield*/, connection.getTokenAccountsByOwner(walletPublicKey, { programId: TOKEN_2022_PROGRAM_ID })];
166
198
  case 1:
199
+ token2022Accounts = _a.sent();
200
+ config.tokenProgram = (token2022Accounts === null || token2022Accounts === void 0 ? void 0 : token2022Accounts.value.length)
201
+ ? TOKEN_2022_PROGRAM_ID.toBase58()
202
+ : TOKEN_PROGRAM_ID.toBase58();
203
+ _a.label = 2;
204
+ case 2: return [4 /*yield*/, createTransferTransaction(config)];
205
+ case 3:
167
206
  transaction = _a.sent();
168
207
  isManualWallet = provider.isTrust ||
169
208
  provider.isTrustWallet ||
170
209
  config.walletName.toLowerCase().includes('trust');
171
- if (!isManualWallet) return [3 /*break*/, 3];
210
+ if (!isManualWallet) return [3 /*break*/, 5];
172
211
  return [4 /*yield*/, handleManualSignAndSend(transaction, provider)];
173
- case 2: return [2 /*return*/, _a.sent()];
174
- case 3:
175
- if (!provider.signAndSendTransaction) return [3 /*break*/, 7];
176
- _a.label = 4;
177
- case 4:
178
- _a.trys.push([4, 6, , 7]);
179
- return [4 /*yield*/, provider.signAndSendTransaction(transaction)];
212
+ case 4: return [2 /*return*/, _a.sent()];
180
213
  case 5:
214
+ if (!provider.signAndSendTransaction) return [3 /*break*/, 9];
215
+ _a.label = 6;
216
+ case 6:
217
+ _a.trys.push([6, 8, , 9]);
218
+ return [4 /*yield*/, provider.signAndSendTransaction(transaction)];
219
+ case 7:
181
220
  signature = (_a.sent()).signature;
182
221
  return [2 /*return*/, signature];
183
- case 6:
222
+ case 8:
184
223
  error_2 = _a.sent();
185
224
  if (isUserRejection(error_2)) {
186
225
  throw new Error('Transaction was rejected by user');
187
226
  }
188
227
  return [2 /*return*/, handleManualSignAndSend(transaction, provider)];
189
- case 7: return [2 /*return*/, handleManualSignAndSend(transaction, provider)];
190
- case 8:
228
+ case 9: return [2 /*return*/, handleManualSignAndSend(transaction, provider)];
229
+ case 10:
191
230
  error_3 = _a.sent();
192
231
  if (isUserRejection(error_3)) {
193
232
  throw new Error('Transaction was rejected by user');
@@ -195,7 +234,7 @@ export var sendSOLTransaction = function (config) { return __awaiter(void 0, voi
195
234
  throw error_3 instanceof Error
196
235
  ? error_3
197
236
  : new Error("Failed to send SOL transaction with ".concat(config.walletName, " wallet"));
198
- case 9: return [2 /*return*/];
237
+ case 11: return [2 /*return*/];
199
238
  }
200
239
  });
201
240
  }); };
@@ -1 +1 @@
1
- export declare const sdkVersion = "3.2.5";
1
+ export declare const sdkVersion = "3.2.6";
package/utils/version.js CHANGED
@@ -1 +1 @@
1
- export var sdkVersion = '3.2.5';
1
+ export var sdkVersion = '3.2.6';