@drift-labs/sdk 0.1.22 → 0.1.23-master.3
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.js +2 -2
- package/lib/accounts/bulkUserSubscription.js +39 -1
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +2 -2
- package/lib/accounts/pollingUserAccountSubscriber.js +39 -18
- package/lib/accounts/types.d.ts +5 -0
- package/lib/accounts/webSocketAccountSubscriber.js +2 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +1 -1
- package/lib/accounts/webSocketUserAccountSubscriber.js +2 -2
- package/lib/admin.js +7 -7
- package/lib/clearingHouse.js +21 -22
- package/lib/clearingHouseUser.js +21 -21
- package/lib/examples/makeTradeExample.js +6 -6
- package/lib/idl/clearing_house.json +68 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- 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 +26 -4
- package/lib/math/position.js +4 -3
- package/lib/math/trade.js +21 -17
- package/lib/orders.d.ts +3 -1
- package/lib/orders.js +47 -19
- package/lib/pythClient.js +1 -1
- package/lib/tx/retryTxSender.d.ts +19 -0
- package/lib/tx/retryTxSender.js +153 -0
- package/lib/tx/types.d.ts +2 -0
- package/package.json +1 -1
- package/src/accounts/bulkUserSubscription.ts +51 -1
- package/src/accounts/pollingUserAccountSubscriber.ts +49 -30
- package/src/accounts/types.ts +6 -0
- package/src/clearingHouse.ts +3 -3
- package/src/idl/clearing_house.json +68 -0
- package/src/index.ts +2 -0
- package/src/math/orders.ts +33 -0
- package/src/math/position.ts +3 -2
- package/src/math/trade.ts +8 -3
- package/src/orders.ts +56 -3
- package/src/tx/retryTxSender.ts +196 -0
- package/src/tx/types.ts +3 -0
|
@@ -23,7 +23,7 @@ class BulkAccountLoader {
|
|
|
23
23
|
}
|
|
24
24
|
addAccount(publicKey, callback) {
|
|
25
25
|
const existingSize = this.accountsToLoad.size;
|
|
26
|
-
const callbackId = uuid_1.v4();
|
|
26
|
+
const callbackId = (0, uuid_1.v4)();
|
|
27
27
|
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
28
28
|
if (existingAccountToLoad) {
|
|
29
29
|
existingAccountToLoad.callbacks.set(callbackId, callback);
|
|
@@ -57,7 +57,7 @@ class BulkAccountLoader {
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
addErrorCallbacks(callback) {
|
|
60
|
-
const callbackId = uuid_1.v4();
|
|
60
|
+
const callbackId = (0, uuid_1.v4)();
|
|
61
61
|
this.errorCallbacks.set(callbackId, callback);
|
|
62
62
|
return callbackId;
|
|
63
63
|
}
|
|
@@ -16,8 +16,46 @@ exports.bulkPollingUserSubscribe = void 0;
|
|
|
16
16
|
*/
|
|
17
17
|
function bulkPollingUserSubscribe(users, accountLoader) {
|
|
18
18
|
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
if (users.length === 0) {
|
|
20
|
+
yield accountLoader.load();
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
// Fetch all the accounts first
|
|
24
|
+
const program = users[0].clearingHouse.program;
|
|
25
|
+
let userProgramAccounts;
|
|
26
|
+
let orderProgramAccounts;
|
|
27
|
+
yield Promise.all([
|
|
28
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
userProgramAccounts = yield program.account.user.all();
|
|
30
|
+
}))(),
|
|
31
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
orderProgramAccounts = yield program.account.userOrders.all();
|
|
33
|
+
}))(),
|
|
34
|
+
]);
|
|
35
|
+
// Create a map of the authority to keys
|
|
36
|
+
const authorityToKeys = new Map();
|
|
37
|
+
const userToAuthority = new Map();
|
|
38
|
+
for (const userProgramAccount of userProgramAccounts) {
|
|
39
|
+
const userAccountPublicKey = userProgramAccount.publicKey;
|
|
40
|
+
const userAccount = userProgramAccount.account;
|
|
41
|
+
authorityToKeys.set(userAccount.authority.toString(), {
|
|
42
|
+
user: userAccountPublicKey,
|
|
43
|
+
userPositions: userAccount.positions,
|
|
44
|
+
userOrders: undefined,
|
|
45
|
+
});
|
|
46
|
+
userToAuthority.set(userAccountPublicKey.toString(), userAccount.authority.toString());
|
|
47
|
+
}
|
|
48
|
+
for (const orderProgramAccount of orderProgramAccounts) {
|
|
49
|
+
const userOrderAccountPublicKey = orderProgramAccount.publicKey;
|
|
50
|
+
const userOrderAccount = orderProgramAccount.account;
|
|
51
|
+
const authority = userToAuthority.get(userOrderAccount.user.toString());
|
|
52
|
+
const userPublicKeys = authorityToKeys.get(authority);
|
|
53
|
+
userPublicKeys.userOrders = userOrderAccountPublicKey;
|
|
54
|
+
}
|
|
19
55
|
yield Promise.all(users.map((user) => {
|
|
20
|
-
|
|
56
|
+
// Pull the keys from the authority map so we can skip fetching them in addToAccountLoader
|
|
57
|
+
const userPublicKeys = authorityToKeys.get(user.authority.toString());
|
|
58
|
+
return user.accountSubscriber.addToAccountLoader(userPublicKeys);
|
|
21
59
|
}));
|
|
22
60
|
yield accountLoader.load();
|
|
23
61
|
yield Promise.all(users.map((user) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -54,7 +54,7 @@ class PollingClearingHouseAccountSubscriber {
|
|
|
54
54
|
if (this.accountsToPoll.size > 0) {
|
|
55
55
|
return;
|
|
56
56
|
}
|
|
57
|
-
const statePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
57
|
+
const statePublicKey = yield (0, addresses_1.getClearingHouseStateAccountPublicKey)(this.program.programId);
|
|
58
58
|
const state = (yield this.program.account.state.fetch(statePublicKey));
|
|
59
59
|
this.accountsToPoll.set(statePublicKey.toString(), {
|
|
60
60
|
key: 'state',
|
|
@@ -127,7 +127,7 @@ class PollingClearingHouseAccountSubscriber {
|
|
|
127
127
|
return __awaiter(this, void 0, void 0, function* () {
|
|
128
128
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
129
129
|
accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer) => {
|
|
130
|
-
const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
130
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
131
131
|
this[accountToPoll.key] = account;
|
|
132
132
|
// @ts-ignore
|
|
133
133
|
this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
@@ -145,7 +145,7 @@ class PollingClearingHouseAccountSubscriber {
|
|
|
145
145
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
146
146
|
const buffer = this.accountLoader.getAccountData(accountToPoll.publicKey);
|
|
147
147
|
if (buffer) {
|
|
148
|
-
this[accountToPoll.key] = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
148
|
+
this[accountToPoll.key] = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
});
|
|
@@ -37,7 +37,7 @@ class PollingTokenAccountSubscriber {
|
|
|
37
37
|
return;
|
|
38
38
|
}
|
|
39
39
|
this.callbackId = this.accountLoader.addAccount(this.publicKey, (buffer) => {
|
|
40
|
-
const tokenAccount = token_1.parseTokenAccount(buffer);
|
|
40
|
+
const tokenAccount = (0, token_1.parseTokenAccount)(buffer);
|
|
41
41
|
this.tokenAccount = tokenAccount;
|
|
42
42
|
// @ts-ignore
|
|
43
43
|
this.eventEmitter.emit('tokenAccountUpdate', tokenAccount);
|
|
@@ -51,7 +51,7 @@ class PollingTokenAccountSubscriber {
|
|
|
51
51
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
52
|
yield this.accountLoader.load();
|
|
53
53
|
const buffer = this.accountLoader.getAccountData(this.publicKey);
|
|
54
|
-
this.tokenAccount = token_1.parseTokenAccount(buffer);
|
|
54
|
+
this.tokenAccount = (0, token_1.parseTokenAccount)(buffer);
|
|
55
55
|
});
|
|
56
56
|
}
|
|
57
57
|
unsubscribe() {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { AccountToPoll, UserAccountEvents, UserAccountSubscriber } from './types';
|
|
2
|
+
import { AccountToPoll, UserAccountEvents, UserAccountSubscriber, UserPublicKeys } from './types';
|
|
3
3
|
import { Program } from '@project-serum/anchor';
|
|
4
4
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
5
|
import { EventEmitter } from 'events';
|
|
@@ -21,7 +21,7 @@ export declare class PollingUserAccountSubscriber implements UserAccountSubscrib
|
|
|
21
21
|
type: ClearingHouseConfigType;
|
|
22
22
|
constructor(program: Program, authority: PublicKey, accountLoader: BulkAccountLoader);
|
|
23
23
|
subscribe(): Promise<boolean>;
|
|
24
|
-
addToAccountLoader(): Promise<void>;
|
|
24
|
+
addToAccountLoader(userPublicKeys?: UserPublicKeys): Promise<void>;
|
|
25
25
|
fetchIfUnloaded(): Promise<void>;
|
|
26
26
|
fetch(): Promise<void>;
|
|
27
27
|
unsubscribe(): Promise<void>;
|
|
@@ -36,35 +36,56 @@ class PollingUserAccountSubscriber {
|
|
|
36
36
|
return true;
|
|
37
37
|
});
|
|
38
38
|
}
|
|
39
|
-
addToAccountLoader() {
|
|
39
|
+
addToAccountLoader(userPublicKeys) {
|
|
40
40
|
return __awaiter(this, void 0, void 0, function* () {
|
|
41
41
|
if (this.accountsToPoll.size > 0) {
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (userOrdersExist) {
|
|
44
|
+
if (!userPublicKeys) {
|
|
45
|
+
const userPublicKey = yield (0, addresses_1.getUserAccountPublicKey)(this.program.programId, this.authority);
|
|
46
|
+
const userAccount = (yield this.program.account.user.fetch(userPublicKey));
|
|
47
|
+
this.accountsToPoll.set(userPublicKey.toString(), {
|
|
48
|
+
key: 'user',
|
|
49
|
+
publicKey: userPublicKey,
|
|
50
|
+
eventType: 'userAccountData',
|
|
51
|
+
});
|
|
52
|
+
this.accountsToPoll.set(userAccount.positions.toString(), {
|
|
53
|
+
key: 'userPositions',
|
|
54
|
+
publicKey: userAccount.positions,
|
|
55
|
+
eventType: 'userPositionsData',
|
|
56
|
+
});
|
|
57
|
+
const userOrdersPublicKey = yield (0, addresses_1.getUserOrdersAccountPublicKey)(this.program.programId, userPublicKey);
|
|
59
58
|
this.accountsToPoll.set(userOrdersPublicKey.toString(), {
|
|
60
59
|
key: 'userOrders',
|
|
61
60
|
publicKey: userOrdersPublicKey,
|
|
62
61
|
eventType: 'userOrdersData',
|
|
63
62
|
});
|
|
64
63
|
}
|
|
64
|
+
else {
|
|
65
|
+
this.accountsToPoll.set(userPublicKeys.user.toString(), {
|
|
66
|
+
key: 'user',
|
|
67
|
+
publicKey: userPublicKeys.user,
|
|
68
|
+
eventType: 'userAccountData',
|
|
69
|
+
});
|
|
70
|
+
this.accountsToPoll.set(userPublicKeys.userPositions.toString(), {
|
|
71
|
+
key: 'userPositions',
|
|
72
|
+
publicKey: userPublicKeys.userPositions,
|
|
73
|
+
eventType: 'userPositionsData',
|
|
74
|
+
});
|
|
75
|
+
if (userPublicKeys.userOrders) {
|
|
76
|
+
this.accountsToPoll.set(userPublicKeys.userOrders.toString(), {
|
|
77
|
+
key: 'userOrders',
|
|
78
|
+
publicKey: userPublicKeys.userOrders,
|
|
79
|
+
eventType: 'userOrdersData',
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
65
83
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
66
84
|
accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer) => {
|
|
67
|
-
|
|
85
|
+
if (!buffer) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
68
89
|
this[accountToPoll.key] = account;
|
|
69
90
|
// @ts-ignore
|
|
70
91
|
this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
@@ -96,7 +117,7 @@ class PollingUserAccountSubscriber {
|
|
|
96
117
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
97
118
|
const buffer = this.accountLoader.getAccountData(accountToPoll.publicKey);
|
|
98
119
|
if (buffer) {
|
|
99
|
-
this[accountToPoll.key] = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
120
|
+
this[accountToPoll.key] = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
100
121
|
}
|
|
101
122
|
}
|
|
102
123
|
});
|
package/lib/accounts/types.d.ts
CHANGED
|
@@ -48,6 +48,11 @@ export interface ClearingHouseAccountSubscriber {
|
|
|
48
48
|
getOrderHistoryAccount(): OrderHistoryAccount;
|
|
49
49
|
type: ClearingHouseConfigType;
|
|
50
50
|
}
|
|
51
|
+
export declare type UserPublicKeys = {
|
|
52
|
+
user: PublicKey;
|
|
53
|
+
userPositions: PublicKey;
|
|
54
|
+
userOrders: PublicKey | undefined;
|
|
55
|
+
};
|
|
51
56
|
export interface UserAccountEvents {
|
|
52
57
|
userAccountData: (payload: UserAccount) => void;
|
|
53
58
|
userPositionsData: (payload: UserPositionsAccount) => void;
|
|
@@ -47,7 +47,7 @@ class WebSocketAccountSubscriber {
|
|
|
47
47
|
slot: newSlot,
|
|
48
48
|
};
|
|
49
49
|
if (newBuffer) {
|
|
50
|
-
this.data = this.program.account[this.accountName].coder.accounts.decode(utils_1.capitalize(this.accountName), newBuffer);
|
|
50
|
+
this.data = this.program.account[this.accountName].coder.accounts.decode((0, utils_1.capitalize)(this.accountName), newBuffer);
|
|
51
51
|
this.onChange(this.data);
|
|
52
52
|
}
|
|
53
53
|
return;
|
|
@@ -61,7 +61,7 @@ class WebSocketAccountSubscriber {
|
|
|
61
61
|
buffer: newBuffer,
|
|
62
62
|
slot: newSlot,
|
|
63
63
|
};
|
|
64
|
-
this.data = this.program.account[this.accountName].coder.accounts.decode(utils_1.capitalize(this.accountName), newBuffer);
|
|
64
|
+
this.data = this.program.account[this.accountName].coder.accounts.decode((0, utils_1.capitalize)(this.accountName), newBuffer);
|
|
65
65
|
this.onChange(this.data);
|
|
66
66
|
}
|
|
67
67
|
}
|
|
@@ -35,7 +35,7 @@ class WebSocketClearingHouseAccountSubscriber {
|
|
|
35
35
|
this.subscriptionPromise = new Promise((res) => {
|
|
36
36
|
this.subscriptionPromiseResolver = res;
|
|
37
37
|
});
|
|
38
|
-
const statePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
38
|
+
const statePublicKey = yield (0, addresses_1.getClearingHouseStateAccountPublicKey)(this.program.programId);
|
|
39
39
|
// create and activate main state account subscription
|
|
40
40
|
this.stateAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('state', this.program, statePublicKey);
|
|
41
41
|
yield this.stateAccountSubscriber.subscribe((data) => {
|
|
@@ -27,7 +27,7 @@ class WebSocketUserAccountSubscriber {
|
|
|
27
27
|
if (this.isSubscribed) {
|
|
28
28
|
return true;
|
|
29
29
|
}
|
|
30
|
-
const userPublicKey = yield addresses_1.getUserAccountPublicKey(this.program.programId, this.authority);
|
|
30
|
+
const userPublicKey = yield (0, addresses_1.getUserAccountPublicKey)(this.program.programId, this.authority);
|
|
31
31
|
this.userDataAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('user', this.program, userPublicKey);
|
|
32
32
|
yield this.userDataAccountSubscriber.subscribe((data) => {
|
|
33
33
|
this.eventEmitter.emit('userAccountData', data);
|
|
@@ -39,7 +39,7 @@ class WebSocketUserAccountSubscriber {
|
|
|
39
39
|
this.eventEmitter.emit('userPositionsData', data);
|
|
40
40
|
this.eventEmitter.emit('update');
|
|
41
41
|
});
|
|
42
|
-
const userOrdersPublicKey = yield addresses_1.getUserOrdersAccountPublicKey(this.program.programId, userPublicKey);
|
|
42
|
+
const userOrdersPublicKey = yield (0, addresses_1.getUserOrdersAccountPublicKey)(this.program.programId, userPublicKey);
|
|
43
43
|
this.userOrdersAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('userOrders', this.program, userOrdersPublicKey);
|
|
44
44
|
yield this.userOrdersAccountSubscriber.subscribe((data) => {
|
|
45
45
|
this.eventEmitter.emit('userOrdersData', data);
|
package/lib/admin.js
CHANGED
|
@@ -41,8 +41,8 @@ const amm_1 = require("./math/amm");
|
|
|
41
41
|
const clearingHouse_2 = require("./factory/clearingHouse");
|
|
42
42
|
class Admin extends clearingHouse_1.ClearingHouse {
|
|
43
43
|
static from(connection, wallet, clearingHouseProgramId, opts = anchor_1.Provider.defaultOptions()) {
|
|
44
|
-
const config = clearingHouse_2.getWebSocketClearingHouseConfig(connection, wallet, clearingHouseProgramId, opts);
|
|
45
|
-
return clearingHouse_2.getAdmin(config);
|
|
44
|
+
const config = (0, clearingHouse_2.getWebSocketClearingHouseConfig)(connection, wallet, clearingHouseProgramId, opts);
|
|
45
|
+
return (0, clearingHouse_2.getAdmin)(config);
|
|
46
46
|
}
|
|
47
47
|
initialize(usdcMint, adminControlsPrices) {
|
|
48
48
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -61,7 +61,7 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
61
61
|
const tradeHistory = anchor.web3.Keypair.generate();
|
|
62
62
|
const liquidationHistory = anchor.web3.Keypair.generate();
|
|
63
63
|
const curveHistory = anchor.web3.Keypair.generate();
|
|
64
|
-
const [clearingHouseStatePublicKey, clearingHouseNonce] = yield addresses_1.getClearingHouseStateAccountPublicKeyAndNonce(this.program.programId);
|
|
64
|
+
const [clearingHouseStatePublicKey, clearingHouseNonce] = yield (0, addresses_1.getClearingHouseStateAccountPublicKeyAndNonce)(this.program.programId);
|
|
65
65
|
const initializeTx = yield this.program.transaction.initialize(clearingHouseNonce, collateralVaultNonce, insuranceVaultNonce, adminControlsPrices, {
|
|
66
66
|
accounts: {
|
|
67
67
|
admin: this.wallet.publicKey,
|
|
@@ -118,8 +118,8 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
118
118
|
initializeOrderState() {
|
|
119
119
|
return __awaiter(this, void 0, void 0, function* () {
|
|
120
120
|
const orderHistory = anchor.web3.Keypair.generate();
|
|
121
|
-
const [orderStatePublicKey, orderStateNonce] = yield addresses_1.getOrderStateAccountPublicKeyAndNonce(this.program.programId);
|
|
122
|
-
const clearingHouseStatePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
121
|
+
const [orderStatePublicKey, orderStateNonce] = yield (0, addresses_1.getOrderStateAccountPublicKeyAndNonce)(this.program.programId);
|
|
122
|
+
const clearingHouseStatePublicKey = yield (0, addresses_1.getClearingHouseStateAccountPublicKey)(this.program.programId);
|
|
123
123
|
const initializeOrderStateTx = yield this.program.transaction.initializeOrderState(orderStateNonce, {
|
|
124
124
|
accounts: {
|
|
125
125
|
admin: this.wallet.publicKey,
|
|
@@ -202,8 +202,8 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
202
202
|
moveAmmToPrice(marketIndex, targetPrice) {
|
|
203
203
|
return __awaiter(this, void 0, void 0, function* () {
|
|
204
204
|
const market = this.getMarket(marketIndex);
|
|
205
|
-
const [direction, tradeSize, _] = trade_1.calculateTargetPriceTrade(market, targetPrice);
|
|
206
|
-
const [newQuoteAssetAmount, newBaseAssetAmount] = amm_1.calculateAmmReservesAfterSwap(market.amm, 'quote', tradeSize, amm_1.getSwapDirection('quote', direction));
|
|
205
|
+
const [direction, tradeSize, _] = (0, trade_1.calculateTargetPriceTrade)(market, targetPrice);
|
|
206
|
+
const [newQuoteAssetAmount, newBaseAssetAmount] = (0, amm_1.calculateAmmReservesAfterSwap)(market.amm, 'quote', tradeSize, (0, amm_1.getSwapDirection)('quote', direction));
|
|
207
207
|
const state = this.getStateAccount();
|
|
208
208
|
return yield this.program.rpc.moveAmmPrice(newBaseAssetAmount, newQuoteAssetAmount, marketIndex, {
|
|
209
209
|
accounts: {
|
package/lib/clearingHouse.js
CHANGED
|
@@ -38,7 +38,6 @@ const anchor = __importStar(require("@project-serum/anchor"));
|
|
|
38
38
|
const clearing_house_json_1 = __importDefault(require("./idl/clearing_house.json"));
|
|
39
39
|
const web3_js_1 = require("@solana/web3.js");
|
|
40
40
|
const addresses_1 = require("./addresses");
|
|
41
|
-
const defaultTxSender_1 = require("./tx/defaultTxSender");
|
|
42
41
|
const utils_1 = require("./tx/utils");
|
|
43
42
|
const clearingHouse_1 = require("./factory/clearingHouse");
|
|
44
43
|
/**
|
|
@@ -74,8 +73,8 @@ class ClearingHouse {
|
|
|
74
73
|
* @returns
|
|
75
74
|
*/
|
|
76
75
|
static from(connection, wallet, clearingHouseProgramId, opts = anchor_1.Provider.defaultOptions()) {
|
|
77
|
-
const config = clearingHouse_1.getWebSocketClearingHouseConfig(connection, wallet, clearingHouseProgramId, opts);
|
|
78
|
-
return clearingHouse_1.getClearingHouse(config);
|
|
76
|
+
const config = (0, clearingHouse_1.getWebSocketClearingHouseConfig)(connection, wallet, clearingHouseProgramId, opts);
|
|
77
|
+
return (0, clearingHouse_1.getClearingHouse)(config);
|
|
79
78
|
}
|
|
80
79
|
/**
|
|
81
80
|
*
|
|
@@ -127,7 +126,7 @@ class ClearingHouse {
|
|
|
127
126
|
if (this.statePublicKey) {
|
|
128
127
|
return this.statePublicKey;
|
|
129
128
|
}
|
|
130
|
-
this.statePublicKey = yield addresses_1.getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
129
|
+
this.statePublicKey = yield (0, addresses_1.getClearingHouseStateAccountPublicKey)(this.program.programId);
|
|
131
130
|
return this.statePublicKey;
|
|
132
131
|
});
|
|
133
132
|
}
|
|
@@ -169,7 +168,7 @@ class ClearingHouse {
|
|
|
169
168
|
if (this.orderStatePublicKey) {
|
|
170
169
|
return this.orderStatePublicKey;
|
|
171
170
|
}
|
|
172
|
-
this.orderStatePublicKey = yield addresses_1.getOrderStateAccountPublicKey(this.program.programId);
|
|
171
|
+
this.orderStatePublicKey = yield (0, addresses_1.getOrderStateAccountPublicKey)(this.program.programId);
|
|
173
172
|
return this.orderStatePublicKey;
|
|
174
173
|
});
|
|
175
174
|
}
|
|
@@ -183,11 +182,11 @@ class ClearingHouse {
|
|
|
183
182
|
updateWallet(newWallet) {
|
|
184
183
|
const newProvider = new anchor_1.Provider(this.connection, newWallet, this.opts);
|
|
185
184
|
const newProgram = new anchor_1.Program(clearing_house_json_1.default, this.program.programId, newProvider);
|
|
186
|
-
|
|
185
|
+
// Update provider for txSender with new wallet details
|
|
186
|
+
this.txSender.provider = newProvider;
|
|
187
187
|
this.wallet = newWallet;
|
|
188
188
|
this.provider = newProvider;
|
|
189
189
|
this.program = newProgram;
|
|
190
|
-
this.txSender = newTxSender;
|
|
191
190
|
this.userAccountPublicKey = undefined;
|
|
192
191
|
this.userAccount = undefined;
|
|
193
192
|
}
|
|
@@ -203,7 +202,7 @@ class ClearingHouse {
|
|
|
203
202
|
}
|
|
204
203
|
getInitializeUserInstructions() {
|
|
205
204
|
return __awaiter(this, void 0, void 0, function* () {
|
|
206
|
-
const [userAccountPublicKey, userAccountNonce] = yield addresses_1.getUserAccountPublicKeyAndNonce(this.program.programId, this.wallet.publicKey);
|
|
205
|
+
const [userAccountPublicKey, userAccountNonce] = yield (0, addresses_1.getUserAccountPublicKeyAndNonce)(this.program.programId, this.wallet.publicKey);
|
|
207
206
|
const remainingAccounts = [];
|
|
208
207
|
const optionalAccounts = {
|
|
209
208
|
whitelistToken: false,
|
|
@@ -244,7 +243,7 @@ class ClearingHouse {
|
|
|
244
243
|
if (!userAccountPublicKey) {
|
|
245
244
|
userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
246
245
|
}
|
|
247
|
-
const [userOrdersAccountPublicKey, userOrdersAccountNonce] = yield addresses_1.getUserOrdersAccountPublicKeyAndNonce(this.program.programId, userAccountPublicKey);
|
|
246
|
+
const [userOrdersAccountPublicKey, userOrdersAccountNonce] = yield (0, addresses_1.getUserOrdersAccountPublicKeyAndNonce)(this.program.programId, userAccountPublicKey);
|
|
248
247
|
return yield this.program.instruction.initializeUserOrders(userOrdersAccountNonce, {
|
|
249
248
|
accounts: {
|
|
250
249
|
user: userAccountPublicKey,
|
|
@@ -266,7 +265,7 @@ class ClearingHouse {
|
|
|
266
265
|
if (this.userAccountPublicKey) {
|
|
267
266
|
return this.userAccountPublicKey;
|
|
268
267
|
}
|
|
269
|
-
this.userAccountPublicKey = yield addresses_1.getUserAccountPublicKey(this.program.programId, this.wallet.publicKey);
|
|
268
|
+
this.userAccountPublicKey = yield (0, addresses_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey);
|
|
270
269
|
return this.userAccountPublicKey;
|
|
271
270
|
});
|
|
272
271
|
}
|
|
@@ -288,7 +287,7 @@ class ClearingHouse {
|
|
|
288
287
|
if (this.userOrdersAccountPublicKey) {
|
|
289
288
|
return this.userOrdersAccountPublicKey;
|
|
290
289
|
}
|
|
291
|
-
this.userOrdersAccountPublicKey = yield addresses_1.getUserOrdersAccountPublicKey(this.program.programId, yield this.getUserAccountPublicKey());
|
|
290
|
+
this.userOrdersAccountPublicKey = yield (0, addresses_1.getUserOrdersAccountPublicKey)(this.program.programId, yield this.getUserAccountPublicKey());
|
|
292
291
|
return this.userOrdersAccountPublicKey;
|
|
293
292
|
});
|
|
294
293
|
}
|
|
@@ -381,7 +380,7 @@ class ClearingHouse {
|
|
|
381
380
|
}
|
|
382
381
|
withdrawCollateral(amount, collateralAccountPublicKey) {
|
|
383
382
|
return __awaiter(this, void 0, void 0, function* () {
|
|
384
|
-
return this.txSender.send(utils_1.wrapInTx(yield this.getWithdrawCollateralIx(amount, collateralAccountPublicKey)), [], this.opts);
|
|
383
|
+
return this.txSender.send((0, utils_1.wrapInTx)(yield this.getWithdrawCollateralIx(amount, collateralAccountPublicKey)), [], this.opts);
|
|
385
384
|
});
|
|
386
385
|
}
|
|
387
386
|
getWithdrawCollateralIx(amount, collateralAccountPublicKey) {
|
|
@@ -410,7 +409,7 @@ class ClearingHouse {
|
|
|
410
409
|
}
|
|
411
410
|
openPosition(direction, amount, marketIndex, limitPrice, discountToken, referrer) {
|
|
412
411
|
return __awaiter(this, void 0, void 0, function* () {
|
|
413
|
-
return yield this.txSender.send(utils_1.wrapInTx(yield this.getOpenPositionIx(direction, amount, marketIndex, limitPrice, discountToken, referrer)), [], this.opts);
|
|
412
|
+
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getOpenPositionIx(direction, amount, marketIndex, limitPrice, discountToken, referrer)), [], this.opts);
|
|
414
413
|
});
|
|
415
414
|
}
|
|
416
415
|
getOpenPositionIx(direction, amount, marketIndex, limitPrice, discountToken, referrer) {
|
|
@@ -476,7 +475,7 @@ class ClearingHouse {
|
|
|
476
475
|
}
|
|
477
476
|
placeOrder(orderParams, discountToken, referrer) {
|
|
478
477
|
return __awaiter(this, void 0, void 0, function* () {
|
|
479
|
-
return yield this.txSender.send(utils_1.wrapInTx(yield this.getPlaceOrderIx(orderParams, discountToken, referrer)), [], this.opts);
|
|
478
|
+
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getPlaceOrderIx(orderParams, discountToken, referrer)), [], this.opts);
|
|
480
479
|
});
|
|
481
480
|
}
|
|
482
481
|
getPlaceOrderIx(orderParams, discountToken, referrer) {
|
|
@@ -528,7 +527,7 @@ class ClearingHouse {
|
|
|
528
527
|
}
|
|
529
528
|
cancelOrder(orderId) {
|
|
530
529
|
return __awaiter(this, void 0, void 0, function* () {
|
|
531
|
-
return yield this.txSender.send(utils_1.wrapInTx(yield this.getCancelOrderIx(orderId)), [], this.opts);
|
|
530
|
+
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getCancelOrderIx(orderId)), [], this.opts);
|
|
532
531
|
});
|
|
533
532
|
}
|
|
534
533
|
getCancelOrderIx(orderId) {
|
|
@@ -555,7 +554,7 @@ class ClearingHouse {
|
|
|
555
554
|
}
|
|
556
555
|
cancelOrderByUserId(userOrderId) {
|
|
557
556
|
return __awaiter(this, void 0, void 0, function* () {
|
|
558
|
-
return yield this.txSender.send(utils_1.wrapInTx(yield this.getCancelOrderByUserIdIx(userOrderId)), [], this.opts);
|
|
557
|
+
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getCancelOrderByUserIdIx(userOrderId)), [], this.opts);
|
|
559
558
|
});
|
|
560
559
|
}
|
|
561
560
|
getCancelOrderByUserIdIx(userOrderId) {
|
|
@@ -582,7 +581,7 @@ class ClearingHouse {
|
|
|
582
581
|
}
|
|
583
582
|
fillOrder(userAccountPublicKey, userOrdersAccountPublicKey, order) {
|
|
584
583
|
return __awaiter(this, void 0, void 0, function* () {
|
|
585
|
-
return yield this.txSender.send(utils_1.wrapInTx(yield this.getFillOrderIx(userAccountPublicKey, userOrdersAccountPublicKey, order)), [], this.opts);
|
|
584
|
+
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getFillOrderIx(userAccountPublicKey, userOrdersAccountPublicKey, order)), [], this.opts);
|
|
586
585
|
});
|
|
587
586
|
}
|
|
588
587
|
getFillOrderIx(userAccountPublicKey, userOrdersAccountPublicKey, order) {
|
|
@@ -640,7 +639,7 @@ class ClearingHouse {
|
|
|
640
639
|
}
|
|
641
640
|
placeAndFillOrder(orderParams, discountToken, referrer) {
|
|
642
641
|
return __awaiter(this, void 0, void 0, function* () {
|
|
643
|
-
return yield this.txSender.send(utils_1.wrapInTx(yield this.getPlaceAndFillOrderIx(orderParams, discountToken, referrer)), [], this.opts);
|
|
642
|
+
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getPlaceAndFillOrderIx(orderParams, discountToken, referrer)), [], this.opts);
|
|
644
643
|
});
|
|
645
644
|
}
|
|
646
645
|
getPlaceAndFillOrderIx(orderParams, discountToken, referrer) {
|
|
@@ -701,7 +700,7 @@ class ClearingHouse {
|
|
|
701
700
|
*/
|
|
702
701
|
closePosition(marketIndex, discountToken, referrer) {
|
|
703
702
|
return __awaiter(this, void 0, void 0, function* () {
|
|
704
|
-
return yield this.txSender.send(utils_1.wrapInTx(yield this.getClosePositionIx(marketIndex, discountToken, referrer)), [], this.opts);
|
|
703
|
+
return yield this.txSender.send((0, utils_1.wrapInTx)(yield this.getClosePositionIx(marketIndex, discountToken, referrer)), [], this.opts);
|
|
705
704
|
});
|
|
706
705
|
}
|
|
707
706
|
getClosePositionIx(marketIndex, discountToken, referrer) {
|
|
@@ -749,7 +748,7 @@ class ClearingHouse {
|
|
|
749
748
|
}
|
|
750
749
|
liquidate(liquidateeUserAccountPublicKey) {
|
|
751
750
|
return __awaiter(this, void 0, void 0, function* () {
|
|
752
|
-
return this.txSender.send(utils_1.wrapInTx(yield this.getLiquidateIx(liquidateeUserAccountPublicKey)), [], this.opts);
|
|
751
|
+
return this.txSender.send((0, utils_1.wrapInTx)(yield this.getLiquidateIx(liquidateeUserAccountPublicKey)), [], this.opts);
|
|
753
752
|
});
|
|
754
753
|
}
|
|
755
754
|
getLiquidateIx(liquidateeUserAccountPublicKey) {
|
|
@@ -793,7 +792,7 @@ class ClearingHouse {
|
|
|
793
792
|
}
|
|
794
793
|
updateFundingRate(oracle, marketIndex) {
|
|
795
794
|
return __awaiter(this, void 0, void 0, function* () {
|
|
796
|
-
return this.txSender.send(utils_1.wrapInTx(yield this.getUpdateFundingRateIx(oracle, marketIndex)), [], this.opts);
|
|
795
|
+
return this.txSender.send((0, utils_1.wrapInTx)(yield this.getUpdateFundingRateIx(oracle, marketIndex)), [], this.opts);
|
|
797
796
|
});
|
|
798
797
|
}
|
|
799
798
|
getUpdateFundingRateIx(oracle, marketIndex) {
|
|
@@ -811,7 +810,7 @@ class ClearingHouse {
|
|
|
811
810
|
}
|
|
812
811
|
settleFundingPayment(userAccount, userPositionsAccount) {
|
|
813
812
|
return __awaiter(this, void 0, void 0, function* () {
|
|
814
|
-
return this.txSender.send(utils_1.wrapInTx(yield this.getSettleFundingPaymentIx(userAccount, userPositionsAccount)), [], this.opts);
|
|
813
|
+
return this.txSender.send((0, utils_1.wrapInTx)(yield this.getSettleFundingPaymentIx(userAccount, userPositionsAccount)), [], this.opts);
|
|
815
814
|
});
|
|
816
815
|
}
|
|
817
816
|
getSettleFundingPaymentIx(userAccount, userPositionsAccount) {
|