@debridge-finance/solana-utils 5.5.0 → 6.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.
package/LICENSE CHANGED
@@ -1,100 +1,100 @@
1
- Business Source License 1.1
2
-
3
- License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
4
- "Business Source License" is a trademark of MariaDB Corporation Ab.
5
-
6
- -----------------------------------------------------------------------------
7
-
8
- Parameters
9
-
10
- Licensor: DxTech Inc. (Company No.155728732)
11
-
12
- Licensed Work: deBridge V1
13
- The Licensed Work is (c) 2021 DxTech Inc.
14
-
15
- Additional Use Grant: Any uses listed and defined at
16
- v1-license-grants.dbr.eth
17
-
18
- Change Date: The earlier of 31 December 2025 or a date specified at
19
- v1-license-date.dbr.eth
20
-
21
- Change License: GNU General Public License v2.0 or later
22
-
23
- -----------------------------------------------------------------------------
24
-
25
- Terms
26
-
27
- The Licensor hereby grants you the right to copy, modify, create derivative
28
- works, redistribute, and make non-production use of the Licensed Work. The
29
- Licensor may make an Additional Use Grant, above, permitting limited
30
- production use.
31
-
32
- Effective on the Change Date, or the fourth anniversary of the first publicly
33
- available distribution of a specific version of the Licensed Work under this
34
- License, whichever comes first, the Licensor hereby grants you rights under
35
- the terms of the Change License, and the rights granted in the paragraph
36
- above terminate.
37
-
38
- If your use of the Licensed Work does not comply with the requirements
39
- currently in effect as described in this License, you must purchase a
40
- commercial license from the Licensor, its affiliated entities, or authorized
41
- resellers, or you must refrain from using the Licensed Work.
42
-
43
- All copies of the original and modified Licensed Work, and derivative works
44
- of the Licensed Work, are subject to this License. This License applies
45
- separately for each version of the Licensed Work and the Change Date may vary
46
- for each version of the Licensed Work released by Licensor.
47
-
48
- You must conspicuously display this License on each original or modified copy
49
- of the Licensed Work. If you receive the Licensed Work in original or
50
- modified form from a third party, the terms and conditions set forth in this
51
- License apply to your use of that work.
52
-
53
- Any use of the Licensed Work in violation of this License will automatically
54
- terminate your rights under this License for the current and all other
55
- versions of the Licensed Work.
56
-
57
- This License does not grant you any right in any trademark or logo of
58
- Licensor or its affiliates (provided that you may use a trademark or logo of
59
- Licensor as expressly required by this License).
60
-
61
- TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
62
- AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
63
- EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
64
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
65
- TITLE.
66
-
67
- MariaDB hereby grants you permission to use this License’s text to license
68
- your works, and to refer to it using the trademark "Business Source License",
69
- as long as you comply with the Covenants of Licensor below.
70
-
71
- -----------------------------------------------------------------------------
72
-
73
- Covenants of Licensor
74
-
75
- In consideration of the right to use this License’s text and the "Business
76
- Source License" name and trademark, Licensor covenants to MariaDB, and to all
77
- other recipients of the licensed work to be provided by Licensor:
78
-
79
- 1. To specify as the Change License the GPL Version 2.0 or any later version,
80
- or a license that is compatible with GPL Version 2.0 or a later version,
81
- where "compatible" means that software provided under the Change License can
82
- be included in a program with software provided under GPL Version 2.0 or a
83
- later version. Licensor may specify additional Change Licenses without
84
- limitation.
85
-
86
- 2. To either: (a) specify an additional grant of rights to use that does not
87
- impose any additional restriction on the right granted in this License, as
88
- the Additional Use Grant; or (b) insert the text "None".
89
-
90
- 3. To specify a Change Date.
91
-
92
- 4. Not to modify this License in any other way.
93
-
94
- -----------------------------------------------------------------------------
95
-
96
- Notice
97
-
98
- The Business Source License (this document, or the "License") is not an Open
99
- Source license. However, the Licensed Work will eventually be made available
100
- under an Open Source License, as stated in this License.
1
+ Business Source License 1.1
2
+
3
+ License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
4
+ "Business Source License" is a trademark of MariaDB Corporation Ab.
5
+
6
+ -----------------------------------------------------------------------------
7
+
8
+ Parameters
9
+
10
+ Licensor: DxTech Inc. (Company No.155728732)
11
+
12
+ Licensed Work: deBridge V1
13
+ The Licensed Work is (c) 2021 DxTech Inc.
14
+
15
+ Additional Use Grant: Any uses listed and defined at
16
+ v1-license-grants.dbr.eth
17
+
18
+ Change Date: The earlier of 31 December 2025 or a date specified at
19
+ v1-license-date.dbr.eth
20
+
21
+ Change License: GNU General Public License v2.0 or later
22
+
23
+ -----------------------------------------------------------------------------
24
+
25
+ Terms
26
+
27
+ The Licensor hereby grants you the right to copy, modify, create derivative
28
+ works, redistribute, and make non-production use of the Licensed Work. The
29
+ Licensor may make an Additional Use Grant, above, permitting limited
30
+ production use.
31
+
32
+ Effective on the Change Date, or the fourth anniversary of the first publicly
33
+ available distribution of a specific version of the Licensed Work under this
34
+ License, whichever comes first, the Licensor hereby grants you rights under
35
+ the terms of the Change License, and the rights granted in the paragraph
36
+ above terminate.
37
+
38
+ If your use of the Licensed Work does not comply with the requirements
39
+ currently in effect as described in this License, you must purchase a
40
+ commercial license from the Licensor, its affiliated entities, or authorized
41
+ resellers, or you must refrain from using the Licensed Work.
42
+
43
+ All copies of the original and modified Licensed Work, and derivative works
44
+ of the Licensed Work, are subject to this License. This License applies
45
+ separately for each version of the Licensed Work and the Change Date may vary
46
+ for each version of the Licensed Work released by Licensor.
47
+
48
+ You must conspicuously display this License on each original or modified copy
49
+ of the Licensed Work. If you receive the Licensed Work in original or
50
+ modified form from a third party, the terms and conditions set forth in this
51
+ License apply to your use of that work.
52
+
53
+ Any use of the Licensed Work in violation of this License will automatically
54
+ terminate your rights under this License for the current and all other
55
+ versions of the Licensed Work.
56
+
57
+ This License does not grant you any right in any trademark or logo of
58
+ Licensor or its affiliates (provided that you may use a trademark or logo of
59
+ Licensor as expressly required by this License).
60
+
61
+ TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
62
+ AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
63
+ EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
64
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
65
+ TITLE.
66
+
67
+ MariaDB hereby grants you permission to use this License’s text to license
68
+ your works, and to refer to it using the trademark "Business Source License",
69
+ as long as you comply with the Covenants of Licensor below.
70
+
71
+ -----------------------------------------------------------------------------
72
+
73
+ Covenants of Licensor
74
+
75
+ In consideration of the right to use this License’s text and the "Business
76
+ Source License" name and trademark, Licensor covenants to MariaDB, and to all
77
+ other recipients of the licensed work to be provided by Licensor:
78
+
79
+ 1. To specify as the Change License the GPL Version 2.0 or any later version,
80
+ or a license that is compatible with GPL Version 2.0 or a later version,
81
+ where "compatible" means that software provided under the Change License can
82
+ be included in a program with software provided under GPL Version 2.0 or a
83
+ later version. Licensor may specify additional Change Licenses without
84
+ limitation.
85
+
86
+ 2. To either: (a) specify an additional grant of rights to use that does not
87
+ impose any additional restriction on the right granted in this License, as
88
+ the Additional Use Grant; or (b) insert the text "None".
89
+
90
+ 3. To specify a Change Date.
91
+
92
+ 4. Not to modify this License in any other way.
93
+
94
+ -----------------------------------------------------------------------------
95
+
96
+ Notice
97
+
98
+ The Business Source License (this document, or the "License") is not an Open
99
+ Source license. However, the Licensed Work will eventually be made available
100
+ under an Open Source License, as stated in this License.
package/README.md CHANGED
@@ -1,3 +1,3 @@
1
- # `solana-utils` by deBridge
2
-
3
- Common utils package to power communication with Solana contracts at [deBridge](https://debridge.finance).
1
+ # `solana-utils` by deBridge
2
+
3
+ Common utils package to power communication with Solana contracts at [deBridge](https://debridge.finance).
@@ -7437,13 +7437,7 @@ const TOKEN_METADATA_PROGRAM_ID = new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzj
7437
7437
  const TOKEN_PROGRAM_ID$1 = new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
7438
7438
  const ASSOCIATED_TOKEN_PROGRAM_ID = new PublicKey("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
7439
7439
  const WRAPPED_SOL_MINT = new PublicKey("So11111111111111111111111111111111111111112");
7440
- function findAssociatedTokenAddress(wallet, tokenMint, tokenProgramId = TOKEN_PROGRAM_ID$1, associatedTokenProramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
7441
- return PublicKey.findProgramAddressSync([wallet.toBytes(), tokenProgramId.toBytes(), tokenMint.toBytes()], associatedTokenProramId);
7442
- }
7443
- /**
7444
- * @deprecated same as findAssociatedTokenAddress
7445
- */
7446
- function findAssociatedTokenAddressSync(wallet, tokenMint, tokenProgramId = TOKEN_PROGRAM_ID$1, associatedTokenProramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
7440
+ function findAssociatedTokenAddress(wallet, tokenMint, tokenProgramId, associatedTokenProramId = ASSOCIATED_TOKEN_PROGRAM_ID) {
7447
7441
  return PublicKey.findProgramAddressSync([wallet.toBytes(), tokenProgramId.toBytes(), tokenMint.toBytes()], associatedTokenProramId);
7448
7442
  }
7449
7443
  function getTokenMetadataAddress(tokenMint, tokenMetadataProgramId) {
@@ -11523,15 +11517,15 @@ function parseSplAccount(dataOrInfo) {
11523
11517
  return AccountLayout.decode(data);
11524
11518
  }
11525
11519
  catch (e) {
11526
- console.error(e);
11520
+ //console.error(e);
11527
11521
  }
11528
11522
  return null;
11529
11523
  }
11530
- function isWalletCorrectATA(walletAddress, data) {
11524
+ function isWalletCorrectATA(walletAddress, data, ownerProgram) {
11531
11525
  const decoded = parseSplAccount(data);
11532
11526
  if (!decoded)
11533
11527
  return false;
11534
- const [realAta] = findAssociatedTokenAddress(decoded.owner, decoded.mint);
11528
+ const [realAta] = findAssociatedTokenAddress(decoded.owner, decoded.mint, ownerProgram);
11535
11529
  return realAta.equals(new PublicKey(walletAddress));
11536
11530
  }
11537
11531
  /**
@@ -11546,7 +11540,7 @@ function getAccountWithType(connection, account) {
11546
11540
  if (!accountData)
11547
11541
  return [null, AccountType.NotExists];
11548
11542
  if (accountData.owner.equals(TOKEN_PROGRAM_ID$1)) {
11549
- return isWalletCorrectATA(account, accountData.data)
11543
+ return isWalletCorrectATA(account, accountData.data, accountData.owner)
11550
11544
  ? [accountData, AccountType.CorrectATA]
11551
11545
  : [accountData, AccountType.Token];
11552
11546
  }
@@ -11556,18 +11550,7 @@ function getAccountWithType(connection, account) {
11556
11550
  });
11557
11551
  }
11558
11552
  /**
11559
- * Returns balance of specified wallet in native tokens
11560
- * @param wallet wallet to inspect
11561
- */
11562
- function getNativeWalletBalance(connection, wallet) {
11563
- return __awaiter(this, void 0, void 0, function* () {
11564
- wallet = new PublicKey(wallet);
11565
- const result = yield connection.getBalance(wallet);
11566
- return new BN(result.toString());
11567
- });
11568
- }
11569
- /**
11570
- * Returns balance of associated wallet in SPL-tokens
11553
+ * Returns balance of associated wallet in SPL-tokens (finds correct ata (token/token22))
11571
11554
  * @param originalWallet native tokens wallet address
11572
11555
  * @param tokenMint mint of SPL-Token
11573
11556
  */
@@ -11575,20 +11558,80 @@ function getAssocSPLWalletBalance(connection, originalWallet, tokenMint) {
11575
11558
  return __awaiter(this, void 0, void 0, function* () {
11576
11559
  originalWallet = new PublicKey(originalWallet);
11577
11560
  tokenMint = new PublicKey(tokenMint);
11578
- const [wallet] = findAssociatedTokenAddress(originalWallet, tokenMint);
11579
- const result = yield connection.getTokenAccountBalance(wallet, "finalized");
11580
- return new BN(result.value.amount);
11561
+ const [wallet] = findAssociatedTokenAddress(originalWallet, tokenMint, TOKEN_PROGRAM_ID$1);
11562
+ const [wallet22] = findAssociatedTokenAddress(originalWallet, tokenMint, TOKEN_2022_PROGRAM_ID);
11563
+ const [tokenMintInfo, walletInfo, wallet22Info] = yield connection.getMultipleAccountsInfo([tokenMint, wallet, wallet22]);
11564
+ if (!tokenMintInfo) {
11565
+ throw new Error("Mint doesn't exist");
11566
+ }
11567
+ const owner = tokenMintInfo.owner.toBase58();
11568
+ switch (owner) {
11569
+ case TOKEN_PROGRAM_ID$1.toBase58(): {
11570
+ if (walletInfo == null || walletInfo == undefined)
11571
+ return null;
11572
+ const spl = parseSplAccount(walletInfo);
11573
+ return {
11574
+ balance: walletInfo.lamports,
11575
+ spl: spl
11576
+ ? {
11577
+ balance: spl.amount,
11578
+ isToken22: false,
11579
+ mint: spl.mint,
11580
+ owner: spl.owner,
11581
+ }
11582
+ : undefined,
11583
+ };
11584
+ }
11585
+ case TOKEN_2022_PROGRAM_ID.toBase58(): {
11586
+ if (wallet22Info == null || wallet22Info == undefined)
11587
+ return null;
11588
+ const spl = parseSplAccount(wallet22Info);
11589
+ return {
11590
+ balance: wallet22Info.lamports,
11591
+ spl: spl
11592
+ ? {
11593
+ balance: spl.amount,
11594
+ isToken22: false,
11595
+ mint: spl.mint,
11596
+ owner: spl.owner,
11597
+ }
11598
+ : undefined,
11599
+ };
11600
+ }
11601
+ default: {
11602
+ throw new Error(`Unexpected tokenMint owner: ${owner}`);
11603
+ }
11604
+ }
11581
11605
  });
11582
11606
  }
11583
11607
  /**
11584
- * Returns balance of specified wallet in SPL-tokens
11608
+ * Returns balance of specified wallets. null if account doesn't exist
11585
11609
  * @param wallet spl-tokens wallet address
11586
11610
  */
11587
- function getSPLWalletBalance(connection, wallet) {
11611
+ function getWalletsBalances(connection, wallets) {
11588
11612
  return __awaiter(this, void 0, void 0, function* () {
11589
- wallet = new PublicKey(wallet);
11590
- const result = yield connection.getTokenAccountBalance(wallet, "finalized");
11591
- return new BN(result.value.amount);
11613
+ const infos = yield connection.getMultipleAccountsInfo(wallets.map((w) => new PublicKey(w)), "finalized");
11614
+ const results = [];
11615
+ for (const info of infos) {
11616
+ if (!info || info.lamports === 0) {
11617
+ results.push(null);
11618
+ continue;
11619
+ }
11620
+ const parsed = parseSplAccount(info.data);
11621
+ const result = {
11622
+ balance: info.lamports,
11623
+ };
11624
+ if (parsed) {
11625
+ result.spl = {
11626
+ balance: parsed.amount,
11627
+ isToken22: info.owner.toBase58() === TOKEN_2022_PROGRAM_ID.toBase58(),
11628
+ mint: parsed.mint,
11629
+ owner: parsed.owner,
11630
+ };
11631
+ }
11632
+ results.push(result);
11633
+ }
11634
+ return results;
11592
11635
  });
11593
11636
  }
11594
11637
  /**
@@ -11691,11 +11734,11 @@ function updateWsolBalance(connection, amount, owner) {
11691
11734
  return __awaiter(this, void 0, void 0, function* () {
11692
11735
  owner = new PublicKey(owner);
11693
11736
  const tx = new Transaction();
11694
- const [wSolATA] = findAssociatedTokenAddress(owner, WRAPPED_SOL_MINT);
11737
+ const [wSolATA] = findAssociatedTokenAddress(owner, WRAPPED_SOL_MINT, TOKEN_PROGRAM_ID$1);
11695
11738
  const existingATA = yield getAccountInfo(connection, wSolATA);
11696
11739
  let amountToWrap = new BN(amount);
11697
11740
  if (existingATA === null) {
11698
- tx.add(createAssociatedWalletInstruction(WRAPPED_SOL_MINT, wSolATA, owner, owner));
11741
+ tx.add(createAssociatedWalletInstruction(WRAPPED_SOL_MINT, wSolATA, owner, owner, false));
11699
11742
  }
11700
11743
  else {
11701
11744
  const parsed = parseSplAccount(existingATA.data);
@@ -11711,19 +11754,6 @@ function updateWsolBalance(connection, amount, owner) {
11711
11754
  return tx;
11712
11755
  });
11713
11756
  }
11714
- /**
11715
- * Checks existance of associated wallet for specified token
11716
- * @param mintAccount mint account of SPL-token
11717
- * @param originalWallet account of solana wallet
11718
- * @returns true if account exists
11719
- */
11720
- function checkIfAssociatedWalletExists(connection, mintAccount, originalWallet) {
11721
- return __awaiter(this, void 0, void 0, function* () {
11722
- const [walletAccount] = findAssociatedTokenAddress(originalWallet, mintAccount);
11723
- const accountData = yield getAccountInfo(connection, walletAccount);
11724
- return accountData !== null;
11725
- });
11726
- }
11727
11757
  function getTransactionAccountsForSimulationDiff(connection, tx) {
11728
11758
  return __awaiter(this, void 0, void 0, function* () {
11729
11759
  var _a, _b;
@@ -11766,12 +11796,13 @@ function getTransactionAccountsForSimulationDiff(connection, tx) {
11766
11796
  }
11767
11797
  function parseMultipleAccounts(accounts) {
11768
11798
  return accounts.map((account) => {
11769
- var _a, _b;
11770
11799
  let parsedSplAccount;
11800
+ let isToken22 = false;
11771
11801
  if (account) {
11772
- const owner = new PublicKey(account.owner);
11773
- if (owner.equals(TOKEN_PROGRAM_ID$1) ||
11774
- owner.equals(TOKEN_2022_PROGRAM_ID)) {
11802
+ const owner = new PublicKey(account.owner).toBase58();
11803
+ isToken22 = owner == TOKEN_2022_PROGRAM_ID.toBase58();
11804
+ // pubkey.equals might not work
11805
+ if (isToken22 || TOKEN_PROGRAM_ID$1.toBase58() == owner) {
11775
11806
  if (account.data instanceof buffer.Buffer) {
11776
11807
  parsedSplAccount = parseSplAccount(account.data);
11777
11808
  }
@@ -11780,14 +11811,18 @@ function parseMultipleAccounts(accounts) {
11780
11811
  }
11781
11812
  }
11782
11813
  }
11814
+ else {
11815
+ return null;
11816
+ }
11783
11817
  const parsedAccount = {
11784
- balance: (_a = account === null || account === void 0 ? void 0 : account.lamports) !== null && _a !== void 0 ? _a : 0,
11818
+ balance: account.lamports,
11785
11819
  };
11786
11820
  if (parsedSplAccount) {
11787
11821
  parsedAccount.spl = {
11788
11822
  mint: parsedSplAccount.mint,
11789
11823
  owner: parsedSplAccount.owner,
11790
- balance: (_b = parsedSplAccount.amount) !== null && _b !== void 0 ? _b : BigInt(0),
11824
+ balance: parsedSplAccount.amount,
11825
+ isToken22,
11791
11826
  };
11792
11827
  }
11793
11828
  return parsedAccount;
@@ -11821,7 +11856,7 @@ function getTransactionDiff(connection, tx, txAccounts) {
11821
11856
  },
11822
11857
  }),
11823
11858
  ]);
11824
- if (simulatedTx.value.err !== null)
11859
+ if (simulatedTx.value.err !== null || !simulatedTx.value.accounts)
11825
11860
  throw new Error(JSON.stringify({
11826
11861
  err: simulatedTx.value.err,
11827
11862
  logs: simulatedTx.value.logs,
@@ -11863,16 +11898,13 @@ var spl = /*#__PURE__*/Object.freeze({
11863
11898
  getTokenInfo: getTokenInfo,
11864
11899
  createAssociatedWalletInstruction: createAssociatedWalletInstruction,
11865
11900
  parseSplAccount: parseSplAccount,
11866
- isWalletCorrectATA: isWalletCorrectATA,
11867
11901
  getAccountWithType: getAccountWithType,
11868
- getNativeWalletBalance: getNativeWalletBalance,
11869
11902
  getAssocSPLWalletBalance: getAssocSPLWalletBalance,
11870
- getSPLWalletBalance: getSPLWalletBalance,
11903
+ getWalletsBalances: getWalletsBalances,
11871
11904
  getAllTokenAccountsWithBalances: getAllTokenAccountsWithBalances,
11872
11905
  getTokenAccountsWithBalance: getTokenAccountsWithBalance,
11873
11906
  buildReplenishWsolBalanceTransaction: buildReplenishWsolBalanceTransaction,
11874
11907
  updateWsolBalance: updateWsolBalance,
11875
- checkIfAssociatedWalletExists: checkIfAssociatedWalletExists,
11876
11908
  getTransactionAccountsForSimulationDiff: getTransactionAccountsForSimulationDiff,
11877
11909
  parseMultipleAccounts: parseMultipleAccounts,
11878
11910
  getSplDiff: getSplDiff,
@@ -29287,5 +29319,5 @@ var transaction = /*#__PURE__*/Object.freeze({
29287
29319
  getErrorData: getErrorData
29288
29320
  });
29289
29321
 
29290
- export { ASSOCIATED_TOKEN_PROGRAM_ID, DeBridgeResolver, TOKEN_2022_PROGRAM_ID, TOKEN_METADATA_PROGRAM_ID, TOKEN_PROGRAM_ID$1 as TOKEN_PROGRAM_ID, WRAPPED_SOL_MINT, constants$1 as constants, crypto$1 as crypto, dlnDstResolver, dlnSrcResolver, findAssociatedTokenAddress, findAssociatedTokenAddressSync, getTokenMetadataAddress, helpers, interfaces, programs, spl, transaction as txs };
29322
+ export { ASSOCIATED_TOKEN_PROGRAM_ID, DeBridgeResolver, TOKEN_2022_PROGRAM_ID, TOKEN_METADATA_PROGRAM_ID, TOKEN_PROGRAM_ID$1 as TOKEN_PROGRAM_ID, WRAPPED_SOL_MINT, constants$1 as constants, crypto$1 as crypto, dlnDstResolver, dlnSrcResolver, findAssociatedTokenAddress, getTokenMetadataAddress, helpers, interfaces, programs, spl, transaction as txs };
29291
29323
  //# sourceMappingURL=index.js.map