@exodus/solana-lib 3.9.4 → 3.10.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.10.0](https://github.com/ExodusMovement/assets/compare/@exodus/solana-lib@3.9.5...@exodus/solana-lib@3.10.0) (2025-04-03)
7
+
8
+
9
+ ### Features
10
+
11
+
12
+ * feat(solana): add optional addExternalFeePayerToTransaction (#5345)
13
+
14
+
15
+
16
+ ## [3.9.5](https://github.com/ExodusMovement/assets/compare/@exodus/solana-lib@3.9.4...@exodus/solana-lib@3.9.5) (2025-03-25)
17
+
18
+
19
+ ### License
20
+
21
+
22
+ * license: re-license under MIT license (#4814)
23
+
24
+
25
+
6
26
  ## [3.9.4](https://github.com/ExodusMovement/assets/compare/@exodus/solana-lib@3.9.3...@exodus/solana-lib@3.9.4) (2025-01-02)
7
27
 
8
28
 
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2024 Exodus Movement, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,21 @@
1
- # @exodus/solana-lib
1
+ # @exodus/solana-lib · [![npm version](https://img.shields.io/badge/npm-public-blue.svg?style=flat)](https://www.npmjs.com/package/@exodus/solana-lib)
2
2
 
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.
3
+ **solana-lib** is a comprehensive library designed for seamless interaction with the Solana blockchain. It provides a robust set of functions and classes to facilitate core blockchain operations.
4
+
5
+ This library encompasses all the essential tools required for cryptographic operations, including key derivation and signing. It also supports transaction serialization and deserialization, data encoding and decoding, and the crafting of various types of transactions such as regular transactions, token transactions, and staking transactions.
6
+
7
+ ---
8
+
9
+ ## Installation
10
+
11
+ Install the package via `yarn`:
12
+
13
+ ```bash
14
+ yarn add @exodus/solana-lib
15
+ ```
16
+
17
+ ## License
18
+
19
+ This project is licensed under the MIT License.
20
+ You are free to use, modify, and distribute this software under the terms of the MIT License.
21
+ For more details, see the [LICENSE](LICENSE) file.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/solana-lib",
3
- "version": "3.9.4",
3
+ "version": "3.10.0",
4
4
  "description": "Solana utils, such as for cryptography, address encoding/decoding, transaction building, etc.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -10,7 +10,7 @@
10
10
  "!src/**/__tests__"
11
11
  ],
12
12
  "author": "Exodus Movement, Inc.",
13
- "license": "ISC",
13
+ "license": "MIT",
14
14
  "publishConfig": {
15
15
  "access": "public"
16
16
  },
@@ -29,6 +29,7 @@
29
29
  "bn.js": "^5.2.1",
30
30
  "borsh": "^0.7.0",
31
31
  "bs58": "^4.0.1",
32
+ "lodash": "^4.17.11",
32
33
  "minimalistic-assert": "^1.0.1"
33
34
  },
34
35
  "devDependencies": {
@@ -45,5 +46,5 @@
45
46
  "type": "git",
46
47
  "url": "git+https://github.com/ExodusMovement/assets.git"
47
48
  },
48
- "gitHead": "8a6a0d1a70d14e86a9cf4d47b8884939f10be724"
49
+ "gitHead": "59c4d4a4a7404eb915e4fa722a2afe01d1556046"
49
50
  }
package/src/constants.js CHANGED
@@ -9,6 +9,10 @@ export const STAKE_PROGRAM_ID = StakeProgram.programId
9
9
 
10
10
  export const TOKEN_PROGRAM_ID = new PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA')
11
11
 
12
+ export const COMPUTE_BUDGET_PROGRAM_ID = new PublicKey(
13
+ 'ComputeBudget111111111111111111111111111111'
14
+ )
15
+
12
16
  export const TOKEN_2022_PROGRAM_ID = new PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb')
13
17
 
14
18
  export const MEMO_PROGRAM_ID = new PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr')
@@ -0,0 +1,44 @@
1
+ # **Computing Fees and Priority Fees in Solana**
2
+
3
+ In Solana, **fees** and **priority fees** are influenced by network conditions, making them **not fully deterministic** but still **predictable to some extent**.
4
+
5
+ ---
6
+
7
+ ## **1. Computing Base Transaction Fees**
8
+
9
+ Solana uses a **fixed fee structure** based on transaction size. The base fee is computed as:
10
+
11
+ base_fee = num_signatures \* signature_fee
12
+
13
+ - **Signature Fee**: A fixed amount per signature (historically 5000 lamports per signature, but subject to change).
14
+ - **Num Signatures**: Typically 1 for standard transactions, more if using multi-signature accounts.
15
+
16
+ Thus, if network parameters remain unchanged, you can **deterministically** compute the **base fee**.
17
+
18
+ ---
19
+
20
+ ## **2. Computing Priority Fee (Not Deterministic)**
21
+
22
+ The **priority fee** is **market-driven**, meaning it depends on **current network congestion**. Users specify the **compute unit price** in lamports per **compute unit**. The total priority fee is then:
23
+
24
+ priority_fee = compute_units_used \* compute_unit_price
25
+
26
+ - **Compute Units Used**: Can be estimated based on the transaction complexity but varies based on program execution.
27
+ - **Compute Unit Price**: Set by the user, competes with others in a bidding system.
28
+
29
+ ### **Why is Priority Fee Not Deterministic?**
30
+
31
+ - The **compute units used** may vary due to transaction execution conditions.
32
+ - The **compute unit price** is market-driven and changes based on network demand.
33
+
34
+ Thus, **you can estimate priority fees, but they are not deterministic**.
35
+
36
+ ---
37
+
38
+ ## **3. Can You Precompute Fees?**
39
+
40
+ | Fee Type | Deterministic? | Notes |
41
+ | ---------------- | -------------- | ------------------------------------------------ |
42
+ | **Base Fee** | ✅ Yes | Fixed per signature and transaction size. |
43
+ | **Priority Fee** | ❌ No | Market-driven and depends on network congestion. |
44
+ | **Total Fees** | ⚠️ Estimated | Can be predicted but not guaranteed. |
@@ -4,9 +4,10 @@ export const createFeeData = ({ asset }) =>
4
4
  new FeeData({
5
5
  config: {
6
6
  fee: `0.000005 ${asset.ticker}`,
7
+ baseFee: `0.000005 ${asset.ticker}`,
7
8
  priorityFee: 1_000_000,
8
9
  fuelThreshold: `0.000015 ${asset.ticker}`,
9
- computeUnitsMultiplier: 4, // compute units buffer
10
+ computeUnitsMultiplier: 1, // compute units buffer, increasing to > 1 can cause Dust issues.
10
11
  },
11
12
  mainKey: 'fee',
12
13
  currency: asset.currency,
@@ -63,7 +63,6 @@ function createIx(
63
63
  export const createTokenTransferInstruction = (owner, fromTokenAddress, to, amount) => {
64
64
  const sourcePubkey = new PublicKey(fromTokenAddress) // the token ADDRESS needed!
65
65
  const destinationPubkey = new PublicKey(to)
66
- console.log(`destination token address: ${destinationPubkey.toBase58()}`)
67
66
  const ownerAccountOrWalletPublicKey = new PublicKey(owner) // the only native SOL address
68
67
 
69
68
  return Token.createTransferInstruction(
package/src/tx/common.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { VersionedTransaction } from '@exodus/solana-web3.js'
1
2
  import base58 from 'bs58'
2
3
 
3
4
  export function isVersionedTransaction(tx) {
@@ -12,6 +13,10 @@ export function transactionToBase58(tx) {
12
13
  return base58.encode(tx.serialize())
13
14
  }
14
15
 
16
+ export function deserializeTransaction(tx) {
17
+ return VersionedTransaction.deserialize(tx)
18
+ }
19
+
15
20
  export function getTxId(tx) {
16
21
  const signature = getFirstSignature(tx)
17
22
  if (signature === null) {
@@ -6,6 +6,7 @@ export function createUnsignedTx({
6
6
  fee,
7
7
  feeData,
8
8
  recentBlockhash,
9
+ useFeePayer,
9
10
  // Tokens related:
10
11
  tokenMintAddress,
11
12
  destinationAddressType,
@@ -73,6 +74,7 @@ export function createUnsignedTx({
73
74
  },
74
75
  txMeta: {
75
76
  assetName: asset.name,
77
+ useFeePayer,
76
78
  },
77
79
  }
78
80
  }
package/src/tx/index.js CHANGED
@@ -7,4 +7,6 @@ export * from './decode-tx-instructions.js'
7
7
  export * from './build-raw-transaction.js'
8
8
  export * from './sign-hardware.js'
9
9
  export * from './prepare-for-signing.js'
10
- export { transactionToBase58 } from './common.js'
10
+ export * from './verify-only-fee-payer-changed.js'
11
+ export * from './parse-tx-buffer.js'
12
+ export { transactionToBase58, deserializeTransaction } from './common.js'
@@ -0,0 +1,136 @@
1
+ import BN from 'bn.js'
2
+ import bs58 from 'bs58'
3
+
4
+ import { COMPUTE_BUDGET_PROGRAM_ID, SYSTEM_PROGRAM_ID, TOKEN_PROGRAM_ID } from '../constants.js'
5
+ import { deserializeTransaction } from './common.js'
6
+
7
+ function decodeSPLTransferData(base58Data) {
8
+ const buffer = bs58.decode(base58Data)
9
+
10
+ // 1 byte
11
+ if (buffer[0] !== 3) {
12
+ throw new Error(`Unsupported instruction type: ${buffer[0]}`)
13
+ }
14
+
15
+ const amountBytes = buffer.slice(1, 9)
16
+ const amountBN = new BN(amountBytes, 'le')
17
+
18
+ return { amount: amountBN, method: 'transfer' }
19
+ }
20
+
21
+ function decodeSystemTransferData(base58Data) {
22
+ const buffer = bs58.decode(base58Data)
23
+
24
+ // 4 bytes
25
+ if (buffer.readUInt32LE(0) !== 2) {
26
+ throw new Error(`Unsupported instruction type for SystemProgram: ${buffer.readUInt32LE(0)}`)
27
+ }
28
+
29
+ const amountBytes = buffer.slice(4, 12)
30
+ const amountBN = new BN(amountBytes, 'le')
31
+
32
+ return {
33
+ amount: amountBN,
34
+ method: 'systemTransfer',
35
+ }
36
+ }
37
+
38
+ // TODO support more tx types/options
39
+ export function isTokenTransfer(tx) {
40
+ const { message } = tx
41
+ const { accountKeys, instructions } = message
42
+
43
+ if (!Array.isArray(accountKeys) || accountKeys.length !== 6) return false
44
+ if (!Array.isArray(instructions) || instructions.length !== 3) return false
45
+
46
+ const [ix1, ix2, ix3] = instructions
47
+ if (
48
+ !accountKeys[ix1.programIdIndex].equals(COMPUTE_BUDGET_PROGRAM_ID) ||
49
+ !accountKeys[ix2.programIdIndex].equals(COMPUTE_BUDGET_PROGRAM_ID)
50
+ ) {
51
+ return false
52
+ }
53
+
54
+ if (!accountKeys[ix3.programIdIndex].equals(TOKEN_PROGRAM_ID)) return false
55
+
56
+ if (!Array.isArray(ix3.accounts) || ix3.accounts.length !== 3) return false
57
+
58
+ try {
59
+ const data = bs58.decode(ix3.data)
60
+ if (data[0] !== 0x03) return false
61
+ } catch {
62
+ return false
63
+ }
64
+
65
+ return true
66
+ }
67
+
68
+ export function isSolanaTransfer(tx) {
69
+ const { message } = tx
70
+ const { accountKeys, instructions } = message
71
+
72
+ if (!Array.isArray(accountKeys) || accountKeys.length !== 5) return false
73
+ if (!Array.isArray(instructions) || instructions.length !== 3) return false
74
+
75
+ if (
76
+ !accountKeys[instructions[0].programIdIndex].equals(COMPUTE_BUDGET_PROGRAM_ID) ||
77
+ !accountKeys[instructions[1].programIdIndex].equals(COMPUTE_BUDGET_PROGRAM_ID)
78
+ ) {
79
+ return false
80
+ }
81
+
82
+ const ix = instructions[2]
83
+ if (!accountKeys[ix.programIdIndex].equals(SYSTEM_PROGRAM_ID)) return false
84
+
85
+ if (!Array.isArray(ix.accounts) || ix.accounts.length !== 2) return false
86
+
87
+ try {
88
+ const data = bs58.decode(ix.data)
89
+ if (data[0] !== 0x02) return false
90
+ } catch {
91
+ return false
92
+ }
93
+
94
+ return true
95
+ }
96
+
97
+ // TODO: Unify with parseTransaction in solana-api and use there as well?
98
+ // TODO: support more tx types.
99
+ export async function parseTxBuffer(buffer, api) {
100
+ const transaction = deserializeTransaction(buffer)
101
+
102
+ try {
103
+ if (isTokenTransfer(transaction)) {
104
+ const mainInstruction = transaction.message.instructions[2]
105
+ const { amount, method } = decodeSPLTransferData(mainInstruction.data)
106
+
107
+ const fromTokenAddress =
108
+ transaction.message.accountKeys[mainInstruction.accounts[0]].toBase58()
109
+ const toTokenAddress = transaction.message.accountKeys[mainInstruction.accounts[1]].toBase58()
110
+ const from = await api.getTokenAddressOwner(fromTokenAddress)
111
+ const to = await api.getTokenAddressOwner(toTokenAddress)
112
+ return {
113
+ method,
114
+ from,
115
+ to,
116
+ amount,
117
+ }
118
+ }
119
+
120
+ if (isSolanaTransfer(transaction)) {
121
+ const mainInstruction = transaction.message.instructions[2]
122
+ const { amount, method } = decodeSystemTransferData(mainInstruction.data)
123
+
124
+ return {
125
+ method,
126
+ from: transaction.message.accountKeys[mainInstruction.accounts[0]],
127
+ to: transaction.message.accountKeys[mainInstruction.accounts[1]],
128
+ amount,
129
+ }
130
+ }
131
+ } catch (error) {
132
+ console.log('transaction check error', error)
133
+ }
134
+
135
+ throw new Error('Transaction not supported for buffer parsing')
136
+ }
@@ -0,0 +1,98 @@
1
+ import lodash from 'lodash'
2
+ import assert from 'minimalistic-assert'
3
+
4
+ export function verifyOnlyFeePayerChanged(beforeTx, afterTx) {
5
+ assert(
6
+ beforeTx.signatures.length + 1 === afterTx.signatures.length &&
7
+ beforeTx.message.header.numRequiredSignatures + 1 ===
8
+ afterTx.message.header.numRequiredSignatures,
9
+ 'A signature was not added for the new signer'
10
+ )
11
+ beforeTx.signatures.forEach((signature, index) => {
12
+ assert(
13
+ lodash.isEqual(signature, afterTx.signatures[index + 1]),
14
+ 'Existing signatures do not match'
15
+ )
16
+ })
17
+ assert(
18
+ beforeTx.message.accountKeys.length + 1 === afterTx.message.accountKeys.length,
19
+ 'Fee payer account key was not added'
20
+ )
21
+ beforeTx.message.accountKeys.forEach((accountKey, index) => {
22
+ assert(
23
+ lodash.isEqual(accountKey, afterTx.message.accountKeys[index + 1]),
24
+ 'Existing account keys do not match'
25
+ )
26
+ })
27
+
28
+ assert(
29
+ beforeTx.message.instructions.length === afterTx.message.instructions.length,
30
+ 'No new instructions are allowed'
31
+ )
32
+
33
+ beforeTx.message.instructions.forEach(({ programIdIndex }, index) => {
34
+ assert(
35
+ programIdIndex + 1 === afterTx.message.instructions[index]?.programIdIndex,
36
+ 'Instructions program ids were not updated'
37
+ )
38
+ })
39
+
40
+ beforeTx.message.instructions.forEach(({ accounts }, index) => {
41
+ assert(
42
+ lodash.isEqual(
43
+ accounts.map((id) => id + 1),
44
+ afterTx.message.instructions[index].accounts
45
+ ),
46
+ 'Instructions account key indexes were not updated'
47
+ )
48
+ })
49
+
50
+ beforeTx.message.instructions.forEach((instruction, index) => {
51
+ assert(
52
+ lodash.isEqual(
53
+ { ...instruction, accounts: null, programIdIndex: null },
54
+ {
55
+ ...afterTx.message.instructions[index],
56
+ accounts: null,
57
+ programIdIndex: null,
58
+ }
59
+ ),
60
+ 'Instructions do not match in some attributes'
61
+ )
62
+ })
63
+
64
+ assert(
65
+ lodash.isEqual(
66
+ {
67
+ ...beforeTx,
68
+ signatures: null,
69
+ message: null,
70
+ },
71
+ {
72
+ ...afterTx,
73
+
74
+ signatures: null,
75
+ message: null,
76
+ }
77
+ ),
78
+ 'Transactions do not match in some attributes'
79
+ )
80
+
81
+ assert(
82
+ lodash.isEqual(
83
+ {
84
+ ...beforeTx.message,
85
+ header: { ...beforeTx.message.header, numRequiredSignatures: null },
86
+ accountKeys: null,
87
+ instructions: null,
88
+ },
89
+ {
90
+ ...afterTx.message,
91
+ header: { ...afterTx.message.header, numRequiredSignatures: null },
92
+ accountKeys: null,
93
+ instructions: null,
94
+ }
95
+ ),
96
+ 'Transactions do not match in some attributes'
97
+ )
98
+ }