@deserialize/multi-vm-wallet 1.2.0 → 1.2.2

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 (60) hide show
  1. package/dist/bip32Old.d.ts +0 -51
  2. package/dist/bip32Old.js +876 -845
  3. package/dist/bip32Old.js.map +1 -1
  4. package/dist/bip32Small.d.ts +0 -9
  5. package/dist/bip32Small.js +78 -113
  6. package/dist/bip32Small.js.map +1 -1
  7. package/dist/constant.d.ts +16 -0
  8. package/dist/constant.js +19 -3
  9. package/dist/constant.js.map +1 -1
  10. package/dist/english.d.ts +1 -0
  11. package/dist/english.js +2052 -0
  12. package/dist/english.js.map +1 -0
  13. package/dist/evm/evm.d.ts +6 -1
  14. package/dist/evm/evm.js +36 -45
  15. package/dist/evm/evm.js.map +1 -1
  16. package/dist/evm/transactionParsing.d.ts +3687 -0
  17. package/dist/evm/transactionParsing.js +441 -0
  18. package/dist/evm/transactionParsing.js.map +1 -0
  19. package/dist/evm/utils.d.ts +2 -9
  20. package/dist/evm/utils.js +19 -22
  21. package/dist/evm/utils.js.map +1 -1
  22. package/dist/helpers/index.d.ts +4 -0
  23. package/dist/helpers/index.js +13 -0
  24. package/dist/helpers/index.js.map +1 -0
  25. package/dist/svm/constant.d.ts +15 -0
  26. package/dist/svm/constant.js +25 -0
  27. package/dist/svm/constant.js.map +1 -0
  28. package/dist/svm/svm.d.ts +5 -2
  29. package/dist/svm/svm.js +10 -0
  30. package/dist/svm/svm.js.map +1 -1
  31. package/dist/svm/transactionParsing.d.ts +28 -0
  32. package/dist/svm/transactionParsing.js +207 -0
  33. package/dist/svm/transactionParsing.js.map +1 -0
  34. package/dist/svm/utils.d.ts +4 -3
  35. package/dist/svm/utils.js +83 -10
  36. package/dist/svm/utils.js.map +1 -1
  37. package/dist/test.d.ts +1 -1
  38. package/dist/test.js +47 -9
  39. package/dist/test.js.map +1 -1
  40. package/dist/types.d.ts +5 -1
  41. package/dist/types.js.map +1 -1
  42. package/dist/walletBip32.js +1 -1
  43. package/dist/walletBip32.js.map +1 -1
  44. package/package.json +4 -2
  45. package/utils/IChainWallet.ts +1 -1
  46. package/utils/bip32Old.ts +988 -988
  47. package/utils/bip32Small.ts +78 -78
  48. package/utils/constant.ts +22 -4
  49. package/utils/english.ts +2048 -0
  50. package/utils/evm/evm.ts +54 -48
  51. package/utils/evm/transactionParsing.ts +639 -0
  52. package/utils/evm/utils.ts +29 -33
  53. package/utils/helpers/index.ts +11 -0
  54. package/utils/svm/constant.ts +29 -0
  55. package/utils/svm/svm.ts +14 -2
  56. package/utils/svm/transactionParsing.ts +294 -0
  57. package/utils/svm/utils.ts +105 -14
  58. package/utils/test.ts +56 -6
  59. package/utils/types.ts +6 -1
  60. package/utils/walletBip32.ts +1 -1
@@ -2,9 +2,10 @@
2
2
 
3
3
  import { Account, createAssociatedTokenAccountIdempotentInstruction, createTransferCheckedInstruction, getAccount, getAssociatedTokenAddress, getAssociatedTokenAddressSync, getMint, Mint, NATIVE_MINT, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@solana/spl-token";
4
4
  import { Connection, Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js";
5
- import { TokenInfo } from "../types";
5
+ import { ChainWalletConfig, UserTokenBalance, TokenInfo } from "../types";
6
6
  import { transactionSenderAndConfirmationWaiter } from "./transactionSender";
7
7
  import { BN } from "bn.js";
8
+ import { Metaplex } from "@metaplex-foundation/js";
8
9
 
9
10
  const JUPITER_BASE_URL = 'https://lite-api.jup.ag';
10
11
 
@@ -270,9 +271,25 @@ export const getTransferNativeTransaction = async (from: Keypair, to: PublicKey,
270
271
  console.log('getTransferNativeTransaction: Completed');
271
272
  return transaction;
272
273
  }
274
+ const getMetaTokenMetaplexData = (mintAddress: PublicKey, connection: Connection) => {
275
+ const metaplex = Metaplex.make(connection);
276
+ return metaplex.nfts().findByMint({ mintAddress: mintAddress });
277
+ }
273
278
 
274
- export const getTokenInfo = async (tokenAddress: PublicKey, connection: Connection): Promise<TokenInfo> => {
279
+ export const getTokenInfo = async (tokenAddress: PublicKey, connection: Connection, programId?: PublicKey): Promise<TokenInfo> => {
275
280
  let mint: Mint
281
+
282
+
283
+ const metaplexData = await getMetaTokenMetaplexData(tokenAddress, connection).catch(() => null);
284
+ if (programId) {
285
+ const mint = await getMint(connection, tokenAddress, "confirmed", programId)
286
+ return {
287
+ address: tokenAddress.toString(),
288
+ decimals: mint.decimals,
289
+ name: metaplexData?.name || "",
290
+ symbol: metaplexData?.symbol || ""
291
+ }
292
+ }
276
293
  try {
277
294
  mint = await getMint(connection, tokenAddress, "confirmed", TOKEN_PROGRAM_ID)
278
295
 
@@ -286,8 +303,8 @@ export const getTokenInfo = async (tokenAddress: PublicKey, connection: Connecti
286
303
  return {
287
304
  address: tokenAddress.toString(),
288
305
  decimals: mint.decimals,
289
- name: "",
290
- symbol: ""
306
+ name: metaplexData?.name || "",
307
+ symbol: metaplexData?.symbol || ""
291
308
  }
292
309
  }
293
310
 
@@ -359,6 +376,35 @@ export const signAndSendTransaction = async (transaction: VersionedTransaction,
359
376
  console.log('Transaction successful, signature:', signature);
360
377
  return signature;
361
378
  }
379
+ export const discoverTokens = async (ownerAddress: PublicKey, connection: Connection): Promise<UserTokenBalance<PublicKey>[]> => {
380
+
381
+ const owner = new PublicKey(ownerAddress);
382
+ let response = await connection.getParsedTokenAccountsByOwner(owner, {
383
+ programId: TOKEN_PROGRAM_ID
384
+ });
385
+ console.log('response: ', response);
386
+ let tokens2022 = await connection.getParsedTokenAccountsByOwner(owner, {
387
+ programId: TOKEN_2022_PROGRAM_ID
388
+ });
389
+ console.log('tokens2022: ', tokens2022);
390
+
391
+ response.value = response.value.concat(tokens2022.value);
392
+ const tokens = await Promise.all(response.value.map(async (accountInfo) => {
393
+
394
+ const mintAddress = accountInfo.account.data["parsed"]["info"]["mint"]
395
+ const mint = await getTokenInfo(new PublicKey(mintAddress), connection)
396
+ return {
397
+ owner: accountInfo.account.data["parsed"]["info"]["owner"],
398
+ address: accountInfo.account.data["parsed"]["info"]["mint"],
399
+ decimals: accountInfo.account.data["parsed"]["info"]["tokenAmount"]["decimals"],
400
+ symbol: mint.symbol,
401
+ name: mint.name,
402
+ balance: accountInfo.account.data["parsed"]["info"]["tokenAmount"]["amount"]
403
+ }
404
+ }))
405
+
406
+ return tokens;
407
+ }
362
408
 
363
409
  //swap
364
410
  //you will. use jupiter for this
@@ -417,17 +463,21 @@ export const getJupiterQuote = async (
417
463
  export const buildJupiterSwapTransaction = async (
418
464
  quote: JupiterQuoteResponse,
419
465
  userPublicKey: string,
420
- prioritizationFeeLamports?: number
466
+ prioritizationFeeLamports?: number,
467
+ useSharedAccounts: boolean = true
421
468
  ): Promise<JupiterSwapResponse> => {
422
469
  console.log('buildJupiterSwapTransaction: Starting');
423
470
  console.log('User public key:', userPublicKey);
471
+ console.log('Use shared accounts:', useSharedAccounts);
472
+
424
473
  const priorityFee = prioritizationFeeLamports || 5000;
425
474
  console.log('Prioritization fee:', priorityFee);
475
+
426
476
  const body = {
427
477
  quoteResponse: quote,
428
478
  userPublicKey,
429
479
  wrapAndUnwrapSol: true,
430
- useSharedAccounts: true,
480
+ useSharedAccounts: useSharedAccounts,
431
481
  feeAccount: undefined,
432
482
  trackingAccount: undefined,
433
483
  computeUnitPriceMicroLamports: undefined,
@@ -436,7 +486,7 @@ export const buildJupiterSwapTransaction = async (
436
486
  useTokenLedger: false,
437
487
  destinationTokenAccount: undefined,
438
488
  dynamicComputeUnitLimit: true,
439
- skipUserAccountsRpcCalls: false
489
+ skipUserAccountsRpcCalls: true
440
490
  };
441
491
 
442
492
  console.log('Request body:', body);
@@ -459,9 +509,23 @@ export const buildJupiterSwapTransaction = async (
459
509
  try {
460
510
  const error = await response.json();
461
511
  console.log('Swap build error details:', error);
512
+
513
+ // Check if this is the shared accounts error
514
+ if (error.errorCode === 'NOT_SUPPORTED' &&
515
+ error.error?.includes('Simple AMMs are not supported with shared accounts')) {
516
+ console.log('Detected shared accounts incompatibility error');
517
+ throw new Error('SHARED_ACCOUNTS_NOT_SUPPORTED');
518
+ }
519
+
462
520
  throw new Error(`Jupiter swap transaction build failed: ${error.message || response.statusText}`);
463
521
  } catch (parseError) {
464
522
  console.log('Failed to parse error response:', parseError);
523
+
524
+ // Re-throw if it's our custom error
525
+ if (parseError instanceof Error && parseError.message === 'SHARED_ACCOUNTS_NOT_SUPPORTED') {
526
+ throw parseError;
527
+ }
528
+
465
529
  throw new Error(`Jupiter swap transaction build failed: ${response.statusText}`);
466
530
  }
467
531
  }
@@ -500,12 +564,38 @@ export const executeJupiterSwap = async (
500
564
  priceImpact: quote.priceImpactPct
501
565
  });
502
566
 
503
- console.log('Building swap transaction...');
504
- const swapResponse = await buildJupiterSwapTransaction(
505
- quote,
506
- swapParams.userPublicKey.toString()
507
- );
567
+ let swapResponse: JupiterSwapResponse;
568
+ let usedSharedAccounts = true;
508
569
 
570
+ try {
571
+ console.log('Building swap transaction with shared accounts enabled...');
572
+ swapResponse = await buildJupiterSwapTransaction(
573
+ quote,
574
+ swapParams.userPublicKey.toString(),
575
+ undefined,
576
+ true // Try with shared accounts first
577
+ );
578
+ console.log('Successfully built transaction with shared accounts');
579
+ } catch (error) {
580
+ if (error instanceof Error && error.message === 'SHARED_ACCOUNTS_NOT_SUPPORTED') {
581
+ console.log('Shared accounts not supported, retrying without shared accounts...');
582
+
583
+ // Retry without shared accounts
584
+ swapResponse = await buildJupiterSwapTransaction(
585
+ quote,
586
+ swapParams.userPublicKey.toString(),
587
+ undefined,
588
+ false // Retry with shared accounts disabled
589
+ );
590
+ usedSharedAccounts = false;
591
+ console.log('Successfully built transaction without shared accounts');
592
+ } else {
593
+ // Re-throw if it's a different error
594
+ throw error;
595
+ }
596
+ }
597
+
598
+ console.log('Transaction build method:', usedSharedAccounts ? 'with shared accounts' : 'without shared accounts');
509
599
  console.log('Deserializing transaction...');
510
600
  const swapTransactionBuf = Buffer.from(swapResponse.swapTransaction, 'base64');
511
601
  console.log('Transaction buffer length:', swapTransactionBuf.length);
@@ -530,7 +620,6 @@ export const executeJupiterSwap = async (
530
620
  }
531
621
  });
532
622
 
533
- // console.log('signature: ', signature);
534
623
  if (!signature) {
535
624
  console.log('Transaction failed to confirm');
536
625
  return {
@@ -541,6 +630,7 @@ export const executeJupiterSwap = async (
541
630
 
542
631
  const txSignature = signature.transaction.signatures[0];
543
632
  console.log('Swap successful! Signature:', txSignature);
633
+ console.log('Used shared accounts:', usedSharedAccounts);
544
634
 
545
635
  return {
546
636
  success: true,
@@ -631,4 +721,5 @@ export const validateJupiterTokens = async (
631
721
  console.log('Token validation failed:', error);
632
722
  return { valid: false, message: 'Failed to validate tokens' };
633
723
  }
634
- };
724
+ };
725
+
package/utils/test.ts CHANGED
@@ -3,11 +3,16 @@ import { generateKey } from "crypto";
3
3
 
4
4
  import base58 from "bs58";
5
5
  import { NATIVE_MINT } from "@solana/spl-token";
6
+ import { Connection, PublicKey, Keypair } from "@solana/web3.js";
6
7
  // import { GenerateNewMnemonic } from "./bip32";
7
- import { SVMChainWallet, SVMVM } from "./svm";
8
+ import { SVMChainWallet, SVMVM, } from "./svm";
8
9
  import { VM } from "./vm";
9
10
  import { ChainWalletConfig } from "./types";
10
- import { Keypair } from ".";
11
+ import { EVMChainWallet } from "./evm";
12
+ import { } from "./svm/transactionParsing";
13
+ import { getEVMTransactionHistory } from "./evm/transactionParsing";
14
+ import { createPublicClient, http, PublicClient } from "viem";
15
+ import { base, baseGoerli } from "viem/chains";
11
16
  // const mnemonic = GenerateNewMnemonic()
12
17
 
13
18
 
@@ -18,6 +23,9 @@ const pKey = "4QxETeX9pndiF1XNghUiDTnZnHq3cfjmuPLBJysrgocsLq1yb8w96aPWALa8ZnRZWm
18
23
 
19
24
  export const testUserKeyPair = Keypair.fromSecretKey(base58.decode(pKey));
20
25
  const x = testUserKeyPair instanceof Keypair;
26
+ const evePrivateKey = "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318"
27
+ const evmPrivateKey2 = "0x0123456789012345678901234567890123456789012345678901234567890123"
28
+
21
29
  // const vm = new SVMVM(seed)
22
30
 
23
31
 
@@ -27,7 +35,7 @@ const x = testUserKeyPair instanceof Keypair;
27
35
  // const key = vm.generatePrivateKey(0)
28
36
  // console.log('key: ', key.privateKey.publicKey);
29
37
  const chainConfig: ChainWalletConfig = {
30
- chainId: "solana-mainnet",
38
+ chainId: 123456789,
31
39
  name: "Solana",
32
40
  rpcUrl: "https://solana-mainnet.g.alchemy.com/v2/vB5mKztdJeFdz9RkW99Qf",
33
41
  explorerUrl: "https://explorer.solana.com",
@@ -37,7 +45,7 @@ const chainConfig: ChainWalletConfig = {
37
45
  }
38
46
 
39
47
  const evmChainConfig: ChainWalletConfig = {
40
- chainId: "evm-mainnet",
48
+ chainId: 8453,
41
49
  name: "Ethereum",
42
50
  rpcUrl: "https://eth-mainnet.g.alchemy.com/v2/vB5mKztdJeFdz9RkW99Qf",
43
51
  explorerUrl: "https://explorer.ethereum.com",
@@ -46,10 +54,13 @@ const evmChainConfig: ChainWalletConfig = {
46
54
  }
47
55
 
48
56
 
49
- const wallet = new SVMChainWallet(chainConfig, testUserKeyPair, 0)
57
+ // const wallet = new SVMChainWallet(chainConfig, testUserKeyPair, 0)
58
+ const wallet = new EVMChainWallet(evmChainConfig, evmPrivateKey2, 0)
50
59
  // console.log('wallet: ', wallet);
51
60
 
52
- wallet.getNativeBalance().then(e => console.log('native balance: ', e))
61
+ // wallet.discoverToken().then(e => console.log('discovered tokens: ', e))
62
+ // wallet.getNativeBalance().then(e => console.log('native balance: ', e))
63
+ console.log('address: ', wallet.address);
53
64
  // const toBuy = new PublicKey("9BB6NFEcjBCtnNLFko2FqVQBq8HHM13kCyYcdQbgpump")
54
65
  // wallet.swap({
55
66
  // name: NATIVE_MINT.toBase58(),
@@ -63,3 +74,42 @@ wallet.getNativeBalance().then(e => console.log('native balance: ', e))
63
74
  // wal.getNativeBalance().then(e => console.log(e))
64
75
 
65
76
 
77
+
78
+
79
+
80
+
81
+ const RPC_URL = chainConfig.rpcUrl;
82
+ const connection = new Connection(RPC_URL);
83
+
84
+ /**
85
+ * Fetches and logs the token metadata for a given mint address.
86
+ * @param mintAddress - The mint address of the token.
87
+ */
88
+
89
+ // get transaction history
90
+ // getTransactionHistory(
91
+ // connection,
92
+ // wallet.address,
93
+ // {
94
+ // limit: 2,
95
+ // before: "5RKG5zKJdz9PqWSav1J358hm1GtfnV1QnYcrw3sRpY7aCgT7f4HTKnp4c9pXrJRujcHHisu3Z6jdtbzq5aTRbikq"
96
+ // }
97
+ // ).then((history: any) => {
98
+ // console.log("Transaction History:", history);
99
+ // }).catch((error: any) => {
100
+ // console.error("Error fetching transaction history:", error);
101
+ // });
102
+ const client = createPublicClient({
103
+ chain: base,
104
+ transport: http(base.rpcUrls.default.http[0]),
105
+ })
106
+
107
+ getEVMTransactionHistory(client as PublicClient, "0x9C82CE0e125F61AdE50BC0c19638F6Ba93d71D5e", {
108
+ startBlock: BigInt(37427020)
109
+ // before: "0xabc..."
110
+ }).then((history: any) => {
111
+ console.log("EVM Transaction History:", history);
112
+ }).catch((error: any) => {
113
+ console.error("Error fetching EVM transaction history:", error);
114
+ });
115
+
package/utils/types.ts CHANGED
@@ -3,7 +3,7 @@ import { EVMVM } from "./evm";
3
3
  import { SVMVM } from "./svm";
4
4
 
5
5
  export interface ChainWalletConfig {
6
- chainId: string | number;
6
+ chainId: number;
7
7
  name: string;
8
8
  rpcUrl: string;
9
9
  explorerUrl: string;
@@ -26,6 +26,11 @@ export interface TokenInfo {
26
26
  decimals: number;
27
27
  }
28
28
 
29
+ export interface UserTokenBalance<AddressType> extends TokenInfo {
30
+ balance: number;
31
+ owner: AddressType;
32
+ }
33
+
29
34
  export interface NFTInfo {
30
35
  tokenId: string;
31
36
  contractAddress: string;
@@ -73,7 +73,7 @@ if (typeof window !== 'undefined') {
73
73
  window.Buffer = Buffer; // Inject Buffer into the global scope
74
74
  }
75
75
 
76
- import { wordlist } from "@scure/bip39/wordlists/english";
76
+ import { wordlist } from "./english";
77
77
  import { hmac } from "@noble/hashes/hmac";
78
78
  import { sha512 } from "@noble/hashes/sha2";
79
79