@exodus/solana-lib 3.7.1 → 3.9.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/CHANGELOG.md CHANGED
@@ -3,6 +3,26 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [3.9.0](https://github.com/ExodusMovement/assets/compare/@exodus/solana-lib@3.8.0...@exodus/solana-lib@3.9.0) (2024-11-29)
7
+
8
+
9
+ ### Features
10
+
11
+
12
+ * feat: avoid checking a balance (#4597)
13
+
14
+
15
+
16
+ ## [3.8.0](https://github.com/ExodusMovement/assets/compare/@exodus/solana-lib@3.7.1...@exodus/solana-lib@3.8.0) (2024-11-29)
17
+
18
+
19
+ ### Features
20
+
21
+
22
+ * feat: support optional `checkBalances` parameter (#4595)
23
+
24
+
25
+
6
26
  ## [3.7.1](https://github.com/ExodusMovement/assets/compare/@exodus/solana-lib@3.7.0...@exodus/solana-lib@3.7.1) (2024-10-30)
7
27
 
8
28
  **Note:** Version bump only for package @exodus/solana-lib
package/README.md CHANGED
@@ -1,8 +1,3 @@
1
- # Solana lib · [![npm version](https://img.shields.io/badge/npm-private-blue.svg?style=flat)](https://www.npmjs.com/package/@exodus/solana-lib)
1
+ # @exodus/solana-lib
2
2
 
3
- Web wallet example by Solana: https://github.com/solana-labs/example-webwallet
4
-
5
- Official lib sdk: https://github.com/solana-labs/solana-web3.js
6
-
7
- - The smallest unit is called Lamports. 9 digits precision (a Lamport is 0.000000001 SOL)
8
- - In addition to fees, Solana has the "Rent" concept, paid every epoch by wallets. If you keep a certain amount of SOL locked (accountReserve) you'd have rent-exemption (https://docs.solana.com/apps/rent#rent-exemption), `0.01 SOL` is more than enough (https://github.com/solana-labs/solana/issues/12332)
3
+ Solana utils, such as for cryptography, address encoding/decoding, transaction building, etc. See [Asset Packages](../../docs/asset-packages.md) for more detail on this package's role.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@exodus/solana-lib",
3
- "version": "3.7.1",
4
- "description": "Exodus internal Solana low-level library",
3
+ "version": "3.9.0",
4
+ "description": "Solana utils, such as for cryptography, address encoding/decoding, transaction building, etc.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
7
7
  "files": [
@@ -23,7 +23,7 @@
23
23
  "@exodus/asset-lib": "^5.0.0",
24
24
  "@exodus/buffer-layout": "^1.2.0-exodus1",
25
25
  "@exodus/crypto": "^1.0.0-rc.16",
26
- "@exodus/currency": "^5.0.2",
26
+ "@exodus/currency": "^6.0.1",
27
27
  "@exodus/key-utils": "^3.7.0",
28
28
  "@exodus/solana-web3.js": "^1.63.1-exodus.9-rc4",
29
29
  "bn.js": "^5.2.1",
@@ -45,5 +45,5 @@
45
45
  "type": "git",
46
46
  "url": "git+https://github.com/ExodusMovement/assets.git"
47
47
  },
48
- "gitHead": "7dda4dfeb25b9a469ee8018f31341ac804c38ab7"
48
+ "gitHead": "a5ab26da60a57ffb982a7001843af9cf06f899f8"
49
49
  }
@@ -24,23 +24,26 @@ import {
24
24
  } from './vendor/index.js'
25
25
 
26
26
  class Tx {
27
- constructor({
28
- from,
29
- to,
30
- amount,
31
- recentBlockhash,
32
- fee, // (Fee per Signature: 5000 lamports)
33
- // Tokens related:
34
- // pass either name or mintAddress, if both, mintAddress has priority
35
- tokenMintAddress,
36
- destinationAddressType,
37
- isAssociatedTokenAccountActive, // true when recipient balance !== 0
38
- fromTokenAddresses, // sender token addresses
39
- instructions,
40
- feePayer,
41
- memo,
42
- reference,
43
- } = {}) {
27
+ constructor(
28
+ {
29
+ from,
30
+ to,
31
+ amount,
32
+ recentBlockhash,
33
+ fee, // (Fee per Signature: 5000 lamports)
34
+ // Tokens related:
35
+ // pass either name or mintAddress, if both, mintAddress has priority
36
+ tokenMintAddress,
37
+ destinationAddressType,
38
+ isAssociatedTokenAccountActive, // true when recipient balance !== 0
39
+ fromTokenAddresses, // sender token addresses
40
+ instructions,
41
+ feePayer,
42
+ memo,
43
+ reference,
44
+ } = {},
45
+ options = {}
46
+ ) {
44
47
  if (!instructions) {
45
48
  assert(from, 'from is required')
46
49
  assert(to, 'to is required')
@@ -78,7 +81,7 @@ class Tx {
78
81
 
79
82
  if (tokenMintAddress) {
80
83
  // TOKEN transfer tx
81
- this.buildTokenTransaction(this.txObj)
84
+ this.buildTokenTransaction(this.txObj, options)
82
85
  } else if (instructions) {
83
86
  this.buildInstructionsTransaction(this.txObj)
84
87
  } else {
@@ -138,18 +141,21 @@ class Tx {
138
141
  })
139
142
  }
140
143
 
141
- buildTokenTransaction({
142
- from,
143
- to,
144
- amount,
145
- recentBlockhash,
146
- tokenMintAddress,
147
- destinationAddressType,
148
- isAssociatedTokenAccountActive,
149
- fromTokenAddresses,
150
- feePayer,
151
- reference,
152
- }) {
144
+ buildTokenTransaction(
145
+ {
146
+ from,
147
+ to,
148
+ amount,
149
+ recentBlockhash,
150
+ tokenMintAddress,
151
+ destinationAddressType,
152
+ isAssociatedTokenAccountActive,
153
+ fromTokenAddresses,
154
+ feePayer,
155
+ reference,
156
+ },
157
+ { checkBalances = true } = {}
158
+ ) {
153
159
  this.transaction = new Transaction({
154
160
  recentBlockhash,
155
161
  feePayer: feePayer ? new PublicKey(feePayer) : undefined,
@@ -166,6 +172,7 @@ class Tx {
166
172
 
167
173
  let amountLeft = amount
168
174
  let amountToSend
175
+ let isNotEnoughBalance = false
169
176
  for (let {
170
177
  mintAddress,
171
178
  tokenAccountAddress,
@@ -176,15 +183,22 @@ class Tx {
176
183
  } of fromTokenAddresses) {
177
184
  // need to add more of this instruction until we reach the desired balance (amount) to send
178
185
  assert(mintAddress === tokenMintAddress, `Got unexpected mintAddress ${mintAddress}`)
179
- if (amountLeft === 0) break
180
186
 
181
- balance = Number(balance)
182
- if (balance >= amountLeft) {
183
- amountToSend = amountLeft
184
- amountLeft = 0
187
+ if (checkBalances) {
188
+ if (amountLeft === 0) break
189
+
190
+ balance = Number(balance)
191
+ if (balance >= amountLeft) {
192
+ amountToSend = amountLeft
193
+ amountLeft = 0
194
+ } else {
195
+ // Not enough balance case.
196
+ isNotEnoughBalance = true
197
+ amountToSend = balance
198
+ amountLeft -= amountToSend
199
+ }
185
200
  } else {
186
- amountToSend = balance // not enough balance
187
- amountLeft -= amountToSend
201
+ amountToSend = amountLeft
188
202
  }
189
203
 
190
204
  const dest = isSOLaddress
@@ -229,7 +243,7 @@ class Tx {
229
243
  this.transaction.add(tokenTransferInstruction)
230
244
  }
231
245
 
232
- assert(amountLeft === 0, `Not enough balance to send ${amount} ${tokenMintAddress}`)
246
+ assert(isNotEnoughBalance === false, `Not enough balance to send ${amount} ${tokenMintAddress}`)
233
247
  }
234
248
 
235
249
  static createStakeAccountTransaction({ address, amount, seed = SEED, pool, recentBlockhash }) {
@@ -27,9 +27,10 @@ const deserializeTransactionBytes = (wireTransactionBuffer) => {
27
27
  /**
28
28
  * Prepares the transaction to be signed (exodus & ledger).
29
29
  * @param {UnsignedTx} unsignedTx
30
+ * @param {checkBalances?: boolean} options
30
31
  * @returns a Solana Web3.js VersionedTransaction object (supports both Legacy & Versioned transactions)
31
32
  */
32
- export function prepareForSigning(unsignedTx) {
33
+ export function prepareForSigning(unsignedTx, { checkBalances = true } = {}) {
33
34
  const {
34
35
  amount: unitAmount,
35
36
  fee: feeAmount,
@@ -58,7 +59,7 @@ export function prepareForSigning(unsignedTx) {
58
59
 
59
60
  const txData = { ...unsignedTx.txData, address, amount, fee }
60
61
 
61
- const legacyTransaction = createTx({ txData, method, from })
62
+ const legacyTransaction = createTx({ txData, method, from }, { checkBalances })
62
63
  return VersionedTransaction.deserialize(
63
64
  legacyTransaction.serialize({ requireAllSignatures: false, verifySignatures: false })
64
65
  )
@@ -70,7 +71,7 @@ export function prepareForSigning(unsignedTx) {
70
71
  )
71
72
  }
72
73
 
73
- const createTx = ({ txData, method, from }) => {
74
+ const createTx = ({ txData, method, from }, options) => {
74
75
  let tx
75
76
  switch (method) {
76
77
  case 'delegate':
@@ -101,7 +102,7 @@ const createTx = ({ txData, method, from }) => {
101
102
  break
102
103
  default:
103
104
  // SOL and Token tx
104
- tx = createTokenTransaction(txData)
105
+ tx = createTokenTransaction(txData, options)
105
106
  break
106
107
  }
107
108
 
@@ -205,22 +206,8 @@ const createMagicEdenExchangeTransaction = ({
205
206
  takerAddress,
206
207
  })
207
208
 
208
- const createTokenTransaction = ({
209
- amount,
210
- destinationAddressType,
211
- fee,
212
- feePayer,
213
- from,
214
- fromTokenAddresses,
215
- instructions,
216
- isAssociatedTokenAccountActive,
217
- recentBlockhash,
218
- to,
219
- tokenMintAddress,
220
- memo,
221
- reference,
222
- }) =>
223
- new Transaction({
209
+ const createTokenTransaction = (
210
+ {
224
211
  amount,
225
212
  destinationAddressType,
226
213
  fee,
@@ -234,7 +221,27 @@ const createTokenTransaction = ({
234
221
  tokenMintAddress,
235
222
  memo,
236
223
  reference,
237
- }).transaction
224
+ },
225
+ options
226
+ ) =>
227
+ new Transaction(
228
+ {
229
+ amount,
230
+ destinationAddressType,
231
+ fee,
232
+ feePayer,
233
+ from,
234
+ fromTokenAddresses,
235
+ instructions,
236
+ isAssociatedTokenAccountActive,
237
+ recentBlockhash,
238
+ to,
239
+ tokenMintAddress,
240
+ memo,
241
+ reference,
242
+ },
243
+ options
244
+ ).transaction
238
245
 
239
246
  const createCloseAccountTransaction = ({ account, programId, recentBlockhash, walletPublicKey }) =>
240
247
  Transaction.createCloseAccount({