@drift-labs/sdk 0.1.23-master.3 → 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 +2 -0
- package/lib/accounts/bulkAccountLoader.js +26 -11
- package/lib/addresses.js +5 -1
- package/lib/admin.d.ts +2 -2
- package/lib/admin.js +11 -5
- package/lib/clearingHouse.js +7 -1
- package/lib/clearingHouseUser.d.ts +12 -17
- package/lib/clearingHouseUser.js +114 -217
- package/lib/constants/markets.d.ts +4 -4
- package/lib/constants/markets.js +22 -0
- package/lib/constants/numericConstants.d.ts +2 -2
- package/lib/constants/numericConstants.js +3 -3
- package/lib/factory/oracleClient.d.ts +5 -0
- package/lib/factory/oracleClient.js +16 -0
- package/lib/idl/clearing_house.json +53 -5
- package/lib/idl/switchboard_v2.json +4663 -0
- package/lib/index.d.ts +4 -1
- package/lib/index.js +9 -2
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +4 -16
- package/lib/math/orders.d.ts +1 -0
- package/lib/math/orders.js +19 -1
- 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 +1 -1
- package/lib/types.d.ts +5 -1
- package/package.json +2 -1
- package/src/accounts/bulkAccountLoader.ts +37 -13
- package/src/accounts/types.js +10 -0
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +76 -0
- package/src/addresses.js +83 -0
- package/src/admin.ts +15 -4
- package/src/clearingHouse.ts +2 -0
- package/src/clearingHouseUser.ts +161 -330
- package/src/constants/markets.ts +26 -3
- package/src/constants/numericConstants.ts +2 -2
- package/src/factory/oracleClient.ts +22 -0
- package/src/idl/clearing_house.json +53 -5
- package/src/idl/switchboard_v2.json +4663 -0
- package/src/index.ts +4 -1
- package/src/math/funding.ts +9 -25
- package/src/math/orders.ts +28 -0
- package/src/mockUSDCFaucet.js +171 -0
- 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/orders.ts +1 -1
- package/src/types.js +60 -0
- package/src/types.ts +6 -1
- package/lib/pythClient.d.ts +0 -7
- package/lib/pythClient.js +0 -25
- package/src/pythClient.ts +0 -15
|
@@ -0,0 +1,76 @@
|
|
|
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.WebSocketAccountSubscriber = void 0;
|
|
13
|
+
const utils_1 = require("./utils");
|
|
14
|
+
class WebSocketAccountSubscriber {
|
|
15
|
+
constructor(accountName, program, accountPublicKey) {
|
|
16
|
+
this.accountName = accountName;
|
|
17
|
+
this.program = program;
|
|
18
|
+
this.accountPublicKey = accountPublicKey;
|
|
19
|
+
}
|
|
20
|
+
subscribe(onChange) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
if (this.listenerId) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
this.onChange = onChange;
|
|
26
|
+
yield this.fetch();
|
|
27
|
+
this.listenerId = this.program.provider.connection.onAccountChange(this.accountPublicKey, (accountInfo, context) => {
|
|
28
|
+
this.handleRpcResponse(context, accountInfo);
|
|
29
|
+
}, this.program.provider.opts.commitment);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
fetch() {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
const rpcResponse = yield this.program.provider.connection.getAccountInfoAndContext(this.accountPublicKey, this.program.provider.opts.commitment);
|
|
35
|
+
this.handleRpcResponse(rpcResponse.context, rpcResponse === null || rpcResponse === void 0 ? void 0 : rpcResponse.value);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
handleRpcResponse(context, accountInfo) {
|
|
39
|
+
const newSlot = context.slot;
|
|
40
|
+
let newBuffer = undefined;
|
|
41
|
+
if (accountInfo) {
|
|
42
|
+
newBuffer = accountInfo.data;
|
|
43
|
+
}
|
|
44
|
+
if (!this.accountData) {
|
|
45
|
+
this.accountData = {
|
|
46
|
+
buffer: newBuffer,
|
|
47
|
+
slot: newSlot,
|
|
48
|
+
};
|
|
49
|
+
if (newBuffer) {
|
|
50
|
+
this.data = this.program.account[this.accountName].coder.accounts.decode(utils_1.capitalize(this.accountName), newBuffer);
|
|
51
|
+
this.onChange(this.data);
|
|
52
|
+
}
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (newSlot <= this.accountData.slot) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const oldBuffer = this.accountData.buffer;
|
|
59
|
+
if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
|
|
60
|
+
this.accountData = {
|
|
61
|
+
buffer: newBuffer,
|
|
62
|
+
slot: newSlot,
|
|
63
|
+
};
|
|
64
|
+
this.data = this.program.account[this.accountName].coder.accounts.decode(utils_1.capitalize(this.accountName), newBuffer);
|
|
65
|
+
this.onChange(this.data);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
unsubscribe() {
|
|
69
|
+
if (this.listenerId) {
|
|
70
|
+
const promise = this.program.provider.connection.removeAccountChangeListener(this.listenerId);
|
|
71
|
+
this.listenerId = undefined;
|
|
72
|
+
return promise;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.WebSocketAccountSubscriber = WebSocketAccountSubscriber;
|
package/src/addresses.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.getUserOrdersAccountPublicKey = exports.getUserOrdersAccountPublicKeyAndNonce = exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getClearingHouseStateAccountPublicKey = exports.getOrderStateAccountPublicKeyAndNonce = exports.getOrderStateAccountPublicKey = exports.getClearingHouseStateAccountPublicKeyAndNonce = void 0;
|
|
32
|
+
const anchor = __importStar(require("@project-serum/anchor"));
|
|
33
|
+
function getClearingHouseStateAccountPublicKeyAndNonce(programId) {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('clearing_house'))], programId);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
exports.getClearingHouseStateAccountPublicKeyAndNonce = getClearingHouseStateAccountPublicKeyAndNonce;
|
|
39
|
+
function getOrderStateAccountPublicKey(programId) {
|
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
+
return (yield getOrderStateAccountPublicKeyAndNonce(programId))[0];
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
exports.getOrderStateAccountPublicKey = getOrderStateAccountPublicKey;
|
|
45
|
+
function getOrderStateAccountPublicKeyAndNonce(programId) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('order_state'))], programId);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
exports.getOrderStateAccountPublicKeyAndNonce = getOrderStateAccountPublicKeyAndNonce;
|
|
51
|
+
function getClearingHouseStateAccountPublicKey(programId) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
return (yield getClearingHouseStateAccountPublicKeyAndNonce(programId))[0];
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
exports.getClearingHouseStateAccountPublicKey = getClearingHouseStateAccountPublicKey;
|
|
57
|
+
function getUserAccountPublicKeyAndNonce(programId, authority) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('user')), authority.toBuffer()], programId);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
exports.getUserAccountPublicKeyAndNonce = getUserAccountPublicKeyAndNonce;
|
|
63
|
+
function getUserAccountPublicKey(programId, authority) {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
return (yield getUserAccountPublicKeyAndNonce(programId, authority))[0];
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
exports.getUserAccountPublicKey = getUserAccountPublicKey;
|
|
69
|
+
function getUserOrdersAccountPublicKeyAndNonce(programId, userAccount) {
|
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
return anchor.web3.PublicKey.findProgramAddress([
|
|
72
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('user_orders')),
|
|
73
|
+
userAccount.toBuffer(),
|
|
74
|
+
], programId);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
exports.getUserOrdersAccountPublicKeyAndNonce = getUserOrdersAccountPublicKeyAndNonce;
|
|
78
|
+
function getUserOrdersAccountPublicKey(programId, userAccount) {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
return (yield getUserOrdersAccountPublicKeyAndNonce(programId, userAccount))[0];
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
exports.getUserOrdersAccountPublicKey = getUserOrdersAccountPublicKey;
|
package/src/admin.ts
CHANGED
|
@@ -216,7 +216,11 @@ export class Admin extends ClearingHouse {
|
|
|
216
216
|
baseAssetReserve: BN,
|
|
217
217
|
quoteAssetReserve: BN,
|
|
218
218
|
periodicity: BN,
|
|
219
|
-
pegMultiplier: BN = PEG_PRECISION
|
|
219
|
+
pegMultiplier: BN = PEG_PRECISION,
|
|
220
|
+
oracleSource: OracleSource = OracleSource.PYTH,
|
|
221
|
+
marginRatioInitial = 2000,
|
|
222
|
+
marginRatioPartial = 625,
|
|
223
|
+
marginRatioMaintenance = 500
|
|
220
224
|
): Promise<TransactionSignature> {
|
|
221
225
|
if (this.getMarketsAccount().markets[marketIndex.toNumber()].initialized) {
|
|
222
226
|
throw Error(`MarketIndex ${marketIndex.toNumber()} already initialized`);
|
|
@@ -228,6 +232,10 @@ export class Admin extends ClearingHouse {
|
|
|
228
232
|
quoteAssetReserve,
|
|
229
233
|
periodicity,
|
|
230
234
|
pegMultiplier,
|
|
235
|
+
oracleSource,
|
|
236
|
+
marginRatioInitial,
|
|
237
|
+
marginRatioPartial,
|
|
238
|
+
marginRatioMaintenance,
|
|
231
239
|
{
|
|
232
240
|
accounts: {
|
|
233
241
|
state: await this.getStatePublicKey(),
|
|
@@ -460,11 +468,13 @@ export class Admin extends ClearingHouse {
|
|
|
460
468
|
}
|
|
461
469
|
|
|
462
470
|
public async updateMarginRatio(
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
471
|
+
marketIndex: BN,
|
|
472
|
+
marginRatioInitial: number,
|
|
473
|
+
marginRatioPartial: number,
|
|
474
|
+
marginRatioMaintenance: number
|
|
466
475
|
): Promise<TransactionSignature> {
|
|
467
476
|
return await this.program.rpc.updateMarginRatio(
|
|
477
|
+
marketIndex,
|
|
468
478
|
marginRatioInitial,
|
|
469
479
|
marginRatioPartial,
|
|
470
480
|
marginRatioMaintenance,
|
|
@@ -472,6 +482,7 @@ export class Admin extends ClearingHouse {
|
|
|
472
482
|
accounts: {
|
|
473
483
|
admin: this.wallet.publicKey,
|
|
474
484
|
state: await this.getStatePublicKey(),
|
|
485
|
+
markets: this.getStateAccount().markets,
|
|
475
486
|
},
|
|
476
487
|
}
|
|
477
488
|
);
|
package/src/clearingHouse.ts
CHANGED
|
@@ -258,6 +258,8 @@ export class ClearingHouse {
|
|
|
258
258
|
this.program = newProgram;
|
|
259
259
|
this.userAccountPublicKey = undefined;
|
|
260
260
|
this.userAccount = undefined;
|
|
261
|
+
this.userOrdersAccountPublicKey = undefined;
|
|
262
|
+
this.userOrdersExist = undefined;
|
|
261
263
|
}
|
|
262
264
|
|
|
263
265
|
public async initializeUserAccount(): Promise<
|