@drift-labs/sdk 0.1.18 → 0.1.20
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 +32 -0
- package/lib/accounts/bulkAccountLoader.d.ts.map +1 -0
- package/lib/accounts/bulkAccountLoader.js +166 -0
- package/lib/accounts/bulkUserSubscription.d.ts +8 -0
- package/lib/accounts/bulkUserSubscription.d.ts.map +1 -0
- package/lib/accounts/bulkUserSubscription.js +28 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +46 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +206 -0
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +26 -0
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/pollingTokenAccountSubscriber.js +79 -0
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +31 -0
- package/lib/accounts/pollingUserAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/pollingUserAccountSubscriber.js +123 -0
- package/lib/accounts/types.d.ts +30 -0
- package/lib/accounts/types.d.ts.map +1 -1
- package/lib/accounts/utils.d.ts +2 -0
- package/lib/accounts/utils.d.ts.map +1 -0
- package/lib/accounts/utils.js +7 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -2
- package/lib/accounts/webSocketAccountSubscriber.d.ts.map +1 -1
- package/lib/accounts/webSocketAccountSubscriber.js +43 -12
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +4 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +4 -3
- package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +4 -2
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +4 -3
- package/lib/admin.d.ts +1 -0
- package/lib/admin.d.ts.map +1 -1
- package/lib/admin.js +20 -11
- package/lib/clearingHouse.d.ts +12 -2
- package/lib/clearingHouse.d.ts.map +1 -1
- package/lib/clearingHouse.js +19 -8
- package/lib/clearingHouseUser.d.ts +11 -3
- package/lib/clearingHouseUser.d.ts.map +1 -1
- package/lib/clearingHouseUser.js +33 -23
- package/lib/constants/markets.d.ts +2 -1
- package/lib/constants/markets.d.ts.map +1 -1
- package/lib/constants/markets.js +20 -15
- package/lib/constants/numericConstants.d.ts +3 -1
- package/lib/constants/numericConstants.d.ts.map +1 -1
- package/lib/constants/numericConstants.js +15 -17
- package/lib/examples/makeTradeExample.js +1 -1
- package/lib/factory/clearingHouse.d.ts +26 -0
- package/lib/factory/clearingHouse.d.ts.map +1 -0
- package/lib/factory/clearingHouse.js +64 -0
- package/lib/factory/clearingHouseUser.d.ts +20 -0
- package/lib/factory/clearingHouseUser.d.ts.map +1 -0
- package/lib/factory/clearingHouseUser.js +34 -0
- package/lib/index.d.ts +8 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +9 -2
- package/lib/math/conversion.d.ts +1 -1
- package/lib/math/conversion.d.ts.map +1 -1
- package/lib/math/insuranceFund.d.ts +2 -1
- package/lib/math/insuranceFund.d.ts.map +1 -1
- package/lib/math/insuranceFund.js +3 -6
- package/lib/math/position.d.ts +2 -1
- package/lib/math/position.d.ts.map +1 -1
- package/lib/math/position.js +2 -5
- package/lib/math/utils.d.ts +2 -1
- package/lib/math/utils.d.ts.map +1 -1
- package/lib/math/utils.js +3 -3
- package/lib/mockUSDCFaucet.d.ts +2 -1
- package/lib/mockUSDCFaucet.d.ts.map +1 -1
- package/lib/token/index.d.ts +4 -0
- package/lib/token/index.d.ts.map +1 -0
- package/lib/token/index.js +38 -0
- package/lib/types.d.ts +3 -1
- package/lib/types.d.ts.map +1 -1
- package/package.json +11 -3
- package/src/accounts/bulkAccountLoader.ts +207 -0
- package/src/accounts/bulkUserSubscription.ts +28 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +293 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +166 -0
- package/src/accounts/types.ts +38 -0
- package/src/accounts/utils.ts +3 -0
- package/src/accounts/webSocketAccountSubscriber.ts +67 -17
- package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +4 -1
- package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +4 -1
- package/src/admin.ts +27 -18
- package/src/clearingHouse.ts +25 -17
- package/src/clearingHouseUser.ts +26 -6
- package/src/constants/markets.ts +9 -1
- package/src/constants/numericConstants.ts +2 -1
- package/src/examples/makeTradeExample.ts +4 -1
- package/src/factory/clearingHouse.ts +125 -0
- package/src/factory/clearingHouseUser.ts +73 -0
- package/src/index.ts +8 -2
- package/src/math/conversion.ts +1 -1
- package/src/math/insuranceFund.ts +1 -1
- package/src/math/position.ts +1 -1
- package/src/math/utils.ts +1 -1
- package/src/mockUSDCFaucet.ts +1 -1
- package/src/token/index.ts +37 -0
- package/src/types.ts +2 -1
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
package/lib/math/conversion.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.d.ts","sourceRoot":"","sources":["../../src/math/conversion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"conversion.d.ts","sourceRoot":"","sources":["../../src/math/conversion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,KAAK,CAAC;AAMzB,eAAO,MAAM,eAAe,cAChB,EAAE,cACF,EAAE,WAOb,CAAC;AAEF,eAAO,MAAM,8BAA8B,oBAAqB,EAAE,WAKjE,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
1
2
|
import { MarketsAccount, StateAccount } from '../types';
|
|
2
|
-
import BN from '
|
|
3
|
+
import { BN } from '../';
|
|
3
4
|
import { Connection } from '@solana/web3.js';
|
|
4
5
|
/**
|
|
5
6
|
* In the case of a levered loss, the exchange first pays out undistributed fees and then the insurance fund.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"insuranceFund.d.ts","sourceRoot":"","sources":["../../src/math/insuranceFund.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"insuranceFund.d.ts","sourceRoot":"","sources":["../../src/math/insuranceFund.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,EAAE,EAAE,MAAM,KAAK,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;;;;;;;;GASG;AACH,wBAAsB,0BAA0B,CAC/C,UAAU,EAAE,UAAU,EACtB,KAAK,EAAE,YAAY,EACnB,cAAc,EAAE,cAAc,GAC5B,OAAO,CAAC,EAAE,CAAC,CAUb"}
|
|
@@ -8,12 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
12
|
exports.calculateInsuranceFundSize = void 0;
|
|
16
|
-
const
|
|
13
|
+
const __1 = require("../");
|
|
17
14
|
/**
|
|
18
15
|
* In the case of a levered loss, the exchange first pays out undistributed fees and then the insurance fund.
|
|
19
16
|
* Thus the de facto size of the insurance fund is the amount in the insurance vault plus the sum of each markets
|
|
@@ -27,9 +24,9 @@ const bn_js_1 = __importDefault(require("bn.js"));
|
|
|
27
24
|
function calculateInsuranceFundSize(connection, state, marketsAccount) {
|
|
28
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
26
|
const insuranceVaultPublicKey = state.insuranceVault;
|
|
30
|
-
const insuranceVaultAmount = new
|
|
27
|
+
const insuranceVaultAmount = new __1.BN((yield connection.getTokenAccountBalance(insuranceVaultPublicKey)).value.amount);
|
|
31
28
|
return marketsAccount.markets.reduce((insuranceVaultAmount, market) => {
|
|
32
|
-
return insuranceVaultAmount.add(market.amm.totalFee.div(new
|
|
29
|
+
return insuranceVaultAmount.add(market.amm.totalFee.div(new __1.BN(2)));
|
|
33
30
|
}, insuranceVaultAmount);
|
|
34
31
|
});
|
|
35
32
|
}
|
package/lib/math/position.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"position.d.ts","sourceRoot":"","sources":["../../src/math/position.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"position.d.ts","sourceRoot":"","sources":["../../src/math/position.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,KAAK,CAAC;AAWzB,OAAO,EAAE,MAAM,EAAqB,YAAY,EAAE,MAAM,UAAU,CAAC;AAGnE;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACtC,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,YAAY,GACxB,EAAE,CA6BJ;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CACnC,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,YAAY,EAC5B,WAAW,UAAQ,GACjB,EAAE,CAwBJ;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAC1C,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,YAAY,GAC1B,EAAE,CAoBJ;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,YAAY,EAAE,YAAY,GAAG,EAAE,CAUlE"}
|
package/lib/math/position.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.calculateEntryPrice = exports.calculatePositionFundingPNL = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
|
|
7
|
-
const
|
|
4
|
+
const __1 = require("../");
|
|
8
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
9
6
|
const types_1 = require("../types");
|
|
10
7
|
const amm_1 = require("./amm");
|
|
@@ -86,7 +83,7 @@ function calculatePositionFundingPNL(market, marketPosition) {
|
|
|
86
83
|
.mul(marketPosition.baseAssetAmount)
|
|
87
84
|
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
88
85
|
.div(numericConstants_1.FUNDING_PAYMENT_PRECISION)
|
|
89
|
-
.mul(new
|
|
86
|
+
.mul(new __1.BN(-1));
|
|
90
87
|
return perPositionFundingRate;
|
|
91
88
|
}
|
|
92
89
|
exports.calculatePositionFundingPNL = calculatePositionFundingPNL;
|
package/lib/math/utils.d.ts
CHANGED
package/lib/math/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/math/utils.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/math/utils.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,KAAK,CAAC;AAEzB,eAAO,MAAM,YAAY,iCA0BxB,CAAC"}
|
package/lib/math/utils.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.squareRootBN = void 0;
|
|
4
|
-
const
|
|
5
|
-
const squareRootBN = (n, closeness = new
|
|
4
|
+
const __1 = require("../");
|
|
5
|
+
const squareRootBN = (n, closeness = new __1.BN(1)) => {
|
|
6
6
|
// Assuming the sqrt of n as n only
|
|
7
7
|
let x = n;
|
|
8
8
|
// The closed guess will be stored in the root
|
|
9
9
|
let root;
|
|
10
10
|
// To count the number of iterations
|
|
11
11
|
let count = 0;
|
|
12
|
-
const TWO = new
|
|
12
|
+
const TWO = new __1.BN(2);
|
|
13
13
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
14
14
|
while (count < Number.MAX_SAFE_INTEGER) {
|
|
15
15
|
count++;
|
package/lib/mockUSDCFaucet.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
1
2
|
import * as anchor from '@project-serum/anchor';
|
|
2
3
|
import { Program, Provider } from '@project-serum/anchor';
|
|
3
4
|
import { AccountInfo } from '@solana/spl-token';
|
|
4
5
|
import { ConfirmOptions, Connection, PublicKey, TransactionInstruction, TransactionSignature } from '@solana/web3.js';
|
|
5
|
-
import BN from '
|
|
6
|
+
import { BN } from '.';
|
|
6
7
|
import { IWallet } from './types';
|
|
7
8
|
export declare class MockUSDCFaucet {
|
|
8
9
|
connection: Connection;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mockUSDCFaucet.d.ts","sourceRoot":"","sources":["../src/mockUSDCFaucet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAO,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EACN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;IAanB,qCAAqC,CACjD,aAAa,EAAE,SAAS,EACxB,MAAM,EAAE,EAAE,GACR,OAAO,CAAC,CAAC,SAAS,EAAE,oBAAoB,CAAC,CAAC;IAWhC,iDAAiD,CAC7D,aAAa,EAAE,SAAS,EACxB,MAAM,EAAE,EAAE,GACR,OAAO,CAAC,CAAC,SAAS,EAAE,sBAAsB,EAAE,sBAAsB,CAAC,CAAC;IA8B1D,gCAAgC,CAAC,KAAK,EAAE;QACpD,UAAU,EAAE,SAAS,CAAC;KACtB,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC;IAWrB,mBAAmB,CAAC,KAAK,EAAE;QACvC,UAAU,EAAE,SAAS,CAAC;KACtB,GAAG,OAAO,CAAC,WAAW,CAAC;IAiBX,uBAAuB,CAAC,KAAK,EAAE;QAC3C,UAAU,EAAE,SAAS,CAAC;QACtB,QAAQ,EAAE,CAAC,WAAW,EAAE,WAAW,KAAK,IAAI,CAAC;KAC7C,GAAG,OAAO,CAAC,OAAO,CAAC;CAuBpB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/token/index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAsB,MAAM,mBAAmB,CAAC;AAGpE,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CAiC3D"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseTokenAccount = void 0;
|
|
4
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
function parseTokenAccount(data) {
|
|
7
|
+
const accountInfo = spl_token_1.AccountLayout.decode(data);
|
|
8
|
+
accountInfo.mint = new web3_js_1.PublicKey(accountInfo.mint);
|
|
9
|
+
accountInfo.owner = new web3_js_1.PublicKey(accountInfo.owner);
|
|
10
|
+
accountInfo.amount = spl_token_1.u64.fromBuffer(accountInfo.amount);
|
|
11
|
+
if (accountInfo.delegateOption === 0) {
|
|
12
|
+
accountInfo.delegate = null;
|
|
13
|
+
// eslint-disable-next-line new-cap
|
|
14
|
+
accountInfo.delegatedAmount = new spl_token_1.u64(0);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
accountInfo.delegate = new web3_js_1.PublicKey(accountInfo.delegate);
|
|
18
|
+
accountInfo.delegatedAmount = spl_token_1.u64.fromBuffer(accountInfo.delegatedAmount);
|
|
19
|
+
}
|
|
20
|
+
accountInfo.isInitialized = accountInfo.state !== 0;
|
|
21
|
+
accountInfo.isFrozen = accountInfo.state === 2;
|
|
22
|
+
if (accountInfo.isNativeOption === 1) {
|
|
23
|
+
accountInfo.rentExemptReserve = spl_token_1.u64.fromBuffer(accountInfo.isNative);
|
|
24
|
+
accountInfo.isNative = true;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
accountInfo.rentExemptReserve = null;
|
|
28
|
+
accountInfo.isNative = false;
|
|
29
|
+
}
|
|
30
|
+
if (accountInfo.closeAuthorityOption === 0) {
|
|
31
|
+
accountInfo.closeAuthority = null;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
accountInfo.closeAuthority = new web3_js_1.PublicKey(accountInfo.closeAuthority);
|
|
35
|
+
}
|
|
36
|
+
return accountInfo;
|
|
37
|
+
}
|
|
38
|
+
exports.parseTokenAccount = parseTokenAccount;
|
package/lib/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
1
2
|
import { PublicKey, Transaction } from '@solana/web3.js';
|
|
2
|
-
import BN from '
|
|
3
|
+
import { BN } from '.';
|
|
3
4
|
export declare class SwapDirection {
|
|
4
5
|
static readonly ADD: {
|
|
5
6
|
add: {};
|
|
@@ -221,6 +222,7 @@ export declare type AMM = {
|
|
|
221
222
|
totalFeeWithdrawn: BN;
|
|
222
223
|
totalFee: BN;
|
|
223
224
|
minimumTradeSize: BN;
|
|
225
|
+
lastOraclePrice: BN;
|
|
224
226
|
};
|
|
225
227
|
export declare type UserPosition = {
|
|
226
228
|
baseAssetAmount: BN;
|
package/lib/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,EAAE,EAAE,MAAM,GAAG,CAAC;AAGvB,qBAAa,aAAa;IACzB,MAAM,CAAC,QAAQ,CAAC,GAAG;;MAAe;IAClC,MAAM,CAAC,QAAQ,CAAC,MAAM;;MAAkB;CACxC;AACD,qBAAa,iBAAiB;IAC7B,MAAM,CAAC,QAAQ,CAAC,IAAI;;MAAgB;IACpC,MAAM,CAAC,QAAQ,CAAC,KAAK;;MAAiB;CACtC;AAED,qBAAa,YAAY;IACxB,MAAM,CAAC,QAAQ,CAAC,IAAI;;MAAgB;IACpC,MAAM,CAAC,QAAQ,CAAC,WAAW;;MAAuB;CAClD;AAED,oBAAY,SAAS;IACpB,IAAI,IAAI;IACR,GAAG,IAAI;IACP,IAAI,IAAI;CACR;AAED,oBAAY,gBAAgB,GACzB,GAAG,GACH,GAAG,GACH,IAAI,GACJ,IAAI,GACJ,KAAK,GACL,GAAG,GACH,GAAG,GACH,GAAG,CAAC;AAGP,oBAAY,mBAAmB,GAAG;IACjC,IAAI,EAAE,EAAE,CAAC;IACT,YAAY,EAAE,WAAW,EAAE,CAAC;CAC5B,CAAC;AAEF,oBAAY,qBAAqB,GAAG;IACnC,IA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version": "0.1.20",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"author": "crispheaney",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"url": "git@github.com:drift-labs/protocol-v1.git"
|
|
11
11
|
},
|
|
12
12
|
"scripts": {
|
|
13
|
+
"lint": "eslint './**/*.{ts,tsx}' --quiet",
|
|
13
14
|
"build": "yarn clean && tsc",
|
|
14
15
|
"clean": "rm -rf lib",
|
|
15
16
|
"patch-and-pub": "npm version patch --force && npm publish"
|
|
@@ -31,8 +32,15 @@
|
|
|
31
32
|
"@pythnetwork/client": "^2.5.1",
|
|
32
33
|
"@solana/spl-token": "^0.1.6",
|
|
33
34
|
"@solana/web3.js": "^1.22.0",
|
|
34
|
-
"
|
|
35
|
-
"
|
|
35
|
+
"strict-event-emitter-types": "^2.0.0",
|
|
36
|
+
"uuid": "^8.3.2"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^4.28.0",
|
|
40
|
+
"@typescript-eslint/parser": "^4.28.0",
|
|
41
|
+
"eslint": "^7.29.0",
|
|
42
|
+
"eslint-config-prettier": "^8.3.0",
|
|
43
|
+
"eslint-plugin-prettier": "^3.4.0"
|
|
36
44
|
},
|
|
37
45
|
"description": "SDK for Drift Protocol v1",
|
|
38
46
|
"engines": {
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { Commitment, Connection, PublicKey } from '@solana/web3.js';
|
|
2
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
3
|
+
import { AccountData } from './types';
|
|
4
|
+
|
|
5
|
+
type AccountToLoad = {
|
|
6
|
+
publicKey: PublicKey;
|
|
7
|
+
callbacks: Map<string, (buffer: Buffer) => void>;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE = 99;
|
|
11
|
+
|
|
12
|
+
export class BulkAccountLoader {
|
|
13
|
+
connection: Connection;
|
|
14
|
+
commitment: Commitment;
|
|
15
|
+
pollingFrequency: number;
|
|
16
|
+
accountsToLoad = new Map<string, AccountToLoad>();
|
|
17
|
+
accountData = new Map<string, AccountData>();
|
|
18
|
+
errorCallbacks = new Map<string, (e) => void>();
|
|
19
|
+
intervalId?: NodeJS.Timer;
|
|
20
|
+
// to handle clients spamming load
|
|
21
|
+
loadPromise?: Promise<void>;
|
|
22
|
+
loadPromiseResolver: () => void;
|
|
23
|
+
|
|
24
|
+
public constructor(
|
|
25
|
+
connection: Connection,
|
|
26
|
+
commitment: Commitment,
|
|
27
|
+
pollingFrequency: number
|
|
28
|
+
) {
|
|
29
|
+
this.connection = connection;
|
|
30
|
+
this.commitment = commitment;
|
|
31
|
+
this.pollingFrequency = pollingFrequency;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public addAccount(
|
|
35
|
+
publicKey: PublicKey,
|
|
36
|
+
callback: (buffer: Buffer) => void
|
|
37
|
+
): string {
|
|
38
|
+
const existingSize = this.accountsToLoad.size;
|
|
39
|
+
|
|
40
|
+
const callbackId = uuidv4();
|
|
41
|
+
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
42
|
+
if (existingAccountToLoad) {
|
|
43
|
+
existingAccountToLoad.callbacks.set(callbackId, callback);
|
|
44
|
+
} else {
|
|
45
|
+
const callbacks = new Map<string, (buffer: Buffer) => void>();
|
|
46
|
+
callbacks.set(callbackId, callback);
|
|
47
|
+
const newAccountToLoad = {
|
|
48
|
+
publicKey,
|
|
49
|
+
callbacks,
|
|
50
|
+
};
|
|
51
|
+
this.accountsToLoad.set(publicKey.toString(), newAccountToLoad);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (existingSize === 0) {
|
|
55
|
+
this.startPolling();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// if a new account needs to be polled, remove the cached loadPromise in case client calls load immediately after
|
|
59
|
+
this.loadPromise = undefined;
|
|
60
|
+
|
|
61
|
+
return callbackId;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public removeAccount(publicKey: PublicKey, callbackId: string): void {
|
|
65
|
+
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
66
|
+
if (existingAccountToLoad) {
|
|
67
|
+
existingAccountToLoad.callbacks.delete(callbackId);
|
|
68
|
+
if (existingAccountToLoad.callbacks.size === 0) {
|
|
69
|
+
this.accountsToLoad.delete(existingAccountToLoad.publicKey.toString());
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (this.accountsToLoad.size === 0) {
|
|
74
|
+
this.stopPolling();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public addErrorCallbacks(callback: (error: Error) => void): string {
|
|
79
|
+
const callbackId = uuidv4();
|
|
80
|
+
this.errorCallbacks.set(callbackId, callback);
|
|
81
|
+
return callbackId;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public removeErrorCallbacks(callbackId: string): void {
|
|
85
|
+
this.errorCallbacks.delete(callbackId);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
chunks<T>(array: readonly T[], size: number): T[][] {
|
|
89
|
+
return new Array(Math.ceil(array.length / size))
|
|
90
|
+
.fill(null)
|
|
91
|
+
.map((_, index) => index * size)
|
|
92
|
+
.map((begin) => array.slice(begin, begin + size));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public async load(): Promise<void> {
|
|
96
|
+
if (this.loadPromise) {
|
|
97
|
+
return this.loadPromise;
|
|
98
|
+
}
|
|
99
|
+
this.loadPromise = new Promise((resolver) => {
|
|
100
|
+
this.loadPromiseResolver = resolver;
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const chunks = this.chunks(
|
|
104
|
+
Array.from(this.accountsToLoad.values()),
|
|
105
|
+
GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
await Promise.all(
|
|
109
|
+
chunks.map((chunk) => {
|
|
110
|
+
return this.loadChunk(chunk);
|
|
111
|
+
})
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
this.loadPromiseResolver();
|
|
115
|
+
this.loadPromise = undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async loadChunk(accountsToLoad: AccountToLoad[]): Promise<void> {
|
|
119
|
+
if (accountsToLoad.length === 0) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const args = [
|
|
124
|
+
accountsToLoad.map((accountToLoad) => {
|
|
125
|
+
return accountToLoad.publicKey.toBase58();
|
|
126
|
+
}),
|
|
127
|
+
{ commitment: this.commitment },
|
|
128
|
+
];
|
|
129
|
+
|
|
130
|
+
let rpcResponse;
|
|
131
|
+
try {
|
|
132
|
+
// @ts-ignore
|
|
133
|
+
rpcResponse = await this.connection._rpcRequest(
|
|
134
|
+
'getMultipleAccounts',
|
|
135
|
+
args
|
|
136
|
+
);
|
|
137
|
+
} catch (e) {
|
|
138
|
+
for (const [_, callback] of this.errorCallbacks) {
|
|
139
|
+
callback(e);
|
|
140
|
+
}
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const newSlot = rpcResponse.result.context.slot;
|
|
145
|
+
|
|
146
|
+
for (const i in accountsToLoad) {
|
|
147
|
+
const accountToLoad = accountsToLoad[i];
|
|
148
|
+
const key = accountToLoad.publicKey.toString();
|
|
149
|
+
const oldRPCResponse = this.accountData.get(key);
|
|
150
|
+
|
|
151
|
+
let newBuffer: Buffer | undefined = undefined;
|
|
152
|
+
if (rpcResponse.result.value[i]) {
|
|
153
|
+
const raw: string = rpcResponse.result.value[i].data[0];
|
|
154
|
+
const dataType = rpcResponse.result.value[i].data[1];
|
|
155
|
+
newBuffer = Buffer.from(raw, dataType);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (!oldRPCResponse) {
|
|
159
|
+
this.accountData.set(key, {
|
|
160
|
+
slot: newSlot,
|
|
161
|
+
buffer: newBuffer,
|
|
162
|
+
});
|
|
163
|
+
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (newSlot <= oldRPCResponse.slot) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const oldBuffer = oldRPCResponse.buffer;
|
|
172
|
+
if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
|
|
173
|
+
this.accountData.set(key, {
|
|
174
|
+
slot: newSlot,
|
|
175
|
+
buffer: newBuffer,
|
|
176
|
+
});
|
|
177
|
+
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
handleAccountCallbacks(accountToLoad: AccountToLoad, buffer: Buffer): void {
|
|
183
|
+
for (const [_, callback] of accountToLoad.callbacks) {
|
|
184
|
+
callback(buffer);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
public getAccountData(publicKey: PublicKey): Buffer | undefined {
|
|
189
|
+
const accountData = this.accountData.get(publicKey.toString());
|
|
190
|
+
return accountData?.buffer;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
public startPolling(): void {
|
|
194
|
+
if (this.intervalId) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
this.intervalId = setInterval(this.load.bind(this), this.pollingFrequency);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public stopPolling(): void {
|
|
202
|
+
if (this.intervalId) {
|
|
203
|
+
clearInterval(this.intervalId);
|
|
204
|
+
this.intervalId = undefined;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ClearingHouseUser } from '../clearingHouseUser';
|
|
2
|
+
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
3
|
+
import { PollingUserAccountSubscriber } from './pollingUserAccountSubscriber';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param users
|
|
7
|
+
* @param accountLoader
|
|
8
|
+
*/
|
|
9
|
+
export async function bulkPollingUserSubscribe(
|
|
10
|
+
users: ClearingHouseUser[],
|
|
11
|
+
accountLoader: BulkAccountLoader
|
|
12
|
+
): Promise<void> {
|
|
13
|
+
await Promise.all(
|
|
14
|
+
users.map((user) => {
|
|
15
|
+
return (
|
|
16
|
+
user.accountSubscriber as PollingUserAccountSubscriber
|
|
17
|
+
).addToAccountLoader();
|
|
18
|
+
})
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
await accountLoader.load();
|
|
22
|
+
|
|
23
|
+
await Promise.all(
|
|
24
|
+
users.map(async (user) => {
|
|
25
|
+
return user.subscribe();
|
|
26
|
+
})
|
|
27
|
+
);
|
|
28
|
+
}
|