@exodus/solana-lib 3.7.1 → 3.8.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 +10 -0
- package/README.md +2 -7
- package/package.json +4 -4
- package/src/transaction.js +46 -32
- package/src/tx/prepare-for-signing.js +28 -21
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
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.8.0](https://github.com/ExodusMovement/assets/compare/@exodus/solana-lib@3.7.1...@exodus/solana-lib@3.8.0) (2024-11-29)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
* feat: support optional `checkBalances` parameter (#4595)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
6
16
|
## [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
17
|
|
|
8
18
|
**Note:** Version bump only for package @exodus/solana-lib
|
package/README.md
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @exodus/solana-lib
|
|
2
2
|
|
|
3
|
-
|
|
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.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.8.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": "^
|
|
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": "
|
|
48
|
+
"gitHead": "a12da3edd237eb63aa06b726374094d63f29612a"
|
|
49
49
|
}
|
package/src/transaction.js
CHANGED
|
@@ -24,23 +24,26 @@ import {
|
|
|
24
24
|
} from './vendor/index.js'
|
|
25
25
|
|
|
26
26
|
class Tx {
|
|
27
|
-
constructor(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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,
|
|
@@ -183,7 +190,9 @@ class Tx {
|
|
|
183
190
|
amountToSend = amountLeft
|
|
184
191
|
amountLeft = 0
|
|
185
192
|
} else {
|
|
186
|
-
|
|
193
|
+
// Not enough balance case.
|
|
194
|
+
isNotEnoughBalance = true
|
|
195
|
+
amountToSend = balance
|
|
187
196
|
amountLeft -= amountToSend
|
|
188
197
|
}
|
|
189
198
|
|
|
@@ -229,7 +238,12 @@ class Tx {
|
|
|
229
238
|
this.transaction.add(tokenTransferInstruction)
|
|
230
239
|
}
|
|
231
240
|
|
|
232
|
-
|
|
241
|
+
if (checkBalances) {
|
|
242
|
+
assert(
|
|
243
|
+
isNotEnoughBalance === false,
|
|
244
|
+
`Not enough balance to send ${amount} ${tokenMintAddress}`
|
|
245
|
+
)
|
|
246
|
+
}
|
|
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
|
-
|
|
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
|
-
}
|
|
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({
|