@drift-labs/sdk 0.1.25-master.0 → 0.1.28
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/pollingOracleSubscriber.d.ts +26 -0
- package/lib/accounts/pollingOracleSubscriber.js +79 -0
- package/lib/accounts/types.d.ts +14 -1
- package/lib/clearingHouse.d.ts +6 -4
- package/lib/clearingHouse.js +56 -7
- package/lib/constants/markets.d.ts +4 -2
- package/lib/constants/markets.js +76 -28
- package/lib/idl/clearing_house.json +65 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/market.d.ts +2 -0
- package/lib/math/market.js +6 -1
- package/lib/orderParams.d.ts +1 -1
- package/lib/orderParams.js +2 -2
- package/lib/orders.d.ts +3 -2
- package/lib/orders.js +11 -4
- package/lib/types.d.ts +3 -0
- package/lib/types.js +1 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.js +180 -0
- package/src/accounts/bulkAccountLoader.js.map +1 -0
- package/src/accounts/bulkUserSubscription.js +56 -0
- package/src/accounts/bulkUserSubscription.js.map +1 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +210 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js.map +1 -0
- package/src/accounts/pollingOracleSubscriber.js +65 -0
- package/src/accounts/pollingOracleSubscriber.js.map +1 -0
- package/src/accounts/pollingOracleSubscriber.ts +103 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +65 -0
- package/src/accounts/pollingTokenAccountSubscriber.js.map +1 -0
- package/src/accounts/pollingUserAccountSubscriber.js +139 -0
- package/src/accounts/pollingUserAccountSubscriber.js.map +1 -0
- package/src/accounts/types.js +1 -0
- package/src/accounts/types.js.map +1 -0
- package/src/accounts/types.ts +22 -1
- package/src/accounts/utils.js +1 -0
- package/src/accounts/utils.js.map +1 -0
- package/src/accounts/webSocketAccountSubscriber.js +15 -27
- package/src/accounts/webSocketAccountSubscriber.js.map +1 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +212 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js.map +1 -0
- package/src/accounts/webSocketUserAccountSubscriber.js +78 -0
- package/src/accounts/webSocketUserAccountSubscriber.js.map +1 -0
- package/src/addresses.js +20 -44
- package/src/addresses.js.map +1 -0
- package/src/admin.js +443 -0
- package/src/admin.js.map +1 -0
- package/src/assert/assert.js +10 -0
- package/src/assert/assert.js.map +1 -0
- package/src/clearingHouse.ts +85 -7
- package/src/clearingHouseUser.js +581 -0
- package/src/clearingHouseUser.js.map +1 -0
- package/src/config.js +37 -0
- package/src/config.js.map +1 -0
- package/src/constants/markets.js +167 -0
- package/src/constants/markets.js.map +1 -0
- package/src/constants/markets.ts +80 -30
- package/src/constants/numericConstants.js +22 -0
- package/src/constants/numericConstants.js.map +1 -0
- package/src/factory/clearingHouse.js +65 -0
- package/src/factory/clearingHouse.js.map +1 -0
- package/src/factory/clearingHouseUser.js +35 -0
- package/src/factory/clearingHouseUser.js.map +1 -0
- package/src/factory/oracleClient.js +17 -0
- package/src/factory/oracleClient.js.map +1 -0
- package/src/idl/clearing_house.json +65 -1
- package/src/index.js +56 -0
- package/src/index.js.map +1 -0
- package/src/index.ts +1 -0
- package/src/math/amm.js +285 -0
- package/src/math/amm.js.map +1 -0
- package/src/math/conversion.js +16 -0
- package/src/math/conversion.js.map +1 -0
- package/src/math/funding.js +223 -0
- package/src/math/funding.js.map +1 -0
- package/src/math/insuranceFund.js +23 -0
- package/src/math/insuranceFund.js.map +1 -0
- package/src/math/market.js +30 -0
- package/src/math/market.js.map +1 -0
- package/src/math/market.ts +9 -0
- package/src/math/orders.js +73 -0
- package/src/math/orders.js.map +1 -0
- package/src/math/position.js +121 -0
- package/src/math/position.js.map +1 -0
- package/src/math/trade.js +182 -0
- package/src/math/trade.js.map +1 -0
- package/src/math/utils.js +27 -0
- package/src/math/utils.js.map +1 -0
- package/src/mockUSDCFaucet.js +88 -116
- package/src/mockUSDCFaucet.js.map +1 -0
- package/src/oracles/pythClient.js +39 -0
- package/src/oracles/pythClient.js.map +1 -0
- package/src/oracles/switchboardClient.js +60 -0
- package/src/oracles/switchboardClient.js.map +1 -0
- package/src/oracles/types.js +3 -0
- package/src/oracles/types.js.map +1 -0
- package/src/orderParams.js +109 -0
- package/src/orderParams.js.map +1 -0
- package/src/orderParams.ts +3 -2
- package/src/orders.js +172 -0
- package/src/orders.js.map +1 -0
- package/src/orders.ts +17 -4
- package/src/token/index.js +39 -0
- package/src/token/index.js.map +1 -0
- package/src/tx/defaultTxSender.js +13 -0
- package/src/tx/defaultTxSender.js.map +1 -0
- package/src/tx/retryTxSender.js +137 -0
- package/src/tx/retryTxSender.js.map +1 -0
- package/src/tx/types.js +3 -0
- package/src/tx/types.js.map +1 -0
- package/src/tx/utils.js +9 -0
- package/src/tx/utils.js.map +1 -0
- package/src/types.js +1 -0
- package/src/types.js.map +1 -0
- package/src/types.ts +1 -0
- package/src/util/computeUnits.js +17 -0
- package/src/util/computeUnits.js.map +1 -0
- package/src/util/tps.js +17 -0
- package/src/util/tps.js.map +1 -0
- package/src/wallet.js +23 -0
- package/src/wallet.js.map +1 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { OracleEvents, OracleSubscriber } from './types';
|
|
3
|
+
import { Program } from '@project-serum/anchor';
|
|
4
|
+
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
import { PublicKey } from '@solana/web3.js';
|
|
7
|
+
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
8
|
+
import { OracleClient, OraclePriceData } from '../oracles/types';
|
|
9
|
+
export declare class PollingOracleSubscriber implements OracleSubscriber {
|
|
10
|
+
isSubscribed: boolean;
|
|
11
|
+
program: Program;
|
|
12
|
+
eventEmitter: StrictEventEmitter<EventEmitter, OracleEvents>;
|
|
13
|
+
publicKey: PublicKey;
|
|
14
|
+
accountLoader: BulkAccountLoader;
|
|
15
|
+
oracleClient: OracleClient;
|
|
16
|
+
callbackId?: string;
|
|
17
|
+
errorCallbackId?: string;
|
|
18
|
+
oraclePriceData?: OraclePriceData;
|
|
19
|
+
constructor(publicKey: PublicKey, oracleClient: OracleClient, accountLoader: BulkAccountLoader);
|
|
20
|
+
subscribe(): Promise<boolean>;
|
|
21
|
+
addToAccountLoader(): void;
|
|
22
|
+
fetch(): Promise<void>;
|
|
23
|
+
unsubscribe(): Promise<void>;
|
|
24
|
+
assertIsSubscribed(): void;
|
|
25
|
+
getOraclePriceData(): OraclePriceData;
|
|
26
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
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.PollingOracleSubscriber = void 0;
|
|
13
|
+
const types_1 = require("./types");
|
|
14
|
+
const events_1 = require("events");
|
|
15
|
+
class PollingOracleSubscriber {
|
|
16
|
+
constructor(publicKey, oracleClient, accountLoader) {
|
|
17
|
+
this.isSubscribed = false;
|
|
18
|
+
this.publicKey = publicKey;
|
|
19
|
+
this.oracleClient = oracleClient;
|
|
20
|
+
this.accountLoader = accountLoader;
|
|
21
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
22
|
+
}
|
|
23
|
+
subscribe() {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
if (this.isSubscribed) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
this.addToAccountLoader();
|
|
29
|
+
yield this.fetch();
|
|
30
|
+
this.eventEmitter.emit('update');
|
|
31
|
+
this.isSubscribed = true;
|
|
32
|
+
return true;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
addToAccountLoader() {
|
|
36
|
+
if (this.callbackId) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
this.callbackId = this.accountLoader.addAccount(this.publicKey, (buffer) => __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
const oraclePriceData = yield this.oracleClient.getOraclePriceDataFromBuffer(buffer);
|
|
41
|
+
this.oraclePriceData = oraclePriceData;
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
this.eventEmitter.emit('oracleUpdate', oraclePriceData);
|
|
44
|
+
this.eventEmitter.emit('update');
|
|
45
|
+
}));
|
|
46
|
+
this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
|
|
47
|
+
this.eventEmitter.emit('error', error);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
fetch() {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
yield this.accountLoader.load();
|
|
53
|
+
const buffer = this.accountLoader.getAccountData(this.publicKey);
|
|
54
|
+
this.oraclePriceData = yield this.oracleClient.getOraclePriceDataFromBuffer(buffer);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
unsubscribe() {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
if (!this.isSubscribed) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
this.accountLoader.removeAccount(this.publicKey, this.callbackId);
|
|
63
|
+
this.callbackId = undefined;
|
|
64
|
+
this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
|
|
65
|
+
this.errorCallbackId = undefined;
|
|
66
|
+
this.isSubscribed = false;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
assertIsSubscribed() {
|
|
70
|
+
if (!this.isSubscribed) {
|
|
71
|
+
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
getOraclePriceData() {
|
|
75
|
+
this.assertIsSubscribed();
|
|
76
|
+
return this.oraclePriceData;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.PollingOracleSubscriber = PollingOracleSubscriber;
|
package/lib/accounts/types.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import StrictEventEmitter from 'strict-event-emitter-types';
|
|
|
4
4
|
import { EventEmitter } from 'events';
|
|
5
5
|
import { PublicKey } from '@solana/web3.js';
|
|
6
6
|
import { AccountInfo } from '@solana/spl-token';
|
|
7
|
-
import { ClearingHouseConfigType, ClearingHouseUserConfigType } from '..';
|
|
7
|
+
import { ClearingHouseConfigType, ClearingHouseUserConfigType, OraclePriceData } from '..';
|
|
8
8
|
export interface AccountSubscriber<T> {
|
|
9
9
|
data?: T;
|
|
10
10
|
subscribe(onChange: (data: T) => void): Promise<void>;
|
|
@@ -84,6 +84,19 @@ export interface TokenAccountSubscriber {
|
|
|
84
84
|
unsubscribe(): Promise<void>;
|
|
85
85
|
getTokenAccount(): AccountInfo;
|
|
86
86
|
}
|
|
87
|
+
export interface OracleEvents {
|
|
88
|
+
oracleUpdate: (payload: OraclePriceData) => void;
|
|
89
|
+
update: void;
|
|
90
|
+
error: (e: Error) => void;
|
|
91
|
+
}
|
|
92
|
+
export interface OracleSubscriber {
|
|
93
|
+
eventEmitter: StrictEventEmitter<EventEmitter, OracleEvents>;
|
|
94
|
+
isSubscribed: boolean;
|
|
95
|
+
subscribe(): Promise<boolean>;
|
|
96
|
+
fetch(): Promise<void>;
|
|
97
|
+
unsubscribe(): Promise<void>;
|
|
98
|
+
getOraclePriceData(): OraclePriceData;
|
|
99
|
+
}
|
|
87
100
|
export declare type AccountToPoll = {
|
|
88
101
|
key: string;
|
|
89
102
|
publicKey: PublicKey;
|
package/lib/clearingHouse.d.ts
CHANGED
|
@@ -121,10 +121,12 @@ export declare class ClearingHouse {
|
|
|
121
121
|
initializeUserOrdersThenPlaceOrder(orderParams: OrderParams, discountToken?: PublicKey, referrer?: PublicKey): Promise<TransactionSignature>;
|
|
122
122
|
placeOrder(orderParams: OrderParams, discountToken?: PublicKey, referrer?: PublicKey): Promise<TransactionSignature>;
|
|
123
123
|
getPlaceOrderIx(orderParams: OrderParams, discountToken?: PublicKey, referrer?: PublicKey): Promise<TransactionInstruction>;
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
124
|
+
expireOrders(userAccountPublicKey: PublicKey, userOrdersAccountPublicKey: PublicKey): Promise<TransactionSignature>;
|
|
125
|
+
getExpireOrdersIx(userAccountPublicKey: PublicKey, userOrdersAccountPublicKey: PublicKey): Promise<TransactionInstruction>;
|
|
126
|
+
cancelOrder(orderId: BN, oracle?: PublicKey): Promise<TransactionSignature>;
|
|
127
|
+
getCancelOrderIx(orderId: BN, oracle?: PublicKey): Promise<TransactionInstruction>;
|
|
128
|
+
cancelOrderByUserId(userOrderId: number, oracle?: PublicKey): Promise<TransactionSignature>;
|
|
129
|
+
getCancelOrderByUserIdIx(userOrderId: number, oracle?: PublicKey): Promise<TransactionInstruction>;
|
|
128
130
|
fillOrder(userAccountPublicKey: PublicKey, userOrdersAccountPublicKey: PublicKey, order: Order): Promise<TransactionSignature>;
|
|
129
131
|
getFillOrderIx(userAccountPublicKey: PublicKey, userOrdersAccountPublicKey: PublicKey, order: Order): Promise<TransactionInstruction>;
|
|
130
132
|
initializeUserOrdersThenPlaceAndFillOrder(orderParams: OrderParams, discountToken?: PublicKey, referrer?: PublicKey): Promise<TransactionSignature>;
|
package/lib/clearingHouse.js
CHANGED
|
@@ -44,6 +44,7 @@ const web3_js_1 = require("@solana/web3.js");
|
|
|
44
44
|
const addresses_1 = require("./addresses");
|
|
45
45
|
const utils_1 = require("./tx/utils");
|
|
46
46
|
const clearingHouse_1 = require("./factory/clearingHouse");
|
|
47
|
+
const numericConstants_1 = require("./constants/numericConstants");
|
|
47
48
|
/**
|
|
48
49
|
* # ClearingHouse
|
|
49
50
|
* 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.
|
|
@@ -511,6 +512,13 @@ class ClearingHouse {
|
|
|
511
512
|
isSigner: false,
|
|
512
513
|
});
|
|
513
514
|
}
|
|
515
|
+
if (!orderParams.oraclePriceOffset.eq(numericConstants_1.ZERO)) {
|
|
516
|
+
remainingAccounts.push({
|
|
517
|
+
pubkey: priceOracle,
|
|
518
|
+
isWritable: false,
|
|
519
|
+
isSigner: false,
|
|
520
|
+
});
|
|
521
|
+
}
|
|
514
522
|
const state = this.getStateAccount();
|
|
515
523
|
const orderState = this.getOrderStateAccount();
|
|
516
524
|
return yield this.program.instruction.placeOrder(orderParams, {
|
|
@@ -525,23 +533,54 @@ class ClearingHouse {
|
|
|
525
533
|
fundingRateHistory: state.fundingRateHistory,
|
|
526
534
|
orderState: yield this.getOrderStatePublicKey(),
|
|
527
535
|
orderHistory: orderState.orderHistory,
|
|
528
|
-
oracle: priceOracle,
|
|
529
536
|
},
|
|
530
537
|
remainingAccounts,
|
|
531
538
|
});
|
|
532
539
|
});
|
|
533
540
|
}
|
|
534
|
-
|
|
541
|
+
expireOrders(userAccountPublicKey, userOrdersAccountPublicKey) {
|
|
542
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
543
|
+
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getExpireOrdersIx(userAccountPublicKey, userOrdersAccountPublicKey)), [], this.opts);
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
getExpireOrdersIx(userAccountPublicKey, userOrdersAccountPublicKey) {
|
|
547
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
548
|
+
const fillerPublicKey = yield this.getUserAccountPublicKey();
|
|
549
|
+
const userAccount = yield this.program.account.user.fetch(userAccountPublicKey);
|
|
550
|
+
const orderState = this.getOrderStateAccount();
|
|
551
|
+
return yield this.program.instruction.expireOrders({
|
|
552
|
+
accounts: {
|
|
553
|
+
state: yield this.getStatePublicKey(),
|
|
554
|
+
filler: fillerPublicKey,
|
|
555
|
+
user: userAccountPublicKey,
|
|
556
|
+
authority: this.wallet.publicKey,
|
|
557
|
+
userPositions: userAccount.positions,
|
|
558
|
+
userOrders: userOrdersAccountPublicKey,
|
|
559
|
+
orderState: yield this.getOrderStatePublicKey(),
|
|
560
|
+
orderHistory: orderState.orderHistory,
|
|
561
|
+
},
|
|
562
|
+
});
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
cancelOrder(orderId, oracle) {
|
|
535
566
|
return __awaiter(this, void 0, void 0, function* () {
|
|
536
|
-
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getCancelOrderIx(orderId)), [], this.opts);
|
|
567
|
+
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getCancelOrderIx(orderId, oracle)), [], this.opts);
|
|
537
568
|
});
|
|
538
569
|
}
|
|
539
|
-
getCancelOrderIx(orderId) {
|
|
570
|
+
getCancelOrderIx(orderId, oracle) {
|
|
540
571
|
return __awaiter(this, void 0, void 0, function* () {
|
|
541
572
|
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
542
573
|
const userAccount = yield this.getUserAccount();
|
|
543
574
|
const state = this.getStateAccount();
|
|
544
575
|
const orderState = this.getOrderStateAccount();
|
|
576
|
+
const remainingAccounts = [];
|
|
577
|
+
if (oracle) {
|
|
578
|
+
remainingAccounts.push({
|
|
579
|
+
pubkey: oracle,
|
|
580
|
+
isWritable: false,
|
|
581
|
+
isSigner: false,
|
|
582
|
+
});
|
|
583
|
+
}
|
|
545
584
|
return yield this.program.instruction.cancelOrder(orderId, {
|
|
546
585
|
accounts: {
|
|
547
586
|
state: yield this.getStatePublicKey(),
|
|
@@ -555,20 +594,29 @@ class ClearingHouse {
|
|
|
555
594
|
orderState: yield this.getOrderStatePublicKey(),
|
|
556
595
|
orderHistory: orderState.orderHistory,
|
|
557
596
|
},
|
|
597
|
+
remainingAccounts,
|
|
558
598
|
});
|
|
559
599
|
});
|
|
560
600
|
}
|
|
561
|
-
cancelOrderByUserId(userOrderId) {
|
|
601
|
+
cancelOrderByUserId(userOrderId, oracle) {
|
|
562
602
|
return __awaiter(this, void 0, void 0, function* () {
|
|
563
|
-
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getCancelOrderByUserIdIx(userOrderId)), [], this.opts);
|
|
603
|
+
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getCancelOrderByUserIdIx(userOrderId, oracle)), [], this.opts);
|
|
564
604
|
});
|
|
565
605
|
}
|
|
566
|
-
getCancelOrderByUserIdIx(userOrderId) {
|
|
606
|
+
getCancelOrderByUserIdIx(userOrderId, oracle) {
|
|
567
607
|
return __awaiter(this, void 0, void 0, function* () {
|
|
568
608
|
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
569
609
|
const userAccount = yield this.getUserAccount();
|
|
570
610
|
const state = this.getStateAccount();
|
|
571
611
|
const orderState = this.getOrderStateAccount();
|
|
612
|
+
const remainingAccounts = [];
|
|
613
|
+
if (oracle) {
|
|
614
|
+
remainingAccounts.push({
|
|
615
|
+
pubkey: oracle,
|
|
616
|
+
isWritable: false,
|
|
617
|
+
isSigner: false,
|
|
618
|
+
});
|
|
619
|
+
}
|
|
572
620
|
return yield this.program.instruction.cancelOrderByUserId(userOrderId, {
|
|
573
621
|
accounts: {
|
|
574
622
|
state: yield this.getStatePublicKey(),
|
|
@@ -582,6 +630,7 @@ class ClearingHouse {
|
|
|
582
630
|
orderState: yield this.getOrderStatePublicKey(),
|
|
583
631
|
orderHistory: orderState.orderHistory,
|
|
584
632
|
},
|
|
633
|
+
remainingAccounts,
|
|
585
634
|
});
|
|
586
635
|
});
|
|
587
636
|
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN, OracleSource } from '../';
|
|
3
3
|
export declare type MarketConfig = {
|
|
4
|
+
fullName?: string;
|
|
5
|
+
category?: string[];
|
|
4
6
|
symbol: string;
|
|
5
7
|
baseAssetSymbol: string;
|
|
6
8
|
marketIndex: BN;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
devnetPublicKey: string;
|
|
10
|
+
mainnetPublicKey: string;
|
|
9
11
|
launchTs: number;
|
|
10
12
|
oracleSource: OracleSource;
|
|
11
13
|
};
|
package/lib/constants/markets.js
CHANGED
|
@@ -4,131 +4,179 @@ exports.Markets = void 0;
|
|
|
4
4
|
const __1 = require("../");
|
|
5
5
|
exports.Markets = [
|
|
6
6
|
{
|
|
7
|
+
fullName: 'Solana',
|
|
8
|
+
category: ['L1', 'Infra'],
|
|
7
9
|
symbol: 'SOL-PERP',
|
|
8
10
|
baseAssetSymbol: 'SOL',
|
|
9
11
|
marketIndex: new __1.BN(0),
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
devnetPublicKey: 'J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix',
|
|
13
|
+
mainnetPublicKey: 'H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG',
|
|
12
14
|
launchTs: 1635209696886,
|
|
13
15
|
oracleSource: __1.OracleSource.PYTH,
|
|
14
16
|
},
|
|
15
17
|
{
|
|
18
|
+
fullName: 'Bitcoin',
|
|
19
|
+
category: ['L1', 'Payment'],
|
|
16
20
|
symbol: 'BTC-PERP',
|
|
17
21
|
baseAssetSymbol: 'BTC',
|
|
18
22
|
marketIndex: new __1.BN(1),
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
devnetPublicKey: 'HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J',
|
|
24
|
+
mainnetPublicKey: 'GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU',
|
|
21
25
|
launchTs: 1637691088868,
|
|
22
26
|
oracleSource: __1.OracleSource.PYTH,
|
|
23
27
|
},
|
|
24
28
|
{
|
|
29
|
+
fullName: 'Ethereum',
|
|
30
|
+
category: ['L1', 'Infra'],
|
|
25
31
|
symbol: 'ETH-PERP',
|
|
26
32
|
baseAssetSymbol: 'ETH',
|
|
27
33
|
marketIndex: new __1.BN(2),
|
|
28
|
-
|
|
29
|
-
|
|
34
|
+
devnetPublicKey: 'EdVCmQ9FSPcVe5YySXDPCRmc8aDQLKJ9xvYBMZPie1Vw',
|
|
35
|
+
mainnetPublicKey: 'JBu1AL4obBcCMqKBBxhpWCNUt136ijcuMZLFvTP7iWdB',
|
|
30
36
|
launchTs: 1637691133472,
|
|
31
37
|
oracleSource: __1.OracleSource.PYTH,
|
|
32
38
|
},
|
|
33
39
|
{
|
|
40
|
+
fullName: 'Terra',
|
|
41
|
+
category: ['L1', 'Infra'],
|
|
34
42
|
symbol: 'LUNA-PERP',
|
|
35
43
|
baseAssetSymbol: 'LUNA',
|
|
36
44
|
marketIndex: new __1.BN(3),
|
|
37
|
-
|
|
38
|
-
|
|
45
|
+
devnetPublicKey: '8PugCXTAHLM9kfLSQWe2njE5pzAgUdpPk3Nx5zSm7BD3',
|
|
46
|
+
mainnetPublicKey: '5bmWuR1dgP4avtGYMNKLuxumZTVKGgoN2BCMXWDNL9nY',
|
|
39
47
|
launchTs: 1638821738525,
|
|
40
48
|
oracleSource: __1.OracleSource.PYTH,
|
|
41
49
|
},
|
|
42
50
|
{
|
|
43
51
|
symbol: 'AVAX-PERP',
|
|
52
|
+
category: ['L1', 'Infra'],
|
|
44
53
|
baseAssetSymbol: 'AVAX',
|
|
45
54
|
marketIndex: new __1.BN(4),
|
|
46
|
-
|
|
47
|
-
|
|
55
|
+
devnetPublicKey: 'FVb5h1VmHPfVb1RfqZckchq18GxRv4iKt8T4eVTQAqdz',
|
|
56
|
+
mainnetPublicKey: 'Ax9ujW5B9oqcv59N8m6f1BpTBq2rGeGaBcpKjC5UYsXU',
|
|
48
57
|
launchTs: 1639092501080,
|
|
49
58
|
oracleSource: __1.OracleSource.PYTH,
|
|
50
59
|
},
|
|
51
60
|
{
|
|
61
|
+
fullName: 'Binance Coin',
|
|
52
62
|
symbol: 'BNB-PERP',
|
|
63
|
+
category: ['L1', 'Exchange'],
|
|
53
64
|
baseAssetSymbol: 'BNB',
|
|
54
65
|
marketIndex: new __1.BN(5),
|
|
55
|
-
|
|
56
|
-
|
|
66
|
+
devnetPublicKey: 'GwzBgrXb4PG59zjce24SF2b9JXbLEjJJTBkmytuEZj1b',
|
|
67
|
+
mainnetPublicKey: '4CkQJBxhU8EZ2UjhigbtdaPbpTe6mqf811fipYBFbSYN',
|
|
57
68
|
launchTs: 1639523193012,
|
|
58
69
|
oracleSource: __1.OracleSource.PYTH,
|
|
59
70
|
},
|
|
60
71
|
{
|
|
72
|
+
fullName: 'Polygon',
|
|
73
|
+
category: ['L1', 'Infra'],
|
|
61
74
|
symbol: 'MATIC-PERP',
|
|
62
75
|
baseAssetSymbol: 'MATIC',
|
|
63
76
|
marketIndex: new __1.BN(6),
|
|
64
|
-
|
|
65
|
-
|
|
77
|
+
devnetPublicKey: 'FBirwuDFuRAu4iSGc7RGxN5koHB7EJM1wbCmyPuQoGur',
|
|
78
|
+
mainnetPublicKey: '7KVswB9vkCgeM3SHP7aGDijvdRAHK8P5wi9JXViCrtYh',
|
|
66
79
|
launchTs: 1641488603564,
|
|
67
80
|
oracleSource: __1.OracleSource.PYTH,
|
|
68
81
|
},
|
|
69
82
|
{
|
|
83
|
+
fullName: 'Cosmos',
|
|
84
|
+
category: ['L1', 'Infra'],
|
|
70
85
|
symbol: 'ATOM-PERP',
|
|
71
86
|
baseAssetSymbol: 'ATOM',
|
|
72
87
|
marketIndex: new __1.BN(7),
|
|
73
|
-
|
|
74
|
-
|
|
88
|
+
devnetPublicKey: '7YAze8qFUMkBnyLVdKT4TFUUFui99EwS5gfRArMcrvFk',
|
|
89
|
+
mainnetPublicKey: 'CrCpTerNqtZvqLcKqz1k13oVeXV9WkMD2zA9hBKXrsbN',
|
|
75
90
|
launchTs: 1641920238195,
|
|
76
91
|
oracleSource: __1.OracleSource.PYTH,
|
|
77
92
|
},
|
|
78
93
|
{
|
|
94
|
+
fullName: 'Polkadot',
|
|
79
95
|
symbol: 'DOT-PERP',
|
|
80
96
|
baseAssetSymbol: 'DOT',
|
|
81
97
|
marketIndex: new __1.BN(8),
|
|
82
|
-
|
|
83
|
-
|
|
98
|
+
devnetPublicKey: '4dqq5VBpN4EwYb7wyywjjfknvMKu7m78j9mKZRXTj462',
|
|
99
|
+
mainnetPublicKey: 'EcV1X1gY2yb4KXxjVQtTHTbioum2gvmPnFk4zYAt7zne',
|
|
84
100
|
launchTs: 1642629253786,
|
|
85
101
|
oracleSource: __1.OracleSource.PYTH,
|
|
86
102
|
},
|
|
87
103
|
{
|
|
104
|
+
fullName: 'Cardano',
|
|
105
|
+
category: ['L1', 'Infra'],
|
|
88
106
|
symbol: 'ADA-PERP',
|
|
89
107
|
baseAssetSymbol: 'ADA',
|
|
90
108
|
marketIndex: new __1.BN(9),
|
|
91
|
-
|
|
92
|
-
|
|
109
|
+
devnetPublicKey: '8oGTURNmSQkrBS1AQ5NjB2p8qY34UVmMA9ojrw8vnHus',
|
|
110
|
+
mainnetPublicKey: '3pyn4svBbxJ9Wnn3RVeafyLWfzie6yC5eTig2S62v9SC',
|
|
93
111
|
launchTs: 1643084413000,
|
|
94
112
|
oracleSource: __1.OracleSource.PYTH,
|
|
95
113
|
},
|
|
96
114
|
{
|
|
115
|
+
fullName: 'Algorand',
|
|
116
|
+
category: ['L1', 'Infra'],
|
|
97
117
|
symbol: 'ALGO-PERP',
|
|
98
118
|
baseAssetSymbol: 'ALGO',
|
|
99
119
|
marketIndex: new __1.BN(10),
|
|
100
|
-
|
|
101
|
-
|
|
120
|
+
devnetPublicKey: 'c1A946dY5NHuVda77C8XXtXytyR3wK1SCP3eA9VRfC3',
|
|
121
|
+
mainnetPublicKey: 'HqFyq1wh1xKvL7KDqqT7NJeSPdAqsDqnmBisUC2XdXAX',
|
|
102
122
|
launchTs: 1643686767000,
|
|
103
123
|
oracleSource: __1.OracleSource.PYTH,
|
|
104
124
|
},
|
|
105
125
|
{
|
|
126
|
+
fullName: 'FTX Coin',
|
|
127
|
+
category: ['L1', 'Exchange'],
|
|
106
128
|
symbol: 'FTT-PERP',
|
|
107
129
|
baseAssetSymbol: 'FTT',
|
|
108
130
|
marketIndex: new __1.BN(11),
|
|
109
|
-
|
|
110
|
-
|
|
131
|
+
devnetPublicKey: '6vivTRs5ZPeeXbjo7dfburfaYDWoXjBtdtuYgQRuGfu',
|
|
132
|
+
mainnetPublicKey: '8JPJJkmDScpcNmBRKGZuPuG2GYAveQgP3t5gFuMymwvF',
|
|
111
133
|
launchTs: 1644382122000,
|
|
112
134
|
oracleSource: __1.OracleSource.PYTH,
|
|
113
135
|
},
|
|
114
136
|
{
|
|
137
|
+
fullName: 'Litecoin',
|
|
138
|
+
category: ['L1', 'Payments'],
|
|
115
139
|
symbol: 'LTC-PERP',
|
|
116
140
|
baseAssetSymbol: 'LTC',
|
|
117
141
|
marketIndex: new __1.BN(12),
|
|
118
|
-
|
|
119
|
-
|
|
142
|
+
devnetPublicKey: 'BLArYBCUYhdWiY8PCUTpvFE21iaJq85dvxLk9bYMobcU',
|
|
143
|
+
mainnetPublicKey: '8RMnV1eD55iqUFJLMguPkYBkq8DCtx81XcmAja93LvRR',
|
|
120
144
|
launchTs: 1645027429000,
|
|
121
145
|
oracleSource: __1.OracleSource.PYTH,
|
|
122
146
|
},
|
|
123
147
|
{
|
|
148
|
+
fullName: 'Ripple',
|
|
149
|
+
category: ['L1', 'Payments'],
|
|
124
150
|
symbol: 'XRP-PERP',
|
|
125
151
|
baseAssetSymbol: 'XRP',
|
|
126
152
|
marketIndex: new __1.BN(13),
|
|
127
|
-
|
|
128
|
-
|
|
153
|
+
devnetPublicKey: 'WMW5xc3HypXwTnPesyUT49uLsyHwNURsWAEk39onKuk',
|
|
154
|
+
mainnetPublicKey: 'WMW5xc3HypXwTnPesyUT49uLsyHwNURsWAEk39onKuk',
|
|
129
155
|
launchTs: 1647543166000,
|
|
130
156
|
oracleSource: __1.OracleSource.SWITCHBOARD,
|
|
131
157
|
},
|
|
158
|
+
{
|
|
159
|
+
fullName: 'ApeCoin',
|
|
160
|
+
category: ['NFT'],
|
|
161
|
+
symbol: 'APE-PERP',
|
|
162
|
+
baseAssetSymbol: 'APE',
|
|
163
|
+
marketIndex: new __1.BN(14),
|
|
164
|
+
devnetPublicKey: 'AwH6kBrJbkL9JTeqRd7Q59EdWh6UjPtoqoA5M4x4K2fA',
|
|
165
|
+
mainnetPublicKey: '74zeQpprjNtEghGiC3VEPsR9y4kR2GTd4Rq9YVk9tnjz',
|
|
166
|
+
launchTs: 1648475932000,
|
|
167
|
+
oracleSource: __1.OracleSource.SWITCHBOARD,
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
fullName: 'DogeCoin',
|
|
171
|
+
category: ['Meme', 'NFT'],
|
|
172
|
+
symbol: 'DOGE-PERP',
|
|
173
|
+
baseAssetSymbol: 'DOGE',
|
|
174
|
+
marketIndex: new __1.BN(15),
|
|
175
|
+
devnetPublicKey: '4L6YhY8VvUgmqG5MvJkUJATtzB2rFqdrJwQCmFLv4Jzy',
|
|
176
|
+
mainnetPublicKey: 'FsSM3s38PX9K7Dn6eGzuE29S2Dsk1Sss1baytTQdCaQj',
|
|
177
|
+
launchTs: 1648607439000,
|
|
178
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
179
|
+
},
|
|
132
180
|
// {
|
|
133
181
|
// symbol: 'mSOL-PERP',
|
|
134
182
|
// baseAssetSymbol: 'mSOL',
|
|
@@ -677,6 +677,52 @@
|
|
|
677
677
|
}
|
|
678
678
|
]
|
|
679
679
|
},
|
|
680
|
+
{
|
|
681
|
+
"name": "expireOrders",
|
|
682
|
+
"accounts": [
|
|
683
|
+
{
|
|
684
|
+
"name": "state",
|
|
685
|
+
"isMut": false,
|
|
686
|
+
"isSigner": false
|
|
687
|
+
},
|
|
688
|
+
{
|
|
689
|
+
"name": "orderState",
|
|
690
|
+
"isMut": false,
|
|
691
|
+
"isSigner": false
|
|
692
|
+
},
|
|
693
|
+
{
|
|
694
|
+
"name": "authority",
|
|
695
|
+
"isMut": false,
|
|
696
|
+
"isSigner": true
|
|
697
|
+
},
|
|
698
|
+
{
|
|
699
|
+
"name": "filler",
|
|
700
|
+
"isMut": true,
|
|
701
|
+
"isSigner": false
|
|
702
|
+
},
|
|
703
|
+
{
|
|
704
|
+
"name": "user",
|
|
705
|
+
"isMut": true,
|
|
706
|
+
"isSigner": false
|
|
707
|
+
},
|
|
708
|
+
{
|
|
709
|
+
"name": "userPositions",
|
|
710
|
+
"isMut": true,
|
|
711
|
+
"isSigner": false
|
|
712
|
+
},
|
|
713
|
+
{
|
|
714
|
+
"name": "userOrders",
|
|
715
|
+
"isMut": true,
|
|
716
|
+
"isSigner": false
|
|
717
|
+
},
|
|
718
|
+
{
|
|
719
|
+
"name": "orderHistory",
|
|
720
|
+
"isMut": true,
|
|
721
|
+
"isSigner": false
|
|
722
|
+
}
|
|
723
|
+
],
|
|
724
|
+
"args": []
|
|
725
|
+
},
|
|
680
726
|
{
|
|
681
727
|
"name": "fillOrder",
|
|
682
728
|
"accounts": [
|
|
@@ -2616,7 +2662,7 @@
|
|
|
2616
2662
|
},
|
|
2617
2663
|
{
|
|
2618
2664
|
"name": "oraclePriceOffset",
|
|
2619
|
-
"type": "
|
|
2665
|
+
"type": "i128"
|
|
2620
2666
|
},
|
|
2621
2667
|
{
|
|
2622
2668
|
"name": "padding0",
|
|
@@ -3800,6 +3846,9 @@
|
|
|
3800
3846
|
},
|
|
3801
3847
|
{
|
|
3802
3848
|
"name": "Fill"
|
|
3849
|
+
},
|
|
3850
|
+
{
|
|
3851
|
+
"name": "Expire"
|
|
3803
3852
|
}
|
|
3804
3853
|
]
|
|
3805
3854
|
}
|
|
@@ -4180,6 +4229,21 @@
|
|
|
4180
4229
|
"code": 6057,
|
|
4181
4230
|
"name": "InvalidMarginRatio",
|
|
4182
4231
|
"msg": "Invalid Margin Ratio"
|
|
4232
|
+
},
|
|
4233
|
+
{
|
|
4234
|
+
"code": 6058,
|
|
4235
|
+
"name": "CantCancelPostOnlyOrder",
|
|
4236
|
+
"msg": "Cant Cancel Post Only Order"
|
|
4237
|
+
},
|
|
4238
|
+
{
|
|
4239
|
+
"code": 6059,
|
|
4240
|
+
"name": "InvalidOracleOffset",
|
|
4241
|
+
"msg": "InvalidOracleOffset"
|
|
4242
|
+
},
|
|
4243
|
+
{
|
|
4244
|
+
"code": 6060,
|
|
4245
|
+
"name": "CantExpireOrders",
|
|
4246
|
+
"msg": "CantExpireOrders"
|
|
4183
4247
|
}
|
|
4184
4248
|
]
|
|
4185
4249
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export * from './accounts/webSocketClearingHouseAccountSubscriber';
|
|
|
10
10
|
export * from './accounts/bulkAccountLoader';
|
|
11
11
|
export * from './accounts/bulkUserSubscription';
|
|
12
12
|
export * from './accounts/pollingClearingHouseAccountSubscriber';
|
|
13
|
+
export * from './accounts/pollingOracleSubscriber';
|
|
13
14
|
export * from './accounts/pollingTokenAccountSubscriber';
|
|
14
15
|
export * from './accounts/types';
|
|
15
16
|
export * from './addresses';
|
package/lib/index.js
CHANGED
|
@@ -29,6 +29,7 @@ __exportStar(require("./accounts/webSocketClearingHouseAccountSubscriber"), expo
|
|
|
29
29
|
__exportStar(require("./accounts/bulkAccountLoader"), exports);
|
|
30
30
|
__exportStar(require("./accounts/bulkUserSubscription"), exports);
|
|
31
31
|
__exportStar(require("./accounts/pollingClearingHouseAccountSubscriber"), exports);
|
|
32
|
+
__exportStar(require("./accounts/pollingOracleSubscriber"), exports);
|
|
32
33
|
__exportStar(require("./accounts/pollingTokenAccountSubscriber"), exports);
|
|
33
34
|
__exportStar(require("./accounts/types"), exports);
|
|
34
35
|
__exportStar(require("./addresses"), exports);
|
package/lib/math/market.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
3
|
import { Market, PositionDirection } from '../types';
|
|
4
|
+
import { OraclePriceData } from '../oracles/types';
|
|
4
5
|
/**
|
|
5
6
|
* Calculates market mark price
|
|
6
7
|
*
|
|
@@ -9,3 +10,4 @@ import { Market, PositionDirection } from '../types';
|
|
|
9
10
|
*/
|
|
10
11
|
export declare function calculateMarkPrice(market: Market): BN;
|
|
11
12
|
export declare function calculateNewMarketAfterTrade(baseAssetAmount: BN, direction: PositionDirection, market: Market): Market;
|
|
13
|
+
export declare function calculateMarkOracleSpread(market: Market, oraclePriceData: OraclePriceData): BN;
|
package/lib/math/market.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateNewMarketAfterTrade = exports.calculateMarkPrice = void 0;
|
|
3
|
+
exports.calculateMarkOracleSpread = exports.calculateNewMarketAfterTrade = exports.calculateMarkPrice = void 0;
|
|
4
4
|
const amm_1 = require("./amm");
|
|
5
5
|
/**
|
|
6
6
|
* Calculates market mark price
|
|
@@ -22,3 +22,8 @@ function calculateNewMarketAfterTrade(baseAssetAmount, direction, market) {
|
|
|
22
22
|
return newMarket;
|
|
23
23
|
}
|
|
24
24
|
exports.calculateNewMarketAfterTrade = calculateNewMarketAfterTrade;
|
|
25
|
+
function calculateMarkOracleSpread(market, oraclePriceData) {
|
|
26
|
+
const markPrice = calculateMarkPrice(market);
|
|
27
|
+
return markPrice.sub(oraclePriceData.price);
|
|
28
|
+
}
|
|
29
|
+
exports.calculateMarkOracleSpread = calculateMarkOracleSpread;
|
package/lib/orderParams.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { OrderParams, OrderTriggerCondition, PositionDirection } from './types';
|
|
3
3
|
import { BN } from '@project-serum/anchor';
|
|
4
|
-
export declare function getLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number, postOnly?: boolean): OrderParams;
|
|
4
|
+
export declare function getLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number, postOnly?: boolean, oraclePriceOffset?: BN): OrderParams;
|
|
5
5
|
export declare function getTriggerMarketOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number): OrderParams;
|
|
6
6
|
export declare function getTriggerLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number): OrderParams;
|
|
7
7
|
export declare function getMarketOrderParams(marketIndex: BN, direction: PositionDirection, quoteAssetAmount: BN, baseAssetAmount: BN, reduceOnly: boolean, price?: BN, discountToken?: boolean, referrer?: boolean): OrderParams;
|