@drift-labs/sdk 2.26.0-beta.0 → 2.26.0-beta.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/examples/phoenix.ts +40 -0
- package/lib/addresses/pda.d.ts +0 -1
- package/lib/adminClient.d.ts +0 -1
- package/lib/constants/numericConstants.d.ts +59 -61
- package/lib/constants/spotMarkets.d.ts +0 -1
- package/lib/dlob/DLOB.d.ts +0 -1
- package/lib/dlob/DLOBNode.d.ts +0 -1
- package/lib/dlob/NodeList.d.ts +0 -1
- package/lib/driftClient.d.ts +0 -1
- package/lib/factory/bigNum.d.ts +7 -8
- package/lib/idl/drift.json +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/math/amm.d.ts +1 -2
- package/lib/math/auction.d.ts +0 -1
- package/lib/math/conversion.d.ts +1 -2
- package/lib/math/funding.d.ts +0 -1
- package/lib/math/insurance.d.ts +0 -1
- package/lib/math/margin.d.ts +0 -1
- package/lib/math/market.d.ts +0 -1
- package/lib/math/oracles.d.ts +0 -1
- package/lib/math/orders.d.ts +0 -1
- package/lib/math/position.d.ts +0 -1
- package/lib/math/repeg.d.ts +0 -1
- package/lib/math/spotBalance.d.ts +0 -1
- package/lib/math/spotMarket.d.ts +0 -1
- package/lib/math/spotPosition.d.ts +0 -1
- package/lib/math/trade.d.ts +0 -1
- package/lib/math/utils.d.ts +0 -1
- package/lib/oracles/pythClient.d.ts +1 -2
- package/lib/oracles/types.d.ts +0 -1
- package/lib/orderParams.d.ts +0 -1
- package/lib/phoenix/phoenixFulfillmentConfigMap.d.ts +10 -0
- package/lib/phoenix/phoenixFulfillmentConfigMap.js +17 -0
- package/lib/phoenix/phoenixSubscriber.d.ts +34 -0
- package/lib/phoenix/phoenixSubscriber.js +79 -0
- package/lib/serum/serumSubscriber.d.ts +0 -1
- package/lib/tokenFaucet.d.ts +0 -1
- package/lib/types.d.ts +0 -1
- package/lib/user.d.ts +0 -1
- package/package.json +1 -1
- package/src/idl/drift.json +1 -1
- package/src/index.ts +2 -0
- package/src/phoenix/phoenixFulfillmentConfigMap.ts +26 -0
- package/src/phoenix/phoenixSubscriber.ts +156 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Connection, PublicKey } from '@solana/web3.js';
|
|
2
|
+
import { PRICE_PRECISION, PhoenixSubscriber } from '../src';
|
|
3
|
+
import { PROGRAM_ID } from '@ellipsis-labs/phoenix-sdk';
|
|
4
|
+
|
|
5
|
+
export async function listenToBook(): Promise<void> {
|
|
6
|
+
const connection = new Connection('https://api.mainnet-beta.solana.com');
|
|
7
|
+
|
|
8
|
+
const phoenixSubscriber = new PhoenixSubscriber({
|
|
9
|
+
connection,
|
|
10
|
+
programId: PROGRAM_ID,
|
|
11
|
+
marketAddress: new PublicKey(
|
|
12
|
+
'4DoNfFBfF7UokCC2FQzriy7yHK6DY6NVdYpuekQ5pRgg'
|
|
13
|
+
),
|
|
14
|
+
accountSubscription: {
|
|
15
|
+
type: 'websocket',
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
await phoenixSubscriber.subscribe();
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < 10; i++) {
|
|
22
|
+
const bid = phoenixSubscriber.getBestBid().toNumber() / PRICE_PRECISION;
|
|
23
|
+
const ask = phoenixSubscriber.getBestAsk().toNumber() / PRICE_PRECISION;
|
|
24
|
+
console.log(`iter ${i}:`, bid.toFixed(3), '@', ask.toFixed(3));
|
|
25
|
+
await new Promise((r) => setTimeout(r, 2000));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
await phoenixSubscriber.unsubscribe();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
(async function () {
|
|
32
|
+
try {
|
|
33
|
+
await listenToBook();
|
|
34
|
+
} catch (err) {
|
|
35
|
+
console.log('Error: ', err);
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
process.exit(0);
|
|
40
|
+
})();
|
package/lib/addresses/pda.d.ts
CHANGED
package/lib/adminClient.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
1
|
import { PublicKey, TransactionSignature } from '@solana/web3.js';
|
|
3
2
|
import { FeeStructure, OracleGuardRails, OracleSource, ExchangeStatus, MarketStatus, ContractTier, AssetTier, SpotFulfillmentConfigStatus } from './types';
|
|
4
3
|
import { BN } from '@coral-xyz/anchor';
|
|
@@ -1,62 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
5
|
-
export declare const
|
|
6
|
-
export declare const
|
|
7
|
-
export declare const
|
|
8
|
-
export declare const
|
|
9
|
-
export declare const
|
|
10
|
-
export declare const
|
|
11
|
-
export declare const
|
|
12
|
-
export declare const
|
|
13
|
-
export declare const
|
|
14
|
-
export declare const
|
|
15
|
-
export declare const
|
|
16
|
-
export declare const
|
|
17
|
-
export declare const
|
|
18
|
-
export declare const
|
|
19
|
-
export declare const
|
|
20
|
-
export declare const
|
|
21
|
-
export declare const
|
|
22
|
-
export declare const
|
|
23
|
-
export declare const
|
|
24
|
-
export declare const
|
|
25
|
-
export declare const
|
|
26
|
-
export declare const
|
|
27
|
-
export declare const
|
|
28
|
-
export declare const
|
|
29
|
-
export declare const
|
|
30
|
-
export declare const
|
|
31
|
-
export declare const
|
|
32
|
-
export declare const
|
|
33
|
-
export declare const
|
|
34
|
-
export declare const
|
|
35
|
-
export declare const
|
|
36
|
-
export declare const
|
|
37
|
-
export declare const
|
|
38
|
-
export declare const
|
|
39
|
-
export declare const
|
|
40
|
-
export declare const
|
|
41
|
-
export declare const
|
|
42
|
-
export declare const
|
|
43
|
-
export declare const
|
|
44
|
-
export declare const
|
|
45
|
-
export declare const
|
|
46
|
-
export declare const
|
|
47
|
-
export declare const
|
|
48
|
-
export declare const
|
|
49
|
-
export declare const
|
|
50
|
-
export declare const
|
|
51
|
-
export declare const
|
|
52
|
-
export declare const
|
|
53
|
-
export declare const
|
|
54
|
-
export declare const
|
|
55
|
-
export declare const
|
|
56
|
-
export declare const ONE_HOUR: BN;
|
|
57
|
-
export declare const ONE_YEAR: BN;
|
|
1
|
+
export declare const ZERO: any;
|
|
2
|
+
export declare const ONE: any;
|
|
3
|
+
export declare const TWO: any;
|
|
4
|
+
export declare const THREE: any;
|
|
5
|
+
export declare const FOUR: any;
|
|
6
|
+
export declare const FIVE: any;
|
|
7
|
+
export declare const SIX: any;
|
|
8
|
+
export declare const SEVEN: any;
|
|
9
|
+
export declare const EIGHT: any;
|
|
10
|
+
export declare const NINE: any;
|
|
11
|
+
export declare const TEN: any;
|
|
12
|
+
export declare const TEN_THOUSAND: any;
|
|
13
|
+
export declare const BN_MAX: any;
|
|
14
|
+
export declare const TEN_MILLION: any;
|
|
15
|
+
export declare const MAX_LEVERAGE: any;
|
|
16
|
+
export declare const MAX_LEVERAGE_ORDER_SIZE: any;
|
|
17
|
+
export declare const PERCENTAGE_PRECISION_EXP: any;
|
|
18
|
+
export declare const PERCENTAGE_PRECISION: any;
|
|
19
|
+
export declare const CONCENTRATION_PRECISION: any;
|
|
20
|
+
export declare const QUOTE_PRECISION_EXP: any;
|
|
21
|
+
export declare const FUNDING_RATE_BUFFER_PRECISION_EXP: any;
|
|
22
|
+
export declare const PRICE_PRECISION_EXP: any;
|
|
23
|
+
export declare const FUNDING_RATE_PRECISION_EXP: any;
|
|
24
|
+
export declare const PEG_PRECISION_EXP: any;
|
|
25
|
+
export declare const AMM_RESERVE_PRECISION_EXP: any;
|
|
26
|
+
export declare const SPOT_MARKET_RATE_PRECISION_EXP: any;
|
|
27
|
+
export declare const SPOT_MARKET_RATE_PRECISION: any;
|
|
28
|
+
export declare const SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION_EXP: any;
|
|
29
|
+
export declare const SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION: any;
|
|
30
|
+
export declare const SPOT_MARKET_UTILIZATION_PRECISION_EXP: any;
|
|
31
|
+
export declare const SPOT_MARKET_UTILIZATION_PRECISION: any;
|
|
32
|
+
export declare const SPOT_MARKET_WEIGHT_PRECISION: any;
|
|
33
|
+
export declare const SPOT_MARKET_BALANCE_PRECISION_EXP: any;
|
|
34
|
+
export declare const SPOT_MARKET_BALANCE_PRECISION: any;
|
|
35
|
+
export declare const SPOT_MARKET_IMF_PRECISION_EXP: any;
|
|
36
|
+
export declare const SPOT_MARKET_IMF_PRECISION: any;
|
|
37
|
+
export declare const LIQUIDATION_FEE_PRECISION: any;
|
|
38
|
+
export declare const QUOTE_PRECISION: any;
|
|
39
|
+
export declare const PRICE_PRECISION: any;
|
|
40
|
+
export declare const FUNDING_RATE_PRECISION: any;
|
|
41
|
+
export declare const FUNDING_RATE_BUFFER_PRECISION: any;
|
|
42
|
+
export declare const PEG_PRECISION: any;
|
|
43
|
+
export declare const AMM_RESERVE_PRECISION: any;
|
|
44
|
+
export declare const BASE_PRECISION: any;
|
|
45
|
+
export declare const BASE_PRECISION_EXP: any;
|
|
46
|
+
export declare const AMM_TO_QUOTE_PRECISION_RATIO: any;
|
|
47
|
+
export declare const PRICE_DIV_PEG: any;
|
|
48
|
+
export declare const PRICE_TO_QUOTE_PRECISION: any;
|
|
49
|
+
export declare const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO: any;
|
|
50
|
+
export declare const MARGIN_PRECISION: any;
|
|
51
|
+
export declare const BID_ASK_SPREAD_PRECISION: any;
|
|
52
|
+
export declare const LIQUIDATION_PCT_PRECISION: any;
|
|
53
|
+
export declare const FIVE_MINUTE: any;
|
|
54
|
+
export declare const ONE_HOUR: any;
|
|
55
|
+
export declare const ONE_YEAR: any;
|
|
58
56
|
export declare const QUOTE_SPOT_MARKET_INDEX = 0;
|
|
59
|
-
export declare const LAMPORTS_PRECISION:
|
|
60
|
-
export declare const LAMPORTS_EXP:
|
|
61
|
-
export declare const OPEN_ORDER_MARGIN_REQUIREMENT:
|
|
62
|
-
export declare const DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT:
|
|
57
|
+
export declare const LAMPORTS_PRECISION: any;
|
|
58
|
+
export declare const LAMPORTS_EXP: any;
|
|
59
|
+
export declare const OPEN_ORDER_MARGIN_REQUIREMENT: any;
|
|
60
|
+
export declare const DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT: any;
|
package/lib/dlob/DLOB.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
1
|
import { NodeList } from './NodeList';
|
|
3
2
|
import { MarketType, BN, DriftClient, Order, SpotMarketAccount, PerpMarketAccount, OraclePriceData, SlotSubscriber, MarketTypeStr, StateAccount, UserMap, OrderRecord, OrderActionRecord } from '..';
|
|
4
3
|
import { PublicKey } from '@solana/web3.js';
|
package/lib/dlob/DLOBNode.d.ts
CHANGED
package/lib/dlob/NodeList.d.ts
CHANGED
package/lib/driftClient.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
/// <reference types="bn.js" />
|
|
3
2
|
import { AnchorProvider, BN, Program, ProgramAccount } from '@coral-xyz/anchor';
|
|
4
3
|
import { StateAccount, IWallet, PositionDirection, UserAccount, PerpMarketAccount, OrderParams, Order, SpotMarketAccount, SpotPosition, MakerInfo, TakerInfo, OptionalOrderParams, ReferrerInfo, MarketType, TxParams, SerumV3FulfillmentConfigAccount, ReferrerNameAccount, OrderTriggerCondition, PerpMarketExtendedInfo, UserStatsAccount, ModifyOrderParams, PhoenixV1FulfillmentConfigAccount } from './types';
|
|
5
4
|
import * as anchor from '@coral-xyz/anchor';
|
package/lib/factory/bigNum.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
1
|
import { BN } from '@coral-xyz/anchor';
|
|
3
2
|
export declare class BigNum {
|
|
4
3
|
val: BN;
|
|
@@ -44,11 +43,11 @@ export declare class BigNum {
|
|
|
44
43
|
gte(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
|
|
45
44
|
lte(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
|
|
46
45
|
eq(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
|
|
47
|
-
eqZero():
|
|
48
|
-
gtZero():
|
|
49
|
-
ltZero():
|
|
50
|
-
gteZero():
|
|
51
|
-
lteZero():
|
|
46
|
+
eqZero(): any;
|
|
47
|
+
gtZero(): any;
|
|
48
|
+
ltZero(): any;
|
|
49
|
+
gteZero(): any;
|
|
50
|
+
lteZero(): any;
|
|
52
51
|
abs(): BigNum;
|
|
53
52
|
neg(): BigNum;
|
|
54
53
|
toString: (base?: number | 'hex', length?: number) => string;
|
|
@@ -88,8 +87,8 @@ export declare class BigNum {
|
|
|
88
87
|
toNotional(useTradePrecision?: boolean, precisionOverride?: number): string;
|
|
89
88
|
toMillified(precision?: number, rounded?: boolean): string;
|
|
90
89
|
toJSON(): {
|
|
91
|
-
val:
|
|
92
|
-
precision:
|
|
90
|
+
val: any;
|
|
91
|
+
precision: any;
|
|
93
92
|
};
|
|
94
93
|
isNeg(): boolean;
|
|
95
94
|
isPos(): boolean;
|
package/lib/idl/drift.json
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -53,6 +53,8 @@ export * from './config';
|
|
|
53
53
|
export * from './constants/numericConstants';
|
|
54
54
|
export * from './serum/serumSubscriber';
|
|
55
55
|
export * from './serum/serumFulfillmentConfigMap';
|
|
56
|
+
export * from './phoenix/phoenixSubscriber';
|
|
57
|
+
export * from './phoenix/phoenixFulfillmentConfigMap';
|
|
56
58
|
export * from './tx/retryTxSender';
|
|
57
59
|
export * from './util/computeUnits';
|
|
58
60
|
export * from './util/tps';
|
package/lib/index.js
CHANGED
|
@@ -77,6 +77,8 @@ __exportStar(require("./config"), exports);
|
|
|
77
77
|
__exportStar(require("./constants/numericConstants"), exports);
|
|
78
78
|
__exportStar(require("./serum/serumSubscriber"), exports);
|
|
79
79
|
__exportStar(require("./serum/serumFulfillmentConfigMap"), exports);
|
|
80
|
+
__exportStar(require("./phoenix/phoenixSubscriber"), exports);
|
|
81
|
+
__exportStar(require("./phoenix/phoenixFulfillmentConfigMap"), exports);
|
|
80
82
|
__exportStar(require("./tx/retryTxSender"), exports);
|
|
81
83
|
__exportStar(require("./util/computeUnits"), exports);
|
|
82
84
|
__exportStar(require("./util/tps"), exports);
|
package/lib/math/amm.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
1
|
import { BN } from '@coral-xyz/anchor';
|
|
3
2
|
import { AMM, PositionDirection, SwapDirection, PerpMarketAccount } from '../types';
|
|
4
3
|
import { OraclePriceData } from '../oracles/types';
|
|
@@ -87,7 +86,7 @@ export declare function getSwapDirection(inputAssetType: AssetType, positionDire
|
|
|
87
86
|
* @param market
|
|
88
87
|
* @returns cost : Precision PRICE_PRECISION
|
|
89
88
|
*/
|
|
90
|
-
export declare function calculateTerminalPrice(market: PerpMarketAccount):
|
|
89
|
+
export declare function calculateTerminalPrice(market: PerpMarketAccount): any;
|
|
91
90
|
export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, limit_price: BN, direction: PositionDirection, oraclePriceData?: OraclePriceData, now?: BN): [BN, PositionDirection];
|
|
92
91
|
export declare function calculateQuoteAssetAmountSwapped(quoteAssetReserves: BN, pegMultiplier: BN, swapDirection: SwapDirection): BN;
|
|
93
92
|
export declare function calculateMaxBaseAssetAmountFillable(amm: AMM, orderDirection: PositionDirection): BN;
|
package/lib/math/auction.d.ts
CHANGED
package/lib/math/conversion.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const convertToNumber: (bigNumber: BN, precision?: BN) => number;
|
|
1
|
+
export declare const convertToNumber: (bigNumber: BN, precision?: BN) => any;
|
package/lib/math/funding.d.ts
CHANGED
package/lib/math/insurance.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
1
|
import { BN } from '../index';
|
|
3
2
|
export declare function stakeAmountToShares(amount: BN, totalIfShares: BN, insuranceFundVaultBalance: BN): BN;
|
|
4
3
|
export declare function unstakeSharesToAmount(nShares: BN, totalIfShares: BN, insuranceFundVaultBalance: BN): BN;
|
package/lib/math/margin.d.ts
CHANGED
package/lib/math/market.d.ts
CHANGED
package/lib/math/oracles.d.ts
CHANGED
package/lib/math/orders.d.ts
CHANGED
package/lib/math/position.d.ts
CHANGED
package/lib/math/repeg.d.ts
CHANGED
package/lib/math/spotMarket.d.ts
CHANGED
package/lib/math/trade.d.ts
CHANGED
package/lib/math/utils.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
1
|
/// <reference types="node" />
|
|
3
2
|
import { Connection, PublicKey } from '@solana/web3.js';
|
|
4
3
|
import { OracleClient, OraclePriceData } from './types';
|
|
@@ -7,7 +6,7 @@ export declare class PythClient implements OracleClient {
|
|
|
7
6
|
private connection;
|
|
8
7
|
private multiple;
|
|
9
8
|
private stableCoin;
|
|
10
|
-
constructor(connection: Connection, multiple?:
|
|
9
|
+
constructor(connection: Connection, multiple?: any, stableCoin?: boolean);
|
|
11
10
|
getOraclePriceData(pricePublicKey: PublicKey): Promise<OraclePriceData>;
|
|
12
11
|
getOraclePriceDataFromBuffer(buffer: Buffer): OraclePriceData;
|
|
13
12
|
}
|
package/lib/oracles/types.d.ts
CHANGED
package/lib/orderParams.d.ts
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PublicKey } from '@solana/web3.js';
|
|
2
|
+
import { PhoenixV1FulfillmentConfigAccount } from '../types';
|
|
3
|
+
import { DriftClient } from '../driftClient';
|
|
4
|
+
export declare class PhoenixFulfillmentConfigMap {
|
|
5
|
+
driftClient: DriftClient;
|
|
6
|
+
map: Map<number, PhoenixV1FulfillmentConfigAccount>;
|
|
7
|
+
constructor(driftClient: DriftClient);
|
|
8
|
+
add(marketIndex: number, phoenixMarketAddress: PublicKey): Promise<void>;
|
|
9
|
+
get(marketIndex: number): PhoenixV1FulfillmentConfigAccount;
|
|
10
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PhoenixFulfillmentConfigMap = void 0;
|
|
4
|
+
class PhoenixFulfillmentConfigMap {
|
|
5
|
+
constructor(driftClient) {
|
|
6
|
+
this.map = new Map();
|
|
7
|
+
this.driftClient = driftClient;
|
|
8
|
+
}
|
|
9
|
+
async add(marketIndex, phoenixMarketAddress) {
|
|
10
|
+
const account = await this.driftClient.getPhoenixV1FulfillmentConfig(phoenixMarketAddress);
|
|
11
|
+
this.map.set(marketIndex, account);
|
|
12
|
+
}
|
|
13
|
+
get(marketIndex) {
|
|
14
|
+
return this.map.get(marketIndex);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.PhoenixFulfillmentConfigMap = PhoenixFulfillmentConfigMap;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Connection, PublicKey } from '@solana/web3.js';
|
|
2
|
+
import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
|
|
3
|
+
import { MarketData, Client } from '@ellipsis-labs/phoenix-sdk';
|
|
4
|
+
import { BN } from '@coral-xyz/anchor';
|
|
5
|
+
export type PhoenixMarketSubscriberConfig = {
|
|
6
|
+
connection: Connection;
|
|
7
|
+
programId: PublicKey;
|
|
8
|
+
marketAddress: PublicKey;
|
|
9
|
+
accountSubscription: {
|
|
10
|
+
type: 'polling';
|
|
11
|
+
accountLoader: BulkAccountLoader;
|
|
12
|
+
} | {
|
|
13
|
+
type: 'websocket';
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export declare class PhoenixSubscriber {
|
|
17
|
+
connection: Connection;
|
|
18
|
+
client: Client;
|
|
19
|
+
programId: PublicKey;
|
|
20
|
+
marketAddress: PublicKey;
|
|
21
|
+
subscriptionType: 'polling' | 'websocket';
|
|
22
|
+
accountLoader: BulkAccountLoader | undefined;
|
|
23
|
+
market: MarketData;
|
|
24
|
+
marketCallbackId: string | number;
|
|
25
|
+
clockCallbackId: string | number;
|
|
26
|
+
subscribed: boolean;
|
|
27
|
+
lastSlot: number;
|
|
28
|
+
lastUnixTimestamp: number;
|
|
29
|
+
constructor(config: PhoenixMarketSubscriberConfig);
|
|
30
|
+
subscribe(): Promise<void>;
|
|
31
|
+
getBestBid(): BN;
|
|
32
|
+
getBestAsk(): BN;
|
|
33
|
+
unsubscribe(): Promise<void>;
|
|
34
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PhoenixSubscriber = void 0;
|
|
4
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
5
|
+
const phoenix_sdk_1 = require("@ellipsis-labs/phoenix-sdk");
|
|
6
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
7
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
8
|
+
class PhoenixSubscriber {
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.connection = config.connection;
|
|
11
|
+
this.programId = config.programId;
|
|
12
|
+
this.marketAddress = config.marketAddress;
|
|
13
|
+
if (config.accountSubscription.type === 'polling') {
|
|
14
|
+
this.subscriptionType = 'polling';
|
|
15
|
+
this.accountLoader = config.accountSubscription.accountLoader;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
this.subscriptionType = 'websocket';
|
|
19
|
+
}
|
|
20
|
+
this.lastSlot = 0;
|
|
21
|
+
this.lastUnixTimestamp = 0;
|
|
22
|
+
}
|
|
23
|
+
async subscribe() {
|
|
24
|
+
if (this.subscribed) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
this.market = (0, phoenix_sdk_1.deserializeMarketData)((await this.connection.getAccountInfo(this.marketAddress, 'confirmed'))
|
|
28
|
+
.data);
|
|
29
|
+
const clock = (0, phoenix_sdk_1.deserializeClockData)((await this.connection.getAccountInfo(web3_js_1.SYSVAR_CLOCK_PUBKEY, 'confirmed'))
|
|
30
|
+
.data);
|
|
31
|
+
this.lastUnixTimestamp = (0, phoenix_sdk_1.toNum)(clock.unixTimestamp);
|
|
32
|
+
if (this.subscriptionType === 'websocket') {
|
|
33
|
+
this.marketCallbackId = this.connection.onAccountChange(this.marketAddress, (accountInfo, _ctx) => {
|
|
34
|
+
this.market = (0, phoenix_sdk_1.deserializeMarketData)(accountInfo.data);
|
|
35
|
+
});
|
|
36
|
+
this.clockCallbackId = this.connection.onAccountChange(web3_js_1.SYSVAR_CLOCK_PUBKEY, (accountInfo, ctx) => {
|
|
37
|
+
this.lastSlot = ctx.slot;
|
|
38
|
+
const clock = (0, phoenix_sdk_1.deserializeClockData)(accountInfo.data);
|
|
39
|
+
this.lastUnixTimestamp = (0, phoenix_sdk_1.toNum)(clock.unixTimestamp);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
this.marketCallbackId = await this.accountLoader.addAccount(this.marketAddress, (buffer, slot) => {
|
|
44
|
+
this.lastSlot = slot;
|
|
45
|
+
this.market = (0, phoenix_sdk_1.deserializeMarketData)(buffer);
|
|
46
|
+
});
|
|
47
|
+
this.clockCallbackId = await this.accountLoader.addAccount(web3_js_1.SYSVAR_CLOCK_PUBKEY, (buffer, slot) => {
|
|
48
|
+
this.lastSlot = slot;
|
|
49
|
+
const clock = (0, phoenix_sdk_1.deserializeClockData)(buffer);
|
|
50
|
+
this.lastUnixTimestamp = (0, phoenix_sdk_1.toNum)(clock.unixTimestamp);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
this.subscribed = true;
|
|
54
|
+
}
|
|
55
|
+
getBestBid() {
|
|
56
|
+
const ladder = (0, phoenix_sdk_1.getMarketUiLadder)(this.market, this.lastSlot, this.lastUnixTimestamp, 1);
|
|
57
|
+
return new anchor_1.BN(Math.floor(ladder.bids[0][0] * numericConstants_1.PRICE_PRECISION.toNumber()));
|
|
58
|
+
}
|
|
59
|
+
getBestAsk() {
|
|
60
|
+
const ladder = (0, phoenix_sdk_1.getMarketUiLadder)(this.market, this.lastSlot, this.lastUnixTimestamp, 1);
|
|
61
|
+
return new anchor_1.BN(Math.floor(ladder.asks[0][0] * numericConstants_1.PRICE_PRECISION.toNumber()));
|
|
62
|
+
}
|
|
63
|
+
async unsubscribe() {
|
|
64
|
+
if (!this.subscribed) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
// remove listeners
|
|
68
|
+
if (this.subscriptionType === 'websocket') {
|
|
69
|
+
await this.connection.removeAccountChangeListener(this.marketCallbackId);
|
|
70
|
+
await this.connection.removeAccountChangeListener(this.clockCallbackId);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
this.accountLoader.removeAccount(this.marketAddress, this.marketCallbackId);
|
|
74
|
+
this.accountLoader.removeAccount(web3_js_1.SYSVAR_CLOCK_PUBKEY, this.clockCallbackId);
|
|
75
|
+
}
|
|
76
|
+
this.subscribed = false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.PhoenixSubscriber = PhoenixSubscriber;
|
package/lib/tokenFaucet.d.ts
CHANGED
package/lib/types.d.ts
CHANGED
package/lib/user.d.ts
CHANGED
package/package.json
CHANGED
package/src/idl/drift.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -55,6 +55,8 @@ export * from './config';
|
|
|
55
55
|
export * from './constants/numericConstants';
|
|
56
56
|
export * from './serum/serumSubscriber';
|
|
57
57
|
export * from './serum/serumFulfillmentConfigMap';
|
|
58
|
+
export * from './phoenix/phoenixSubscriber';
|
|
59
|
+
export * from './phoenix/phoenixFulfillmentConfigMap';
|
|
58
60
|
export * from './tx/retryTxSender';
|
|
59
61
|
export * from './util/computeUnits';
|
|
60
62
|
export * from './util/tps';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { PublicKey } from '@solana/web3.js';
|
|
2
|
+
import { PhoenixV1FulfillmentConfigAccount } from '../types';
|
|
3
|
+
import { DriftClient } from '../driftClient';
|
|
4
|
+
|
|
5
|
+
export class PhoenixFulfillmentConfigMap {
|
|
6
|
+
driftClient: DriftClient;
|
|
7
|
+
map = new Map<number, PhoenixV1FulfillmentConfigAccount>();
|
|
8
|
+
|
|
9
|
+
public constructor(driftClient: DriftClient) {
|
|
10
|
+
this.driftClient = driftClient;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public async add(
|
|
14
|
+
marketIndex: number,
|
|
15
|
+
phoenixMarketAddress: PublicKey
|
|
16
|
+
): Promise<void> {
|
|
17
|
+
const account = await this.driftClient.getPhoenixV1FulfillmentConfig(
|
|
18
|
+
phoenixMarketAddress
|
|
19
|
+
);
|
|
20
|
+
this.map.set(marketIndex, account);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public get(marketIndex: number): PhoenixV1FulfillmentConfigAccount {
|
|
24
|
+
return this.map.get(marketIndex);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { Connection, PublicKey, SYSVAR_CLOCK_PUBKEY } from '@solana/web3.js';
|
|
2
|
+
import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
|
|
3
|
+
import {
|
|
4
|
+
MarketData,
|
|
5
|
+
Client,
|
|
6
|
+
deserializeMarketData,
|
|
7
|
+
deserializeClockData,
|
|
8
|
+
toNum,
|
|
9
|
+
getMarketUiLadder,
|
|
10
|
+
} from '@ellipsis-labs/phoenix-sdk';
|
|
11
|
+
import { PRICE_PRECISION } from '../constants/numericConstants';
|
|
12
|
+
import { BN } from '@coral-xyz/anchor';
|
|
13
|
+
|
|
14
|
+
export type PhoenixMarketSubscriberConfig = {
|
|
15
|
+
connection: Connection;
|
|
16
|
+
programId: PublicKey;
|
|
17
|
+
marketAddress: PublicKey;
|
|
18
|
+
accountSubscription:
|
|
19
|
+
| {
|
|
20
|
+
// enables use to add web sockets in the future
|
|
21
|
+
type: 'polling';
|
|
22
|
+
accountLoader: BulkAccountLoader;
|
|
23
|
+
}
|
|
24
|
+
| {
|
|
25
|
+
type: 'websocket';
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export class PhoenixSubscriber {
|
|
30
|
+
connection: Connection;
|
|
31
|
+
client: Client;
|
|
32
|
+
programId: PublicKey;
|
|
33
|
+
marketAddress: PublicKey;
|
|
34
|
+
subscriptionType: 'polling' | 'websocket';
|
|
35
|
+
accountLoader: BulkAccountLoader | undefined;
|
|
36
|
+
market: MarketData;
|
|
37
|
+
marketCallbackId: string | number;
|
|
38
|
+
clockCallbackId: string | number;
|
|
39
|
+
|
|
40
|
+
subscribed: boolean;
|
|
41
|
+
lastSlot: number;
|
|
42
|
+
lastUnixTimestamp: number;
|
|
43
|
+
|
|
44
|
+
public constructor(config: PhoenixMarketSubscriberConfig) {
|
|
45
|
+
this.connection = config.connection;
|
|
46
|
+
this.programId = config.programId;
|
|
47
|
+
this.marketAddress = config.marketAddress;
|
|
48
|
+
if (config.accountSubscription.type === 'polling') {
|
|
49
|
+
this.subscriptionType = 'polling';
|
|
50
|
+
this.accountLoader = config.accountSubscription.accountLoader;
|
|
51
|
+
} else {
|
|
52
|
+
this.subscriptionType = 'websocket';
|
|
53
|
+
}
|
|
54
|
+
this.lastSlot = 0;
|
|
55
|
+
this.lastUnixTimestamp = 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public async subscribe(): Promise<void> {
|
|
59
|
+
if (this.subscribed) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this.market = deserializeMarketData(
|
|
64
|
+
(await this.connection.getAccountInfo(this.marketAddress, 'confirmed'))
|
|
65
|
+
.data
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const clock = deserializeClockData(
|
|
69
|
+
(await this.connection.getAccountInfo(SYSVAR_CLOCK_PUBKEY, 'confirmed'))
|
|
70
|
+
.data
|
|
71
|
+
);
|
|
72
|
+
this.lastUnixTimestamp = toNum(clock.unixTimestamp);
|
|
73
|
+
|
|
74
|
+
if (this.subscriptionType === 'websocket') {
|
|
75
|
+
this.marketCallbackId = this.connection.onAccountChange(
|
|
76
|
+
this.marketAddress,
|
|
77
|
+
(accountInfo, _ctx) => {
|
|
78
|
+
this.market = deserializeMarketData(accountInfo.data);
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
this.clockCallbackId = this.connection.onAccountChange(
|
|
82
|
+
SYSVAR_CLOCK_PUBKEY,
|
|
83
|
+
(accountInfo, ctx) => {
|
|
84
|
+
this.lastSlot = ctx.slot;
|
|
85
|
+
const clock = deserializeClockData(accountInfo.data);
|
|
86
|
+
this.lastUnixTimestamp = toNum(clock.unixTimestamp);
|
|
87
|
+
}
|
|
88
|
+
);
|
|
89
|
+
} else {
|
|
90
|
+
this.marketCallbackId = await this.accountLoader.addAccount(
|
|
91
|
+
this.marketAddress,
|
|
92
|
+
(buffer, slot) => {
|
|
93
|
+
this.lastSlot = slot;
|
|
94
|
+
this.market = deserializeMarketData(buffer);
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
this.clockCallbackId = await this.accountLoader.addAccount(
|
|
98
|
+
SYSVAR_CLOCK_PUBKEY,
|
|
99
|
+
(buffer, slot) => {
|
|
100
|
+
this.lastSlot = slot;
|
|
101
|
+
const clock = deserializeClockData(buffer);
|
|
102
|
+
this.lastUnixTimestamp = toNum(clock.unixTimestamp);
|
|
103
|
+
}
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
this.subscribed = true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public getBestBid(): BN {
|
|
111
|
+
const ladder = getMarketUiLadder(
|
|
112
|
+
this.market,
|
|
113
|
+
this.lastSlot,
|
|
114
|
+
this.lastUnixTimestamp,
|
|
115
|
+
1
|
|
116
|
+
);
|
|
117
|
+
return new BN(Math.floor(ladder.bids[0][0] * PRICE_PRECISION.toNumber()));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public getBestAsk(): BN {
|
|
121
|
+
const ladder = getMarketUiLadder(
|
|
122
|
+
this.market,
|
|
123
|
+
this.lastSlot,
|
|
124
|
+
this.lastUnixTimestamp,
|
|
125
|
+
1
|
|
126
|
+
);
|
|
127
|
+
return new BN(Math.floor(ladder.asks[0][0] * PRICE_PRECISION.toNumber()));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
public async unsubscribe(): Promise<void> {
|
|
131
|
+
if (!this.subscribed) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// remove listeners
|
|
136
|
+
if (this.subscriptionType === 'websocket') {
|
|
137
|
+
await this.connection.removeAccountChangeListener(
|
|
138
|
+
this.marketCallbackId as number
|
|
139
|
+
);
|
|
140
|
+
await this.connection.removeAccountChangeListener(
|
|
141
|
+
this.clockCallbackId as number
|
|
142
|
+
);
|
|
143
|
+
} else {
|
|
144
|
+
this.accountLoader.removeAccount(
|
|
145
|
+
this.marketAddress,
|
|
146
|
+
this.marketCallbackId as string
|
|
147
|
+
);
|
|
148
|
+
this.accountLoader.removeAccount(
|
|
149
|
+
SYSVAR_CLOCK_PUBKEY,
|
|
150
|
+
this.clockCallbackId as string
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
this.subscribed = false;
|
|
155
|
+
}
|
|
156
|
+
}
|