@velocity-exchange/sdk 0.4.0 → 0.6.1
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 +52 -0
- package/lib/browser/adminClient.js +97 -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 +191 -15
- package/lib/browser/idl/velocity.json +192 -16
- package/lib/browser/orderSubscriber/OrderSubscriber.d.ts +1 -0
- package/lib/browser/orderSubscriber/OrderSubscriber.js +10 -4
- package/lib/browser/pyth/pythLazerSubscriber.js +37 -27
- package/lib/browser/types.d.ts +6 -0
- package/lib/browser/user.d.ts +20 -3
- package/lib/browser/user.js +52 -4
- package/lib/browser/velocityClient.d.ts +46 -10
- package/lib/browser/velocityClient.js +107 -35
- package/lib/node/adminClient.d.ts +52 -0
- package/lib/node/adminClient.d.ts.map +1 -1
- package/lib/node/adminClient.js +97 -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 +191 -15
- package/lib/node/idl/velocity.d.ts.map +1 -1
- package/lib/node/idl/velocity.json +192 -16
- package/lib/node/orderSubscriber/OrderSubscriber.d.ts +1 -0
- package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
- package/lib/node/orderSubscriber/OrderSubscriber.js +10 -4
- package/lib/node/pyth/pythLazerSubscriber.d.ts.map +1 -1
- package/lib/node/pyth/pythLazerSubscriber.js +37 -27
- package/lib/node/types.d.ts +6 -0
- package/lib/node/types.d.ts.map +1 -1
- package/lib/node/user.d.ts +20 -3
- package/lib/node/user.d.ts.map +1 -1
- package/lib/node/user.js +52 -4
- 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,251 +0,0 @@
|
|
|
1
|
-
import { getSignedMsgUserOrdersFilter } from '../memcmp';
|
|
2
|
-
import { WebSocketProgramAccountSubscriber } from '../accounts/webSocketProgramAccountSubscriber';
|
|
3
|
-
import { SignedMsgOrderId, SignedMsgUserOrdersAccount } from '../types';
|
|
4
|
-
import { Commitment, Context, PublicKey } from '@solana/web3.js';
|
|
5
|
-
import { ResubOpts } from '../accounts/types';
|
|
6
|
-
import { VelocityClient } from '../velocityClient';
|
|
7
|
-
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
8
|
-
import { EventEmitter } from 'events';
|
|
9
|
-
|
|
10
|
-
export type SignedMsgUserOrdersAccountSubscriberConfig = {
|
|
11
|
-
commitment?: Commitment;
|
|
12
|
-
resubOpts?: ResubOpts;
|
|
13
|
-
decodeFn?: (name: string, data: Buffer) => SignedMsgUserOrdersAccount;
|
|
14
|
-
resyncIntervalMs?: number;
|
|
15
|
-
} & { velocityClient: VelocityClient };
|
|
16
|
-
|
|
17
|
-
export interface SignedMsgUserOrdersAccountSubscriberEvents {
|
|
18
|
-
onAccountUpdate: (
|
|
19
|
-
activeSignedMsgOrderIds: SignedMsgOrderId[],
|
|
20
|
-
authorityPubkey: PublicKey,
|
|
21
|
-
slot: number
|
|
22
|
-
) => void;
|
|
23
|
-
|
|
24
|
-
newSignedMsgOrderIds: (
|
|
25
|
-
newSignedMsgOrderIds: SignedMsgOrderId[],
|
|
26
|
-
authorityPubkey: PublicKey,
|
|
27
|
-
slot: number
|
|
28
|
-
) => void;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export class SignedMsgUserOrdersAccountSubscriber {
|
|
32
|
-
protected velocityClient: VelocityClient;
|
|
33
|
-
protected commitment: Commitment;
|
|
34
|
-
protected resubOpts?: ResubOpts;
|
|
35
|
-
protected resyncTimeoutId?: ReturnType<typeof setTimeout>;
|
|
36
|
-
protected resyncIntervalMs?: number;
|
|
37
|
-
protected decodeFn: (
|
|
38
|
-
name: string,
|
|
39
|
-
data: Buffer
|
|
40
|
-
) => SignedMsgUserOrdersAccount;
|
|
41
|
-
public signedMsgUserOrderAccounts = new Map<
|
|
42
|
-
string,
|
|
43
|
-
{ slot: number; signedMsgUserOrdersAccount: SignedMsgUserOrdersAccount }
|
|
44
|
-
>();
|
|
45
|
-
mostRecentSlot = 0;
|
|
46
|
-
|
|
47
|
-
fetchPromise?: Promise<void>;
|
|
48
|
-
private fetchPromiseResolver: () => void = () => {};
|
|
49
|
-
|
|
50
|
-
protected _subscriber?: WebSocketProgramAccountSubscriber<SignedMsgUserOrdersAccount>;
|
|
51
|
-
protected get subscriber(): WebSocketProgramAccountSubscriber<SignedMsgUserOrdersAccount> {
|
|
52
|
-
if (!this._subscriber) {
|
|
53
|
-
throw new Error(
|
|
54
|
-
'SignedMsgUserOrdersAccountSubscriber: subscriber accessed before subscribe()'
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
return this._subscriber;
|
|
58
|
-
}
|
|
59
|
-
public eventEmitter: StrictEventEmitter<
|
|
60
|
-
EventEmitter,
|
|
61
|
-
SignedMsgUserOrdersAccountSubscriberEvents
|
|
62
|
-
>;
|
|
63
|
-
|
|
64
|
-
constructor({
|
|
65
|
-
velocityClient,
|
|
66
|
-
commitment,
|
|
67
|
-
resubOpts,
|
|
68
|
-
decodeFn,
|
|
69
|
-
resyncIntervalMs,
|
|
70
|
-
}: SignedMsgUserOrdersAccountSubscriberConfig) {
|
|
71
|
-
this.commitment = commitment ?? 'confirmed';
|
|
72
|
-
this.resubOpts = resubOpts;
|
|
73
|
-
// Type-system guarantees at least one of the two is supplied.
|
|
74
|
-
this.velocityClient = velocityClient!;
|
|
75
|
-
this.decodeFn =
|
|
76
|
-
decodeFn ??
|
|
77
|
-
(
|
|
78
|
-
this.velocityClient.program.account as any
|
|
79
|
-
).signedMsgUserOrders.coder.accounts.decodeUnchecked.bind(
|
|
80
|
-
(this.velocityClient.program.account as any).signedMsgUserOrders.coder
|
|
81
|
-
.accounts
|
|
82
|
-
);
|
|
83
|
-
this.resyncIntervalMs = resyncIntervalMs;
|
|
84
|
-
this.eventEmitter = new EventEmitter();
|
|
85
|
-
this.resubOpts = resubOpts;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public async subscribe(): Promise<void> {
|
|
89
|
-
if (!this._subscriber) {
|
|
90
|
-
const filters = [getSignedMsgUserOrdersFilter()];
|
|
91
|
-
this._subscriber =
|
|
92
|
-
new WebSocketProgramAccountSubscriber<SignedMsgUserOrdersAccount>(
|
|
93
|
-
'SingedMsgUserOrdersAccountMap',
|
|
94
|
-
'signedMsgUserOrders',
|
|
95
|
-
this.velocityClient.program,
|
|
96
|
-
this.decodeFn,
|
|
97
|
-
{
|
|
98
|
-
filters,
|
|
99
|
-
commitment: this.commitment,
|
|
100
|
-
},
|
|
101
|
-
this.resubOpts
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
await this.subscriber.subscribe(
|
|
106
|
-
(
|
|
107
|
-
_accountId: PublicKey,
|
|
108
|
-
account: SignedMsgUserOrdersAccount,
|
|
109
|
-
context: Context
|
|
110
|
-
) => {
|
|
111
|
-
this.tryUpdateSignedMsgUserOrdersAccount(
|
|
112
|
-
account,
|
|
113
|
-
'decoded',
|
|
114
|
-
context.slot
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
);
|
|
118
|
-
|
|
119
|
-
await this.fetch();
|
|
120
|
-
|
|
121
|
-
if (this.resyncIntervalMs) {
|
|
122
|
-
const recursiveResync = () => {
|
|
123
|
-
this.resyncTimeoutId = setTimeout(() => {
|
|
124
|
-
this.fetch()
|
|
125
|
-
.catch((e) => {
|
|
126
|
-
console.error('Failed to resync in OrderSubscriber');
|
|
127
|
-
console.log(e);
|
|
128
|
-
})
|
|
129
|
-
.finally(() => {
|
|
130
|
-
if (!this.resyncTimeoutId) return;
|
|
131
|
-
recursiveResync();
|
|
132
|
-
});
|
|
133
|
-
}, this.resyncIntervalMs);
|
|
134
|
-
};
|
|
135
|
-
recursiveResync();
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
async fetch(): Promise<void> {
|
|
140
|
-
if (this.fetchPromise) {
|
|
141
|
-
return this.fetchPromise;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
this.fetchPromise = new Promise((resolver) => {
|
|
145
|
-
this.fetchPromiseResolver = resolver;
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
const skipEventEmitting = this.signedMsgUserOrderAccounts.size === 0;
|
|
149
|
-
|
|
150
|
-
try {
|
|
151
|
-
const rpcResponseAndContext =
|
|
152
|
-
await this.velocityClient.connection.getProgramAccounts(
|
|
153
|
-
this.velocityClient.program.programId,
|
|
154
|
-
{
|
|
155
|
-
commitment: this.commitment,
|
|
156
|
-
filters: [getSignedMsgUserOrdersFilter()],
|
|
157
|
-
encoding: 'base64',
|
|
158
|
-
withContext: true,
|
|
159
|
-
}
|
|
160
|
-
);
|
|
161
|
-
|
|
162
|
-
const slot: number = rpcResponseAndContext.context.slot;
|
|
163
|
-
|
|
164
|
-
for (const programAccount of rpcResponseAndContext.value) {
|
|
165
|
-
this.tryUpdateSignedMsgUserOrdersAccount(
|
|
166
|
-
programAccount.account.data,
|
|
167
|
-
'buffer',
|
|
168
|
-
slot,
|
|
169
|
-
skipEventEmitting
|
|
170
|
-
);
|
|
171
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
172
|
-
}
|
|
173
|
-
} catch (e) {
|
|
174
|
-
console.error(e);
|
|
175
|
-
} finally {
|
|
176
|
-
this.fetchPromiseResolver();
|
|
177
|
-
this.fetchPromise = undefined;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
tryUpdateSignedMsgUserOrdersAccount(
|
|
182
|
-
data: Buffer | SignedMsgUserOrdersAccount,
|
|
183
|
-
dataType: 'buffer' | 'decoded',
|
|
184
|
-
slot: number,
|
|
185
|
-
skipEventEmitting = false
|
|
186
|
-
): void {
|
|
187
|
-
if (!this.mostRecentSlot || slot > this.mostRecentSlot) {
|
|
188
|
-
this.mostRecentSlot = slot;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const signedMsgUserOrdersAccount =
|
|
192
|
-
dataType === 'buffer'
|
|
193
|
-
? this.decodeFn('signedMsgUserOrders', data as Buffer)
|
|
194
|
-
: (data as SignedMsgUserOrdersAccount);
|
|
195
|
-
|
|
196
|
-
const key = signedMsgUserOrdersAccount.authorityPubkey.toBase58();
|
|
197
|
-
|
|
198
|
-
const slotAndSignedMsgUserOrdersAccount =
|
|
199
|
-
this.signedMsgUserOrderAccounts.get(key);
|
|
200
|
-
if (
|
|
201
|
-
!slotAndSignedMsgUserOrdersAccount ||
|
|
202
|
-
slotAndSignedMsgUserOrdersAccount.slot <= slot
|
|
203
|
-
) {
|
|
204
|
-
if (!skipEventEmitting) {
|
|
205
|
-
this.eventEmitter.emit(
|
|
206
|
-
'onAccountUpdate',
|
|
207
|
-
signedMsgUserOrdersAccount.signedMsgOrderData.filter(
|
|
208
|
-
(signedMsgOrderId) => signedMsgOrderId.orderId !== 0
|
|
209
|
-
),
|
|
210
|
-
signedMsgUserOrdersAccount.authorityPubkey,
|
|
211
|
-
slot
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const existingSignedMsgOrderIds =
|
|
216
|
-
slotAndSignedMsgUserOrdersAccount?.signedMsgUserOrdersAccount.signedMsgOrderData.map(
|
|
217
|
-
(signedMsgOrderId) => signedMsgOrderId.orderId
|
|
218
|
-
) ?? [];
|
|
219
|
-
|
|
220
|
-
const newSignedMsgOrderIds =
|
|
221
|
-
signedMsgUserOrdersAccount.signedMsgOrderData.filter(
|
|
222
|
-
(signedMsgOrderId: SignedMsgOrderId) =>
|
|
223
|
-
!existingSignedMsgOrderIds.includes(signedMsgOrderId.orderId) &&
|
|
224
|
-
signedMsgOrderId.orderId !== 0
|
|
225
|
-
);
|
|
226
|
-
if (newSignedMsgOrderIds.length > 0 && !skipEventEmitting) {
|
|
227
|
-
this.eventEmitter.emit(
|
|
228
|
-
'newSignedMsgOrderIds',
|
|
229
|
-
newSignedMsgOrderIds,
|
|
230
|
-
signedMsgUserOrdersAccount.authorityPubkey,
|
|
231
|
-
slot
|
|
232
|
-
);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
this.signedMsgUserOrderAccounts.set(key, {
|
|
236
|
-
slot,
|
|
237
|
-
signedMsgUserOrdersAccount,
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
public async unsubscribe(): Promise<void> {
|
|
243
|
-
if (!this._subscriber) return;
|
|
244
|
-
await this._subscriber.unsubscribe();
|
|
245
|
-
this._subscriber = undefined;
|
|
246
|
-
if (this.resyncTimeoutId !== undefined) {
|
|
247
|
-
clearTimeout(this.resyncTimeoutId);
|
|
248
|
-
this.resyncTimeoutId = undefined;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
@@ -1,351 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DevnetPerpMarkets,
|
|
3
|
-
MainnetPerpMarkets,
|
|
4
|
-
} from '../constants/perpMarkets';
|
|
5
|
-
import { VelocityClient } from '../velocityClient';
|
|
6
|
-
import { VelocityEnv } from '../config';
|
|
7
|
-
import {
|
|
8
|
-
getUserAccountPublicKey,
|
|
9
|
-
getUserStatsAccountPublicKey,
|
|
10
|
-
} from '../addresses/pda';
|
|
11
|
-
import {
|
|
12
|
-
MarketType,
|
|
13
|
-
OptionalOrderParams,
|
|
14
|
-
PostOnlyParams,
|
|
15
|
-
SignedMsgOrderParamsDelegateMessage,
|
|
16
|
-
SignedMsgOrderParamsMessage,
|
|
17
|
-
UserAccount,
|
|
18
|
-
} from '../types';
|
|
19
|
-
import { Keypair, PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
20
|
-
import nacl from 'tweetnacl';
|
|
21
|
-
import { decodeUTF8 } from 'tweetnacl-util';
|
|
22
|
-
import WebSocket from 'ws';
|
|
23
|
-
import { sha256 } from '@noble/hashes/sha256';
|
|
24
|
-
|
|
25
|
-
// In practice, this for now is just an OrderSubscriber or a UserMap
|
|
26
|
-
export interface AccountGetter {
|
|
27
|
-
mustGetUserAccount(publicKey: string): Promise<UserAccount>;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
type SwiftOrderSubscriberConfigBase = {
|
|
31
|
-
userAccountGetter?: AccountGetter;
|
|
32
|
-
endpoint?: string;
|
|
33
|
-
marketIndexes: number[];
|
|
34
|
-
/**
|
|
35
|
-
In the future, this will be used for verifying $VELOCITY stake as we add
|
|
36
|
-
authentication for delegate signers
|
|
37
|
-
For now, pass a new keypair or a keypair to an empty wallet
|
|
38
|
-
*/
|
|
39
|
-
keypair: Keypair;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export type SwiftOrderSubscriberConfig = SwiftOrderSubscriberConfigBase & {
|
|
43
|
-
velocityEnv: VelocityEnv;
|
|
44
|
-
} & { velocityClient: VelocityClient };
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Swift order message received from WebSocket
|
|
48
|
-
*/
|
|
49
|
-
export interface SwiftOrderMessage {
|
|
50
|
-
/** Hex string of the order message */
|
|
51
|
-
order_message: string;
|
|
52
|
-
/** Base58 string of taker authority */
|
|
53
|
-
taker_authority: string;
|
|
54
|
-
/** Base58 string of signing authority */
|
|
55
|
-
signing_authority: string;
|
|
56
|
-
/** Base64 string containing the order signature */
|
|
57
|
-
order_signature: string;
|
|
58
|
-
/** Swift order UUID */
|
|
59
|
-
uuid: string;
|
|
60
|
-
/** Whether the order auction params are likely to be sanitized on submission to program */
|
|
61
|
-
will_sanitize?: boolean;
|
|
62
|
-
/** Base64 string of a prerequisite deposit tx. The swift order_message should be bundled
|
|
63
|
-
* after the deposit when present */
|
|
64
|
-
depositTx?: string;
|
|
65
|
-
/** order market index */
|
|
66
|
-
market_index: number;
|
|
67
|
-
/** order timestamp in unix ms */
|
|
68
|
-
ts: number;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export class SwiftOrderSubscriber {
|
|
72
|
-
private heartbeatTimeout: ReturnType<typeof setTimeout> | null = null;
|
|
73
|
-
private readonly heartbeatIntervalMs = 60000;
|
|
74
|
-
private ws: WebSocket | null = null;
|
|
75
|
-
private velocityClient: VelocityClient;
|
|
76
|
-
public userAccountGetter?: AccountGetter; // In practice, this for now is just an OrderSubscriber or a UserMap
|
|
77
|
-
public onOrder?: (
|
|
78
|
-
orderMessageRaw: SwiftOrderMessage,
|
|
79
|
-
signedMessage:
|
|
80
|
-
| SignedMsgOrderParamsMessage
|
|
81
|
-
| SignedMsgOrderParamsDelegateMessage,
|
|
82
|
-
isDelegateSigner?: boolean
|
|
83
|
-
) => Promise<void>;
|
|
84
|
-
|
|
85
|
-
subscribed = false;
|
|
86
|
-
|
|
87
|
-
constructor(private config: SwiftOrderSubscriberConfig) {
|
|
88
|
-
// Type-system guarantees at least one of the two is supplied.
|
|
89
|
-
this.velocityClient = config.velocityClient!;
|
|
90
|
-
this.userAccountGetter = config.userAccountGetter;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
unsubscribe() {
|
|
94
|
-
if (this.subscribed) {
|
|
95
|
-
this.ws?.removeAllListeners();
|
|
96
|
-
this.ws?.terminate();
|
|
97
|
-
this.ws = null;
|
|
98
|
-
this.subscribed = false;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
getSymbolForMarketIndex(marketIndex: number): string {
|
|
103
|
-
const env = this.config.velocityEnv;
|
|
104
|
-
const markets = env === 'devnet' ? DevnetPerpMarkets : MainnetPerpMarkets;
|
|
105
|
-
return markets[marketIndex].symbol;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
generateChallengeResponse(nonce: string): string {
|
|
109
|
-
const messageBytes = decodeUTF8(nonce);
|
|
110
|
-
const signature = nacl.sign.detached(
|
|
111
|
-
messageBytes,
|
|
112
|
-
this.config.keypair.secretKey
|
|
113
|
-
);
|
|
114
|
-
const signatureBase64 = Buffer.from(signature).toString('base64');
|
|
115
|
-
return signatureBase64;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
handleAuthMessage(message: any): void {
|
|
119
|
-
if (message['channel'] === 'auth' && message['nonce'] != null) {
|
|
120
|
-
const signatureBase64 = this.generateChallengeResponse(message['nonce']);
|
|
121
|
-
this.ws?.send(
|
|
122
|
-
JSON.stringify({
|
|
123
|
-
pubkey: this.config.keypair.publicKey.toBase58(),
|
|
124
|
-
signature: signatureBase64,
|
|
125
|
-
})
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (
|
|
130
|
-
message['channel'] === 'auth' &&
|
|
131
|
-
message['message']?.toLowerCase() === 'authenticated'
|
|
132
|
-
) {
|
|
133
|
-
this.subscribed = true;
|
|
134
|
-
this.config.marketIndexes.forEach(async (marketIndex) => {
|
|
135
|
-
this.ws?.send(
|
|
136
|
-
JSON.stringify({
|
|
137
|
-
action: 'subscribe',
|
|
138
|
-
market_type: 'perp',
|
|
139
|
-
market_name: this.getSymbolForMarketIndex(marketIndex),
|
|
140
|
-
})
|
|
141
|
-
);
|
|
142
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
async subscribe(
|
|
148
|
-
onOrder: (
|
|
149
|
-
orderMessageRaw: SwiftOrderMessage,
|
|
150
|
-
signedMessage:
|
|
151
|
-
| SignedMsgOrderParamsMessage
|
|
152
|
-
| SignedMsgOrderParamsDelegateMessage,
|
|
153
|
-
isDelegateSigner?: boolean
|
|
154
|
-
) => Promise<void>,
|
|
155
|
-
acceptSanitized = false,
|
|
156
|
-
acceptDepositTrade = false
|
|
157
|
-
): Promise<void> {
|
|
158
|
-
this.onOrder = onOrder;
|
|
159
|
-
|
|
160
|
-
const env = this.config.velocityEnv;
|
|
161
|
-
const endpoint =
|
|
162
|
-
this.config.endpoint ??
|
|
163
|
-
(env === 'devnet'
|
|
164
|
-
? 'wss://swift.master.velocity.exchange/ws'
|
|
165
|
-
: 'wss://swift.velocity.exchange/ws');
|
|
166
|
-
const ws = new WebSocket(
|
|
167
|
-
endpoint + '?pubkey=' + this.config.keypair.publicKey.toBase58()
|
|
168
|
-
);
|
|
169
|
-
this.ws = ws;
|
|
170
|
-
ws.on('open', async () => {
|
|
171
|
-
console.log('Connected to the server');
|
|
172
|
-
|
|
173
|
-
ws.on('message', async (data: WebSocket.Data) => {
|
|
174
|
-
const message = JSON.parse(data.toString());
|
|
175
|
-
this.startHeartbeatTimer();
|
|
176
|
-
|
|
177
|
-
if (message['channel'] === 'auth') {
|
|
178
|
-
this.handleAuthMessage(message);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (message['order']) {
|
|
182
|
-
const order = message['order'] as SwiftOrderMessage;
|
|
183
|
-
// ignore likely sanitized orders by default
|
|
184
|
-
if (order.will_sanitize === true && !acceptSanitized) {
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
// order has a prerequisite deposit tx attached
|
|
188
|
-
if (message['deposit']) {
|
|
189
|
-
order.depositTx = message['deposit'];
|
|
190
|
-
if (!acceptDepositTrade) {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
const signedMsgOrderParamsBuf = Buffer.from(
|
|
195
|
-
order.order_message,
|
|
196
|
-
'hex'
|
|
197
|
-
);
|
|
198
|
-
const isDelegateSigner = signedMsgOrderParamsBuf
|
|
199
|
-
.slice(0, 8)
|
|
200
|
-
.equals(
|
|
201
|
-
Uint8Array.from(
|
|
202
|
-
Buffer.from(
|
|
203
|
-
sha256('global' + ':' + 'SignedMsgOrderParamsDelegateMessage')
|
|
204
|
-
).slice(0, 8)
|
|
205
|
-
)
|
|
206
|
-
);
|
|
207
|
-
const signedMessage =
|
|
208
|
-
this.velocityClient.decodeSignedMsgOrderParamsMessage(
|
|
209
|
-
signedMsgOrderParamsBuf,
|
|
210
|
-
isDelegateSigner
|
|
211
|
-
);
|
|
212
|
-
|
|
213
|
-
if (!signedMessage.signedMsgOrderParams.price) {
|
|
214
|
-
console.error(
|
|
215
|
-
`order has no price: ${JSON.stringify(
|
|
216
|
-
signedMessage.signedMsgOrderParams
|
|
217
|
-
)}`
|
|
218
|
-
);
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
onOrder(order, signedMessage, isDelegateSigner);
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
ws.on('close', () => {
|
|
227
|
-
console.log('Disconnected from the server');
|
|
228
|
-
this.reconnect();
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
ws.on('error', (error: Error) => {
|
|
232
|
-
console.error('WebSocket error:', error);
|
|
233
|
-
this.reconnect();
|
|
234
|
-
});
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
ws.on('unexpected-response', async (request, response) => {
|
|
238
|
-
console.error(
|
|
239
|
-
'Unexpected response, reconnecting in 5s:',
|
|
240
|
-
response.statusCode
|
|
241
|
-
);
|
|
242
|
-
setTimeout(() => {
|
|
243
|
-
if (this.heartbeatTimeout) clearTimeout(this.heartbeatTimeout);
|
|
244
|
-
this.reconnect();
|
|
245
|
-
}, 5000);
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
ws.on('error', async (error: Error) => {
|
|
249
|
-
console.error('WS closed from error, reconnecting in 1s:', error);
|
|
250
|
-
setTimeout(() => {
|
|
251
|
-
if (this.heartbeatTimeout) clearTimeout(this.heartbeatTimeout);
|
|
252
|
-
this.reconnect();
|
|
253
|
-
}, 1000);
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
async getPlaceAndMakeSignedMsgOrderIxs(
|
|
258
|
-
orderMessageRaw: SwiftOrderMessage,
|
|
259
|
-
signedMsgOrderParamsMessage:
|
|
260
|
-
| SignedMsgOrderParamsMessage
|
|
261
|
-
| SignedMsgOrderParamsDelegateMessage,
|
|
262
|
-
makerOrderParams: OptionalOrderParams
|
|
263
|
-
): Promise<TransactionInstruction[]> {
|
|
264
|
-
if (!this.userAccountGetter) {
|
|
265
|
-
throw new Error('userAccountGetter must be set to use this function');
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
const signedMsgOrderParamsBuf = Buffer.from(
|
|
269
|
-
orderMessageRaw.order_message,
|
|
270
|
-
'hex'
|
|
271
|
-
);
|
|
272
|
-
|
|
273
|
-
const isDelegateSigner = signedMsgOrderParamsBuf
|
|
274
|
-
.slice(0, 8)
|
|
275
|
-
.equals(
|
|
276
|
-
Uint8Array.from(
|
|
277
|
-
Buffer.from(
|
|
278
|
-
sha256('global' + ':' + 'SignedMsgOrderParamsDelegateMessage')
|
|
279
|
-
).slice(0, 8)
|
|
280
|
-
)
|
|
281
|
-
);
|
|
282
|
-
const signedMessage = this.velocityClient.decodeSignedMsgOrderParamsMessage(
|
|
283
|
-
signedMsgOrderParamsBuf,
|
|
284
|
-
isDelegateSigner
|
|
285
|
-
);
|
|
286
|
-
|
|
287
|
-
const takerAuthority = new PublicKey(orderMessageRaw.taker_authority);
|
|
288
|
-
const signingAuthority = new PublicKey(orderMessageRaw.signing_authority);
|
|
289
|
-
const takerUserPubkey = isDelegateSigner
|
|
290
|
-
? (signedMessage as SignedMsgOrderParamsDelegateMessage).takerPubkey
|
|
291
|
-
: await getUserAccountPublicKey(
|
|
292
|
-
this.velocityClient.program.programId,
|
|
293
|
-
takerAuthority,
|
|
294
|
-
(signedMessage as SignedMsgOrderParamsMessage).subAccountId
|
|
295
|
-
);
|
|
296
|
-
const takerUserAccount = await this.userAccountGetter.mustGetUserAccount(
|
|
297
|
-
takerUserPubkey.toString()
|
|
298
|
-
);
|
|
299
|
-
const ixs = await this.velocityClient.getPlaceAndMakeSignedMsgPerpOrderIxs(
|
|
300
|
-
{
|
|
301
|
-
orderParams: signedMsgOrderParamsBuf,
|
|
302
|
-
signature: Buffer.from(orderMessageRaw.order_signature, 'base64'),
|
|
303
|
-
},
|
|
304
|
-
decodeUTF8(orderMessageRaw.uuid),
|
|
305
|
-
{
|
|
306
|
-
taker: takerUserPubkey,
|
|
307
|
-
takerUserAccount,
|
|
308
|
-
takerStats: getUserStatsAccountPublicKey(
|
|
309
|
-
this.velocityClient.program.programId,
|
|
310
|
-
takerUserAccount.authority
|
|
311
|
-
),
|
|
312
|
-
signingAuthority: signingAuthority,
|
|
313
|
-
},
|
|
314
|
-
Object.assign({}, makerOrderParams, {
|
|
315
|
-
postOnly: PostOnlyParams.MUST_POST_ONLY,
|
|
316
|
-
immediateOrCancel: true,
|
|
317
|
-
marketType: MarketType.PERP,
|
|
318
|
-
})
|
|
319
|
-
);
|
|
320
|
-
return ixs;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
private startHeartbeatTimer() {
|
|
324
|
-
if (this.heartbeatTimeout) {
|
|
325
|
-
clearTimeout(this.heartbeatTimeout);
|
|
326
|
-
}
|
|
327
|
-
if (!this.onOrder) {
|
|
328
|
-
throw new Error('onOrder callback function must be set');
|
|
329
|
-
}
|
|
330
|
-
this.heartbeatTimeout = setTimeout(() => {
|
|
331
|
-
console.warn('No heartbeat received within 30 seconds, reconnecting...');
|
|
332
|
-
this.reconnect();
|
|
333
|
-
}, this.heartbeatIntervalMs);
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
private reconnect() {
|
|
337
|
-
if (this.ws) {
|
|
338
|
-
this.ws.removeAllListeners();
|
|
339
|
-
this.ws.terminate();
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
console.log('Reconnecting to WebSocket...');
|
|
343
|
-
const onOrder = this.onOrder;
|
|
344
|
-
if (!onOrder) {
|
|
345
|
-
throw new Error('onOrder callback function must be set');
|
|
346
|
-
}
|
|
347
|
-
setTimeout(() => {
|
|
348
|
-
this.subscribe(onOrder);
|
|
349
|
-
}, 1000);
|
|
350
|
-
}
|
|
351
|
-
}
|
package/src/testClient.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { AdminClient } from './adminClient';
|
|
2
|
-
import { ConfirmOptions, Signer, Transaction } from '@solana/web3.js';
|
|
3
|
-
import { TxSigAndSlot } from './tx/types';
|
|
4
|
-
import { PollingVelocityClientAccountSubscriber } from './accounts/pollingVelocityClientAccountSubscriber';
|
|
5
|
-
import { VelocityClientConfig } from './velocityClientConfig';
|
|
6
|
-
|
|
7
|
-
export class TestClient extends AdminClient {
|
|
8
|
-
public constructor(config: VelocityClientConfig) {
|
|
9
|
-
config.txVersion = 'legacy';
|
|
10
|
-
if (config.accountSubscription?.type !== 'polling') {
|
|
11
|
-
throw new Error('Test client must be polling');
|
|
12
|
-
}
|
|
13
|
-
super(config);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async sendTransaction(
|
|
17
|
-
tx: Transaction,
|
|
18
|
-
additionalSigners?: Array<Signer>,
|
|
19
|
-
opts?: ConfirmOptions,
|
|
20
|
-
preSigned?: boolean
|
|
21
|
-
): Promise<TxSigAndSlot> {
|
|
22
|
-
const { txSig, slot } = await super.sendTransaction(
|
|
23
|
-
tx,
|
|
24
|
-
additionalSigners,
|
|
25
|
-
opts,
|
|
26
|
-
preSigned
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
let lastFetchedSlot = (
|
|
30
|
-
this.accountSubscriber as PollingVelocityClientAccountSubscriber
|
|
31
|
-
).accountLoader.mostRecentSlot;
|
|
32
|
-
await this.fetchAccounts();
|
|
33
|
-
while (slot !== undefined && lastFetchedSlot < slot) {
|
|
34
|
-
await this.fetchAccounts();
|
|
35
|
-
lastFetchedSlot = (
|
|
36
|
-
this.accountSubscriber as PollingVelocityClientAccountSubscriber
|
|
37
|
-
).accountLoader.mostRecentSlot;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return { txSig, slot };
|
|
41
|
-
}
|
|
42
|
-
}
|