@drift-labs/sdk 0.1.36-master.5 → 0.2.0-master.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/lib/accounts/bulkAccountLoader.d.ts +7 -6
- package/lib/accounts/bulkAccountLoader.js +83 -93
- package/lib/accounts/bulkUserSubscription.js +13 -57
- package/lib/accounts/fetch.d.ts +4 -0
- package/lib/accounts/fetch.js +18 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
- package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
- package/lib/accounts/pollingOracleSubscriber.js +37 -49
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
- package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
- package/lib/accounts/types.d.ts +34 -41
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
- package/lib/accounts/webSocketAccountSubscriber.js +39 -35
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
- package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
- package/lib/addresses/marketAddresses.d.ts +4 -0
- package/lib/addresses/marketAddresses.js +15 -0
- package/lib/addresses/pda.d.ts +12 -0
- package/lib/addresses/pda.js +83 -0
- package/lib/admin.d.ts +8 -12
- package/lib/admin.js +366 -490
- package/lib/clearingHouse.d.ts +84 -103
- package/lib/clearingHouse.js +779 -810
- package/lib/clearingHouseConfig.d.ts +34 -0
- package/lib/clearingHouseConfig.js +2 -0
- package/lib/clearingHouseUser.d.ts +19 -18
- package/lib/clearingHouseUser.js +157 -115
- package/lib/clearingHouseUserConfig.d.ts +14 -0
- package/lib/clearingHouseUserConfig.js +2 -0
- package/lib/config.d.ts +12 -0
- package/lib/config.js +35 -4
- package/lib/constants/banks.d.ts +16 -0
- package/lib/constants/banks.js +34 -0
- package/lib/constants/markets.d.ts +8 -3
- package/lib/constants/markets.js +13 -206
- package/lib/constants/numericConstants.d.ts +16 -0
- package/lib/constants/numericConstants.js +22 -6
- package/lib/events/eventList.d.ts +22 -0
- package/lib/events/eventList.js +77 -0
- package/lib/events/eventSubscriber.d.ts +34 -0
- package/lib/events/eventSubscriber.js +126 -0
- package/lib/events/fetchLogs.d.ts +13 -0
- package/lib/events/fetchLogs.js +39 -0
- package/lib/events/pollingLogProvider.d.ts +15 -0
- package/lib/events/pollingLogProvider.js +53 -0
- package/lib/events/sort.d.ts +2 -0
- package/lib/events/sort.js +44 -0
- package/lib/events/txEventCache.d.ts +24 -0
- package/lib/events/txEventCache.js +71 -0
- package/lib/events/types.d.ts +49 -0
- package/lib/events/types.js +20 -0
- package/lib/events/webSocketLogProvider.d.ts +12 -0
- package/lib/events/webSocketLogProvider.js +30 -0
- package/lib/examples/makeTradeExample.js +26 -27
- package/lib/factory/bigNum.d.ts +112 -0
- package/lib/factory/bigNum.js +356 -0
- package/lib/factory/oracleClient.d.ts +1 -2
- package/lib/factory/oracleClient.js +6 -2
- package/lib/idl/clearing_house.json +1739 -2287
- package/lib/index.d.ts +13 -4
- package/lib/index.js +13 -4
- package/lib/math/amm.d.ts +19 -29
- package/lib/math/amm.js +129 -179
- package/lib/math/auction.d.ts +5 -0
- package/lib/math/auction.js +39 -0
- package/lib/math/bankBalance.d.ts +9 -0
- package/lib/math/bankBalance.js +75 -0
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -5
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +158 -175
- package/lib/math/market.d.ts +6 -6
- package/lib/math/market.js +10 -9
- package/lib/math/oracles.d.ts +3 -0
- package/lib/math/oracles.js +26 -0
- package/lib/math/orders.d.ts +5 -0
- package/lib/math/orders.js +31 -1
- package/lib/math/position.d.ts +7 -4
- package/lib/math/position.js +27 -9
- package/lib/math/repeg.d.ts +22 -0
- package/lib/math/repeg.js +128 -0
- package/lib/math/trade.d.ts +13 -8
- package/lib/math/trade.js +85 -22
- package/lib/mockUSDCFaucet.js +87 -116
- package/lib/oracles/oracleClientCache.d.ts +8 -0
- package/lib/oracles/oracleClientCache.js +19 -0
- package/lib/oracles/pythClient.d.ts +3 -5
- package/lib/oracles/pythClient.js +12 -30
- package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
- package/lib/oracles/quoteAssetOracleClient.js +21 -0
- package/lib/oracles/switchboardClient.d.ts +3 -5
- package/lib/oracles/switchboardClient.js +29 -47
- package/lib/oracles/types.d.ts +7 -1
- package/lib/orders.d.ts +6 -6
- package/lib/orders.js +10 -9
- package/lib/slot/SlotSubscriber.d.ts +12 -0
- package/lib/slot/SlotSubscriber.js +23 -0
- package/lib/tx/retryTxSender.d.ts +2 -2
- package/lib/tx/retryTxSender.js +108 -123
- package/lib/tx/types.d.ts +5 -1
- package/lib/tx/utils.d.ts +1 -1
- package/lib/tx/utils.js +11 -2
- package/lib/types.d.ts +105 -98
- package/lib/types.js +13 -1
- package/lib/userName.d.ts +4 -0
- package/lib/userName.js +20 -0
- package/lib/util/computeUnits.js +10 -21
- package/lib/util/tps.js +11 -22
- package/lib/wallet.js +7 -20
- package/package.json +10 -3
- package/src/accounts/bulkAccountLoader.ts +26 -15
- package/src/accounts/bulkUserSubscription.ts +1 -45
- package/src/accounts/fetch.ts +33 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
- package/src/accounts/pollingOracleSubscriber.ts +16 -8
- package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
- package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
- package/src/accounts/types.ts +41 -70
- package/src/accounts/webSocketAccountSubscriber.ts +33 -16
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
- package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
- package/src/addresses/marketAddresses.ts +18 -0
- package/src/addresses/pda.ts +118 -0
- package/src/admin.ts +209 -267
- package/src/clearingHouse.ts +921 -829
- package/src/clearingHouseConfig.ts +37 -0
- package/src/clearingHouseUser.ts +280 -127
- package/src/clearingHouseUserConfig.ts +18 -0
- package/src/config.ts +54 -1
- package/src/constants/banks.ts +43 -0
- package/src/constants/markets.ts +16 -207
- package/src/constants/numericConstants.ts +33 -5
- package/src/events/eventList.ts +94 -0
- package/src/events/eventSubscriber.ts +194 -0
- package/src/events/fetchLogs.ts +80 -0
- package/src/events/pollingLogProvider.ts +79 -0
- package/src/events/sort.ts +65 -0
- package/src/events/txEventCache.ts +74 -0
- package/src/events/types.ts +98 -0
- package/src/events/webSocketLogProvider.ts +38 -0
- package/src/examples/makeTradeExample.ts +20 -11
- package/src/factory/bigNum.ts +507 -0
- package/src/factory/oracleClient.ts +7 -4
- package/src/idl/clearing_house.json +1739 -2287
- package/src/index.ts +13 -4
- package/src/math/amm.ts +229 -245
- package/src/math/auction.ts +39 -0
- package/src/math/bankBalance.ts +112 -0
- package/src/math/conversion.ts +1 -11
- package/src/math/funding.ts +12 -9
- package/src/math/market.ts +37 -30
- package/src/math/oracles.ts +36 -0
- package/src/math/orders.ts +38 -0
- package/src/math/position.ts +48 -13
- package/src/math/repeg.ts +175 -0
- package/src/math/trade.ts +114 -36
- package/src/math/utils.js +27 -0
- package/src/math/utils.js.map +1 -0
- package/src/oracles/oracleClientCache.ts +20 -0
- package/src/oracles/pythClient.ts +5 -11
- package/src/oracles/quoteAssetOracleClient.ts +25 -0
- package/src/oracles/switchboardClient.ts +16 -24
- package/src/oracles/types.ts +8 -1
- package/src/orders.ts +35 -20
- package/src/slot/SlotSubscriber.ts +32 -0
- package/src/tx/retryTxSender.ts +6 -4
- package/src/tx/types.ts +6 -1
- package/src/tx/utils.ts +22 -3
- package/src/types.ts +108 -110
- package/src/userName.ts +20 -0
- package/src/util/computeUnits.js +17 -0
- package/src/util/computeUnits.js.map +1 -0
- package/tests/bn/test.ts +255 -0
- package/tsconfig.json +12 -12
- package/lib/addresses.d.ts +0 -9
- package/lib/addresses.js +0 -87
- package/lib/constants/accounts.d.ts +0 -15
- package/lib/constants/accounts.js +0 -18
- package/lib/factory/clearingHouse.d.ts +0 -35
- package/lib/factory/clearingHouse.js +0 -81
- package/lib/factory/clearingHouseUser.d.ts +0 -19
- package/lib/factory/clearingHouseUser.js +0 -34
- package/lib/math/insuranceFund.d.ts +0 -15
- package/lib/math/insuranceFund.js +0 -33
- package/lib/tx/defaultTxSender.d.ts +0 -8
- package/lib/tx/defaultTxSender.js +0 -12
- package/src/addresses.ts +0 -71
- package/src/constants/accounts.ts +0 -26
- package/src/factory/clearingHouse.ts +0 -173
- package/src/factory/clearingHouseUser.ts +0 -73
- package/src/math/insuranceFund.ts +0 -29
- package/src/tx/defaultTxSender.ts +0 -24
package/src/addresses.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { PublicKey } from '@solana/web3.js';
|
|
2
|
-
import * as anchor from '@project-serum/anchor';
|
|
3
|
-
|
|
4
|
-
export async function getClearingHouseStateAccountPublicKeyAndNonce(
|
|
5
|
-
programId: PublicKey
|
|
6
|
-
): Promise<[PublicKey, number]> {
|
|
7
|
-
return anchor.web3.PublicKey.findProgramAddress(
|
|
8
|
-
[Buffer.from(anchor.utils.bytes.utf8.encode('clearing_house'))],
|
|
9
|
-
programId
|
|
10
|
-
);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export async function getOrderStateAccountPublicKey(
|
|
14
|
-
programId: PublicKey
|
|
15
|
-
): Promise<PublicKey> {
|
|
16
|
-
return (await getOrderStateAccountPublicKeyAndNonce(programId))[0];
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export async function getOrderStateAccountPublicKeyAndNonce(
|
|
20
|
-
programId: PublicKey
|
|
21
|
-
): Promise<[PublicKey, number]> {
|
|
22
|
-
return anchor.web3.PublicKey.findProgramAddress(
|
|
23
|
-
[Buffer.from(anchor.utils.bytes.utf8.encode('order_state'))],
|
|
24
|
-
programId
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export async function getClearingHouseStateAccountPublicKey(
|
|
29
|
-
programId: PublicKey
|
|
30
|
-
): Promise<PublicKey> {
|
|
31
|
-
return (await getClearingHouseStateAccountPublicKeyAndNonce(programId))[0];
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export async function getUserAccountPublicKeyAndNonce(
|
|
35
|
-
programId: PublicKey,
|
|
36
|
-
authority: PublicKey
|
|
37
|
-
): Promise<[PublicKey, number]> {
|
|
38
|
-
return anchor.web3.PublicKey.findProgramAddress(
|
|
39
|
-
[Buffer.from(anchor.utils.bytes.utf8.encode('user')), authority.toBuffer()],
|
|
40
|
-
programId
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export async function getUserAccountPublicKey(
|
|
45
|
-
programId: PublicKey,
|
|
46
|
-
authority: PublicKey
|
|
47
|
-
): Promise<PublicKey> {
|
|
48
|
-
return (await getUserAccountPublicKeyAndNonce(programId, authority))[0];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export async function getUserOrdersAccountPublicKeyAndNonce(
|
|
52
|
-
programId: PublicKey,
|
|
53
|
-
userAccount: PublicKey
|
|
54
|
-
): Promise<[PublicKey, number]> {
|
|
55
|
-
return anchor.web3.PublicKey.findProgramAddress(
|
|
56
|
-
[
|
|
57
|
-
Buffer.from(anchor.utils.bytes.utf8.encode('user_orders')),
|
|
58
|
-
userAccount.toBuffer(),
|
|
59
|
-
],
|
|
60
|
-
programId
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export async function getUserOrdersAccountPublicKey(
|
|
65
|
-
programId: PublicKey,
|
|
66
|
-
userAccount: PublicKey
|
|
67
|
-
): Promise<PublicKey> {
|
|
68
|
-
return (
|
|
69
|
-
await getUserOrdersAccountPublicKeyAndNonce(programId, userAccount)
|
|
70
|
-
)[0];
|
|
71
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { PublicKey } from '@solana/web3.js';
|
|
2
|
-
|
|
3
|
-
export const CLEARING_HOUSE_STATE_ACCOUNTS = {
|
|
4
|
-
dammHkt7jmytvbS3nHTxQNEcP59aE57nxwV21YdqEDN: {
|
|
5
|
-
state: new PublicKey('FExhvPycCCwYnZGeDsVtLhpEQ3yEkVY2k1HuPyfLj91L'),
|
|
6
|
-
markets: new PublicKey('773hq3SbGPKVj93TXi5qV5CREuhxobywfALjS3XVHhLH'),
|
|
7
|
-
orderState: new PublicKey('4cC34bWwTPGncBaX2S6v5mH3Lj4Nb3byPMrxQSDcY985'),
|
|
8
|
-
tradeHistory: new PublicKey('FCuCXEoQppoaCYdttA7rK3HNQfYkTNEGpwuBESzYcENp'),
|
|
9
|
-
depositHistory: new PublicKey(
|
|
10
|
-
'C7rF2Qy2rnDGQLRijBRRArJyaeuQcFi81BXDrUCQ45ya'
|
|
11
|
-
),
|
|
12
|
-
fundingPaymentHistory: new PublicKey(
|
|
13
|
-
'895iPhzwT2tBufLnRpYtBG3gif1HDDUfpH2AqbS5joo4'
|
|
14
|
-
),
|
|
15
|
-
fundingRateHistory: new PublicKey(
|
|
16
|
-
'BWiJLMbmrwfqHVpcPJa8715XamNyNYamDDKQVQMnduEC'
|
|
17
|
-
),
|
|
18
|
-
extendedCurveHistory: new PublicKey(
|
|
19
|
-
'7vBbqvMdtZLQdTVdzt5y63pZdAFE5W42kP3avngSJKCk'
|
|
20
|
-
),
|
|
21
|
-
liquidationHistory: new PublicKey(
|
|
22
|
-
'CSFaaf8yVoTx6NcXUKtNPYAewv76CH2jATqSVRBvUWKM'
|
|
23
|
-
),
|
|
24
|
-
orderHistory: new PublicKey('DZ7XfUqyHoRKnJLRApxmJ943xHJ7NDBUTqpbooviEtbU'),
|
|
25
|
-
},
|
|
26
|
-
};
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import { ConfirmOptions, Connection, PublicKey } from '@solana/web3.js';
|
|
2
|
-
import { IWallet } from '../types';
|
|
3
|
-
import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
|
|
4
|
-
import { TxSender } from '../tx/types';
|
|
5
|
-
import { AnchorProvider, Idl, Program } from '@project-serum/anchor';
|
|
6
|
-
import { ClearingHouse } from '../clearingHouse';
|
|
7
|
-
import clearingHouseIDL from '../idl/clearing_house.json';
|
|
8
|
-
import { WebSocketClearingHouseAccountSubscriber } from '../accounts/webSocketClearingHouseAccountSubscriber';
|
|
9
|
-
import { DefaultTxSender } from '../tx/defaultTxSender';
|
|
10
|
-
import { ClearingHouseAccountSubscriber } from '../accounts/types';
|
|
11
|
-
import { PollingClearingHouseAccountSubscriber } from '../accounts/pollingClearingHouseAccountSubscriber';
|
|
12
|
-
import { Admin } from '../admin';
|
|
13
|
-
import { RetryTxSender } from '../tx/retryTxSender';
|
|
14
|
-
|
|
15
|
-
export type ClearingHouseConfigType = 'websocket' | 'polling' | 'custom';
|
|
16
|
-
|
|
17
|
-
type BaseClearingHouseConfig = {
|
|
18
|
-
type: ClearingHouseConfigType;
|
|
19
|
-
connection: Connection;
|
|
20
|
-
wallet: IWallet;
|
|
21
|
-
programID: PublicKey;
|
|
22
|
-
opts?: ConfirmOptions;
|
|
23
|
-
txSenderConfig?: TxSenderConfig;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
type WebSocketClearingHouseConfiguration = BaseClearingHouseConfig;
|
|
27
|
-
|
|
28
|
-
type PollingClearingHouseConfiguration = BaseClearingHouseConfig & {
|
|
29
|
-
accountLoader: BulkAccountLoader;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
type ClearingHouseConfig =
|
|
33
|
-
| PollingClearingHouseConfiguration
|
|
34
|
-
| WebSocketClearingHouseConfiguration;
|
|
35
|
-
|
|
36
|
-
export type TxSenderType = 'default' | 'retry';
|
|
37
|
-
|
|
38
|
-
type BaseTxSenderConfig = {
|
|
39
|
-
type: TxSenderType;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
type DefaultTxSenderConfig = BaseTxSenderConfig;
|
|
43
|
-
|
|
44
|
-
type RetryTxSenderConfig = BaseTxSenderConfig & {
|
|
45
|
-
timeout?: number;
|
|
46
|
-
retrySleep?: number;
|
|
47
|
-
additionalConnections?: Connection[];
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
type TxSenderConfig = DefaultTxSenderConfig | RetryTxSenderConfig;
|
|
51
|
-
|
|
52
|
-
export function getWebSocketClearingHouseConfig(
|
|
53
|
-
connection: Connection,
|
|
54
|
-
wallet: IWallet,
|
|
55
|
-
programID: PublicKey,
|
|
56
|
-
opts: ConfirmOptions = AnchorProvider.defaultOptions(),
|
|
57
|
-
txSenderConfig?: TxSenderConfig
|
|
58
|
-
): WebSocketClearingHouseConfiguration {
|
|
59
|
-
return {
|
|
60
|
-
type: 'websocket',
|
|
61
|
-
connection,
|
|
62
|
-
wallet,
|
|
63
|
-
programID,
|
|
64
|
-
opts,
|
|
65
|
-
txSenderConfig,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function getPollingClearingHouseConfig(
|
|
70
|
-
connection: Connection,
|
|
71
|
-
wallet: IWallet,
|
|
72
|
-
programID: PublicKey,
|
|
73
|
-
accountLoader: BulkAccountLoader,
|
|
74
|
-
opts: ConfirmOptions = AnchorProvider.defaultOptions(),
|
|
75
|
-
txSenderConfig?: TxSenderConfig
|
|
76
|
-
): PollingClearingHouseConfiguration {
|
|
77
|
-
return {
|
|
78
|
-
type: 'polling',
|
|
79
|
-
connection,
|
|
80
|
-
wallet,
|
|
81
|
-
programID,
|
|
82
|
-
accountLoader,
|
|
83
|
-
opts,
|
|
84
|
-
txSenderConfig,
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function getClearingHouse(config: ClearingHouseConfig): ClearingHouse {
|
|
89
|
-
const provider = new AnchorProvider(
|
|
90
|
-
config.connection,
|
|
91
|
-
config.wallet,
|
|
92
|
-
config.opts
|
|
93
|
-
);
|
|
94
|
-
const program = new Program(
|
|
95
|
-
clearingHouseIDL as Idl,
|
|
96
|
-
config.programID,
|
|
97
|
-
provider
|
|
98
|
-
);
|
|
99
|
-
let accountSubscriber: ClearingHouseAccountSubscriber;
|
|
100
|
-
if (config.type === 'websocket') {
|
|
101
|
-
accountSubscriber = new WebSocketClearingHouseAccountSubscriber(program);
|
|
102
|
-
} else if (config.type === 'polling') {
|
|
103
|
-
accountSubscriber = new PollingClearingHouseAccountSubscriber(
|
|
104
|
-
program,
|
|
105
|
-
(config as PollingClearingHouseConfiguration).accountLoader
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
let txSender: TxSender;
|
|
110
|
-
if (config.txSenderConfig?.type === 'retry') {
|
|
111
|
-
const txSenderConfig = config.txSenderConfig as RetryTxSenderConfig;
|
|
112
|
-
txSender = new RetryTxSender(
|
|
113
|
-
provider,
|
|
114
|
-
txSenderConfig.timeout,
|
|
115
|
-
txSenderConfig.retrySleep,
|
|
116
|
-
txSenderConfig.additionalConnections
|
|
117
|
-
);
|
|
118
|
-
} else {
|
|
119
|
-
txSender = new DefaultTxSender(provider);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return new ClearingHouse(
|
|
123
|
-
config.connection,
|
|
124
|
-
config.wallet,
|
|
125
|
-
program,
|
|
126
|
-
accountSubscriber,
|
|
127
|
-
txSender,
|
|
128
|
-
config.opts
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function getAdmin(config: ClearingHouseConfig): Admin {
|
|
133
|
-
const provider = new AnchorProvider(
|
|
134
|
-
config.connection,
|
|
135
|
-
config.wallet,
|
|
136
|
-
config.opts
|
|
137
|
-
);
|
|
138
|
-
const program = new Program(
|
|
139
|
-
clearingHouseIDL as Idl,
|
|
140
|
-
config.programID,
|
|
141
|
-
provider
|
|
142
|
-
);
|
|
143
|
-
let accountSubscriber: ClearingHouseAccountSubscriber;
|
|
144
|
-
if (config.type === 'websocket') {
|
|
145
|
-
accountSubscriber = new WebSocketClearingHouseAccountSubscriber(program);
|
|
146
|
-
} else if (config.type === 'polling') {
|
|
147
|
-
accountSubscriber = new PollingClearingHouseAccountSubscriber(
|
|
148
|
-
program,
|
|
149
|
-
(config as PollingClearingHouseConfiguration).accountLoader
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
let txSender: TxSender;
|
|
154
|
-
if (config.txSenderConfig?.type === 'retry') {
|
|
155
|
-
const txSenderConfig = config.txSenderConfig as RetryTxSenderConfig;
|
|
156
|
-
txSender = new RetryTxSender(
|
|
157
|
-
provider,
|
|
158
|
-
txSenderConfig.timeout,
|
|
159
|
-
txSenderConfig.retrySleep,
|
|
160
|
-
txSenderConfig.additionalConnections
|
|
161
|
-
);
|
|
162
|
-
} else {
|
|
163
|
-
txSender = new DefaultTxSender(provider);
|
|
164
|
-
}
|
|
165
|
-
return new Admin(
|
|
166
|
-
config.connection,
|
|
167
|
-
config.wallet,
|
|
168
|
-
program,
|
|
169
|
-
accountSubscriber,
|
|
170
|
-
txSender,
|
|
171
|
-
config.opts
|
|
172
|
-
);
|
|
173
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { PublicKey } from '@solana/web3.js';
|
|
2
|
-
import { ClearingHouse } from '../clearingHouse';
|
|
3
|
-
import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
|
|
4
|
-
import { ClearingHouseUser } from '../clearingHouseUser';
|
|
5
|
-
import { UserAccountSubscriber } from '../accounts/types';
|
|
6
|
-
import { WebSocketUserAccountSubscriber } from '../accounts/webSocketUserAccountSubscriber';
|
|
7
|
-
import { PollingUserAccountSubscriber } from '../accounts/pollingUserAccountSubscriber';
|
|
8
|
-
|
|
9
|
-
export type ClearingHouseUserConfigType = 'websocket' | 'polling' | 'custom';
|
|
10
|
-
|
|
11
|
-
type BaseClearingHouseUserConfig = {
|
|
12
|
-
type: ClearingHouseUserConfigType;
|
|
13
|
-
clearingHouse: ClearingHouse;
|
|
14
|
-
authority: PublicKey;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
type WebSocketClearingHouseUserConfig = BaseClearingHouseUserConfig;
|
|
18
|
-
|
|
19
|
-
type PollingClearingHouseUserConfig = BaseClearingHouseUserConfig & {
|
|
20
|
-
accountLoader: BulkAccountLoader;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
type ClearingHouseUserConfig =
|
|
24
|
-
| PollingClearingHouseUserConfig
|
|
25
|
-
| WebSocketClearingHouseUserConfig;
|
|
26
|
-
|
|
27
|
-
export function getWebSocketClearingHouseUserConfig(
|
|
28
|
-
clearingHouse: ClearingHouse,
|
|
29
|
-
authority: PublicKey
|
|
30
|
-
): WebSocketClearingHouseUserConfig {
|
|
31
|
-
return {
|
|
32
|
-
type: 'websocket',
|
|
33
|
-
clearingHouse,
|
|
34
|
-
authority,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function getPollingClearingHouseUserConfig(
|
|
39
|
-
clearingHouse: ClearingHouse,
|
|
40
|
-
authority: PublicKey,
|
|
41
|
-
accountLoader: BulkAccountLoader
|
|
42
|
-
): PollingClearingHouseUserConfig {
|
|
43
|
-
return {
|
|
44
|
-
type: 'polling',
|
|
45
|
-
clearingHouse,
|
|
46
|
-
authority,
|
|
47
|
-
accountLoader,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function getClearingHouseUser(
|
|
52
|
-
config: ClearingHouseUserConfig
|
|
53
|
-
): ClearingHouseUser {
|
|
54
|
-
let accountSubscriber: UserAccountSubscriber;
|
|
55
|
-
if (config.type === 'websocket') {
|
|
56
|
-
accountSubscriber = new WebSocketUserAccountSubscriber(
|
|
57
|
-
config.clearingHouse.program,
|
|
58
|
-
config.authority
|
|
59
|
-
);
|
|
60
|
-
} else if (config.type === 'polling') {
|
|
61
|
-
accountSubscriber = new PollingUserAccountSubscriber(
|
|
62
|
-
config.clearingHouse.program,
|
|
63
|
-
config.authority,
|
|
64
|
-
(config as PollingClearingHouseUserConfig).accountLoader
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return new ClearingHouseUser(
|
|
69
|
-
config.clearingHouse,
|
|
70
|
-
config.authority,
|
|
71
|
-
accountSubscriber
|
|
72
|
-
);
|
|
73
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { MarketsAccount, StateAccount } from '../types';
|
|
2
|
-
import { BN } from '../';
|
|
3
|
-
import { Connection } from '@solana/web3.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* In the case of a levered loss, the exchange first pays out undistributed fees and then the insurance fund.
|
|
7
|
-
* Thus the de facto size of the insurance fund is the amount in the insurance vault plus the sum of each markets
|
|
8
|
-
* undistributed fees.
|
|
9
|
-
*
|
|
10
|
-
* @param connection
|
|
11
|
-
* @param state
|
|
12
|
-
* @param marketsAccount
|
|
13
|
-
* @returns Precision : QUOTE_ASSET_PRECISION
|
|
14
|
-
*/
|
|
15
|
-
export async function calculateInsuranceFundSize(
|
|
16
|
-
connection: Connection,
|
|
17
|
-
state: StateAccount,
|
|
18
|
-
marketsAccount: MarketsAccount
|
|
19
|
-
): Promise<BN> {
|
|
20
|
-
const insuranceVaultPublicKey = state.insuranceVault;
|
|
21
|
-
const insuranceVaultAmount = new BN(
|
|
22
|
-
(
|
|
23
|
-
await connection.getTokenAccountBalance(insuranceVaultPublicKey)
|
|
24
|
-
).value.amount
|
|
25
|
-
);
|
|
26
|
-
return marketsAccount.markets.reduce((insuranceVaultAmount, market) => {
|
|
27
|
-
return insuranceVaultAmount.add(market.amm.totalFee.div(new BN(2)));
|
|
28
|
-
}, insuranceVaultAmount);
|
|
29
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { TxSender } from './types';
|
|
2
|
-
import {
|
|
3
|
-
ConfirmOptions,
|
|
4
|
-
Signer,
|
|
5
|
-
Transaction,
|
|
6
|
-
TransactionSignature,
|
|
7
|
-
} from '@solana/web3.js';
|
|
8
|
-
import { AnchorProvider } from '@project-serum/anchor';
|
|
9
|
-
|
|
10
|
-
export class DefaultTxSender implements TxSender {
|
|
11
|
-
provider: AnchorProvider;
|
|
12
|
-
|
|
13
|
-
public constructor(provider: AnchorProvider) {
|
|
14
|
-
this.provider = provider;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
send(
|
|
18
|
-
tx: Transaction,
|
|
19
|
-
additionalSigners?: Array<Signer>,
|
|
20
|
-
opts?: ConfirmOptions
|
|
21
|
-
): Promise<TransactionSignature> {
|
|
22
|
-
return this.provider.sendAndConfirm(tx, additionalSigners, opts);
|
|
23
|
-
}
|
|
24
|
-
}
|