@drift-labs/sdk 2.10.0-beta.2 → 2.10.0-beta.3
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 +2 -1
- package/lib/accounts/types.d.ts +5 -4
- package/lib/config.d.ts +2 -2
- package/lib/constants/perpMarkets.d.ts +1 -1
- package/lib/constants/spotMarkets.d.ts +1 -1
- package/lib/dlob/DLOB.d.ts +4 -4
- package/lib/dlob/DLOBNode.d.ts +2 -2
- package/lib/dlob/DLOBOrders.d.ts +2 -2
- package/lib/dlob/NodeList.d.ts +1 -1
- package/lib/driftClient.d.ts +1 -1
- package/lib/driftClient.js +6 -6
- package/lib/driftClientConfig.d.ts +3 -3
- package/lib/events/fetchLogs.d.ts +2 -2
- package/lib/events/types.d.ts +13 -13
- package/lib/factory/bigNum.js +4 -4
- package/lib/idl/drift.json +1 -1
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/trade.d.ts +1 -1
- package/lib/oracles/types.d.ts +2 -2
- package/lib/serum/types.d.ts +1 -1
- package/lib/slot/SlotSubscriber.d.ts +1 -1
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +43 -43
- package/lib/user.js +6 -6
- package/lib/userConfig.d.ts +2 -2
- package/lib/userMap/userMap.js +3 -0
- package/lib/userMap/userStatsMap.js +3 -0
- package/lib/userStatsConfig.d.ts +2 -2
- package/package.json +1 -1
- package/src/driftClientConfig.ts +1 -1
- package/src/idl/drift.json +1 -1
- package/src/userMap/userMap.ts +2 -0
- package/src/userMap/userStatsMap.ts +2 -0
- package/src/assert/assert.js +0 -9
- package/src/examples/makeTradeExample.js +0 -157
- package/src/token/index.js +0 -38
- package/src/tx/types.js +0 -2
- package/src/tx/utils.js +0 -17
- package/src/util/computeUnits.js +0 -27
- package/src/util/getTokenAddress.js +0 -9
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -27
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
2
3
|
import { Commitment, Connection, PublicKey } from '@solana/web3.js';
|
|
3
4
|
import { BufferAndSlot } from './types';
|
|
4
|
-
|
|
5
|
+
type AccountToLoad = {
|
|
5
6
|
publicKey: PublicKey;
|
|
6
7
|
callbacks: Map<string, (buffer: Buffer, slot: number) => void>;
|
|
7
8
|
};
|
package/lib/accounts/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
2
3
|
import { SpotMarketAccount, PerpMarketAccount, OracleSource, StateAccount, UserAccount, UserStatsAccount } from '../types';
|
|
3
4
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
4
5
|
import { EventEmitter } from 'events';
|
|
@@ -78,23 +79,23 @@ export interface OracleAccountSubscriber {
|
|
|
78
79
|
unsubscribe(): Promise<void>;
|
|
79
80
|
getOraclePriceData(): DataAndSlot<OraclePriceData>;
|
|
80
81
|
}
|
|
81
|
-
export
|
|
82
|
+
export type AccountToPoll = {
|
|
82
83
|
key: string;
|
|
83
84
|
publicKey: PublicKey;
|
|
84
85
|
eventType: string;
|
|
85
86
|
callbackId?: string;
|
|
86
87
|
mapKey?: number;
|
|
87
88
|
};
|
|
88
|
-
export
|
|
89
|
+
export type OraclesToPoll = {
|
|
89
90
|
publicKey: PublicKey;
|
|
90
91
|
source: OracleSource;
|
|
91
92
|
callbackId?: string;
|
|
92
93
|
};
|
|
93
|
-
export
|
|
94
|
+
export type BufferAndSlot = {
|
|
94
95
|
slot: number;
|
|
95
96
|
buffer: Buffer | undefined;
|
|
96
97
|
};
|
|
97
|
-
export
|
|
98
|
+
export type DataAndSlot<T> = {
|
|
98
99
|
data: T;
|
|
99
100
|
slot: number;
|
|
100
101
|
};
|
package/lib/config.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { PerpMarketConfig } from './constants/perpMarkets';
|
|
2
2
|
import { SpotMarketConfig } from './constants/spotMarkets';
|
|
3
3
|
import { OracleInfo } from './oracles/types';
|
|
4
|
-
|
|
4
|
+
type DriftConfig = {
|
|
5
5
|
ENV: DriftEnv;
|
|
6
6
|
PYTH_ORACLE_MAPPING_ADDRESS: string;
|
|
7
7
|
DRIFT_PROGRAM_ID: string;
|
|
@@ -11,7 +11,7 @@ declare type DriftConfig = {
|
|
|
11
11
|
PERP_MARKETS: PerpMarketConfig[];
|
|
12
12
|
SPOT_MARKETS: SpotMarketConfig[];
|
|
13
13
|
};
|
|
14
|
-
export
|
|
14
|
+
export type DriftEnv = 'devnet' | 'mainnet-beta';
|
|
15
15
|
export declare const configs: {
|
|
16
16
|
[key in DriftEnv]: DriftConfig;
|
|
17
17
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { BN, DriftEnv, OracleSource } from '../';
|
|
4
|
-
export
|
|
4
|
+
export type SpotMarketConfig = {
|
|
5
5
|
symbol: string;
|
|
6
6
|
marketIndex: number;
|
|
7
7
|
oracle: PublicKey;
|
package/lib/dlob/DLOB.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { MarketType, BN, DriftClient, Order, SpotMarketAccount, PerpMarketAccoun
|
|
|
4
4
|
import { PublicKey } from '@solana/web3.js';
|
|
5
5
|
import { DLOBNode, DLOBNodeType, TriggerOrderNode } from '..';
|
|
6
6
|
import { DLOBOrders } from './DLOBOrders';
|
|
7
|
-
export
|
|
7
|
+
export type MarketNodeLists = {
|
|
8
8
|
limit: {
|
|
9
9
|
ask: NodeList<'limit'>;
|
|
10
10
|
bid: NodeList<'limit'>;
|
|
@@ -22,12 +22,12 @@ export declare type MarketNodeLists = {
|
|
|
22
22
|
below: NodeList<'trigger'>;
|
|
23
23
|
};
|
|
24
24
|
};
|
|
25
|
-
|
|
26
|
-
export
|
|
25
|
+
type OrderBookCallback = () => void;
|
|
26
|
+
export type NodeToFill = {
|
|
27
27
|
node: DLOBNode;
|
|
28
28
|
makerNode?: DLOBNode;
|
|
29
29
|
};
|
|
30
|
-
export
|
|
30
|
+
export type NodeToTrigger = {
|
|
31
31
|
node: TriggerOrderNode;
|
|
32
32
|
};
|
|
33
33
|
export declare class DLOB {
|
package/lib/dlob/DLOBNode.d.ts
CHANGED
|
@@ -42,11 +42,11 @@ export declare class TriggerOrderNode extends OrderNode {
|
|
|
42
42
|
previous?: TriggerOrderNode;
|
|
43
43
|
getSortValue(order: Order): BN;
|
|
44
44
|
}
|
|
45
|
-
export
|
|
45
|
+
export type DLOBNodeMap = {
|
|
46
46
|
limit: LimitOrderNode;
|
|
47
47
|
floatingLimit: FloatingLimitOrderNode;
|
|
48
48
|
market: MarketOrderNode;
|
|
49
49
|
trigger: TriggerOrderNode;
|
|
50
50
|
};
|
|
51
|
-
export
|
|
51
|
+
export type DLOBNodeType = 'limit' | 'floatingLimit' | 'market' | ('trigger' & keyof DLOBNodeMap);
|
|
52
52
|
export declare function createNode<T extends DLOBNodeType>(nodeType: T, order: Order, userAccount: PublicKey): DLOBNodeMap[T];
|
package/lib/dlob/DLOBOrders.d.ts
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { Idl } from '@project-serum/anchor';
|
|
4
4
|
import { Order } from '../types';
|
|
5
|
-
export
|
|
5
|
+
export type DLOBOrder = {
|
|
6
6
|
user: PublicKey;
|
|
7
7
|
order: Order;
|
|
8
8
|
};
|
|
9
|
-
export
|
|
9
|
+
export type DLOBOrders = DLOBOrder[];
|
|
10
10
|
export declare class DLOBOrdersCoder {
|
|
11
11
|
private idl;
|
|
12
12
|
constructor(idl: Idl);
|
package/lib/dlob/NodeList.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { BN, MarketTypeStr, Order } from '..';
|
|
3
3
|
import { PublicKey } from '@solana/web3.js';
|
|
4
4
|
import { DLOBNode, DLOBNodeMap } from './DLOBNode';
|
|
5
|
-
export
|
|
5
|
+
export type SortDirection = 'asc' | 'desc';
|
|
6
6
|
export declare function getOrderSignature(orderId: number, userAccount: PublicKey): string;
|
|
7
7
|
export interface DLOBNodeGenerator {
|
|
8
8
|
getGenerator(): Generator<DLOBNode>;
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ import { DriftClientConfig } from './driftClientConfig';
|
|
|
14
14
|
import { User } from './user';
|
|
15
15
|
import { UserSubscriptionConfig } from './userConfig';
|
|
16
16
|
import { UserStats } from './userStats';
|
|
17
|
-
|
|
17
|
+
type RemainingAccountParams = {
|
|
18
18
|
userAccounts: UserAccount[];
|
|
19
19
|
writablePerpMarketIndexes?: number[];
|
|
20
20
|
writableSpotMarketIndexes?: number[];
|
package/lib/driftClient.js
CHANGED
|
@@ -53,6 +53,12 @@ const spotPosition_1 = require("./math/spotPosition");
|
|
|
53
53
|
* This class is the main way to interact with Drift Protocol. It allows you to subscribe to the various accounts where the Market's state is stored, as well as: opening positions, liquidating, settling funding, depositing & withdrawing, and more.
|
|
54
54
|
*/
|
|
55
55
|
class DriftClient {
|
|
56
|
+
get isSubscribed() {
|
|
57
|
+
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
58
|
+
}
|
|
59
|
+
set isSubscribed(val) {
|
|
60
|
+
this._isSubscribed = val;
|
|
61
|
+
}
|
|
56
62
|
constructor(config) {
|
|
57
63
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
58
64
|
this.users = new Map();
|
|
@@ -106,12 +112,6 @@ class DriftClient {
|
|
|
106
112
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
107
113
|
this.txSender = new retryTxSender_1.RetryTxSender(this.provider, (_f = config.txSenderConfig) === null || _f === void 0 ? void 0 : _f.timeout, (_g = config.txSenderConfig) === null || _g === void 0 ? void 0 : _g.retrySleep, (_h = config.txSenderConfig) === null || _h === void 0 ? void 0 : _h.additionalConnections);
|
|
108
114
|
}
|
|
109
|
-
get isSubscribed() {
|
|
110
|
-
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
111
|
-
}
|
|
112
|
-
set isSubscribed(val) {
|
|
113
|
-
this._isSubscribed = val;
|
|
114
|
-
}
|
|
115
115
|
createUsers(subAccountIds, accountSubscriptionConfig) {
|
|
116
116
|
for (const subAccountId of subAccountIds) {
|
|
117
117
|
const user = this.createUser(subAccountId, accountSubscriptionConfig);
|
|
@@ -3,7 +3,7 @@ import { IWallet } from './types';
|
|
|
3
3
|
import { OracleInfo } from './oracles/types';
|
|
4
4
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
5
5
|
import { DriftEnv } from './config';
|
|
6
|
-
export
|
|
6
|
+
export type DriftClientConfig = {
|
|
7
7
|
connection: Connection;
|
|
8
8
|
wallet: IWallet;
|
|
9
9
|
programID: PublicKey;
|
|
@@ -19,13 +19,13 @@ export declare type DriftClientConfig = {
|
|
|
19
19
|
userStats?: boolean;
|
|
20
20
|
authority?: PublicKey;
|
|
21
21
|
};
|
|
22
|
-
|
|
22
|
+
export type DriftClientSubscriptionConfig = {
|
|
23
23
|
type: 'websocket';
|
|
24
24
|
} | {
|
|
25
25
|
type: 'polling';
|
|
26
26
|
accountLoader: BulkAccountLoader;
|
|
27
27
|
};
|
|
28
|
-
|
|
28
|
+
type TxSenderConfig = {
|
|
29
29
|
type: 'retry';
|
|
30
30
|
timeout?: number;
|
|
31
31
|
retrySleep?: number;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Program } from '@project-serum/anchor';
|
|
2
2
|
import { Connection, Finality, PublicKey, TransactionResponse, TransactionSignature } from '@solana/web3.js';
|
|
3
3
|
import { WrappedEvents } from './types';
|
|
4
|
-
|
|
4
|
+
type Log = {
|
|
5
5
|
txSig: TransactionSignature;
|
|
6
6
|
slot: number;
|
|
7
7
|
logs: string[];
|
|
8
8
|
};
|
|
9
|
-
|
|
9
|
+
type FetchLogsResponse = {
|
|
10
10
|
earliestTx: string;
|
|
11
11
|
mostRecentTx: string;
|
|
12
12
|
earliestSlot: number;
|
package/lib/events/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Commitment, TransactionSignature } from '@solana/web3.js';
|
|
2
2
|
import { DepositRecord, FundingPaymentRecord, FundingRateRecord, LiquidationRecord, NewUserRecord, OrderActionRecord, OrderRecord, SettlePnlRecord, LPRecord, InsuranceFundRecord, SpotInterestRecord, InsuranceFundStakeRecord, CurveRecord } from '../index';
|
|
3
|
-
export
|
|
3
|
+
export type EventSubscriptionOptions = {
|
|
4
4
|
eventTypes?: EventType[];
|
|
5
5
|
maxEventsPerType?: number;
|
|
6
6
|
orderBy?: EventSubscriptionOrderBy;
|
|
@@ -11,17 +11,17 @@ export declare type EventSubscriptionOptions = {
|
|
|
11
11
|
untilTx?: TransactionSignature;
|
|
12
12
|
};
|
|
13
13
|
export declare const DefaultEventSubscriptionOptions: EventSubscriptionOptions;
|
|
14
|
-
export
|
|
15
|
-
export
|
|
16
|
-
export
|
|
14
|
+
export type EventSubscriptionOrderBy = 'blockchain' | 'client';
|
|
15
|
+
export type EventSubscriptionOrderDirection = 'asc' | 'desc';
|
|
16
|
+
export type Event<T> = T & {
|
|
17
17
|
txSig: TransactionSignature;
|
|
18
18
|
slot: number;
|
|
19
19
|
};
|
|
20
|
-
export
|
|
20
|
+
export type WrappedEvent<Type extends EventType> = EventMap[Type] & {
|
|
21
21
|
eventType: Type;
|
|
22
22
|
};
|
|
23
|
-
export
|
|
24
|
-
export
|
|
23
|
+
export type WrappedEvents = WrappedEvent<EventType>[];
|
|
24
|
+
export type EventMap = {
|
|
25
25
|
DepositRecord: Event<DepositRecord>;
|
|
26
26
|
FundingPaymentRecord: Event<FundingPaymentRecord>;
|
|
27
27
|
LiquidationRecord: Event<LiquidationRecord>;
|
|
@@ -36,22 +36,22 @@ export declare type EventMap = {
|
|
|
36
36
|
InsuranceFundStakeRecord: Event<InsuranceFundStakeRecord>;
|
|
37
37
|
CurveRecord: Event<CurveRecord>;
|
|
38
38
|
};
|
|
39
|
-
export
|
|
39
|
+
export type EventType = keyof EventMap;
|
|
40
40
|
export interface EventSubscriberEvents {
|
|
41
41
|
newEvent: (event: WrappedEvent<EventType>) => void;
|
|
42
42
|
}
|
|
43
|
-
export
|
|
44
|
-
export
|
|
43
|
+
export type SortFn = (currentRecord: EventMap[EventType], newRecord: EventMap[EventType]) => 'less than' | 'greater than';
|
|
44
|
+
export type logProviderCallback = (txSig: TransactionSignature, slot: number, logs: string[], mostRecentBlockTime: number | undefined) => void;
|
|
45
45
|
export interface LogProvider {
|
|
46
46
|
isSubscribed(): boolean;
|
|
47
47
|
subscribe(callback: logProviderCallback, skipHistory?: boolean): boolean;
|
|
48
48
|
unsubscribe(): Promise<boolean>;
|
|
49
49
|
}
|
|
50
|
-
export
|
|
50
|
+
export type WebSocketLogProviderConfig = {
|
|
51
51
|
type: 'websocket';
|
|
52
52
|
};
|
|
53
|
-
export
|
|
53
|
+
export type PollingLogProviderConfig = {
|
|
54
54
|
type: 'polling';
|
|
55
55
|
frequency: number;
|
|
56
56
|
};
|
|
57
|
-
export
|
|
57
|
+
export type LogProviderConfig = WebSocketLogProviderConfig | PollingLogProviderConfig;
|
package/lib/factory/bigNum.js
CHANGED
|
@@ -5,15 +5,15 @@ const anchor_1 = require("@project-serum/anchor");
|
|
|
5
5
|
const assert_1 = require("../assert/assert");
|
|
6
6
|
const numericConstants_1 = require("./../constants/numericConstants");
|
|
7
7
|
class BigNum {
|
|
8
|
+
static setLocale(locale) {
|
|
9
|
+
BigNum.delim = (1.1).toLocaleString(locale).slice(1, 2) || '.';
|
|
10
|
+
BigNum.spacer = (1000).toLocaleString(locale).slice(1, 2) || ',';
|
|
11
|
+
}
|
|
8
12
|
constructor(val, precisionVal = new anchor_1.BN(0)) {
|
|
9
13
|
this.toString = (base, length) => this.val.toString(base, length);
|
|
10
14
|
this.val = new anchor_1.BN(val);
|
|
11
15
|
this.precision = new anchor_1.BN(precisionVal);
|
|
12
16
|
}
|
|
13
|
-
static setLocale(locale) {
|
|
14
|
-
BigNum.delim = (1.1).toLocaleString(locale).slice(1, 2) || '.';
|
|
15
|
-
BigNum.spacer = (1000).toLocaleString(locale).slice(1, 2) || ',';
|
|
16
|
-
}
|
|
17
17
|
bigNumFromParam(bn) {
|
|
18
18
|
return anchor_1.BN.isBN(bn) ? BigNum.from(bn) : bn;
|
|
19
19
|
}
|
package/lib/idl/drift.json
CHANGED
package/lib/math/amm.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export declare function calculateBidAskPrice(amm: AMM, oraclePriceData: OraclePr
|
|
|
22
22
|
* @returns price : Precision PRICE_PRECISION
|
|
23
23
|
*/
|
|
24
24
|
export declare function calculatePrice(baseAssetReserves: BN, quoteAssetReserves: BN, pegMultiplier: BN): BN;
|
|
25
|
-
export
|
|
25
|
+
export type AssetType = 'quote' | 'base';
|
|
26
26
|
/**
|
|
27
27
|
* Calculates what the amm reserves would be after swapping a quote or base asset amount.
|
|
28
28
|
*
|
package/lib/math/trade.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { PerpMarketAccount, PositionDirection } from '../types';
|
|
|
3
3
|
import { BN } from '@project-serum/anchor';
|
|
4
4
|
import { AssetType } from './amm';
|
|
5
5
|
import { OraclePriceData } from '../oracles/types';
|
|
6
|
-
export
|
|
6
|
+
export type PriceImpactUnit = 'entryPrice' | 'maxPrice' | 'priceDelta' | 'priceDeltaAsNumber' | 'pctAvg' | 'pctMax' | 'quoteAssetAmount' | 'quoteAssetAmountPeg' | 'acquiredBaseAssetAmount' | 'acquiredQuoteAssetAmount' | 'all';
|
|
7
7
|
/**
|
|
8
8
|
* Calculates avg/max slippage (price impact) for candidate trade
|
|
9
9
|
* @param direction
|
package/lib/oracles/types.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { BN } from '@project-serum/anchor';
|
|
4
4
|
import { PublicKey } from '@solana/web3.js';
|
|
5
5
|
import { OracleSource } from '../types';
|
|
6
|
-
export
|
|
6
|
+
export type OraclePriceData = {
|
|
7
7
|
price: BN;
|
|
8
8
|
slot: BN;
|
|
9
9
|
confidence: BN;
|
|
@@ -11,7 +11,7 @@ export declare type OraclePriceData = {
|
|
|
11
11
|
twap?: BN;
|
|
12
12
|
twapConfidence?: BN;
|
|
13
13
|
};
|
|
14
|
-
export
|
|
14
|
+
export type OracleInfo = {
|
|
15
15
|
publicKey: PublicKey;
|
|
16
16
|
source: OracleSource;
|
|
17
17
|
};
|
package/lib/serum/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Connection, PublicKey } from '@solana/web3.js';
|
|
2
2
|
import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
|
|
3
|
-
export
|
|
3
|
+
export type SerumMarketSubscriberConfig = {
|
|
4
4
|
connection: Connection;
|
|
5
5
|
programId: PublicKey;
|
|
6
6
|
marketAddress: PublicKey;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { Connection } from '@solana/web3.js';
|
|
3
3
|
import { EventEmitter } from 'events';
|
|
4
4
|
import StrictEventEmitter from 'strict-event-emitter-types/types/src';
|
|
5
|
-
|
|
5
|
+
type SlotSubscriberConfig = {};
|
|
6
6
|
export interface SlotSubscriberEvents {
|
|
7
7
|
newSlot: (newSlot: number) => void;
|
|
8
8
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { TxSender, TxSigAndSlot } from './types';
|
|
3
3
|
import { Commitment, ConfirmOptions, RpcResponseAndContext, Signer, SignatureResult, Transaction, TransactionSignature, Connection } from '@solana/web3.js';
|
|
4
4
|
import { AnchorProvider } from '@project-serum/anchor';
|
|
5
|
-
|
|
5
|
+
type ResolveReference = {
|
|
6
6
|
resolve?: () => void;
|
|
7
7
|
};
|
|
8
8
|
export declare class RetryTxSender implements TxSender {
|
package/lib/tx/types.d.ts
CHANGED
package/lib/types.d.ts
CHANGED
|
@@ -317,8 +317,8 @@ export declare enum TradeSide {
|
|
|
317
317
|
Buy = 1,
|
|
318
318
|
Sell = 2
|
|
319
319
|
}
|
|
320
|
-
export
|
|
321
|
-
export
|
|
320
|
+
export type CandleResolution = '1' | '5' | '15' | '60' | '240' | 'D' | 'W' | 'M';
|
|
321
|
+
export type NewUserRecord = {
|
|
322
322
|
ts: BN;
|
|
323
323
|
userAuthority: PublicKey;
|
|
324
324
|
user: PublicKey;
|
|
@@ -326,7 +326,7 @@ export declare type NewUserRecord = {
|
|
|
326
326
|
name: number[];
|
|
327
327
|
referrer: PublicKey;
|
|
328
328
|
};
|
|
329
|
-
export
|
|
329
|
+
export type DepositRecord = {
|
|
330
330
|
ts: BN;
|
|
331
331
|
userAuthority: PublicKey;
|
|
332
332
|
user: PublicKey;
|
|
@@ -347,7 +347,7 @@ export declare type DepositRecord = {
|
|
|
347
347
|
explanation: DepositExplanation;
|
|
348
348
|
transferUser?: PublicKey;
|
|
349
349
|
};
|
|
350
|
-
export
|
|
350
|
+
export type SpotInterestRecord = {
|
|
351
351
|
ts: BN;
|
|
352
352
|
marketIndex: number;
|
|
353
353
|
depositBalance: BN;
|
|
@@ -358,7 +358,7 @@ export declare type SpotInterestRecord = {
|
|
|
358
358
|
optimalBorrowRate: number;
|
|
359
359
|
maxBorrowRate: number;
|
|
360
360
|
};
|
|
361
|
-
export
|
|
361
|
+
export type CurveRecord = {
|
|
362
362
|
ts: BN;
|
|
363
363
|
recordId: BN;
|
|
364
364
|
marketIndex: number;
|
|
@@ -406,7 +406,7 @@ export declare type InsuranceFundStakeRecord = {
|
|
|
406
406
|
userIfSharesAfter: BN;
|
|
407
407
|
totalIfSharesAfter: BN;
|
|
408
408
|
};
|
|
409
|
-
export
|
|
409
|
+
export type LPRecord = {
|
|
410
410
|
ts: BN;
|
|
411
411
|
user: PublicKey;
|
|
412
412
|
action: LPAction;
|
|
@@ -427,7 +427,7 @@ export declare class LPAction {
|
|
|
427
427
|
settleLiquidity: {};
|
|
428
428
|
};
|
|
429
429
|
}
|
|
430
|
-
export
|
|
430
|
+
export type FundingRateRecord = {
|
|
431
431
|
ts: BN;
|
|
432
432
|
recordId: BN;
|
|
433
433
|
marketIndex: number;
|
|
@@ -442,7 +442,7 @@ export declare type FundingRateRecord = {
|
|
|
442
442
|
baseAssetAmountWithAmm: BN;
|
|
443
443
|
baseAssetAmountWithUnsettledLp: BN;
|
|
444
444
|
};
|
|
445
|
-
export
|
|
445
|
+
export type FundingPaymentRecord = {
|
|
446
446
|
ts: BN;
|
|
447
447
|
userAuthority: PublicKey;
|
|
448
448
|
user: PublicKey;
|
|
@@ -453,7 +453,7 @@ export declare type FundingPaymentRecord = {
|
|
|
453
453
|
ammCumulativeFundingLong: BN;
|
|
454
454
|
ammCumulativeFundingShort: BN;
|
|
455
455
|
};
|
|
456
|
-
export
|
|
456
|
+
export type LiquidationRecord = {
|
|
457
457
|
ts: BN;
|
|
458
458
|
user: PublicKey;
|
|
459
459
|
liquidator: PublicKey;
|
|
@@ -491,7 +491,7 @@ export declare class LiquidationType {
|
|
|
491
491
|
liquidateSpot: {};
|
|
492
492
|
};
|
|
493
493
|
}
|
|
494
|
-
export
|
|
494
|
+
export type LiquidatePerpRecord = {
|
|
495
495
|
marketIndex: number;
|
|
496
496
|
oraclePrice: BN;
|
|
497
497
|
baseAssetAmount: BN;
|
|
@@ -503,7 +503,7 @@ export declare type LiquidatePerpRecord = {
|
|
|
503
503
|
liquidatorFee: BN;
|
|
504
504
|
ifFee: BN;
|
|
505
505
|
};
|
|
506
|
-
export
|
|
506
|
+
export type LiquidateSpotRecord = {
|
|
507
507
|
assetMarketIndex: number;
|
|
508
508
|
assetPrice: BN;
|
|
509
509
|
assetTransfer: BN;
|
|
@@ -512,7 +512,7 @@ export declare type LiquidateSpotRecord = {
|
|
|
512
512
|
liabilityTransfer: BN;
|
|
513
513
|
ifFee: BN;
|
|
514
514
|
};
|
|
515
|
-
export
|
|
515
|
+
export type LiquidateBorrowForPerpPnlRecord = {
|
|
516
516
|
perpMarketIndex: number;
|
|
517
517
|
marketOraclePrice: BN;
|
|
518
518
|
pnlTransfer: BN;
|
|
@@ -520,7 +520,7 @@ export declare type LiquidateBorrowForPerpPnlRecord = {
|
|
|
520
520
|
liabilityPrice: BN;
|
|
521
521
|
liabilityTransfer: BN;
|
|
522
522
|
};
|
|
523
|
-
export
|
|
523
|
+
export type LiquidatePerpPnlForDepositRecord = {
|
|
524
524
|
perpMarketIndex: number;
|
|
525
525
|
marketOraclePrice: BN;
|
|
526
526
|
pnlTransfer: BN;
|
|
@@ -528,7 +528,7 @@ export declare type LiquidatePerpPnlForDepositRecord = {
|
|
|
528
528
|
assetPrice: BN;
|
|
529
529
|
assetTransfer: BN;
|
|
530
530
|
};
|
|
531
|
-
export
|
|
531
|
+
export type PerpBankruptcyRecord = {
|
|
532
532
|
marketIndex: number;
|
|
533
533
|
pnl: BN;
|
|
534
534
|
ifPayment: BN;
|
|
@@ -536,13 +536,13 @@ export declare type PerpBankruptcyRecord = {
|
|
|
536
536
|
clawbackUserPayment: BN | null;
|
|
537
537
|
cumulativeFundingRateDelta: BN;
|
|
538
538
|
};
|
|
539
|
-
export
|
|
539
|
+
export type SpotBankruptcyRecord = {
|
|
540
540
|
marketIndex: number;
|
|
541
541
|
borrowAmount: BN;
|
|
542
542
|
cumulativeDepositInterestDelta: BN;
|
|
543
543
|
ifPayment: BN;
|
|
544
544
|
};
|
|
545
|
-
export
|
|
545
|
+
export type SettlePnlRecord = {
|
|
546
546
|
ts: BN;
|
|
547
547
|
user: PublicKey;
|
|
548
548
|
marketIndex: number;
|
|
@@ -553,12 +553,12 @@ export declare type SettlePnlRecord = {
|
|
|
553
553
|
settlePrice: BN;
|
|
554
554
|
explanation: SettlePnlExplanation;
|
|
555
555
|
};
|
|
556
|
-
export
|
|
556
|
+
export type OrderRecord = {
|
|
557
557
|
ts: BN;
|
|
558
558
|
user: PublicKey;
|
|
559
559
|
order: Order;
|
|
560
560
|
};
|
|
561
|
-
export
|
|
561
|
+
export type OrderActionRecord = {
|
|
562
562
|
ts: BN;
|
|
563
563
|
action: OrderAction;
|
|
564
564
|
actionExplanation: OrderActionExplanation;
|
|
@@ -588,7 +588,7 @@ export declare type OrderActionRecord = {
|
|
|
588
588
|
makerOrderCumulativeQuoteAssetAmountFilled: BN | null;
|
|
589
589
|
oraclePrice: BN;
|
|
590
590
|
};
|
|
591
|
-
export
|
|
591
|
+
export type StateAccount = {
|
|
592
592
|
admin: PublicKey;
|
|
593
593
|
exchangeStatus: ExchangeStatus;
|
|
594
594
|
whitelistMint: PublicKey;
|
|
@@ -612,7 +612,7 @@ export declare type StateAccount = {
|
|
|
612
612
|
initialPctToLiquidate: number;
|
|
613
613
|
liquidationDuration: number;
|
|
614
614
|
};
|
|
615
|
-
export
|
|
615
|
+
export type PerpMarketAccount = {
|
|
616
616
|
status: MarketStatus;
|
|
617
617
|
contractType: ContractType;
|
|
618
618
|
contractTier: ContractTier;
|
|
@@ -645,7 +645,7 @@ export declare type PerpMarketAccount = {
|
|
|
645
645
|
quoteMaxInsurance: BN;
|
|
646
646
|
};
|
|
647
647
|
};
|
|
648
|
-
export
|
|
648
|
+
export type HistoricalOracleData = {
|
|
649
649
|
lastOraclePrice: BN;
|
|
650
650
|
lastOracleDelay: BN;
|
|
651
651
|
lastOracleConf: BN;
|
|
@@ -653,14 +653,14 @@ export declare type HistoricalOracleData = {
|
|
|
653
653
|
lastOraclePriceTwap5Min: BN;
|
|
654
654
|
lastOraclePriceTwapTs: BN;
|
|
655
655
|
};
|
|
656
|
-
export
|
|
656
|
+
export type HistoricalIndexData = {
|
|
657
657
|
lastIndexBidPrice: BN;
|
|
658
658
|
lastIndexAskPrice: BN;
|
|
659
659
|
lastIndexPriceTwap: BN;
|
|
660
660
|
lastIndexPriceTwap5Min: BN;
|
|
661
661
|
lastIndexPriceTwapTs: BN;
|
|
662
662
|
};
|
|
663
|
-
export
|
|
663
|
+
export type SpotMarketAccount = {
|
|
664
664
|
status: MarketStatus;
|
|
665
665
|
assetTier: AssetTier;
|
|
666
666
|
marketIndex: number;
|
|
@@ -715,11 +715,11 @@ export declare type SpotMarketAccount = {
|
|
|
715
715
|
totalSpotFee: BN;
|
|
716
716
|
ordersEnabled: boolean;
|
|
717
717
|
};
|
|
718
|
-
export
|
|
718
|
+
export type PoolBalance = {
|
|
719
719
|
scaledBalance: BN;
|
|
720
720
|
marketIndex: number;
|
|
721
721
|
};
|
|
722
|
-
export
|
|
722
|
+
export type AMM = {
|
|
723
723
|
baseAssetReserve: BN;
|
|
724
724
|
sqrtK: BN;
|
|
725
725
|
cumulativeFundingRate: BN;
|
|
@@ -797,7 +797,7 @@ export declare type AMM = {
|
|
|
797
797
|
askBaseAssetReserve: BN;
|
|
798
798
|
askQuoteAssetReserve: BN;
|
|
799
799
|
};
|
|
800
|
-
export
|
|
800
|
+
export type PerpPosition = {
|
|
801
801
|
baseAssetAmount: BN;
|
|
802
802
|
lastCumulativeFundingRate: BN;
|
|
803
803
|
marketIndex: number;
|
|
@@ -813,7 +813,7 @@ export declare type PerpPosition = {
|
|
|
813
813
|
lastBaseAssetAmountPerLp: BN;
|
|
814
814
|
lastQuoteAssetAmountPerLp: BN;
|
|
815
815
|
};
|
|
816
|
-
export
|
|
816
|
+
export type UserStatsAccount = {
|
|
817
817
|
numberOfSubAccounts: number;
|
|
818
818
|
numberOfSubAccountsCreated: number;
|
|
819
819
|
makerVolume30D: BN;
|
|
@@ -835,7 +835,7 @@ export declare type UserStatsAccount = {
|
|
|
835
835
|
authority: PublicKey;
|
|
836
836
|
ifStakedQuoteAssetAmount: BN;
|
|
837
837
|
};
|
|
838
|
-
export
|
|
838
|
+
export type UserAccount = {
|
|
839
839
|
authority: PublicKey;
|
|
840
840
|
delegate: PublicKey;
|
|
841
841
|
name: number[];
|
|
@@ -857,7 +857,7 @@ export declare type UserAccount = {
|
|
|
857
857
|
liquidationStartSlot: BN;
|
|
858
858
|
isMarginTradingEnabled: boolean;
|
|
859
859
|
};
|
|
860
|
-
export
|
|
860
|
+
export type SpotPosition = {
|
|
861
861
|
marketIndex: number;
|
|
862
862
|
balanceType: SpotBalanceType;
|
|
863
863
|
scaledBalance: BN;
|
|
@@ -866,7 +866,7 @@ export declare type SpotPosition = {
|
|
|
866
866
|
openAsks: BN;
|
|
867
867
|
cumulativeDeposits: BN;
|
|
868
868
|
};
|
|
869
|
-
export
|
|
869
|
+
export type Order = {
|
|
870
870
|
status: OrderStatus;
|
|
871
871
|
orderType: OrderType;
|
|
872
872
|
marketType: MarketType;
|
|
@@ -892,7 +892,7 @@ export declare type Order = {
|
|
|
892
892
|
auctionEndPrice: BN;
|
|
893
893
|
maxTs: BN;
|
|
894
894
|
};
|
|
895
|
-
export
|
|
895
|
+
export type OrderParams = {
|
|
896
896
|
orderType: OrderType;
|
|
897
897
|
marketType: MarketType;
|
|
898
898
|
userOrderId: number;
|
|
@@ -912,29 +912,29 @@ export declare type OrderParams = {
|
|
|
912
912
|
auctionStartPrice: BN | null;
|
|
913
913
|
auctionEndPrice: BN | null;
|
|
914
914
|
};
|
|
915
|
-
export
|
|
915
|
+
export type NecessaryOrderParams = {
|
|
916
916
|
orderType: OrderType;
|
|
917
917
|
marketIndex: number;
|
|
918
918
|
baseAssetAmount: BN;
|
|
919
919
|
direction: PositionDirection;
|
|
920
920
|
};
|
|
921
|
-
export
|
|
921
|
+
export type OptionalOrderParams = {
|
|
922
922
|
[Property in keyof OrderParams]?: OrderParams[Property];
|
|
923
923
|
} & NecessaryOrderParams;
|
|
924
924
|
export declare const DefaultOrderParams: OrderParams;
|
|
925
|
-
export
|
|
925
|
+
export type MakerInfo = {
|
|
926
926
|
maker: PublicKey;
|
|
927
927
|
makerStats: PublicKey;
|
|
928
928
|
makerUserAccount: UserAccount;
|
|
929
929
|
order: Order;
|
|
930
930
|
};
|
|
931
|
-
export
|
|
931
|
+
export type TakerInfo = {
|
|
932
932
|
taker: PublicKey;
|
|
933
933
|
takerStats: PublicKey;
|
|
934
934
|
takerUserAccount: UserAccount;
|
|
935
935
|
order: Order;
|
|
936
936
|
};
|
|
937
|
-
export
|
|
937
|
+
export type ReferrerInfo = {
|
|
938
938
|
referrer: PublicKey;
|
|
939
939
|
referrerStats: PublicKey;
|
|
940
940
|
};
|
|
@@ -943,7 +943,7 @@ export interface IWallet {
|
|
|
943
943
|
signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;
|
|
944
944
|
publicKey: PublicKey;
|
|
945
945
|
}
|
|
946
|
-
export
|
|
946
|
+
export type FeeStructure = {
|
|
947
947
|
feeTiers: FeeTier[];
|
|
948
948
|
makerRebateNumerator: BN;
|
|
949
949
|
makerRebateDenominator: BN;
|
|
@@ -951,7 +951,7 @@ export declare type FeeStructure = {
|
|
|
951
951
|
flatFillerFee: BN;
|
|
952
952
|
referrerRewardEpochUpperBound: BN;
|
|
953
953
|
};
|
|
954
|
-
export
|
|
954
|
+
export type FeeTier = {
|
|
955
955
|
feeNumerator: number;
|
|
956
956
|
feeDenominator: number;
|
|
957
957
|
makerRebateNumerator: number;
|
|
@@ -961,12 +961,12 @@ export declare type FeeTier = {
|
|
|
961
961
|
refereeFeeNumerator: number;
|
|
962
962
|
refereeFeeDenominator: number;
|
|
963
963
|
};
|
|
964
|
-
export
|
|
964
|
+
export type OrderFillerRewardStructure = {
|
|
965
965
|
rewardNumerator: BN;
|
|
966
966
|
rewardDenominator: BN;
|
|
967
967
|
timeBasedRewardLowerBound: BN;
|
|
968
968
|
};
|
|
969
|
-
export
|
|
969
|
+
export type OracleGuardRails = {
|
|
970
970
|
priceDivergence: {
|
|
971
971
|
markOracleDivergenceNumerator: BN;
|
|
972
972
|
markOracleDivergenceDenominator: BN;
|
|
@@ -978,8 +978,8 @@ export declare type OracleGuardRails = {
|
|
|
978
978
|
tooVolatileRatio: BN;
|
|
979
979
|
};
|
|
980
980
|
};
|
|
981
|
-
export
|
|
982
|
-
export
|
|
981
|
+
export type MarginCategory = 'Initial' | 'Maintenance';
|
|
982
|
+
export type InsuranceFundStake = {
|
|
983
983
|
marketIndex: number;
|
|
984
984
|
authority: PublicKey;
|
|
985
985
|
ifShares: BN;
|
|
@@ -988,7 +988,7 @@ export declare type InsuranceFundStake = {
|
|
|
988
988
|
lastWithdrawRequestValue: BN;
|
|
989
989
|
lastWithdrawRequestTs: BN;
|
|
990
990
|
};
|
|
991
|
-
export
|
|
991
|
+
export type SerumV3FulfillmentConfigAccount = {
|
|
992
992
|
fulfillmentType: SpotFulfillmentType;
|
|
993
993
|
status: SpotFulfillmentStatus;
|
|
994
994
|
pubkey: PublicKey;
|
package/lib/user.js
CHANGED
|
@@ -12,6 +12,12 @@ const pollingUserAccountSubscriber_1 = require("./accounts/pollingUserAccountSub
|
|
|
12
12
|
const webSocketUserAccountSubscriber_1 = require("./accounts/webSocketUserAccountSubscriber");
|
|
13
13
|
const spotPosition_1 = require("./math/spotPosition");
|
|
14
14
|
class User {
|
|
15
|
+
get isSubscribed() {
|
|
16
|
+
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
17
|
+
}
|
|
18
|
+
set isSubscribed(val) {
|
|
19
|
+
this._isSubscribed = val;
|
|
20
|
+
}
|
|
15
21
|
constructor(config) {
|
|
16
22
|
var _a;
|
|
17
23
|
this._isSubscribed = false;
|
|
@@ -25,12 +31,6 @@ class User {
|
|
|
25
31
|
}
|
|
26
32
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
27
33
|
}
|
|
28
|
-
get isSubscribed() {
|
|
29
|
-
return this._isSubscribed && this.accountSubscriber.isSubscribed;
|
|
30
|
-
}
|
|
31
|
-
set isSubscribed(val) {
|
|
32
|
-
this._isSubscribed = val;
|
|
33
|
-
}
|
|
34
34
|
/**
|
|
35
35
|
* Subscribe to User state accounts
|
|
36
36
|
* @returns SusbcriptionSuccess result
|
package/lib/userConfig.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { DriftClient } from './driftClient';
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
4
|
-
export
|
|
4
|
+
export type UserConfig = {
|
|
5
5
|
accountSubscription?: UserSubscriptionConfig;
|
|
6
6
|
driftClient: DriftClient;
|
|
7
7
|
userAccountPublicKey: PublicKey;
|
|
8
8
|
};
|
|
9
|
-
export
|
|
9
|
+
export type UserSubscriptionConfig = {
|
|
10
10
|
type: 'websocket';
|
|
11
11
|
} | {
|
|
12
12
|
type: 'polling';
|
package/lib/userMap/userMap.js
CHANGED
|
@@ -26,6 +26,9 @@ class UserMap {
|
|
|
26
26
|
if (this.accountSubscription.type === 'polling') {
|
|
27
27
|
await (0, __1.bulkPollingUserSubscribe)(userArray, this.accountSubscription.accountLoader);
|
|
28
28
|
}
|
|
29
|
+
else {
|
|
30
|
+
await Promise.all(userArray.map((user) => user.subscribe()));
|
|
31
|
+
}
|
|
29
32
|
for (const user of userArray) {
|
|
30
33
|
this.userMap.set(user.getUserAccountPublicKey().toString(), user);
|
|
31
34
|
}
|
|
@@ -30,6 +30,9 @@ class UserStatsMap {
|
|
|
30
30
|
if (this.accountSubscription.type === 'polling') {
|
|
31
31
|
await (0, __1.bulkPollingUserStatsSubscribe)(userStatArray, this.accountSubscription.accountLoader);
|
|
32
32
|
}
|
|
33
|
+
else {
|
|
34
|
+
await Promise.all(userStatArray.map((userStat) => userStat.subscribe()));
|
|
35
|
+
}
|
|
33
36
|
for (const userStat of userStatArray) {
|
|
34
37
|
this.userStatsMap.set(userStat.getAccount().authority.toString(), userStat);
|
|
35
38
|
}
|
package/lib/userStatsConfig.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { DriftClient } from './driftClient';
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
4
|
-
export
|
|
4
|
+
export type UserStatsConfig = {
|
|
5
5
|
accountSubscription?: UserStatsSubscriptionConfig;
|
|
6
6
|
driftClient: DriftClient;
|
|
7
7
|
userStatsAccountPublicKey: PublicKey;
|
|
8
8
|
};
|
|
9
|
-
export
|
|
9
|
+
export type UserStatsSubscriptionConfig = {
|
|
10
10
|
type: 'websocket';
|
|
11
11
|
} | {
|
|
12
12
|
type: 'polling';
|
package/package.json
CHANGED
package/src/driftClientConfig.ts
CHANGED
package/src/idl/drift.json
CHANGED
package/src/userMap/userMap.ts
CHANGED
package/src/assert/assert.js
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
var __awaiter =
|
|
3
|
-
(this && this.__awaiter) ||
|
|
4
|
-
function (thisArg, _arguments, P, generator) {
|
|
5
|
-
function adopt(value) {
|
|
6
|
-
return value instanceof P
|
|
7
|
-
? value
|
|
8
|
-
: new P(function (resolve) {
|
|
9
|
-
resolve(value);
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
-
function fulfilled(value) {
|
|
14
|
-
try {
|
|
15
|
-
step(generator.next(value));
|
|
16
|
-
} catch (e) {
|
|
17
|
-
reject(e);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function rejected(value) {
|
|
21
|
-
try {
|
|
22
|
-
step(generator['throw'](value));
|
|
23
|
-
} catch (e) {
|
|
24
|
-
reject(e);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
function step(result) {
|
|
28
|
-
result.done
|
|
29
|
-
? resolve(result.value)
|
|
30
|
-
: adopt(result.value).then(fulfilled, rejected);
|
|
31
|
-
}
|
|
32
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
36
|
-
exports.getTokenAddress = void 0;
|
|
37
|
-
const anchor_1 = require('@project-serum/anchor');
|
|
38
|
-
const __1 = require('..');
|
|
39
|
-
const spl_token_1 = require('@solana/spl-token');
|
|
40
|
-
const web3_js_1 = require('@solana/web3.js');
|
|
41
|
-
const __2 = require('..');
|
|
42
|
-
const banks_1 = require('../constants/spotMarkets');
|
|
43
|
-
const getTokenAddress = (mintAddress, userPubKey) => {
|
|
44
|
-
return spl_token_1.Token.getAssociatedTokenAddress(
|
|
45
|
-
new web3_js_1.PublicKey(`ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL`),
|
|
46
|
-
spl_token_1.TOKEN_PROGRAM_ID,
|
|
47
|
-
new web3_js_1.PublicKey(mintAddress),
|
|
48
|
-
new web3_js_1.PublicKey(userPubKey)
|
|
49
|
-
);
|
|
50
|
-
};
|
|
51
|
-
exports.getTokenAddress = getTokenAddress;
|
|
52
|
-
const main = () =>
|
|
53
|
-
__awaiter(void 0, void 0, void 0, function* () {
|
|
54
|
-
// Initialize Drift SDK
|
|
55
|
-
const sdkConfig = __2.initialize({ env: 'devnet' });
|
|
56
|
-
// Set up the Wallet and Provider
|
|
57
|
-
const privateKey = process.env.BOT_PRIVATE_KEY; // stored as an array string
|
|
58
|
-
const keypair = web3_js_1.Keypair.fromSecretKey(
|
|
59
|
-
Uint8Array.from(JSON.parse(privateKey))
|
|
60
|
-
);
|
|
61
|
-
const wallet = new __1.Wallet(keypair);
|
|
62
|
-
// Set up the Connection
|
|
63
|
-
const rpcAddress = process.env.RPC_ADDRESS; // can use: https://api.devnet.solana.com for devnet; https://api.mainnet-beta.solana.com for mainnet;
|
|
64
|
-
const connection = new web3_js_1.Connection(rpcAddress);
|
|
65
|
-
// Set up the Provider
|
|
66
|
-
const provider = new anchor_1.AnchorProvider(
|
|
67
|
-
connection,
|
|
68
|
-
wallet,
|
|
69
|
-
anchor_1.AnchorProvider.defaultOptions()
|
|
70
|
-
);
|
|
71
|
-
// Check SOL Balance
|
|
72
|
-
const lamportsBalance = yield connection.getBalance(wallet.publicKey);
|
|
73
|
-
console.log('SOL balance:', lamportsBalance / Math.pow(10, 9));
|
|
74
|
-
// Misc. other things to set up
|
|
75
|
-
const usdcTokenAddress = yield exports.getTokenAddress(
|
|
76
|
-
sdkConfig.USDC_MINT_ADDRESS,
|
|
77
|
-
wallet.publicKey.toString()
|
|
78
|
-
);
|
|
79
|
-
// Set up the Drift Clearing House
|
|
80
|
-
const clearingHousePublicKey = new web3_js_1.PublicKey(
|
|
81
|
-
sdkConfig.DRIFT_PROGRAM_ID
|
|
82
|
-
);
|
|
83
|
-
const clearingHouse = new __2.ClearingHouse({
|
|
84
|
-
connection,
|
|
85
|
-
wallet: provider.wallet,
|
|
86
|
-
programID: clearingHousePublicKey,
|
|
87
|
-
});
|
|
88
|
-
yield clearingHouse.subscribe();
|
|
89
|
-
// Set up Clearing House user client
|
|
90
|
-
const user = new __2.ClearingHouseUser({
|
|
91
|
-
clearingHouse,
|
|
92
|
-
userAccountPublicKey: yield clearingHouse.getUserAccountPublicKey(),
|
|
93
|
-
});
|
|
94
|
-
//// Check if clearing house account exists for the current wallet
|
|
95
|
-
const userAccountExists = yield user.exists();
|
|
96
|
-
if (!userAccountExists) {
|
|
97
|
-
//// Create a Clearing House account by Depositing some USDC ($10,000 in this case)
|
|
98
|
-
const depositAmount = new anchor_1.BN(10000).mul(__2.QUOTE_PRECISION);
|
|
99
|
-
yield clearingHouse.initializeUserAccountAndDepositCollateral(
|
|
100
|
-
depositAmount,
|
|
101
|
-
yield exports.getTokenAddress(
|
|
102
|
-
usdcTokenAddress.toString(),
|
|
103
|
-
wallet.publicKey.toString()
|
|
104
|
-
),
|
|
105
|
-
banks_1.SpotMarkets['devnet'][0].marketIndex
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
yield user.subscribe();
|
|
109
|
-
// Get current price
|
|
110
|
-
const solMarketInfo = sdkConfig.PERP_MARKETS.find(
|
|
111
|
-
(market) => market.baseAssetSymbol === 'SOL'
|
|
112
|
-
);
|
|
113
|
-
const currentMarketPrice = __2.calculateMarkPrice(
|
|
114
|
-
clearingHouse.getMarketAccount(solMarketInfo.marketIndex),
|
|
115
|
-
undefined
|
|
116
|
-
);
|
|
117
|
-
const formattedPrice = __2.convertToNumber(
|
|
118
|
-
currentMarketPrice,
|
|
119
|
-
__2.PRICE_PRECISION
|
|
120
|
-
);
|
|
121
|
-
console.log(`Current Market Price is $${formattedPrice}`);
|
|
122
|
-
// Estimate the slippage for a $5000 LONG trade
|
|
123
|
-
const solMarketAccount = clearingHouse.getMarketAccount(
|
|
124
|
-
solMarketInfo.marketIndex
|
|
125
|
-
);
|
|
126
|
-
const longAmount = new anchor_1.BN(5000).mul(__2.QUOTE_PRECISION);
|
|
127
|
-
const slippage = __2.convertToNumber(
|
|
128
|
-
__2.calculateTradeSlippage(
|
|
129
|
-
__2.PositionDirection.LONG,
|
|
130
|
-
longAmount,
|
|
131
|
-
solMarketAccount,
|
|
132
|
-
'quote',
|
|
133
|
-
undefined
|
|
134
|
-
)[0],
|
|
135
|
-
__2.PRICE_PRECISION
|
|
136
|
-
);
|
|
137
|
-
console.log(
|
|
138
|
-
`Slippage for a $5000 LONG on the SOL market would be $${slippage}`
|
|
139
|
-
);
|
|
140
|
-
// Make a $5000 LONG trade
|
|
141
|
-
yield clearingHouse.openPosition(
|
|
142
|
-
__2.PositionDirection.LONG,
|
|
143
|
-
longAmount,
|
|
144
|
-
solMarketInfo.marketIndex
|
|
145
|
-
);
|
|
146
|
-
console.log(`LONGED $5000 SOL`);
|
|
147
|
-
// Reduce the position by $2000
|
|
148
|
-
const reduceAmount = new anchor_1.BN(2000).mul(__2.QUOTE_PRECISION);
|
|
149
|
-
yield clearingHouse.openPosition(
|
|
150
|
-
__2.PositionDirection.SHORT,
|
|
151
|
-
reduceAmount,
|
|
152
|
-
solMarketInfo.marketIndex
|
|
153
|
-
);
|
|
154
|
-
// Close the rest of the position
|
|
155
|
-
yield clearingHouse.closePosition(solMarketInfo.marketIndex);
|
|
156
|
-
});
|
|
157
|
-
main();
|
package/src/token/index.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseTokenAccount = void 0;
|
|
4
|
-
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
-
function parseTokenAccount(data) {
|
|
7
|
-
const accountInfo = spl_token_1.AccountLayout.decode(data);
|
|
8
|
-
accountInfo.mint = new web3_js_1.PublicKey(accountInfo.mint);
|
|
9
|
-
accountInfo.owner = new web3_js_1.PublicKey(accountInfo.owner);
|
|
10
|
-
accountInfo.amount = spl_token_1.u64.fromBuffer(accountInfo.amount);
|
|
11
|
-
if (accountInfo.delegateOption === 0) {
|
|
12
|
-
accountInfo.delegate = null;
|
|
13
|
-
// eslint-disable-next-line new-cap
|
|
14
|
-
accountInfo.delegatedAmount = new spl_token_1.u64(0);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
accountInfo.delegate = new web3_js_1.PublicKey(accountInfo.delegate);
|
|
18
|
-
accountInfo.delegatedAmount = spl_token_1.u64.fromBuffer(accountInfo.delegatedAmount);
|
|
19
|
-
}
|
|
20
|
-
accountInfo.isInitialized = accountInfo.state !== 0;
|
|
21
|
-
accountInfo.isFrozen = accountInfo.state === 2;
|
|
22
|
-
if (accountInfo.isNativeOption === 1) {
|
|
23
|
-
accountInfo.rentExemptReserve = spl_token_1.u64.fromBuffer(accountInfo.isNative);
|
|
24
|
-
accountInfo.isNative = true;
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
accountInfo.rentExemptReserve = null;
|
|
28
|
-
accountInfo.isNative = false;
|
|
29
|
-
}
|
|
30
|
-
if (accountInfo.closeAuthorityOption === 0) {
|
|
31
|
-
accountInfo.closeAuthority = null;
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
accountInfo.closeAuthority = new web3_js_1.PublicKey(accountInfo.closeAuthority);
|
|
35
|
-
}
|
|
36
|
-
return accountInfo;
|
|
37
|
-
}
|
|
38
|
-
exports.parseTokenAccount = parseTokenAccount;
|
package/src/tx/types.js
DELETED
package/src/tx/utils.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.wrapInTx = void 0;
|
|
4
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
5
|
-
const COMPUTE_UNITS_DEFAULT = 200000;
|
|
6
|
-
function wrapInTx(instruction, computeUnits = 600000 // TODO, requires less code change
|
|
7
|
-
) {
|
|
8
|
-
const tx = new web3_js_1.Transaction();
|
|
9
|
-
if (computeUnits != COMPUTE_UNITS_DEFAULT) {
|
|
10
|
-
tx.add(web3_js_1.ComputeBudgetProgram.requestUnits({
|
|
11
|
-
units: computeUnits,
|
|
12
|
-
additionalFee: 0,
|
|
13
|
-
}));
|
|
14
|
-
}
|
|
15
|
-
return tx.add(instruction);
|
|
16
|
-
}
|
|
17
|
-
exports.wrapInTx = wrapInTx;
|
package/src/util/computeUnits.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.findComputeUnitConsumption = void 0;
|
|
13
|
-
function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
|
|
14
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
-
const tx = yield connection.getTransaction(txSignature, { commitment });
|
|
16
|
-
const computeUnits = [];
|
|
17
|
-
const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`);
|
|
18
|
-
tx.meta.logMessages.forEach((logMessage) => {
|
|
19
|
-
const match = logMessage.match(regex);
|
|
20
|
-
if (match && match[1]) {
|
|
21
|
-
computeUnits.push(match[1]);
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
return computeUnits;
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
exports.findComputeUnitConsumption = findComputeUnitConsumption;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTokenAddress = void 0;
|
|
4
|
-
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
-
const getTokenAddress = (mintAddress, userPubKey) => {
|
|
7
|
-
return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
|
|
8
|
-
};
|
|
9
|
-
exports.getTokenAddress = getTokenAddress;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.promiseTimeout = void 0;
|
|
4
|
-
function promiseTimeout(promise, timeoutMs) {
|
|
5
|
-
let timeoutId;
|
|
6
|
-
const timeoutPromise = new Promise((resolve) => {
|
|
7
|
-
timeoutId = setTimeout(() => resolve(null), timeoutMs);
|
|
8
|
-
});
|
|
9
|
-
return Promise.race([promise, timeoutPromise]).then((result) => {
|
|
10
|
-
clearTimeout(timeoutId);
|
|
11
|
-
return result;
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
exports.promiseTimeout = promiseTimeout;
|
package/src/util/tps.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.estimateTps = void 0;
|
|
13
|
-
function estimateTps(programId, connection, failed) {
|
|
14
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
-
let signatures = yield connection.getSignaturesForAddress(programId, undefined, 'finalized');
|
|
16
|
-
if (failed) {
|
|
17
|
-
signatures = signatures.filter((signature) => signature.err);
|
|
18
|
-
}
|
|
19
|
-
const numberOfSignatures = signatures.length;
|
|
20
|
-
if (numberOfSignatures === 0) {
|
|
21
|
-
return 0;
|
|
22
|
-
}
|
|
23
|
-
return (numberOfSignatures /
|
|
24
|
-
(signatures[0].blockTime - signatures[numberOfSignatures - 1].blockTime));
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
exports.estimateTps = estimateTps;
|