@drift-labs/sdk 0.1.24 → 0.1.25-master.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/accounts/bulkAccountLoader.d.ts +1 -0
- package/lib/accounts/bulkAccountLoader.js +16 -2
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserAccountSubscriber.js +4 -4
- package/lib/accounts/webSocketAccountSubscriber.js +2 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +1 -1
- package/lib/accounts/webSocketUserAccountSubscriber.js +2 -2
- package/lib/addresses.js +5 -1
- package/lib/admin.d.ts +1 -1
- package/lib/admin.js +15 -10
- package/lib/clearingHouse.js +26 -20
- package/lib/clearingHouseUser.js +18 -18
- package/lib/constants/markets.d.ts +4 -4
- package/lib/constants/markets.js +22 -0
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.js +2 -1
- package/lib/examples/makeTradeExample.js +6 -6
- package/lib/factory/oracleClient.d.ts +5 -0
- package/lib/factory/oracleClient.js +16 -0
- package/lib/idl/clearing_house.json +11 -1
- package/lib/idl/switchboard_v2.json +4663 -0
- package/lib/index.d.ts +4 -1
- package/lib/index.js +9 -2
- package/lib/math/amm.js +12 -12
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +5 -17
- package/lib/math/market.js +2 -2
- package/lib/math/orders.js +9 -9
- package/lib/math/position.js +1 -1
- package/lib/math/trade.js +18 -18
- package/lib/mockUSDCFaucet.js +5 -1
- package/lib/oracles/pythClient.d.ts +14 -0
- package/lib/oracles/pythClient.js +53 -0
- package/lib/oracles/switchboardClient.d.ts +13 -0
- package/lib/oracles/switchboardClient.js +76 -0
- package/lib/oracles/types.d.ts +15 -0
- package/lib/oracles/types.js +2 -0
- package/lib/orderParams.d.ts +1 -1
- package/lib/orderParams.js +2 -2
- package/lib/orders.js +19 -19
- package/lib/tx/retryTxSender.js +1 -1
- package/lib/types.d.ts +1 -0
- package/package.json +2 -1
- package/src/accounts/bulkAccountLoader.ts +19 -0
- package/src/admin.ts +2 -0
- package/src/clearingHouse.ts +2 -0
- package/src/constants/markets.ts +26 -3
- package/src/constants/numericConstants.ts +1 -0
- package/src/factory/oracleClient.ts +22 -0
- package/src/idl/clearing_house.json +11 -1
- package/src/idl/switchboard_v2.json +4663 -0
- package/src/index.ts +4 -1
- package/src/math/funding.ts +9 -25
- package/src/oracles/pythClient.ts +49 -0
- package/src/oracles/switchboardClient.ts +87 -0
- package/src/oracles/types.ts +15 -0
- package/src/orderParams.ts +3 -2
- package/src/types.ts +1 -0
- package/lib/pythClient.d.ts +0 -7
- package/lib/pythClient.js +0 -25
- package/src/pythClient.ts +0 -15
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
|
-
import { BN } from '../';
|
|
3
|
-
declare type
|
|
2
|
+
import { BN, OracleSource } from '../';
|
|
3
|
+
export declare type MarketConfig = {
|
|
4
4
|
symbol: string;
|
|
5
5
|
baseAssetSymbol: string;
|
|
6
6
|
marketIndex: BN;
|
|
7
7
|
devnetPythOracle: string;
|
|
8
8
|
mainnetPythOracle: string;
|
|
9
9
|
launchTs: number;
|
|
10
|
+
oracleSource: OracleSource;
|
|
10
11
|
};
|
|
11
|
-
export declare const Markets:
|
|
12
|
-
export {};
|
|
12
|
+
export declare const Markets: MarketConfig[];
|
package/lib/constants/markets.js
CHANGED
|
@@ -10,6 +10,7 @@ exports.Markets = [
|
|
|
10
10
|
devnetPythOracle: 'J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix',
|
|
11
11
|
mainnetPythOracle: 'H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG',
|
|
12
12
|
launchTs: 1635209696886,
|
|
13
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
13
14
|
},
|
|
14
15
|
{
|
|
15
16
|
symbol: 'BTC-PERP',
|
|
@@ -18,6 +19,7 @@ exports.Markets = [
|
|
|
18
19
|
devnetPythOracle: 'HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J',
|
|
19
20
|
mainnetPythOracle: 'GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU',
|
|
20
21
|
launchTs: 1637691088868,
|
|
22
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
21
23
|
},
|
|
22
24
|
{
|
|
23
25
|
symbol: 'ETH-PERP',
|
|
@@ -26,6 +28,7 @@ exports.Markets = [
|
|
|
26
28
|
devnetPythOracle: 'EdVCmQ9FSPcVe5YySXDPCRmc8aDQLKJ9xvYBMZPie1Vw',
|
|
27
29
|
mainnetPythOracle: 'JBu1AL4obBcCMqKBBxhpWCNUt136ijcuMZLFvTP7iWdB',
|
|
28
30
|
launchTs: 1637691133472,
|
|
31
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
29
32
|
},
|
|
30
33
|
{
|
|
31
34
|
symbol: 'LUNA-PERP',
|
|
@@ -34,6 +37,7 @@ exports.Markets = [
|
|
|
34
37
|
devnetPythOracle: '8PugCXTAHLM9kfLSQWe2njE5pzAgUdpPk3Nx5zSm7BD3',
|
|
35
38
|
mainnetPythOracle: '5bmWuR1dgP4avtGYMNKLuxumZTVKGgoN2BCMXWDNL9nY',
|
|
36
39
|
launchTs: 1638821738525,
|
|
40
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
37
41
|
},
|
|
38
42
|
{
|
|
39
43
|
symbol: 'AVAX-PERP',
|
|
@@ -42,6 +46,7 @@ exports.Markets = [
|
|
|
42
46
|
devnetPythOracle: 'FVb5h1VmHPfVb1RfqZckchq18GxRv4iKt8T4eVTQAqdz',
|
|
43
47
|
mainnetPythOracle: 'Ax9ujW5B9oqcv59N8m6f1BpTBq2rGeGaBcpKjC5UYsXU',
|
|
44
48
|
launchTs: 1639092501080,
|
|
49
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
45
50
|
},
|
|
46
51
|
{
|
|
47
52
|
symbol: 'BNB-PERP',
|
|
@@ -50,6 +55,7 @@ exports.Markets = [
|
|
|
50
55
|
devnetPythOracle: 'GwzBgrXb4PG59zjce24SF2b9JXbLEjJJTBkmytuEZj1b',
|
|
51
56
|
mainnetPythOracle: '4CkQJBxhU8EZ2UjhigbtdaPbpTe6mqf811fipYBFbSYN',
|
|
52
57
|
launchTs: 1639523193012,
|
|
58
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
53
59
|
},
|
|
54
60
|
{
|
|
55
61
|
symbol: 'MATIC-PERP',
|
|
@@ -58,6 +64,7 @@ exports.Markets = [
|
|
|
58
64
|
devnetPythOracle: 'FBirwuDFuRAu4iSGc7RGxN5koHB7EJM1wbCmyPuQoGur',
|
|
59
65
|
mainnetPythOracle: '7KVswB9vkCgeM3SHP7aGDijvdRAHK8P5wi9JXViCrtYh',
|
|
60
66
|
launchTs: 1641488603564,
|
|
67
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
61
68
|
},
|
|
62
69
|
{
|
|
63
70
|
symbol: 'ATOM-PERP',
|
|
@@ -66,6 +73,7 @@ exports.Markets = [
|
|
|
66
73
|
devnetPythOracle: '7YAze8qFUMkBnyLVdKT4TFUUFui99EwS5gfRArMcrvFk',
|
|
67
74
|
mainnetPythOracle: 'CrCpTerNqtZvqLcKqz1k13oVeXV9WkMD2zA9hBKXrsbN',
|
|
68
75
|
launchTs: 1641920238195,
|
|
76
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
69
77
|
},
|
|
70
78
|
{
|
|
71
79
|
symbol: 'DOT-PERP',
|
|
@@ -74,6 +82,7 @@ exports.Markets = [
|
|
|
74
82
|
devnetPythOracle: '4dqq5VBpN4EwYb7wyywjjfknvMKu7m78j9mKZRXTj462',
|
|
75
83
|
mainnetPythOracle: 'EcV1X1gY2yb4KXxjVQtTHTbioum2gvmPnFk4zYAt7zne',
|
|
76
84
|
launchTs: 1642629253786,
|
|
85
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
77
86
|
},
|
|
78
87
|
{
|
|
79
88
|
symbol: 'ADA-PERP',
|
|
@@ -82,6 +91,7 @@ exports.Markets = [
|
|
|
82
91
|
devnetPythOracle: '8oGTURNmSQkrBS1AQ5NjB2p8qY34UVmMA9ojrw8vnHus',
|
|
83
92
|
mainnetPythOracle: '3pyn4svBbxJ9Wnn3RVeafyLWfzie6yC5eTig2S62v9SC',
|
|
84
93
|
launchTs: 1643084413000,
|
|
94
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
85
95
|
},
|
|
86
96
|
{
|
|
87
97
|
symbol: 'ALGO-PERP',
|
|
@@ -90,6 +100,7 @@ exports.Markets = [
|
|
|
90
100
|
devnetPythOracle: 'c1A946dY5NHuVda77C8XXtXytyR3wK1SCP3eA9VRfC3',
|
|
91
101
|
mainnetPythOracle: 'HqFyq1wh1xKvL7KDqqT7NJeSPdAqsDqnmBisUC2XdXAX',
|
|
92
102
|
launchTs: 1643686767000,
|
|
103
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
93
104
|
},
|
|
94
105
|
{
|
|
95
106
|
symbol: 'FTT-PERP',
|
|
@@ -98,6 +109,7 @@ exports.Markets = [
|
|
|
98
109
|
devnetPythOracle: '6vivTRs5ZPeeXbjo7dfburfaYDWoXjBtdtuYgQRuGfu',
|
|
99
110
|
mainnetPythOracle: '8JPJJkmDScpcNmBRKGZuPuG2GYAveQgP3t5gFuMymwvF',
|
|
100
111
|
launchTs: 1644382122000,
|
|
112
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
101
113
|
},
|
|
102
114
|
{
|
|
103
115
|
symbol: 'LTC-PERP',
|
|
@@ -106,6 +118,16 @@ exports.Markets = [
|
|
|
106
118
|
devnetPythOracle: 'BLArYBCUYhdWiY8PCUTpvFE21iaJq85dvxLk9bYMobcU',
|
|
107
119
|
mainnetPythOracle: '8RMnV1eD55iqUFJLMguPkYBkq8DCtx81XcmAja93LvRR',
|
|
108
120
|
launchTs: 1645027429000,
|
|
121
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
symbol: 'XRP-PERP',
|
|
125
|
+
baseAssetSymbol: 'XRP',
|
|
126
|
+
marketIndex: new __1.BN(13),
|
|
127
|
+
devnetPythOracle: 'WMW5xc3HypXwTnPesyUT49uLsyHwNURsWAEk39onKuk',
|
|
128
|
+
mainnetPythOracle: 'WMW5xc3HypXwTnPesyUT49uLsyHwNURsWAEk39onKuk',
|
|
129
|
+
launchTs: 1647543166000,
|
|
130
|
+
oracleSource: __1.OracleSource.SWITCHBOARD,
|
|
109
131
|
},
|
|
110
132
|
// {
|
|
111
133
|
// symbol: 'mSOL-PERP',
|
|
@@ -3,6 +3,7 @@ import { BN } from '../';
|
|
|
3
3
|
export declare const ZERO: BN;
|
|
4
4
|
export declare const ONE: BN;
|
|
5
5
|
export declare const TWO: BN;
|
|
6
|
+
export declare const TEN: BN;
|
|
6
7
|
export declare const TEN_THOUSAND: BN;
|
|
7
8
|
export declare const BN_MAX: BN;
|
|
8
9
|
export declare const MAX_LEVERAGE: BN;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MARGIN_PRECISION = exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.MAX_LEVERAGE = exports.BN_MAX = exports.TEN_THOUSAND = exports.TWO = exports.ONE = exports.ZERO = void 0;
|
|
3
|
+
exports.MARGIN_PRECISION = exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.MAX_LEVERAGE = exports.BN_MAX = exports.TEN_THOUSAND = exports.TEN = exports.TWO = exports.ONE = exports.ZERO = void 0;
|
|
4
4
|
const __1 = require("../");
|
|
5
5
|
exports.ZERO = new __1.BN(0);
|
|
6
6
|
exports.ONE = new __1.BN(1);
|
|
7
7
|
exports.TWO = new __1.BN(2);
|
|
8
|
+
exports.TEN = new __1.BN(10);
|
|
8
9
|
exports.TEN_THOUSAND = new __1.BN(10000);
|
|
9
10
|
exports.BN_MAX = new __1.BN(Number.MAX_SAFE_INTEGER);
|
|
10
11
|
exports.MAX_LEVERAGE = new __1.BN(5);
|
|
@@ -21,7 +21,7 @@ const getTokenAddress = (mintAddress, userPubKey) => {
|
|
|
21
21
|
exports.getTokenAddress = getTokenAddress;
|
|
22
22
|
const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
23
23
|
// Initialize Drift SDK
|
|
24
|
-
const sdkConfig = __2.initialize({ env: 'devnet' });
|
|
24
|
+
const sdkConfig = (0, __2.initialize)({ env: 'devnet' });
|
|
25
25
|
// Set up the Wallet and Provider
|
|
26
26
|
const privateKey = process.env.BOT_PRIVATE_KEY; // stored as an array string
|
|
27
27
|
const keypair = web3_js_1.Keypair.fromSecretKey(Uint8Array.from(JSON.parse(privateKey)));
|
|
@@ -35,7 +35,7 @@ const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
35
35
|
const lamportsBalance = yield connection.getBalance(wallet.publicKey);
|
|
36
36
|
console.log('SOL balance:', lamportsBalance / Math.pow(10, 9));
|
|
37
37
|
// Misc. other things to set up
|
|
38
|
-
const usdcTokenAddress = yield exports.getTokenAddress(sdkConfig.USDC_MINT_ADDRESS, wallet.publicKey.toString());
|
|
38
|
+
const usdcTokenAddress = yield (0, exports.getTokenAddress)(sdkConfig.USDC_MINT_ADDRESS, wallet.publicKey.toString());
|
|
39
39
|
// Set up the Drift Clearing House
|
|
40
40
|
const clearingHousePublicKey = new web3_js_1.PublicKey(sdkConfig.CLEARING_HOUSE_PROGRAM_ID);
|
|
41
41
|
const clearingHouse = __2.ClearingHouse.from(connection, provider.wallet, clearingHousePublicKey);
|
|
@@ -47,18 +47,18 @@ const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
47
47
|
if (!userAccountExists) {
|
|
48
48
|
//// Create a Clearing House account by Depositing some USDC ($10,000 in this case)
|
|
49
49
|
const depositAmount = new anchor_1.BN(10000).mul(__2.QUOTE_PRECISION);
|
|
50
|
-
yield clearingHouse.initializeUserAccountAndDepositCollateral(depositAmount, yield exports.getTokenAddress(usdcTokenAddress.toString(), wallet.publicKey.toString()));
|
|
50
|
+
yield clearingHouse.initializeUserAccountAndDepositCollateral(depositAmount, yield (0, exports.getTokenAddress)(usdcTokenAddress.toString(), wallet.publicKey.toString()));
|
|
51
51
|
}
|
|
52
52
|
yield user.subscribe();
|
|
53
53
|
// Get current price
|
|
54
54
|
const solMarketInfo = __2.Markets.find((market) => market.baseAssetSymbol === 'SOL');
|
|
55
|
-
const currentMarketPrice = __2.calculateMarkPrice(clearingHouse.getMarket(solMarketInfo.marketIndex));
|
|
56
|
-
const formattedPrice = __2.convertToNumber(currentMarketPrice, __2.MARK_PRICE_PRECISION);
|
|
55
|
+
const currentMarketPrice = (0, __2.calculateMarkPrice)(clearingHouse.getMarket(solMarketInfo.marketIndex));
|
|
56
|
+
const formattedPrice = (0, __2.convertToNumber)(currentMarketPrice, __2.MARK_PRICE_PRECISION);
|
|
57
57
|
console.log(`Current Market Price is $${formattedPrice}`);
|
|
58
58
|
// Estimate the slippage for a $5000 LONG trade
|
|
59
59
|
const solMarketAccount = clearingHouse.getMarket(solMarketInfo.marketIndex);
|
|
60
60
|
const longAmount = new anchor_1.BN(5000).mul(__2.QUOTE_PRECISION);
|
|
61
|
-
const slippage = __2.convertToNumber(__2.calculateTradeSlippage(__2.PositionDirection.LONG, longAmount, solMarketAccount)[0], __2.MARK_PRICE_PRECISION);
|
|
61
|
+
const slippage = (0, __2.convertToNumber)((0, __2.calculateTradeSlippage)(__2.PositionDirection.LONG, longAmount, solMarketAccount)[0], __2.MARK_PRICE_PRECISION);
|
|
62
62
|
console.log(`Slippage for a $5000 LONG on the SOL market would be $${slippage}`);
|
|
63
63
|
// Make a $5000 LONG trade
|
|
64
64
|
yield clearingHouse.openPosition(__2.PositionDirection.LONG, longAmount, solMarketInfo.marketIndex);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { OracleSource } from '../types';
|
|
2
|
+
import { Connection } from '@solana/web3.js';
|
|
3
|
+
import { DriftEnv } from '../config';
|
|
4
|
+
import { OracleClient } from '../oracles/types';
|
|
5
|
+
export declare function getOracleClient(oracleSource: OracleSource, connection: Connection, env: DriftEnv): OracleClient;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getOracleClient = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const pythClient_1 = require("../oracles/pythClient");
|
|
6
|
+
const switchboardClient_1 = require("../oracles/switchboardClient");
|
|
7
|
+
function getOracleClient(oracleSource, connection, env) {
|
|
8
|
+
if ((0, types_1.isVariant)(oracleSource, 'pyth')) {
|
|
9
|
+
return new pythClient_1.PythClient(connection);
|
|
10
|
+
}
|
|
11
|
+
if ((0, types_1.isVariant)(oracleSource, 'switchboard')) {
|
|
12
|
+
return new switchboardClient_1.SwitchboardClient(connection, env);
|
|
13
|
+
}
|
|
14
|
+
throw new Error(`Unknown oracle source ${oracleSource}`);
|
|
15
|
+
}
|
|
16
|
+
exports.getOracleClient = getOracleClient;
|
|
@@ -212,6 +212,12 @@
|
|
|
212
212
|
"name": "ammPegMultiplier",
|
|
213
213
|
"type": "u128"
|
|
214
214
|
},
|
|
215
|
+
{
|
|
216
|
+
"name": "oracleSource",
|
|
217
|
+
"type": {
|
|
218
|
+
"defined": "OracleSource"
|
|
219
|
+
}
|
|
220
|
+
},
|
|
215
221
|
{
|
|
216
222
|
"name": "marginRatioInitial",
|
|
217
223
|
"type": "u32"
|
|
@@ -3255,12 +3261,16 @@
|
|
|
3255
3261
|
"name": "fillerReward",
|
|
3256
3262
|
"type": "u128"
|
|
3257
3263
|
},
|
|
3264
|
+
{
|
|
3265
|
+
"name": "quoteAssetAmountSurplus",
|
|
3266
|
+
"type": "u128"
|
|
3267
|
+
},
|
|
3258
3268
|
{
|
|
3259
3269
|
"name": "padding",
|
|
3260
3270
|
"type": {
|
|
3261
3271
|
"array": [
|
|
3262
3272
|
"u64",
|
|
3263
|
-
|
|
3273
|
+
8
|
|
3264
3274
|
]
|
|
3265
3275
|
}
|
|
3266
3276
|
}
|