@drift-labs/sdk 0.1.26 → 0.1.27
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 +4 -4
- package/lib/clearingHouse.js +32 -7
- package/lib/constants/markets.d.ts +2 -2
- package/lib/constants/markets.js +39 -28
- package/lib/idl/clearing_house.json +6 -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/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.js +769 -0
- package/src/clearingHouse.js.map +1 -0
- package/src/clearingHouse.ts +45 -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 +41 -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 +6 -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/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,10 @@ 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
|
-
cancelOrder(orderId: BN): Promise<TransactionSignature>;
|
|
125
|
-
getCancelOrderIx(orderId: BN): Promise<TransactionInstruction>;
|
|
126
|
-
cancelOrderByUserId(userOrderId: number): Promise<TransactionSignature>;
|
|
127
|
-
getCancelOrderByUserIdIx(userOrderId: number): Promise<TransactionInstruction>;
|
|
124
|
+
cancelOrder(orderId: BN, oracle?: PublicKey): Promise<TransactionSignature>;
|
|
125
|
+
getCancelOrderIx(orderId: BN, oracle?: PublicKey): Promise<TransactionInstruction>;
|
|
126
|
+
cancelOrderByUserId(userOrderId: number, oracle?: PublicKey): Promise<TransactionSignature>;
|
|
127
|
+
getCancelOrderByUserIdIx(userOrderId: number, oracle?: PublicKey): Promise<TransactionInstruction>;
|
|
128
128
|
fillOrder(userAccountPublicKey: PublicKey, userOrdersAccountPublicKey: PublicKey, order: Order): Promise<TransactionSignature>;
|
|
129
129
|
getFillOrderIx(userAccountPublicKey: PublicKey, userOrdersAccountPublicKey: PublicKey, order: Order): Promise<TransactionInstruction>;
|
|
130
130
|
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,30 @@ 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
|
-
cancelOrder(orderId) {
|
|
541
|
+
cancelOrder(orderId, oracle) {
|
|
535
542
|
return __awaiter(this, void 0, void 0, function* () {
|
|
536
|
-
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getCancelOrderIx(orderId)), [], this.opts);
|
|
543
|
+
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getCancelOrderIx(orderId, oracle)), [], this.opts);
|
|
537
544
|
});
|
|
538
545
|
}
|
|
539
|
-
getCancelOrderIx(orderId) {
|
|
546
|
+
getCancelOrderIx(orderId, oracle) {
|
|
540
547
|
return __awaiter(this, void 0, void 0, function* () {
|
|
541
548
|
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
542
549
|
const userAccount = yield this.getUserAccount();
|
|
543
550
|
const state = this.getStateAccount();
|
|
544
551
|
const orderState = this.getOrderStateAccount();
|
|
552
|
+
const remainingAccounts = [];
|
|
553
|
+
if (oracle) {
|
|
554
|
+
remainingAccounts.push({
|
|
555
|
+
pubkey: oracle,
|
|
556
|
+
isWritable: false,
|
|
557
|
+
isSigner: false,
|
|
558
|
+
});
|
|
559
|
+
}
|
|
545
560
|
return yield this.program.instruction.cancelOrder(orderId, {
|
|
546
561
|
accounts: {
|
|
547
562
|
state: yield this.getStatePublicKey(),
|
|
@@ -555,20 +570,29 @@ class ClearingHouse {
|
|
|
555
570
|
orderState: yield this.getOrderStatePublicKey(),
|
|
556
571
|
orderHistory: orderState.orderHistory,
|
|
557
572
|
},
|
|
573
|
+
remainingAccounts,
|
|
558
574
|
});
|
|
559
575
|
});
|
|
560
576
|
}
|
|
561
|
-
cancelOrderByUserId(userOrderId) {
|
|
577
|
+
cancelOrderByUserId(userOrderId, oracle) {
|
|
562
578
|
return __awaiter(this, void 0, void 0, function* () {
|
|
563
|
-
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getCancelOrderByUserIdIx(userOrderId)), [], this.opts);
|
|
579
|
+
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getCancelOrderByUserIdIx(userOrderId, oracle)), [], this.opts);
|
|
564
580
|
});
|
|
565
581
|
}
|
|
566
|
-
getCancelOrderByUserIdIx(userOrderId) {
|
|
582
|
+
getCancelOrderByUserIdIx(userOrderId, oracle) {
|
|
567
583
|
return __awaiter(this, void 0, void 0, function* () {
|
|
568
584
|
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
569
585
|
const userAccount = yield this.getUserAccount();
|
|
570
586
|
const state = this.getStateAccount();
|
|
571
587
|
const orderState = this.getOrderStateAccount();
|
|
588
|
+
const remainingAccounts = [];
|
|
589
|
+
if (oracle) {
|
|
590
|
+
remainingAccounts.push({
|
|
591
|
+
pubkey: oracle,
|
|
592
|
+
isWritable: false,
|
|
593
|
+
isSigner: false,
|
|
594
|
+
});
|
|
595
|
+
}
|
|
572
596
|
return yield this.program.instruction.cancelOrderByUserId(userOrderId, {
|
|
573
597
|
accounts: {
|
|
574
598
|
state: yield this.getStatePublicKey(),
|
|
@@ -582,6 +606,7 @@ class ClearingHouse {
|
|
|
582
606
|
orderState: yield this.getOrderStatePublicKey(),
|
|
583
607
|
orderHistory: orderState.orderHistory,
|
|
584
608
|
},
|
|
609
|
+
remainingAccounts,
|
|
585
610
|
});
|
|
586
611
|
});
|
|
587
612
|
}
|
|
@@ -6,8 +6,8 @@ export declare type MarketConfig = {
|
|
|
6
6
|
symbol: string;
|
|
7
7
|
baseAssetSymbol: string;
|
|
8
8
|
marketIndex: BN;
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
devnetPublicKey: string;
|
|
10
|
+
mainnetPublicKey: string;
|
|
11
11
|
launchTs: number;
|
|
12
12
|
oracleSource: OracleSource;
|
|
13
13
|
};
|
package/lib/constants/markets.js
CHANGED
|
@@ -9,8 +9,8 @@ exports.Markets = [
|
|
|
9
9
|
symbol: 'SOL-PERP',
|
|
10
10
|
baseAssetSymbol: 'SOL',
|
|
11
11
|
marketIndex: new __1.BN(0),
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
devnetPublicKey: 'J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix',
|
|
13
|
+
mainnetPublicKey: 'H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG',
|
|
14
14
|
launchTs: 1635209696886,
|
|
15
15
|
oracleSource: __1.OracleSource.PYTH,
|
|
16
16
|
},
|
|
@@ -20,8 +20,8 @@ exports.Markets = [
|
|
|
20
20
|
symbol: 'BTC-PERP',
|
|
21
21
|
baseAssetSymbol: 'BTC',
|
|
22
22
|
marketIndex: new __1.BN(1),
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
devnetPublicKey: 'HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J',
|
|
24
|
+
mainnetPublicKey: 'GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU',
|
|
25
25
|
launchTs: 1637691088868,
|
|
26
26
|
oracleSource: __1.OracleSource.PYTH,
|
|
27
27
|
},
|
|
@@ -31,8 +31,8 @@ exports.Markets = [
|
|
|
31
31
|
symbol: 'ETH-PERP',
|
|
32
32
|
baseAssetSymbol: 'ETH',
|
|
33
33
|
marketIndex: new __1.BN(2),
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
devnetPublicKey: 'EdVCmQ9FSPcVe5YySXDPCRmc8aDQLKJ9xvYBMZPie1Vw',
|
|
35
|
+
mainnetPublicKey: 'JBu1AL4obBcCMqKBBxhpWCNUt136ijcuMZLFvTP7iWdB',
|
|
36
36
|
launchTs: 1637691133472,
|
|
37
37
|
oracleSource: __1.OracleSource.PYTH,
|
|
38
38
|
},
|
|
@@ -42,8 +42,8 @@ exports.Markets = [
|
|
|
42
42
|
symbol: 'LUNA-PERP',
|
|
43
43
|
baseAssetSymbol: 'LUNA',
|
|
44
44
|
marketIndex: new __1.BN(3),
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
devnetPublicKey: '8PugCXTAHLM9kfLSQWe2njE5pzAgUdpPk3Nx5zSm7BD3',
|
|
46
|
+
mainnetPublicKey: '5bmWuR1dgP4avtGYMNKLuxumZTVKGgoN2BCMXWDNL9nY',
|
|
47
47
|
launchTs: 1638821738525,
|
|
48
48
|
oracleSource: __1.OracleSource.PYTH,
|
|
49
49
|
},
|
|
@@ -52,8 +52,8 @@ exports.Markets = [
|
|
|
52
52
|
category: ['L1', 'Infra'],
|
|
53
53
|
baseAssetSymbol: 'AVAX',
|
|
54
54
|
marketIndex: new __1.BN(4),
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
devnetPublicKey: 'FVb5h1VmHPfVb1RfqZckchq18GxRv4iKt8T4eVTQAqdz',
|
|
56
|
+
mainnetPublicKey: 'Ax9ujW5B9oqcv59N8m6f1BpTBq2rGeGaBcpKjC5UYsXU',
|
|
57
57
|
launchTs: 1639092501080,
|
|
58
58
|
oracleSource: __1.OracleSource.PYTH,
|
|
59
59
|
},
|
|
@@ -63,8 +63,8 @@ exports.Markets = [
|
|
|
63
63
|
category: ['L1', 'Exchange'],
|
|
64
64
|
baseAssetSymbol: 'BNB',
|
|
65
65
|
marketIndex: new __1.BN(5),
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
devnetPublicKey: 'GwzBgrXb4PG59zjce24SF2b9JXbLEjJJTBkmytuEZj1b',
|
|
67
|
+
mainnetPublicKey: '4CkQJBxhU8EZ2UjhigbtdaPbpTe6mqf811fipYBFbSYN',
|
|
68
68
|
launchTs: 1639523193012,
|
|
69
69
|
oracleSource: __1.OracleSource.PYTH,
|
|
70
70
|
},
|
|
@@ -74,8 +74,8 @@ exports.Markets = [
|
|
|
74
74
|
symbol: 'MATIC-PERP',
|
|
75
75
|
baseAssetSymbol: 'MATIC',
|
|
76
76
|
marketIndex: new __1.BN(6),
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
devnetPublicKey: 'FBirwuDFuRAu4iSGc7RGxN5koHB7EJM1wbCmyPuQoGur',
|
|
78
|
+
mainnetPublicKey: '7KVswB9vkCgeM3SHP7aGDijvdRAHK8P5wi9JXViCrtYh',
|
|
79
79
|
launchTs: 1641488603564,
|
|
80
80
|
oracleSource: __1.OracleSource.PYTH,
|
|
81
81
|
},
|
|
@@ -85,8 +85,8 @@ exports.Markets = [
|
|
|
85
85
|
symbol: 'ATOM-PERP',
|
|
86
86
|
baseAssetSymbol: 'ATOM',
|
|
87
87
|
marketIndex: new __1.BN(7),
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
devnetPublicKey: '7YAze8qFUMkBnyLVdKT4TFUUFui99EwS5gfRArMcrvFk',
|
|
89
|
+
mainnetPublicKey: 'CrCpTerNqtZvqLcKqz1k13oVeXV9WkMD2zA9hBKXrsbN',
|
|
90
90
|
launchTs: 1641920238195,
|
|
91
91
|
oracleSource: __1.OracleSource.PYTH,
|
|
92
92
|
},
|
|
@@ -95,8 +95,8 @@ exports.Markets = [
|
|
|
95
95
|
symbol: 'DOT-PERP',
|
|
96
96
|
baseAssetSymbol: 'DOT',
|
|
97
97
|
marketIndex: new __1.BN(8),
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
devnetPublicKey: '4dqq5VBpN4EwYb7wyywjjfknvMKu7m78j9mKZRXTj462',
|
|
99
|
+
mainnetPublicKey: 'EcV1X1gY2yb4KXxjVQtTHTbioum2gvmPnFk4zYAt7zne',
|
|
100
100
|
launchTs: 1642629253786,
|
|
101
101
|
oracleSource: __1.OracleSource.PYTH,
|
|
102
102
|
},
|
|
@@ -106,8 +106,8 @@ exports.Markets = [
|
|
|
106
106
|
symbol: 'ADA-PERP',
|
|
107
107
|
baseAssetSymbol: 'ADA',
|
|
108
108
|
marketIndex: new __1.BN(9),
|
|
109
|
-
|
|
110
|
-
|
|
109
|
+
devnetPublicKey: '8oGTURNmSQkrBS1AQ5NjB2p8qY34UVmMA9ojrw8vnHus',
|
|
110
|
+
mainnetPublicKey: '3pyn4svBbxJ9Wnn3RVeafyLWfzie6yC5eTig2S62v9SC',
|
|
111
111
|
launchTs: 1643084413000,
|
|
112
112
|
oracleSource: __1.OracleSource.PYTH,
|
|
113
113
|
},
|
|
@@ -117,8 +117,8 @@ exports.Markets = [
|
|
|
117
117
|
symbol: 'ALGO-PERP',
|
|
118
118
|
baseAssetSymbol: 'ALGO',
|
|
119
119
|
marketIndex: new __1.BN(10),
|
|
120
|
-
|
|
121
|
-
|
|
120
|
+
devnetPublicKey: 'c1A946dY5NHuVda77C8XXtXytyR3wK1SCP3eA9VRfC3',
|
|
121
|
+
mainnetPublicKey: 'HqFyq1wh1xKvL7KDqqT7NJeSPdAqsDqnmBisUC2XdXAX',
|
|
122
122
|
launchTs: 1643686767000,
|
|
123
123
|
oracleSource: __1.OracleSource.PYTH,
|
|
124
124
|
},
|
|
@@ -128,8 +128,8 @@ exports.Markets = [
|
|
|
128
128
|
symbol: 'FTT-PERP',
|
|
129
129
|
baseAssetSymbol: 'FTT',
|
|
130
130
|
marketIndex: new __1.BN(11),
|
|
131
|
-
|
|
132
|
-
|
|
131
|
+
devnetPublicKey: '6vivTRs5ZPeeXbjo7dfburfaYDWoXjBtdtuYgQRuGfu',
|
|
132
|
+
mainnetPublicKey: '8JPJJkmDScpcNmBRKGZuPuG2GYAveQgP3t5gFuMymwvF',
|
|
133
133
|
launchTs: 1644382122000,
|
|
134
134
|
oracleSource: __1.OracleSource.PYTH,
|
|
135
135
|
},
|
|
@@ -139,8 +139,8 @@ exports.Markets = [
|
|
|
139
139
|
symbol: 'LTC-PERP',
|
|
140
140
|
baseAssetSymbol: 'LTC',
|
|
141
141
|
marketIndex: new __1.BN(12),
|
|
142
|
-
|
|
143
|
-
|
|
142
|
+
devnetPublicKey: 'BLArYBCUYhdWiY8PCUTpvFE21iaJq85dvxLk9bYMobcU',
|
|
143
|
+
mainnetPublicKey: '8RMnV1eD55iqUFJLMguPkYBkq8DCtx81XcmAja93LvRR',
|
|
144
144
|
launchTs: 1645027429000,
|
|
145
145
|
oracleSource: __1.OracleSource.PYTH,
|
|
146
146
|
},
|
|
@@ -150,11 +150,22 @@ exports.Markets = [
|
|
|
150
150
|
symbol: 'XRP-PERP',
|
|
151
151
|
baseAssetSymbol: 'XRP',
|
|
152
152
|
marketIndex: new __1.BN(13),
|
|
153
|
-
|
|
154
|
-
|
|
153
|
+
devnetPublicKey: 'WMW5xc3HypXwTnPesyUT49uLsyHwNURsWAEk39onKuk',
|
|
154
|
+
mainnetPublicKey: 'WMW5xc3HypXwTnPesyUT49uLsyHwNURsWAEk39onKuk',
|
|
155
155
|
launchTs: 1647543166000,
|
|
156
156
|
oracleSource: __1.OracleSource.SWITCHBOARD,
|
|
157
157
|
},
|
|
158
|
+
{
|
|
159
|
+
fullName: 'Ape',
|
|
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
|
+
},
|
|
158
169
|
// {
|
|
159
170
|
// symbol: 'mSOL-PERP',
|
|
160
171
|
// baseAssetSymbol: 'mSOL',
|
|
@@ -2616,7 +2616,7 @@
|
|
|
2616
2616
|
},
|
|
2617
2617
|
{
|
|
2618
2618
|
"name": "oraclePriceOffset",
|
|
2619
|
-
"type": "
|
|
2619
|
+
"type": "i128"
|
|
2620
2620
|
},
|
|
2621
2621
|
{
|
|
2622
2622
|
"name": "padding0",
|
|
@@ -4185,6 +4185,11 @@
|
|
|
4185
4185
|
"code": 6058,
|
|
4186
4186
|
"name": "CantCancelPostOnlyOrder",
|
|
4187
4187
|
"msg": "Cant Cancel Post Only Order"
|
|
4188
|
+
},
|
|
4189
|
+
{
|
|
4190
|
+
"code": 6059,
|
|
4191
|
+
"name": "InvalidOracleOffset",
|
|
4192
|
+
"msg": "InvalidOracleOffset"
|
|
4188
4193
|
}
|
|
4189
4194
|
]
|
|
4190
4195
|
}
|
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;
|
package/lib/orderParams.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
5
|
const numericConstants_1 = require("./constants/numericConstants");
|
|
6
|
-
function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false, userOrderId = 0, postOnly = false) {
|
|
6
|
+
function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false, userOrderId = 0, postOnly = false, oraclePriceOffset = numericConstants_1.ZERO) {
|
|
7
7
|
return {
|
|
8
8
|
orderType: types_1.OrderType.LIMIT,
|
|
9
9
|
userOrderId,
|
|
@@ -24,7 +24,7 @@ function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, red
|
|
|
24
24
|
},
|
|
25
25
|
triggerCondition: types_1.OrderTriggerCondition.ABOVE,
|
|
26
26
|
triggerPrice: numericConstants_1.ZERO,
|
|
27
|
-
oraclePriceOffset
|
|
27
|
+
oraclePriceOffset,
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
30
|
exports.getLimitOrderParams = getLimitOrderParams;
|
package/lib/orders.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { Market, Order, UserAccount, UserPosition } from './types';
|
|
3
3
|
import { BN, ClearingHouseUser } from '.';
|
|
4
|
+
import { OraclePriceData } from '.';
|
|
4
5
|
export declare function calculateNewStateAfterOrder(userAccount: UserAccount, userPosition: UserPosition, market: Market, order: Order): [UserAccount, UserPosition, Market] | null;
|
|
5
|
-
export declare function calculateBaseAssetAmountMarketCanExecute(market: Market, order: Order): BN;
|
|
6
|
-
export declare function calculateAmountToTradeForLimit(market: Market, order: Order): BN;
|
|
6
|
+
export declare function calculateBaseAssetAmountMarketCanExecute(market: Market, order: Order, oraclePriceData?: OraclePriceData): BN;
|
|
7
|
+
export declare function calculateAmountToTradeForLimit(market: Market, order: Order, oraclePriceData?: OraclePriceData): BN;
|
|
7
8
|
export declare function calculateAmountToTradeForTriggerLimit(market: Market, order: Order): BN;
|
|
8
9
|
export declare function calculateBaseAssetAmountUserCanExecute(market: Market, order: Order, user: ClearingHouseUser): BN;
|
package/lib/orders.js
CHANGED
|
@@ -80,9 +80,9 @@ function calculateAmountSwapped(marketBefore, marketAfter) {
|
|
|
80
80
|
baseAssetAmountSwapped: marketBefore.amm.baseAssetReserve.sub(marketAfter.amm.baseAssetReserve),
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
|
-
function calculateBaseAssetAmountMarketCanExecute(market, order) {
|
|
83
|
+
function calculateBaseAssetAmountMarketCanExecute(market, order, oraclePriceData) {
|
|
84
84
|
if ((0, types_1.isVariant)(order.orderType, 'limit')) {
|
|
85
|
-
return calculateAmountToTradeForLimit(market, order);
|
|
85
|
+
return calculateAmountToTradeForLimit(market, order, oraclePriceData);
|
|
86
86
|
}
|
|
87
87
|
else if ((0, types_1.isVariant)(order.orderType, 'triggerLimit')) {
|
|
88
88
|
return calculateAmountToTradeForTriggerLimit(market, order);
|
|
@@ -96,8 +96,15 @@ function calculateBaseAssetAmountMarketCanExecute(market, order) {
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
exports.calculateBaseAssetAmountMarketCanExecute = calculateBaseAssetAmountMarketCanExecute;
|
|
99
|
-
function calculateAmountToTradeForLimit(market, order) {
|
|
100
|
-
|
|
99
|
+
function calculateAmountToTradeForLimit(market, order, oraclePriceData) {
|
|
100
|
+
let limitPrice = order.price;
|
|
101
|
+
if (!order.oraclePriceOffset.eq(numericConstants_1.ZERO)) {
|
|
102
|
+
if (!oraclePriceData) {
|
|
103
|
+
throw Error('Cant calculate limit price for oracle offset oracle without OraclePriceData');
|
|
104
|
+
}
|
|
105
|
+
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
106
|
+
}
|
|
107
|
+
const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(market.amm, limitPrice);
|
|
101
108
|
// Check that directions are the same
|
|
102
109
|
const sameDirection = isSameDirection(direction, order.direction);
|
|
103
110
|
if (!sameDirection) {
|