@drift-labs/sdk 0.1.23-master.4 → 0.1.24
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 +12 -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/admin.d.ts +2 -2
- package/lib/admin.js +12 -11
- package/lib/clearingHouse.js +19 -19
- package/lib/clearingHouseUser.d.ts +12 -17
- package/lib/clearingHouseUser.js +125 -228
- package/lib/constants/numericConstants.d.ts +1 -2
- package/lib/constants/numericConstants.js +2 -3
- package/lib/examples/makeTradeExample.js +6 -6
- package/lib/idl/clearing_house.json +42 -4
- package/lib/math/amm.js +12 -12
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.js +1 -1
- package/lib/math/market.js +2 -2
- package/lib/math/orders.d.ts +1 -0
- package/lib/math/orders.js +25 -7
- package/lib/math/position.js +1 -1
- package/lib/math/trade.js +18 -18
- package/lib/orders.js +20 -20
- package/lib/pythClient.js +1 -1
- package/lib/tx/retryTxSender.js +1 -1
- package/lib/types.d.ts +4 -1
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +13 -0
- 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 +13 -4
- package/src/clearingHouseUser.ts +161 -330
- package/src/constants/numericConstants.ts +1 -2
- package/src/idl/clearing_house.json +42 -4
- package/src/math/orders.ts +28 -0
- package/src/mockUSDCFaucet.js +171 -0
- package/src/orders.ts +1 -1
- package/src/types.js +60 -0
- package/src/types.ts +5 -1
|
@@ -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,10 @@ 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
|
+
marginRatioInitial = 2000,
|
|
221
|
+
marginRatioPartial = 625,
|
|
222
|
+
marginRatioMaintenance = 500
|
|
220
223
|
): Promise<TransactionSignature> {
|
|
221
224
|
if (this.getMarketsAccount().markets[marketIndex.toNumber()].initialized) {
|
|
222
225
|
throw Error(`MarketIndex ${marketIndex.toNumber()} already initialized`);
|
|
@@ -228,6 +231,9 @@ export class Admin extends ClearingHouse {
|
|
|
228
231
|
quoteAssetReserve,
|
|
229
232
|
periodicity,
|
|
230
233
|
pegMultiplier,
|
|
234
|
+
marginRatioInitial,
|
|
235
|
+
marginRatioPartial,
|
|
236
|
+
marginRatioMaintenance,
|
|
231
237
|
{
|
|
232
238
|
accounts: {
|
|
233
239
|
state: await this.getStatePublicKey(),
|
|
@@ -460,11 +466,13 @@ export class Admin extends ClearingHouse {
|
|
|
460
466
|
}
|
|
461
467
|
|
|
462
468
|
public async updateMarginRatio(
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
469
|
+
marketIndex: BN,
|
|
470
|
+
marginRatioInitial: number,
|
|
471
|
+
marginRatioPartial: number,
|
|
472
|
+
marginRatioMaintenance: number
|
|
466
473
|
): Promise<TransactionSignature> {
|
|
467
474
|
return await this.program.rpc.updateMarginRatio(
|
|
475
|
+
marketIndex,
|
|
468
476
|
marginRatioInitial,
|
|
469
477
|
marginRatioPartial,
|
|
470
478
|
marginRatioMaintenance,
|
|
@@ -472,6 +480,7 @@ export class Admin extends ClearingHouse {
|
|
|
472
480
|
accounts: {
|
|
473
481
|
admin: this.wallet.publicKey,
|
|
474
482
|
state: await this.getStatePublicKey(),
|
|
483
|
+
markets: this.getStateAccount().markets,
|
|
475
484
|
},
|
|
476
485
|
}
|
|
477
486
|
);
|