@velocity-exchange/sdk 0.4.0 → 0.6.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/README.md +3 -3
- package/lib/browser/adminClient.d.ts +35 -0
- package/lib/browser/adminClient.js +65 -0
- package/lib/browser/config.js +6 -7
- package/lib/browser/constants/numericConstants.d.ts +1 -1
- package/lib/browser/constants/numericConstants.js +1 -1
- package/lib/browser/decode/user.js +5 -1
- package/lib/browser/idl/velocity.d.ts +146 -3
- package/lib/browser/idl/velocity.json +147 -4
- package/lib/browser/orderSubscriber/OrderSubscriber.js +3 -0
- package/lib/browser/pyth/pythLazerSubscriber.js +37 -27
- package/lib/browser/types.d.ts +4 -0
- package/lib/browser/user.d.ts +15 -0
- package/lib/browser/user.js +37 -1
- package/lib/browser/velocityClient.d.ts +46 -10
- package/lib/browser/velocityClient.js +107 -35
- package/lib/node/adminClient.d.ts +35 -0
- package/lib/node/adminClient.d.ts.map +1 -1
- package/lib/node/adminClient.js +65 -0
- package/lib/node/config.d.ts.map +1 -1
- package/lib/node/config.js +6 -7
- package/lib/node/constants/numericConstants.d.ts +1 -1
- package/lib/node/constants/numericConstants.d.ts.map +1 -1
- package/lib/node/constants/numericConstants.js +1 -1
- package/lib/node/decode/user.d.ts.map +1 -1
- package/lib/node/decode/user.js +5 -1
- package/lib/node/idl/velocity.d.ts +146 -3
- package/lib/node/idl/velocity.d.ts.map +1 -1
- package/lib/node/idl/velocity.json +147 -4
- package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
- package/lib/node/orderSubscriber/OrderSubscriber.js +3 -0
- package/lib/node/pyth/pythLazerSubscriber.d.ts.map +1 -1
- package/lib/node/pyth/pythLazerSubscriber.js +37 -27
- package/lib/node/types.d.ts +4 -0
- package/lib/node/types.d.ts.map +1 -1
- package/lib/node/user.d.ts +15 -0
- package/lib/node/user.d.ts.map +1 -1
- package/lib/node/user.js +37 -1
- package/lib/node/velocityClient.d.ts +46 -10
- package/lib/node/velocityClient.d.ts.map +1 -1
- package/lib/node/velocityClient.js +107 -35
- package/package.json +7 -3
- package/.prettierignore +0 -1
- package/CHANGELOG.md +0 -132
- package/build-browser.js +0 -58
- package/bunfig.toml +0 -3
- package/esbuild-shims.js +0 -12
- package/scripts/deposit-isolated-positions.ts +0 -110
- package/scripts/find-flagged-users.ts +0 -213
- package/scripts/grpc-client-test-comparison.ts +0 -372
- package/scripts/grpc-multiuser-client-test-comparison.ts +0 -158
- package/scripts/postbuild.js +0 -95
- package/scripts/single-grpc-client-test.ts +0 -284
- package/scripts/withdraw-isolated-positions.ts +0 -174
- package/src/accounts/README_WebSocketAccountSubscriberV2.md +0 -95
- package/src/accounts/README_WebSocketProgramAccountSubscriberV2.md +0 -135
- package/src/accounts/basicUserAccountSubscriber.ts +0 -79
- package/src/accounts/basicUserStatsAccountSubscriber.ts +0 -87
- package/src/accounts/bulkAccountLoader.ts +0 -358
- package/src/accounts/bulkUserStatsSubscription.ts +0 -40
- package/src/accounts/bulkUserSubscription.ts +0 -39
- package/src/accounts/customizedCadenceBulkAccountLoader.ts +0 -250
- package/src/accounts/fetch.ts +0 -200
- package/src/accounts/grpcAccountSubscriber.ts +0 -225
- package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +0 -73
- package/src/accounts/grpcMultiAccountSubscriber.ts +0 -566
- package/src/accounts/grpcMultiUserAccountSubscriber.ts +0 -328
- package/src/accounts/grpcProgramAccountSubscriber.ts +0 -264
- package/src/accounts/grpcUserAccountSubscriber.ts +0 -63
- package/src/accounts/grpcUserStatsAccountSubscriber.ts +0 -65
- package/src/accounts/grpcVelocityClientAccountSubscriber.ts +0 -257
- package/src/accounts/grpcVelocityClientAccountSubscriberV2.ts +0 -941
- package/src/accounts/laserProgramAccountSubscriber.ts +0 -266
- package/src/accounts/oneShotUserAccountSubscriber.ts +0 -88
- package/src/accounts/oneShotUserStatsAccountSubscriber.ts +0 -94
- package/src/accounts/pollingInsuranceFundStakeAccountSubscriber.ts +0 -223
- package/src/accounts/pollingOracleAccountSubscriber.ts +0 -151
- package/src/accounts/pollingTokenAccountSubscriber.ts +0 -147
- package/src/accounts/pollingUserAccountSubscriber.ts +0 -198
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +0 -180
- package/src/accounts/pollingVelocityClientAccountSubscriber.ts +0 -774
- package/src/accounts/testBulkAccountLoader.ts +0 -59
- package/src/accounts/types.ts +0 -428
- package/src/accounts/utils.ts +0 -90
- package/src/accounts/webSocketAccountSubscriber.ts +0 -304
- package/src/accounts/webSocketAccountSubscriberV2.ts +0 -536
- package/src/accounts/webSocketInsuranceFundStakeAccountSubscriber.ts +0 -171
- package/src/accounts/webSocketProgramAccountSubscriber.ts +0 -244
- package/src/accounts/webSocketProgramAccountSubscriberV2.ts +0 -1048
- package/src/accounts/webSocketProgramAccountsSubscriberV2.ts +0 -1042
- package/src/accounts/webSocketUserAccountSubscriber.ts +0 -139
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +0 -131
- package/src/accounts/webSocketVelocityClientAccountSubscriber.ts +0 -865
- package/src/accounts/webSocketVelocityClientAccountSubscriberV2.ts +0 -897
- package/src/accounts/websocketProgramUserAccountSubscriber.ts +0 -127
- package/src/addresses/marketAddresses.ts +0 -28
- package/src/addresses/pda.ts +0 -620
- package/src/adminClient.ts +0 -8136
- package/src/assert/assert.ts +0 -5
- package/src/auctionSubscriber/auctionSubscriber.ts +0 -86
- package/src/auctionSubscriber/auctionSubscriberGrpc.ts +0 -99
- package/src/auctionSubscriber/index.ts +0 -9
- package/src/auctionSubscriber/types.ts +0 -30
- package/src/bankrun/bankrunConnection.ts +0 -630
- package/src/blockhashSubscriber/BlockhashSubscriber.ts +0 -160
- package/src/blockhashSubscriber/index.ts +0 -6
- package/src/blockhashSubscriber/types.ts +0 -13
- package/src/clock/clockSubscriber.ts +0 -146
- package/src/config.ts +0 -312
- package/src/constants/index.ts +0 -9
- package/src/constants/numericConstants.ts +0 -194
- package/src/constants/perpMarkets.ts +0 -127
- package/src/constants/spotMarkets.ts +0 -104
- package/src/constants/txConstants.ts +0 -2
- package/src/constituentMap/constituentMap.ts +0 -305
- package/src/constituentMap/pollingConstituentAccountSubscriber.ts +0 -99
- package/src/constituentMap/webSocketConstituentAccountSubscriber.ts +0 -121
- package/src/core/VelocityCore.ts +0 -606
- package/src/core/index.ts +0 -12
- package/src/core/instructions/deposit.ts +0 -64
- package/src/core/instructions/fill.ts +0 -51
- package/src/core/instructions/funding.ts +0 -33
- package/src/core/instructions/liquidation.ts +0 -55
- package/src/core/instructions/orders.ts +0 -84
- package/src/core/instructions/perpOrders.ts +0 -312
- package/src/core/instructions/settlement.ts +0 -40
- package/src/core/instructions/trigger.ts +0 -41
- package/src/core/instructions/withdraw.ts +0 -65
- package/src/core/remainingAccounts.ts +0 -320
- package/src/core/signedMsg.ts +0 -109
- package/src/decode/customCoder.ts +0 -148
- package/src/decode/user.ts +0 -401
- package/src/dlob/DLOB.ts +0 -2793
- package/src/dlob/DLOBNode.ts +0 -305
- package/src/dlob/DLOBSubscriber.ts +0 -269
- package/src/dlob/NodeList.ts +0 -244
- package/src/dlob/orderBookLevels.ts +0 -695
- package/src/dlob/types.ts +0 -37
- package/src/events/eventList.ts +0 -114
- package/src/events/eventSubscriber.ts +0 -521
- package/src/events/eventsServerLogProvider.ts +0 -189
- package/src/events/fetchLogs.ts +0 -219
- package/src/events/parse.ts +0 -271
- package/src/events/pollingLogProvider.ts +0 -113
- package/src/events/sort.ts +0 -51
- package/src/events/txEventCache.ts +0 -92
- package/src/events/types.ts +0 -337
- package/src/events/webSocketLogProvider.ts +0 -152
- package/src/factory/bigNum.ts +0 -709
- package/src/factory/oracleClient.ts +0 -73
- package/src/idl/pyth.json +0 -142
- package/src/idl/pyth.ts +0 -97
- package/src/idl/pyth_solana_receiver.json +0 -628
- package/src/idl/token_faucet.json +0 -229
- package/src/idl/token_faucet.ts +0 -197
- package/src/idl/velocity.json +0 -23974
- package/src/idl/velocity.ts +0 -23980
- package/src/index.ts +0 -150
- package/src/indicative-quotes/indicativeQuotesSender.ts +0 -233
- package/src/isomorphic/README.md +0 -19
- package/src/isomorphic/anchor.browser.ts +0 -44
- package/src/isomorphic/anchor.node.ts +0 -1
- package/src/isomorphic/anchor.ts +0 -1
- package/src/isomorphic/anchor29.browser.ts +0 -24
- package/src/isomorphic/anchor29.node.ts +0 -1
- package/src/isomorphic/anchor29.ts +0 -1
- package/src/isomorphic/grpc.browser.ts +0 -4
- package/src/isomorphic/grpc.node.ts +0 -130
- package/src/isomorphic/grpc.ts +0 -1
- package/src/jupiter/jupiterClient.ts +0 -502
- package/src/keypair.ts +0 -32
- package/src/margin/README.md +0 -138
- package/src/marginCalculation.ts +0 -407
- package/src/marinade/idl/idl.json +0 -1962
- package/src/marinade/index.ts +0 -73
- package/src/marinade/types.ts +0 -3925
- package/src/math/amm.ts +0 -1758
- package/src/math/auction.ts +0 -480
- package/src/math/bankruptcy.ts +0 -138
- package/src/math/builder.ts +0 -130
- package/src/math/conversion.ts +0 -45
- package/src/math/exchangeStatus.ts +0 -261
- package/src/math/funding.ts +0 -498
- package/src/math/insurance.ts +0 -171
- package/src/math/liquidation.ts +0 -433
- package/src/math/margin.ts +0 -327
- package/src/math/market.ts +0 -487
- package/src/math/oracles.ts +0 -477
- package/src/math/orders.ts +0 -500
- package/src/math/position.ts +0 -381
- package/src/math/repeg.ts +0 -223
- package/src/math/spotBalance.ts +0 -996
- package/src/math/spotMarket.ts +0 -115
- package/src/math/spotPosition.ts +0 -263
- package/src/math/state.ts +0 -71
- package/src/math/superStake.ts +0 -602
- package/src/math/tiers.ts +0 -73
- package/src/math/trade.ts +0 -879
- package/src/math/utils.ts +0 -183
- package/src/memcmp.ts +0 -279
- package/src/oracles/oracleClientCache.ts +0 -41
- package/src/oracles/oracleId.ts +0 -99
- package/src/oracles/prelaunchOracleClient.ts +0 -63
- package/src/oracles/pythClient.ts +0 -125
- package/src/oracles/pythLazerClient.ts +0 -141
- package/src/oracles/quoteAssetOracleClient.ts +0 -43
- package/src/oracles/strictOraclePrice.ts +0 -40
- package/src/oracles/types.ts +0 -82
- package/src/oracles/utils.ts +0 -45
- package/src/orderParams.ts +0 -117
- package/src/orderSubscriber/OrderSubscriber.ts +0 -378
- package/src/orderSubscriber/PollingSubscription.ts +0 -50
- package/src/orderSubscriber/WebsocketSubscription.ts +0 -137
- package/src/orderSubscriber/grpcSubscription.ts +0 -155
- package/src/orderSubscriber/index.ts +0 -7
- package/src/orderSubscriber/types.ts +0 -64
- package/src/priorityFee/averageOverSlotsStrategy.ts +0 -17
- package/src/priorityFee/averageStrategy.ts +0 -13
- package/src/priorityFee/ewmaStrategy.ts +0 -51
- package/src/priorityFee/heliusPriorityFeeMethod.ts +0 -68
- package/src/priorityFee/index.ts +0 -16
- package/src/priorityFee/maxOverSlotsStrategy.ts +0 -18
- package/src/priorityFee/maxStrategy.ts +0 -8
- package/src/priorityFee/priorityFeeSubscriber.ts +0 -328
- package/src/priorityFee/priorityFeeSubscriberMap.ts +0 -134
- package/src/priorityFee/solanaPriorityFeeMethod.ts +0 -46
- package/src/priorityFee/types.ts +0 -82
- package/src/priorityFee/velocityPriorityFeeMethod.ts +0 -53
- package/src/pyth/constants.ts +0 -9
- package/src/pyth/index.ts +0 -15
- package/src/pyth/pythLazerSubscriber.ts +0 -365
- package/src/pyth/types.ts +0 -4451
- package/src/pyth/utils.ts +0 -13
- package/src/slot/SlotSubscriber.ts +0 -126
- package/src/slot/SlothashSubscriber.ts +0 -163
- package/src/swap/UnifiedSwapClient.ts +0 -315
- package/src/swift/grpcSignedMsgUserAccountSubscriber.ts +0 -81
- package/src/swift/index.ts +0 -10
- package/src/swift/signedMsgUserAccountSubscriber.ts +0 -251
- package/src/swift/swiftOrderSubscriber.ts +0 -351
- package/src/testClient.ts +0 -42
- package/src/titan/titanClient.ts +0 -438
- package/src/token/index.ts +0 -13
- package/src/tokenFaucet.ts +0 -296
- package/src/tx/baseTxSender.ts +0 -651
- package/src/tx/blockhashFetcher/baseBlockhashFetcher.ts +0 -31
- package/src/tx/blockhashFetcher/cachedBlockhashFetcher.ts +0 -105
- package/src/tx/blockhashFetcher/types.ts +0 -9
- package/src/tx/fastSingleTxSender.ts +0 -200
- package/src/tx/forwardOnlyTxSender.ts +0 -193
- package/src/tx/priorityFeeCalculator.ts +0 -117
- package/src/tx/reportTransactionError.ts +0 -176
- package/src/tx/retryTxSender.ts +0 -177
- package/src/tx/txHandler.ts +0 -1009
- package/src/tx/txParamProcessor.ts +0 -225
- package/src/tx/types.ts +0 -117
- package/src/tx/utils.ts +0 -133
- package/src/tx/whileValidTxSender.ts +0 -377
- package/src/types.ts +0 -2575
- package/src/user.ts +0 -5316
- package/src/userConfig.ts +0 -38
- package/src/userMap/PollingSubscription.ts +0 -61
- package/src/userMap/WebsocketSubscription.ts +0 -93
- package/src/userMap/grpcSubscription.ts +0 -93
- package/src/userMap/referrerMap.ts +0 -360
- package/src/userMap/revenueShareEscrowMap.ts +0 -342
- package/src/userMap/userMap.ts +0 -784
- package/src/userMap/userMapConfig.ts +0 -82
- package/src/userMap/userStatsMap.ts +0 -393
- package/src/userName.ts +0 -37
- package/src/userStats.ts +0 -159
- package/src/userStatsConfig.ts +0 -35
- package/src/util/TransactionConfirmationManager.ts +0 -333
- package/src/util/chainClock.ts +0 -65
- package/src/util/computeUnits.ts +0 -101
- package/src/util/digest.ts +0 -33
- package/src/util/ed25519Utils.ts +0 -95
- package/src/util/promiseTimeout.ts +0 -23
- package/src/velocityClient.ts +0 -14802
- package/src/velocityClientConfig.ts +0 -119
- package/src/wallet.ts +0 -91
- package/tests/VelocityCore/builder_escrow.test.ts +0 -121
- package/tests/VelocityCore/decode.test.ts +0 -16
- package/tests/VelocityCore/fill_trigger.test.ts +0 -73
- package/tests/VelocityCore/instructions.test.ts +0 -46
- package/tests/VelocityCore/pdas.test.ts +0 -30
- package/tests/VelocityCore/perp_orders.test.ts +0 -205
- package/tests/VelocityCore/remainingAccounts.test.ts +0 -72
- package/tests/VelocityCore/settlement_liquidation.test.ts +0 -69
- package/tests/accounts/customizedCadenceBulkAccountLoader.test.ts +0 -211
- package/tests/amm/test.ts +0 -2402
- package/tests/auctions/test.ts +0 -81
- package/tests/bn/test.ts +0 -355
- package/tests/builder/builderFee.test.ts +0 -42
- package/tests/ci/verifyConstants.ts +0 -393
- package/tests/decode/test.ts +0 -262
- package/tests/decode/userAccountBufferStrings.ts +0 -102
- package/tests/dlob/helpers.ts +0 -748
- package/tests/dlob/test.ts +0 -6969
- package/tests/dlob/tickSizeStandardization.ts +0 -545
- package/tests/events/parseLogsForCuUsage.ts +0 -139
- package/tests/exchangeStatus/test.ts +0 -45
- package/tests/insurance/test.ts +0 -40
- package/tests/liquidation/test.ts +0 -125
- package/tests/oracles/mmOracleGate.test.ts +0 -379
- package/tests/oracles/pythPegSnap.test.ts +0 -76
- package/tests/sdkParity/enumParity.test.ts +0 -84
- package/tests/sdkParity/marginCategoryFill.test.ts +0 -143
- package/tests/sdkParity/memcmpOffsets.test.ts +0 -139
- package/tests/spot/test.ts +0 -281
- package/tests/tx/TransactionConfirmationManager.test.ts +0 -312
- package/tests/tx/cachedBlockhashFetcher.test.ts +0 -96
- package/tests/tx/priorityFeeCalculator.ts +0 -77
- package/tests/tx/priorityFeeStrategy.ts +0 -95
- package/tests/user/bankruptcy.ts +0 -165
- package/tests/user/feeAndWithdrawLimits.ts +0 -284
- package/tests/user/getMarginCalculation.ts +0 -586
- package/tests/user/helpers.ts +0 -184
- package/tests/user/liquidations.ts +0 -129
- package/tests/user/marginCalculations.test.ts +0 -321
- package/tests/user/test.ts +0 -833
- package/tsconfig.browser.json +0 -18
- package/tsconfig.json +0 -36
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Connection,
|
|
3
|
-
RpcResponseAndContext,
|
|
4
|
-
SimulatedTransactionResponse,
|
|
5
|
-
VersionedTransaction,
|
|
6
|
-
} from '@solana/web3.js';
|
|
7
|
-
import { BaseTxParams, ProcessingTxParams } from '../types';
|
|
8
|
-
|
|
9
|
-
const COMPUTE_UNIT_BUFFER_FACTOR = 1.2;
|
|
10
|
-
const MAX_COMPUTE_UNITS = 1_400_000;
|
|
11
|
-
|
|
12
|
-
const TEST_SIMS_ALWAYS_FAIL = false;
|
|
13
|
-
|
|
14
|
-
type TransactionBuildingProps = {
|
|
15
|
-
txParams: BaseTxParams;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* This class is responsible for running through a "processing" pipeline for a base transaction, to adjust the standard transaction parameters based on a given configuration.
|
|
20
|
-
*
|
|
21
|
-
* Currently the only pipeline step is simulation-based compute unit resolution: build the
|
|
22
|
-
* transaction with a max compute-unit budget, simulate it, and replace the compute-unit limit
|
|
23
|
-
* (and optionally the compute-unit price) with values derived from actual simulated usage.
|
|
24
|
-
*/
|
|
25
|
-
export class TransactionParamProcessor {
|
|
26
|
-
private static async getComputeUnitsFromSim(
|
|
27
|
-
txSim: RpcResponseAndContext<SimulatedTransactionResponse>
|
|
28
|
-
) {
|
|
29
|
-
if (txSim?.value?.unitsConsumed) {
|
|
30
|
-
return txSim?.value?.unitsConsumed;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return undefined;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Determines a compute-unit limit to use for a transaction by simulating it (or reusing a
|
|
38
|
-
* pre-supplied simulation), applying `bufferMultiplier` to the actually-consumed units, and
|
|
39
|
-
* clamping the result to at most `MAX_COMPUTE_UNITS` (1,400,000). Simulation runs with
|
|
40
|
-
* `replaceRecentBlockhash: true` to avoid spurious `blockHashNotFound` failures from an
|
|
41
|
-
* already-stale blockhash on the transaction being sized. Never throws: any error (including
|
|
42
|
-
* a simulation error result) is caught and reported via the `success: false` return.
|
|
43
|
-
* @param tx - Transaction to simulate (ignored if `simulatedTx` is supplied instead).
|
|
44
|
-
* @param connection - RPC connection used to simulate, if `simulatedTx` isn't provided.
|
|
45
|
-
* @param bufferMultiplier - Multiplier applied to the raw consumed compute units before
|
|
46
|
-
* clamping (e.g. `1.2` for a 20% buffer) — mandatory to force callers to account for
|
|
47
|
-
* simulated CU counts being an imperfect predictor of the real on-chain cost.
|
|
48
|
-
* @param lowerBoundCu - If provided, the result is floored at `min(lowerBoundCu, MAX_COMPUTE_UNITS)`.
|
|
49
|
-
* @param simulatedTx - A pre-computed simulation result to reuse instead of simulating `tx` again.
|
|
50
|
-
* @returns `{ success: true, computeUnits }` with the buffered/clamped compute-unit limit on
|
|
51
|
-
* success, or `{ success: false, computeUnits: undefined }` if simulation failed or returned no
|
|
52
|
-
* `unitsConsumed`.
|
|
53
|
-
*/
|
|
54
|
-
public static async getTxSimComputeUnits(
|
|
55
|
-
tx: VersionedTransaction,
|
|
56
|
-
connection: Connection,
|
|
57
|
-
bufferMultiplier: number, // Making this a mandatory param to force the user to remember that simulated CU's can be inaccurate and a buffer should be applied
|
|
58
|
-
lowerBoundCu?: number,
|
|
59
|
-
simulatedTx?: SimulatedTransactionResponse
|
|
60
|
-
): Promise<
|
|
61
|
-
| { success: true; computeUnits: number }
|
|
62
|
-
| { success: false; computeUnits: undefined }
|
|
63
|
-
> {
|
|
64
|
-
try {
|
|
65
|
-
if (TEST_SIMS_ALWAYS_FAIL)
|
|
66
|
-
throw new Error('Test Error::SIMS_ALWAYS_FAIL');
|
|
67
|
-
|
|
68
|
-
const simTxResult = simulatedTx
|
|
69
|
-
? ({
|
|
70
|
-
value: simulatedTx,
|
|
71
|
-
} as RpcResponseAndContext<SimulatedTransactionResponse>)
|
|
72
|
-
: await connection.simulateTransaction(tx, {
|
|
73
|
-
replaceRecentBlockhash: true, // This is important to ensure that the blockhash is not too new.. Otherwise we will very often receive a "blockHashNotFound" error
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
if (simTxResult?.value?.err) {
|
|
77
|
-
throw simTxResult?.value?.err;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const computeUnits = await this.getComputeUnitsFromSim(simTxResult);
|
|
81
|
-
|
|
82
|
-
if (computeUnits === undefined) {
|
|
83
|
-
throw new Error(
|
|
84
|
-
'TransactionParamProcessor: simulation did not return units consumed'
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Apply the buffer, but round down to the MAX_COMPUTE_UNITS, and round up to the nearest whole number
|
|
89
|
-
let bufferedComputeUnits = Math.ceil(
|
|
90
|
-
Math.min(computeUnits * bufferMultiplier, MAX_COMPUTE_UNITS)
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
// If a lower bound CU is passed then enforce it
|
|
94
|
-
if (lowerBoundCu) {
|
|
95
|
-
bufferedComputeUnits = Math.max(
|
|
96
|
-
bufferedComputeUnits,
|
|
97
|
-
Math.min(lowerBoundCu, MAX_COMPUTE_UNITS)
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return {
|
|
102
|
-
success: true as const,
|
|
103
|
-
computeUnits: bufferedComputeUnits,
|
|
104
|
-
};
|
|
105
|
-
} catch (e) {
|
|
106
|
-
console.warn(
|
|
107
|
-
`Failed to get Simulated Compute Units for txParamProcessor`,
|
|
108
|
-
e
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
return {
|
|
112
|
-
success: false as const,
|
|
113
|
-
computeUnits: undefined,
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Runs the configured processing steps against a base set of tx params and returns the
|
|
120
|
-
* adjusted params. If `processConfig` is empty/absent, returns `baseTxParams` unchanged
|
|
121
|
-
* without building or simulating anything.
|
|
122
|
-
*
|
|
123
|
-
* When `processConfig.useSimulatedComputeUnits` is set: rebuilds the transaction (via
|
|
124
|
-
* `txBuilder`) with `computeUnits` forced to `MAX_COMPUTE_UNITS` so simulation isn't
|
|
125
|
-
* constrained by an under-sized limit, simulates it, and — on success — replaces
|
|
126
|
-
* `computeUnits` with the result of `getTxSimComputeUnits` (failure leaves the original
|
|
127
|
-
* `baseTxParams.computeUnits` untouched, it does not throw).
|
|
128
|
-
*
|
|
129
|
-
* When additionally `processConfig.useSimulatedComputeUnitsForCUPriceCalculation` is set:
|
|
130
|
-
* derives `computeUnitsPrice` from the simulated `computeUnits` via
|
|
131
|
-
* `processConfig.getCUPriceFromComputeUnits`.
|
|
132
|
-
* @param props.baseTxParams - Starting compute-unit limit/price to adjust.
|
|
133
|
-
* @param props.processConfig - Which processing steps to run (see `ProcessingTxParams`).
|
|
134
|
-
* @param props.processParams.connection - RPC connection used for simulation.
|
|
135
|
-
* @param props.processParams.simulatedTx - Pre-computed simulation result to reuse instead of simulating again.
|
|
136
|
-
* @param props.txBuilder - Builds a `VersionedTransaction` from a given `BaseTxParams`, used to
|
|
137
|
-
* produce the transaction that gets simulated.
|
|
138
|
-
* @returns The adjusted `BaseTxParams`.
|
|
139
|
-
* @throws Error if `useSimulatedComputeUnitsForCUPriceCalculation` is set without
|
|
140
|
-
* `useSimulatedComputeUnits`, without `getCUPriceFromComputeUnits`, or if the simulated compute
|
|
141
|
-
* units are unavailable (simulation failed).
|
|
142
|
-
*/
|
|
143
|
-
static async process(props: {
|
|
144
|
-
baseTxParams: BaseTxParams;
|
|
145
|
-
processConfig: ProcessingTxParams;
|
|
146
|
-
processParams: {
|
|
147
|
-
connection: Connection;
|
|
148
|
-
simulatedTx?: SimulatedTransactionResponse;
|
|
149
|
-
};
|
|
150
|
-
txBuilder: (
|
|
151
|
-
baseTransactionProps: TransactionBuildingProps
|
|
152
|
-
) => Promise<VersionedTransaction>;
|
|
153
|
-
}): Promise<BaseTxParams> {
|
|
154
|
-
// # Exit early if no process config is provided
|
|
155
|
-
if (!props.processConfig || Object.keys(props.processConfig).length === 0) {
|
|
156
|
-
return props.baseTxParams;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// # Setup
|
|
160
|
-
const {
|
|
161
|
-
txBuilder: txBuilder,
|
|
162
|
-
processConfig,
|
|
163
|
-
processParams: processProps,
|
|
164
|
-
} = props;
|
|
165
|
-
|
|
166
|
-
const finalTxParams: BaseTxParams = {
|
|
167
|
-
...props.baseTxParams,
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
// # Run Processes
|
|
171
|
-
if (processConfig.useSimulatedComputeUnits) {
|
|
172
|
-
const txToSim = await txBuilder({
|
|
173
|
-
txParams: { ...finalTxParams, computeUnits: MAX_COMPUTE_UNITS },
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
const txSimComputeUnitsResult = await this.getTxSimComputeUnits(
|
|
177
|
-
txToSim,
|
|
178
|
-
processProps.connection,
|
|
179
|
-
processConfig?.computeUnitsBufferMultiplier ??
|
|
180
|
-
COMPUTE_UNIT_BUFFER_FACTOR,
|
|
181
|
-
undefined,
|
|
182
|
-
processProps.simulatedTx
|
|
183
|
-
);
|
|
184
|
-
|
|
185
|
-
if (txSimComputeUnitsResult.success) {
|
|
186
|
-
// Adjust the transaction based on the simulated compute units
|
|
187
|
-
finalTxParams.computeUnits = txSimComputeUnitsResult.computeUnits;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (processConfig?.useSimulatedComputeUnitsForCUPriceCalculation) {
|
|
192
|
-
if (!processConfig?.useSimulatedComputeUnits) {
|
|
193
|
-
throw new Error(
|
|
194
|
-
`encountered useSimulatedComputeUnitsForFees=true, but useSimulatedComputeUnits is false`
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
if (!processConfig?.getCUPriceFromComputeUnits) {
|
|
198
|
-
throw new Error(
|
|
199
|
-
`encountered useSimulatedComputeUnitsForFees=true, but getComputeUnitPriceFromUnitsToUse helper method is undefined`
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
const simulatedComputeUnits = finalTxParams.computeUnits;
|
|
204
|
-
|
|
205
|
-
if (simulatedComputeUnits === undefined) {
|
|
206
|
-
throw new Error(
|
|
207
|
-
`encountered useSimulatedComputeUnitsForFees=true, but simulated compute units are unavailable (simulation likely failed)`
|
|
208
|
-
);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const computeUnitPrice = processConfig.getCUPriceFromComputeUnits(
|
|
212
|
-
simulatedComputeUnits
|
|
213
|
-
);
|
|
214
|
-
|
|
215
|
-
console.debug(
|
|
216
|
-
`🔧:: Adjusting compute unit price for simulated compute unit budget :: ${finalTxParams.computeUnitsPrice}=>${computeUnitPrice}`
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
finalTxParams.computeUnitsPrice = computeUnitPrice;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// # Return Final Tx Params
|
|
223
|
-
return finalTxParams;
|
|
224
|
-
}
|
|
225
|
-
}
|
package/src/tx/types.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AddressLookupTableAccount,
|
|
3
|
-
BlockhashWithExpiryBlockHeight,
|
|
4
|
-
ConfirmOptions,
|
|
5
|
-
Signer,
|
|
6
|
-
Transaction,
|
|
7
|
-
TransactionInstruction,
|
|
8
|
-
TransactionSignature,
|
|
9
|
-
VersionedTransaction,
|
|
10
|
-
} from '@solana/web3.js';
|
|
11
|
-
import { IWallet } from '../types';
|
|
12
|
-
|
|
13
|
-
/** How a `TxSender` implementation waits for a transaction to confirm. */
|
|
14
|
-
export enum ConfirmationStrategy {
|
|
15
|
-
/** Confirm via an RPC websocket `onSignature` subscription only. */
|
|
16
|
-
WebSocket = 'websocket',
|
|
17
|
-
/** Confirm by polling `getSignatureStatuses` only. */
|
|
18
|
-
Polling = 'polling',
|
|
19
|
-
/** Race a websocket subscription against polling and resolve on whichever confirms first. */
|
|
20
|
-
Combo = 'combo',
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** A landed transaction's signature plus the slot it was confirmed at (`undefined` if the slot wasn't available). */
|
|
24
|
-
export type TxSigAndSlot = {
|
|
25
|
-
txSig: TransactionSignature;
|
|
26
|
-
slot: number | undefined;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
/** Common interface implemented by every transaction-sending strategy (`BaseTxSender` and its subclasses). */
|
|
30
|
-
export interface TxSender {
|
|
31
|
-
wallet: IWallet;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Signs (unless `preSigned`), sends, and confirms a legacy `Transaction`.
|
|
35
|
-
* @param tx - Transaction to send.
|
|
36
|
-
* @param additionalSigners - Extra signers to co-sign alongside the wallet.
|
|
37
|
-
* @param opts - Confirmation options overriding the sender's defaults.
|
|
38
|
-
* @param preSigned - If `true`, skip wallet signing (the transaction is already fully signed).
|
|
39
|
-
* @returns The landed transaction's signature and confirmation slot.
|
|
40
|
-
*/
|
|
41
|
-
send(
|
|
42
|
-
tx: Transaction,
|
|
43
|
-
additionalSigners?: Array<Signer>,
|
|
44
|
-
opts?: ConfirmOptions,
|
|
45
|
-
preSigned?: boolean
|
|
46
|
-
): Promise<TxSigAndSlot>;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Signs (unless `preSigned`), sends, and confirms a `VersionedTransaction`.
|
|
50
|
-
* @param tx - Transaction to send.
|
|
51
|
-
* @param additionalSigners - Extra signers to co-sign alongside the wallet.
|
|
52
|
-
* @param opts - Confirmation options overriding the sender's defaults.
|
|
53
|
-
* @param preSigned - If `true`, skip wallet signing (the transaction is already fully signed).
|
|
54
|
-
* @returns The landed transaction's signature and confirmation slot.
|
|
55
|
-
*/
|
|
56
|
-
sendVersionedTransaction(
|
|
57
|
-
tx: VersionedTransaction,
|
|
58
|
-
additionalSigners?: Array<Signer>,
|
|
59
|
-
opts?: ConfirmOptions,
|
|
60
|
-
preSigned?: boolean
|
|
61
|
-
): Promise<TxSigAndSlot>;
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Builds (but does not send or sign) an unsigned `VersionedTransaction` from raw instructions.
|
|
65
|
-
* @param ixs - Instructions to include, in order.
|
|
66
|
-
* @param lookupTableAccounts - Address lookup tables to compile the message against.
|
|
67
|
-
* @param additionalSigners - Unused by `BaseTxSender` (signing happens at send time, e.g. in `sendVersionedTransaction`).
|
|
68
|
-
* @param opts - Unused by `BaseTxSender`.
|
|
69
|
-
* @param blockhash - Blockhash to compile the message with; if omitted, the sender fetches a current one.
|
|
70
|
-
* @returns The compiled, unsigned `VersionedTransaction`, not yet sent.
|
|
71
|
-
*/
|
|
72
|
-
getVersionedTransaction(
|
|
73
|
-
ixs: TransactionInstruction[],
|
|
74
|
-
lookupTableAccounts: AddressLookupTableAccount[],
|
|
75
|
-
additionalSigners?: Array<Signer>,
|
|
76
|
-
opts?: ConfirmOptions,
|
|
77
|
-
blockhash?: BlockhashWithExpiryBlockHeight
|
|
78
|
-
): Promise<VersionedTransaction>;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Sends an already-serialized, already-signed transaction and confirms it.
|
|
82
|
-
* @param rawTransaction - Wire-format serialized transaction bytes.
|
|
83
|
-
* @param opts - Confirmation options.
|
|
84
|
-
* @returns The landed transaction's signature and confirmation slot.
|
|
85
|
-
*/
|
|
86
|
-
sendRawTransaction(
|
|
87
|
-
rawTransaction: Buffer | Uint8Array,
|
|
88
|
-
opts: ConfirmOptions
|
|
89
|
-
): Promise<TxSigAndSlot>;
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Simulates a transaction against the cluster without sending it.
|
|
93
|
-
* @param tx - Transaction to simulate.
|
|
94
|
-
* @returns `true` if the simulation succeeded (no error), `false` otherwise.
|
|
95
|
-
*/
|
|
96
|
-
simulateTransaction(tx: VersionedTransaction): Promise<boolean>;
|
|
97
|
-
|
|
98
|
-
/** Number of transactions sent by this sender that timed out waiting for confirmation. */
|
|
99
|
-
getTimeoutCount(): number;
|
|
100
|
-
/** Multiplier this sender currently recommends applying to a base priority fee, derived from recent land/timeout behavior. */
|
|
101
|
-
getSuggestedPriorityFeeMultiplier(): number;
|
|
102
|
-
/** Fraction (0-1) of recently sent transactions that landed successfully. */
|
|
103
|
-
getTxLandRate(): number;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/** Error thrown by tx senders when a transaction fails to send or confirm, carrying a stable numeric `code` (see `constants/txConstants`) alongside the message. */
|
|
107
|
-
export class TxSendError extends Error {
|
|
108
|
-
constructor(
|
|
109
|
-
public message: string,
|
|
110
|
-
public code: number
|
|
111
|
-
) {
|
|
112
|
-
super(message);
|
|
113
|
-
if (Error.captureStackTrace) {
|
|
114
|
-
Error.captureStackTrace(this, TxSendError);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
package/src/tx/utils.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AddressLookupTableAccount,
|
|
3
|
-
Transaction,
|
|
4
|
-
TransactionInstruction,
|
|
5
|
-
VersionedTransaction,
|
|
6
|
-
} from '@solana/web3.js';
|
|
7
|
-
|
|
8
|
-
/** Maximum serialized transaction size (bytes) Solana will accept over the wire — the 1232-byte MTU-derived limit shared by legacy and versioned transactions. */
|
|
9
|
-
export const MAX_TX_BYTE_SIZE = 1232;
|
|
10
|
-
/** 4-byte magic prefix identifying velocity's native (non-Anchor) entrypoint instructions, followed by a 1-byte opcode — see `createNativeInstructionDiscriminatorBuffer`. */
|
|
11
|
-
export const NATIVE_INSTRUCTION_MAGIC_BYTES = [0xff, 0xff, 0xff, 0xff];
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Checks whether a transaction is a `VersionedTransaction` (v0+) rather than a legacy `Transaction`.
|
|
15
|
-
* @param tx - Transaction to check.
|
|
16
|
-
* @returns `true` if `tx` is versioned.
|
|
17
|
-
*/
|
|
18
|
-
export const isVersionedTransaction = (
|
|
19
|
-
tx: Transaction | VersionedTransaction
|
|
20
|
-
): boolean => {
|
|
21
|
-
const version = (tx as VersionedTransaction)?.version;
|
|
22
|
-
const isVersionedTx =
|
|
23
|
-
tx instanceof VersionedTransaction || version !== undefined;
|
|
24
|
-
|
|
25
|
-
return isVersionedTx;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Estimates the serialized wire size (bytes) a transaction built from `instructions` would have,
|
|
30
|
-
* without actually building/signing it — used to decide whether instructions still fit under
|
|
31
|
-
* `MAX_TX_BYTE_SIZE` before committing to a blockhash/signature. Accounts for account
|
|
32
|
-
* deduplication and, for versioned transactions, the byte savings from any accounts resolvable via
|
|
33
|
-
* `addressLookupTables`.
|
|
34
|
-
* @param instructions - Instructions the transaction would contain.
|
|
35
|
-
* @param versionedTransaction - Whether to size as a v0 `VersionedTransaction` (includes the
|
|
36
|
-
* version byte and lookup-table address/index overhead) or a legacy `Transaction`; defaults to `true`.
|
|
37
|
-
* @param addressLookupTables - Lookup tables to credit toward reducing the static account-keys list.
|
|
38
|
-
* @returns The estimated total transaction size in bytes.
|
|
39
|
-
*/
|
|
40
|
-
export const getSizeOfTransaction = (
|
|
41
|
-
instructions: TransactionInstruction[],
|
|
42
|
-
versionedTransaction = true,
|
|
43
|
-
addressLookupTables: AddressLookupTableAccount[] = []
|
|
44
|
-
): number => {
|
|
45
|
-
const programs = new Set<string>();
|
|
46
|
-
const signers = new Set<string>();
|
|
47
|
-
let accounts = new Set<string>();
|
|
48
|
-
|
|
49
|
-
instructions.forEach((ix) => {
|
|
50
|
-
try {
|
|
51
|
-
if (ix.programId) {
|
|
52
|
-
programs.add(ix.programId.toBase58());
|
|
53
|
-
accounts.add(ix.programId.toBase58());
|
|
54
|
-
}
|
|
55
|
-
if (ix.keys) {
|
|
56
|
-
ix.keys.forEach((key) => {
|
|
57
|
-
if (key.isSigner) {
|
|
58
|
-
signers.add(key.pubkey.toBase58());
|
|
59
|
-
}
|
|
60
|
-
accounts.add(key.pubkey.toBase58());
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
} catch (e) {
|
|
64
|
-
console.log(e);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
const instructionSizes: number = instructions
|
|
69
|
-
.map(
|
|
70
|
-
(ix) =>
|
|
71
|
-
1 +
|
|
72
|
-
getSizeOfCompressedU16(ix.keys.length) +
|
|
73
|
-
ix.keys.length +
|
|
74
|
-
getSizeOfCompressedU16(ix.data.length) +
|
|
75
|
-
ix.data.length
|
|
76
|
-
)
|
|
77
|
-
.reduce((a, b) => a + b, 0);
|
|
78
|
-
|
|
79
|
-
let numberOfAddressLookups = 0;
|
|
80
|
-
// Filter out null/undefined lookup tables before accessing .state
|
|
81
|
-
const validLookupTables = addressLookupTables.filter(
|
|
82
|
-
(table): table is AddressLookupTableAccount =>
|
|
83
|
-
table !== null && table !== undefined
|
|
84
|
-
);
|
|
85
|
-
if (validLookupTables.length > 0) {
|
|
86
|
-
const lookupTableAddresses = validLookupTables
|
|
87
|
-
.map((addressLookupTable) =>
|
|
88
|
-
addressLookupTable.state.addresses.map((address) => address.toBase58())
|
|
89
|
-
)
|
|
90
|
-
.flat();
|
|
91
|
-
const totalNumberOfAccounts = accounts.size;
|
|
92
|
-
accounts = new Set(
|
|
93
|
-
[...accounts].filter((account) => !lookupTableAddresses.includes(account))
|
|
94
|
-
);
|
|
95
|
-
accounts = new Set([...accounts, ...programs, ...signers]);
|
|
96
|
-
numberOfAddressLookups = totalNumberOfAccounts - accounts.size;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return (
|
|
100
|
-
getSizeOfCompressedU16(signers.size) +
|
|
101
|
-
signers.size * 64 + // array of signatures
|
|
102
|
-
3 +
|
|
103
|
-
getSizeOfCompressedU16(accounts.size) +
|
|
104
|
-
32 * accounts.size + // array of account addresses
|
|
105
|
-
32 + // recent blockhash
|
|
106
|
-
getSizeOfCompressedU16(instructions.length) +
|
|
107
|
-
instructionSizes + // array of instructions
|
|
108
|
-
(versionedTransaction ? 1 + getSizeOfCompressedU16(0) : 0) +
|
|
109
|
-
(versionedTransaction ? 32 * addressLookupTables.length : 0) +
|
|
110
|
-
(versionedTransaction && addressLookupTables.length > 0 ? 2 : 0) +
|
|
111
|
-
numberOfAddressLookups
|
|
112
|
-
);
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
function getSizeOfCompressedU16(n: number) {
|
|
116
|
-
return 1 + Number(n >= 128) + Number(n >= 16384);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Builds the 5-byte instruction-data prefix for velocity's native (non-Anchor) entrypoint:
|
|
121
|
-
* `NATIVE_INSTRUCTION_MAGIC_BYTES` (`[0xFF, 0xFF, 0xFF, 0xFF]`) followed by a 1-byte opcode. Used
|
|
122
|
-
* for high-frequency keeper instructions that bypass Anchor's instruction-dispatch overhead.
|
|
123
|
-
* @param discriminator - The native instruction's opcode byte.
|
|
124
|
-
* @returns The 5-byte discriminator buffer to prepend to the instruction's data.
|
|
125
|
-
*/
|
|
126
|
-
export function createNativeInstructionDiscriminatorBuffer(
|
|
127
|
-
discriminator: number
|
|
128
|
-
): Uint8Array {
|
|
129
|
-
const buffer = new Uint8Array(5);
|
|
130
|
-
buffer.set(NATIVE_INSTRUCTION_MAGIC_BYTES, 0);
|
|
131
|
-
buffer.set([discriminator], 4);
|
|
132
|
-
return buffer;
|
|
133
|
-
}
|