@drift-labs/sdk 0.2.0-master.25 → 0.2.0-master.26
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/pollingClearingHouseAccountSubscriber.d.ts +13 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
- package/lib/accounts/types.d.ts +8 -9
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
- package/lib/addresses/pda.d.ts +7 -6
- package/lib/addresses/pda.js +31 -27
- package/lib/admin.d.ts +9 -6
- package/lib/admin.js +83 -42
- package/lib/clearingHouse.d.ts +69 -42
- package/lib/clearingHouse.js +753 -277
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +16 -16
- package/lib/clearingHouseUser.js +139 -119
- package/lib/config.d.ts +7 -7
- package/lib/config.js +20 -20
- package/lib/constants/numericConstants.d.ts +12 -12
- package/lib/constants/numericConstants.js +13 -13
- package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
- package/{src/constants/markets.js → lib/constants/perpMarkets.js} +4 -4
- package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
- package/lib/constants/{banks.js → spotMarkets.js} +16 -16
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/examples/makeTradeExample.js +7 -7
- package/lib/idl/clearing_house.json +1008 -279
- package/lib/index.d.ts +5 -3
- package/lib/index.js +5 -3
- package/lib/math/amm.d.ts +2 -2
- package/lib/math/amm.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +2 -1
- package/lib/math/margin.d.ts +4 -4
- package/lib/math/margin.js +18 -11
- package/lib/math/market.d.ts +10 -9
- package/lib/math/market.js +29 -6
- package/lib/math/oracles.d.ts +2 -1
- package/lib/math/oracles.js +11 -1
- package/lib/math/orders.d.ts +5 -5
- package/lib/math/position.d.ts +13 -13
- package/lib/math/position.js +19 -19
- package/lib/math/spotBalance.d.ts +19 -0
- package/lib/math/spotBalance.js +176 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +2 -0
- package/lib/math/spotPosition.js +8 -0
- package/lib/math/state.js +2 -2
- package/lib/math/trade.d.ts +4 -4
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +4 -2
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +123 -33
- package/lib/types.js +31 -9
- package/my-script/.env +7 -0
- package/my-script/getUserStats.ts +106 -0
- package/my-script/multiConnections.ts +119 -0
- package/my-script/test-regex.ts +11 -0
- package/my-script/utils.ts +52 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.js +249 -0
- package/src/accounts/bulkUserStatsSubscription.js +75 -0
- package/src/accounts/bulkUserSubscription.js +75 -0
- package/src/accounts/fetch.js +92 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
- package/src/accounts/pollingOracleSubscriber.js +156 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
- package/src/accounts/pollingUserAccountSubscriber.js +208 -0
- package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
- package/src/accounts/types.js +28 -0
- package/src/accounts/types.ts +11 -9
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +138 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
- package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
- package/src/addresses/pda.js +186 -0
- package/src/addresses/pda.ts +49 -44
- package/src/admin.js +1284 -0
- package/src/admin.ts +140 -47
- package/src/assert/assert.js +1 -1
- package/src/clearingHouse.js +3433 -0
- package/src/clearingHouse.ts +1083 -378
- package/src/clearingHouseConfig.js +2 -0
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.js +874 -0
- package/src/clearingHouseUser.ts +232 -168
- package/src/clearingHouseUserConfig.js +2 -0
- package/src/clearingHouseUserStats.js +115 -0
- package/src/clearingHouseUserStatsConfig.js +2 -0
- package/src/config.js +80 -0
- package/src/config.ts +29 -29
- package/src/constants/numericConstants.js +18 -11
- package/src/constants/numericConstants.ts +17 -15
- package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
- package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
- package/src/constants/spotMarkets.js +51 -0
- package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
- package/src/events/eventList.js +66 -23
- package/src/events/eventSubscriber.js +202 -0
- package/src/events/fetchLogs.js +117 -0
- package/src/events/pollingLogProvider.js +113 -0
- package/src/events/sort.js +41 -0
- package/src/events/txEventCache.js +22 -19
- package/src/events/types.js +25 -0
- package/src/events/types.ts +3 -0
- package/src/events/webSocketLogProvider.js +76 -0
- package/src/examples/makeTradeExample.ts +10 -8
- package/src/factory/bigNum.js +183 -180
- package/src/factory/oracleClient.js +9 -9
- package/src/idl/clearing_house.json +1008 -279
- package/src/index.js +75 -0
- package/src/index.ts +5 -3
- package/src/math/amm.js +422 -0
- package/src/math/amm.ts +6 -3
- package/src/math/auction.js +10 -10
- package/src/math/conversion.js +4 -3
- package/src/math/funding.js +223 -175
- package/src/math/funding.ts +7 -7
- package/src/math/insurance.js +27 -0
- package/src/math/margin.js +77 -0
- package/src/math/margin.ts +34 -23
- package/src/math/market.js +105 -0
- package/src/math/market.ts +71 -19
- package/src/math/oracles.js +40 -10
- package/src/math/oracles.ts +18 -1
- package/src/math/orders.js +153 -0
- package/src/math/orders.ts +5 -5
- package/src/math/position.js +172 -0
- package/src/math/position.ts +31 -31
- package/src/math/repeg.js +40 -40
- package/src/math/spotBalance.js +176 -0
- package/src/math/spotBalance.ts +290 -0
- package/src/math/spotMarket.js +8 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.js +8 -0
- package/src/math/spotPosition.ts +6 -0
- package/src/math/state.ts +2 -2
- package/src/math/trade.js +81 -74
- package/src/math/trade.ts +4 -4
- package/src/math/utils.js +8 -7
- package/src/oracles/oracleClientCache.js +10 -9
- package/src/oracles/pythClient.js +52 -17
- package/src/oracles/quoteAssetOracleClient.js +44 -13
- package/src/oracles/switchboardClient.js +69 -37
- package/src/oracles/types.js +1 -1
- package/src/orderParams.js +14 -6
- package/src/orderParams.ts +16 -8
- package/src/serum/serumSubscriber.js +102 -0
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.js +2 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.js +67 -20
- package/src/token/index.js +4 -4
- package/src/tokenFaucet.js +288 -154
- package/src/tx/retryTxSender.js +280 -0
- package/src/tx/retryTxSender.ts +5 -2
- package/src/tx/types.js +1 -1
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +7 -6
- package/src/types.js +216 -0
- package/src/types.ts +110 -33
- package/src/userName.js +5 -5
- package/src/util/computeUnits.js +46 -11
- package/src/util/promiseTimeout.js +5 -5
- package/src/util/tps.js +46 -12
- package/src/wallet.js +55 -18
- package/lib/math/bankBalance.d.ts +0 -15
- package/lib/math/bankBalance.js +0 -150
- package/src/addresses/marketAddresses.js +0 -26
- package/src/constants/banks.js +0 -42
- package/src/examples/makeTradeExample.js +0 -80
- package/src/math/bankBalance.ts +0 -258
- package/src/math/state.js +0 -15
- package/src/math/utils.js.map +0 -1
- package/src/util/getTokenAddress.js +0 -9
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
exports.__esModule = true;
|
|
3
3
|
exports.OracleClientCache = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
var oracleClient_1 = require("../factory/oracleClient");
|
|
5
|
+
var OracleClientCache = /** @class */ (function () {
|
|
6
|
+
function OracleClientCache() {
|
|
7
7
|
this.cache = new Map();
|
|
8
8
|
}
|
|
9
|
-
get(oracleSource, connection) {
|
|
10
|
-
|
|
9
|
+
OracleClientCache.prototype.get = function (oracleSource, connection) {
|
|
10
|
+
var key = Object.keys(oracleSource)[0];
|
|
11
11
|
if (this.cache.has(key)) {
|
|
12
12
|
return this.cache.get(key);
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
var client = (0, oracleClient_1.getOracleClient)(oracleSource, connection);
|
|
15
15
|
this.cache.set(key, client);
|
|
16
16
|
return client;
|
|
17
|
-
}
|
|
18
|
-
|
|
17
|
+
};
|
|
18
|
+
return OracleClientCache;
|
|
19
|
+
}());
|
|
19
20
|
exports.OracleClientCache = OracleClientCache;
|
|
@@ -8,37 +8,72 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.__esModule = true;
|
|
12
39
|
exports.convertPythPrice = exports.PythClient = void 0;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
40
|
+
var client_1 = require("@pythnetwork/client");
|
|
41
|
+
var anchor_1 = require("@project-serum/anchor");
|
|
42
|
+
var numericConstants_1 = require("../constants/numericConstants");
|
|
43
|
+
var PythClient = /** @class */ (function () {
|
|
44
|
+
function PythClient(connection) {
|
|
18
45
|
this.connection = connection;
|
|
19
46
|
}
|
|
20
|
-
getOraclePriceData(pricePublicKey) {
|
|
21
|
-
return __awaiter(this, void 0, void 0, function
|
|
22
|
-
|
|
23
|
-
return this
|
|
47
|
+
PythClient.prototype.getOraclePriceData = function (pricePublicKey) {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
49
|
+
var accountInfo;
|
|
50
|
+
return __generator(this, function (_a) {
|
|
51
|
+
switch (_a.label) {
|
|
52
|
+
case 0: return [4 /*yield*/, this.connection.getAccountInfo(pricePublicKey)];
|
|
53
|
+
case 1:
|
|
54
|
+
accountInfo = _a.sent();
|
|
55
|
+
return [2 /*return*/, this.getOraclePriceDataFromBuffer(accountInfo.data)];
|
|
56
|
+
}
|
|
57
|
+
});
|
|
24
58
|
});
|
|
25
|
-
}
|
|
26
|
-
getOraclePriceDataFromBuffer(buffer) {
|
|
27
|
-
|
|
59
|
+
};
|
|
60
|
+
PythClient.prototype.getOraclePriceDataFromBuffer = function (buffer) {
|
|
61
|
+
var priceData = (0, client_1.parsePriceData)(buffer);
|
|
28
62
|
return {
|
|
29
63
|
price: convertPythPrice(priceData.aggregate.price, priceData.exponent),
|
|
30
64
|
slot: new anchor_1.BN(priceData.lastSlot.toString()),
|
|
31
65
|
confidence: convertPythPrice(priceData.confidence, priceData.exponent),
|
|
32
66
|
twap: convertPythPrice(priceData.twap.value, priceData.exponent),
|
|
33
67
|
twapConfidence: convertPythPrice(priceData.twac.value, priceData.exponent),
|
|
34
|
-
hasSufficientNumberOfDataPoints: true
|
|
68
|
+
hasSufficientNumberOfDataPoints: true
|
|
35
69
|
};
|
|
36
|
-
}
|
|
37
|
-
|
|
70
|
+
};
|
|
71
|
+
return PythClient;
|
|
72
|
+
}());
|
|
38
73
|
exports.PythClient = PythClient;
|
|
39
74
|
function convertPythPrice(price, exponent) {
|
|
40
75
|
exponent = Math.abs(exponent);
|
|
41
|
-
|
|
76
|
+
var pythPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(exponent).abs());
|
|
42
77
|
return new anchor_1.BN(price * Math.pow(10, exponent))
|
|
43
78
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
44
79
|
.div(pythPrecision);
|
|
@@ -8,25 +8,56 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.__esModule = true;
|
|
12
39
|
exports.QuoteAssetOracleClient = exports.QUOTE_ORACLE_PRICE_DATA = void 0;
|
|
13
|
-
|
|
14
|
-
|
|
40
|
+
var anchor_1 = require("@project-serum/anchor");
|
|
41
|
+
var numericConstants_1 = require("../constants/numericConstants");
|
|
15
42
|
exports.QUOTE_ORACLE_PRICE_DATA = {
|
|
16
43
|
price: numericConstants_1.MARK_PRICE_PRECISION,
|
|
17
44
|
slot: new anchor_1.BN(0),
|
|
18
45
|
confidence: new anchor_1.BN(1),
|
|
19
|
-
hasSufficientNumberOfDataPoints: true
|
|
46
|
+
hasSufficientNumberOfDataPoints: true
|
|
20
47
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
getOraclePriceData(_pricePublicKey) {
|
|
24
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
return Promise.resolve(exports.QUOTE_ORACLE_PRICE_DATA);
|
|
26
|
-
});
|
|
48
|
+
var QuoteAssetOracleClient = /** @class */ (function () {
|
|
49
|
+
function QuoteAssetOracleClient() {
|
|
27
50
|
}
|
|
28
|
-
|
|
51
|
+
QuoteAssetOracleClient.prototype.getOraclePriceData = function (_pricePublicKey) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
53
|
+
return __generator(this, function (_a) {
|
|
54
|
+
return [2 /*return*/, Promise.resolve(exports.QUOTE_ORACLE_PRICE_DATA)];
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
QuoteAssetOracleClient.prototype.getOraclePriceDataFromBuffer = function (_buffer) {
|
|
29
59
|
return exports.QUOTE_ORACLE_PRICE_DATA;
|
|
30
|
-
}
|
|
31
|
-
|
|
60
|
+
};
|
|
61
|
+
return QuoteAssetOracleClient;
|
|
62
|
+
}());
|
|
32
63
|
exports.QuoteAssetOracleClient = QuoteAssetOracleClient;
|
|
@@ -8,61 +8,93 @@ 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
|
|
12
|
-
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
13
37
|
};
|
|
14
|
-
|
|
38
|
+
exports.__esModule = true;
|
|
15
39
|
exports.SwitchboardClient = void 0;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
40
|
+
var web3_js_1 = require("@solana/web3.js");
|
|
41
|
+
var anchor_1 = require("@project-serum/anchor");
|
|
42
|
+
var numericConstants_1 = require("../constants/numericConstants");
|
|
43
|
+
var wallet_1 = require("../wallet");
|
|
44
|
+
var switchboard_v2_json_1 = require("../idl/switchboard_v2.json");
|
|
45
|
+
var program;
|
|
46
|
+
var SwitchboardClient = /** @class */ (function () {
|
|
47
|
+
function SwitchboardClient(connection) {
|
|
24
48
|
this.connection = connection;
|
|
25
49
|
}
|
|
26
|
-
getOraclePriceData(pricePublicKey) {
|
|
27
|
-
return __awaiter(this, void 0, void 0, function
|
|
28
|
-
|
|
29
|
-
return this
|
|
50
|
+
SwitchboardClient.prototype.getOraclePriceData = function (pricePublicKey) {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
52
|
+
var accountInfo;
|
|
53
|
+
return __generator(this, function (_a) {
|
|
54
|
+
switch (_a.label) {
|
|
55
|
+
case 0: return [4 /*yield*/, this.connection.getAccountInfo(pricePublicKey)];
|
|
56
|
+
case 1:
|
|
57
|
+
accountInfo = _a.sent();
|
|
58
|
+
return [2 /*return*/, this.getOraclePriceDataFromBuffer(accountInfo.data)];
|
|
59
|
+
}
|
|
60
|
+
});
|
|
30
61
|
});
|
|
31
|
-
}
|
|
32
|
-
getOraclePriceDataFromBuffer(buffer) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
62
|
+
};
|
|
63
|
+
SwitchboardClient.prototype.getOraclePriceDataFromBuffer = function (buffer) {
|
|
64
|
+
var program = this.getProgram();
|
|
65
|
+
var aggregatorAccountData = program.account.aggregatorAccountData.coder.accounts.decode('AggregatorAccountData', buffer);
|
|
66
|
+
var price = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.result);
|
|
67
|
+
var confidence = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound
|
|
37
68
|
.stdDeviation);
|
|
38
|
-
|
|
69
|
+
var hasSufficientNumberOfDataPoints = aggregatorAccountData.latestConfirmedRound.numSuccess >=
|
|
39
70
|
aggregatorAccountData.minOracleResults;
|
|
40
|
-
|
|
71
|
+
var slot = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
|
|
41
72
|
return {
|
|
42
|
-
price,
|
|
43
|
-
slot,
|
|
44
|
-
confidence,
|
|
45
|
-
hasSufficientNumberOfDataPoints
|
|
73
|
+
price: price,
|
|
74
|
+
slot: slot,
|
|
75
|
+
confidence: confidence,
|
|
76
|
+
hasSufficientNumberOfDataPoints: hasSufficientNumberOfDataPoints
|
|
46
77
|
};
|
|
47
|
-
}
|
|
48
|
-
getProgram() {
|
|
78
|
+
};
|
|
79
|
+
SwitchboardClient.prototype.getProgram = function () {
|
|
49
80
|
if (program) {
|
|
50
81
|
return program;
|
|
51
82
|
}
|
|
52
83
|
program = getSwitchboardProgram(this.connection);
|
|
53
84
|
return program;
|
|
54
|
-
}
|
|
55
|
-
|
|
85
|
+
};
|
|
86
|
+
return SwitchboardClient;
|
|
87
|
+
}());
|
|
56
88
|
exports.SwitchboardClient = SwitchboardClient;
|
|
57
89
|
function getSwitchboardProgram(connection) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return new anchor_1.Program(switchboard_v2_json_1
|
|
90
|
+
var DEFAULT_KEYPAIR = web3_js_1.Keypair.fromSeed(new Uint8Array(32).fill(1));
|
|
91
|
+
var programId = web3_js_1.PublicKey["default"];
|
|
92
|
+
var wallet = new wallet_1.Wallet(DEFAULT_KEYPAIR);
|
|
93
|
+
var provider = new anchor_1.AnchorProvider(connection, wallet, {});
|
|
94
|
+
return new anchor_1.Program(switchboard_v2_json_1["default"], programId, provider);
|
|
63
95
|
}
|
|
64
96
|
function convertSwitchboardDecimal(switchboardDecimal) {
|
|
65
|
-
|
|
97
|
+
var switchboardPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(switchboardDecimal.scale));
|
|
66
98
|
return switchboardDecimal.mantissa
|
|
67
99
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
68
100
|
.div(switchboardPrecision);
|
package/src/oracles/types.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
exports.__esModule = true;
|
package/src/orderParams.js
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
exports.__esModule = true;
|
|
3
3
|
exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
|
|
4
|
-
|
|
4
|
+
var types_1 = require("./types");
|
|
5
5
|
function getLimitOrderParams(params) {
|
|
6
|
-
return Object.assign({}, params, {
|
|
6
|
+
return Object.assign({}, params, {
|
|
7
|
+
orderType: types_1.OrderType.LIMIT
|
|
8
|
+
});
|
|
7
9
|
}
|
|
8
10
|
exports.getLimitOrderParams = getLimitOrderParams;
|
|
9
11
|
function getTriggerMarketOrderParams(params) {
|
|
10
|
-
return Object.assign({}, params, {
|
|
12
|
+
return Object.assign({}, params, {
|
|
13
|
+
orderType: types_1.OrderType.TRIGGER_MARKET
|
|
14
|
+
});
|
|
11
15
|
}
|
|
12
16
|
exports.getTriggerMarketOrderParams = getTriggerMarketOrderParams;
|
|
13
17
|
function getTriggerLimitOrderParams(params) {
|
|
14
|
-
return Object.assign({}, params, {
|
|
18
|
+
return Object.assign({}, params, {
|
|
19
|
+
orderType: types_1.OrderType.TRIGGER_LIMIT
|
|
20
|
+
});
|
|
15
21
|
}
|
|
16
22
|
exports.getTriggerLimitOrderParams = getTriggerLimitOrderParams;
|
|
17
23
|
function getMarketOrderParams(params) {
|
|
18
|
-
return Object.assign({}, params, {
|
|
24
|
+
return Object.assign({}, params, {
|
|
25
|
+
orderType: types_1.OrderType.MARKET
|
|
26
|
+
});
|
|
19
27
|
}
|
|
20
28
|
exports.getMarketOrderParams = getMarketOrderParams;
|
package/src/orderParams.ts
CHANGED
|
@@ -2,32 +2,40 @@ import { OptionalOrderParams, OrderTriggerCondition, OrderType } from './types';
|
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
3
|
|
|
4
4
|
export function getLimitOrderParams(
|
|
5
|
-
params: Omit<OptionalOrderParams, 'orderType'> & { price: BN }
|
|
5
|
+
params: Omit<OptionalOrderParams, 'orderType' | 'marketType'> & { price: BN }
|
|
6
6
|
): OptionalOrderParams {
|
|
7
|
-
return Object.assign({}, params, {
|
|
7
|
+
return Object.assign({}, params, {
|
|
8
|
+
orderType: OrderType.LIMIT,
|
|
9
|
+
});
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
export function getTriggerMarketOrderParams(
|
|
11
|
-
params: Omit<OptionalOrderParams, 'orderType'> & {
|
|
13
|
+
params: Omit<OptionalOrderParams, 'orderType' | 'marketType'> & {
|
|
12
14
|
triggerCondition: OrderTriggerCondition;
|
|
13
15
|
triggerPrice: BN;
|
|
14
16
|
}
|
|
15
17
|
): OptionalOrderParams {
|
|
16
|
-
return Object.assign({}, params, {
|
|
18
|
+
return Object.assign({}, params, {
|
|
19
|
+
orderType: OrderType.TRIGGER_MARKET,
|
|
20
|
+
});
|
|
17
21
|
}
|
|
18
22
|
|
|
19
23
|
export function getTriggerLimitOrderParams(
|
|
20
|
-
params: Omit<OptionalOrderParams, 'orderType'> & {
|
|
24
|
+
params: Omit<OptionalOrderParams, 'orderType' | 'marketType'> & {
|
|
21
25
|
triggerCondition: OrderTriggerCondition;
|
|
22
26
|
triggerPrice: BN;
|
|
23
27
|
price: BN;
|
|
24
28
|
}
|
|
25
29
|
): OptionalOrderParams {
|
|
26
|
-
return Object.assign({}, params, {
|
|
30
|
+
return Object.assign({}, params, {
|
|
31
|
+
orderType: OrderType.TRIGGER_LIMIT,
|
|
32
|
+
});
|
|
27
33
|
}
|
|
28
34
|
|
|
29
35
|
export function getMarketOrderParams(
|
|
30
|
-
params: Omit<OptionalOrderParams, 'orderType'>
|
|
36
|
+
params: Omit<OptionalOrderParams, 'orderType' | 'marketType'>
|
|
31
37
|
): OptionalOrderParams {
|
|
32
|
-
return Object.assign({}, params, {
|
|
38
|
+
return Object.assign({}, params, {
|
|
39
|
+
orderType: OrderType.MARKET,
|
|
40
|
+
});
|
|
33
41
|
}
|
|
@@ -0,0 +1,102 @@
|
|
|
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
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.__esModule = true;
|
|
39
|
+
exports.SerumSubscriber = void 0;
|
|
40
|
+
var serum_1 = require("@project-serum/serum");
|
|
41
|
+
var SerumSubscriber = /** @class */ (function () {
|
|
42
|
+
function SerumSubscriber(config) {
|
|
43
|
+
this.connection = config.connection;
|
|
44
|
+
this.programId = config.programId;
|
|
45
|
+
this.marketAddress = config.marketAddress;
|
|
46
|
+
this.accountLoader = config.accountSubscription.accountLoader;
|
|
47
|
+
}
|
|
48
|
+
SerumSubscriber.prototype.subscribe = function () {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
50
|
+
var _a, _b, _c;
|
|
51
|
+
var _this = this;
|
|
52
|
+
return __generator(this, function (_d) {
|
|
53
|
+
switch (_d.label) {
|
|
54
|
+
case 0:
|
|
55
|
+
if (this.subscribed) {
|
|
56
|
+
return [2 /*return*/];
|
|
57
|
+
}
|
|
58
|
+
_a = this;
|
|
59
|
+
return [4 /*yield*/, serum_1.Market.load(this.connection, this.marketAddress, undefined, this.programId)];
|
|
60
|
+
case 1:
|
|
61
|
+
_a.market = _d.sent();
|
|
62
|
+
this.asksAddress = this.market.asksAddress;
|
|
63
|
+
_b = this;
|
|
64
|
+
return [4 /*yield*/, this.market.loadAsks(this.connection)];
|
|
65
|
+
case 2:
|
|
66
|
+
_b.asks = _d.sent();
|
|
67
|
+
this.asksCallbackId = this.accountLoader.addAccount(this.asksAddress, function (buffer, slot) {
|
|
68
|
+
_this.lastAsksSlot = slot;
|
|
69
|
+
_this.asks = serum_1.Orderbook.decode(_this.market, buffer);
|
|
70
|
+
console.log(_this.asks.getL2(3));
|
|
71
|
+
});
|
|
72
|
+
this.bidsAddress = this.market.bidsAddress;
|
|
73
|
+
_c = this;
|
|
74
|
+
return [4 /*yield*/, this.market.loadBids(this.connection)];
|
|
75
|
+
case 3:
|
|
76
|
+
_c.bids = _d.sent();
|
|
77
|
+
this.bidsCallbackId = this.accountLoader.addAccount(this.bidsAddress, function (buffer, slot) {
|
|
78
|
+
_this.lastBidsSlot = slot;
|
|
79
|
+
_this.bids = serum_1.Orderbook.decode(_this.market, buffer);
|
|
80
|
+
});
|
|
81
|
+
this.subscribed = true;
|
|
82
|
+
return [2 /*return*/];
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
SerumSubscriber.prototype.unsubscribe = function () {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
89
|
+
return __generator(this, function (_a) {
|
|
90
|
+
if (!this.subscribed) {
|
|
91
|
+
return [2 /*return*/];
|
|
92
|
+
}
|
|
93
|
+
this.accountLoader.removeAccount(this.asksAddress, this.asksCallbackId);
|
|
94
|
+
this.accountLoader.removeAccount(this.bidsAddress, this.bidsCallbackId);
|
|
95
|
+
this.subscribed = false;
|
|
96
|
+
return [2 /*return*/];
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
return SerumSubscriber;
|
|
101
|
+
}());
|
|
102
|
+
exports.SerumSubscriber = SerumSubscriber;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Connection, PublicKey } from '@solana/web3.js';
|
|
2
|
+
import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
|
|
3
|
+
import { Market, Orderbook } from '@project-serum/serum';
|
|
4
|
+
import { SerumMarketSubscriberConfig } from './types';
|
|
5
|
+
|
|
6
|
+
export class SerumSubscriber {
|
|
7
|
+
connection: Connection;
|
|
8
|
+
programId: PublicKey;
|
|
9
|
+
marketAddress: PublicKey;
|
|
10
|
+
accountLoader: BulkAccountLoader;
|
|
11
|
+
market: Market;
|
|
12
|
+
|
|
13
|
+
subscribed: boolean;
|
|
14
|
+
|
|
15
|
+
asksAddress: PublicKey;
|
|
16
|
+
asks: Orderbook;
|
|
17
|
+
asksCallbackId: string;
|
|
18
|
+
lastAsksSlot: number;
|
|
19
|
+
|
|
20
|
+
bidsAddress: PublicKey;
|
|
21
|
+
bids: Orderbook;
|
|
22
|
+
bidsCallbackId: string;
|
|
23
|
+
lastBidsSlot: number;
|
|
24
|
+
|
|
25
|
+
public constructor(config: SerumMarketSubscriberConfig) {
|
|
26
|
+
this.connection = config.connection;
|
|
27
|
+
this.programId = config.programId;
|
|
28
|
+
this.marketAddress = config.marketAddress;
|
|
29
|
+
this.accountLoader = config.accountSubscription.accountLoader;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public async subscribe(): Promise<void> {
|
|
33
|
+
if (this.subscribed) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
this.market = await Market.load(
|
|
38
|
+
this.connection,
|
|
39
|
+
this.marketAddress,
|
|
40
|
+
undefined,
|
|
41
|
+
this.programId
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
this.asksAddress = this.market.asksAddress;
|
|
45
|
+
this.asks = await this.market.loadAsks(this.connection);
|
|
46
|
+
|
|
47
|
+
this.asksCallbackId = this.accountLoader.addAccount(
|
|
48
|
+
this.asksAddress,
|
|
49
|
+
(buffer, slot) => {
|
|
50
|
+
this.lastAsksSlot = slot;
|
|
51
|
+
this.asks = Orderbook.decode(this.market, buffer);
|
|
52
|
+
console.log(this.asks.getL2(3));
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
this.bidsAddress = this.market.bidsAddress;
|
|
57
|
+
this.bids = await this.market.loadBids(this.connection);
|
|
58
|
+
|
|
59
|
+
this.bidsCallbackId = this.accountLoader.addAccount(
|
|
60
|
+
this.bidsAddress,
|
|
61
|
+
(buffer, slot) => {
|
|
62
|
+
this.lastBidsSlot = slot;
|
|
63
|
+
this.bids = Orderbook.decode(this.market, buffer);
|
|
64
|
+
}
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
this.subscribed = true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public async unsubscribe(): Promise<void> {
|
|
71
|
+
if (!this.subscribed) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
this.accountLoader.removeAccount(this.asksAddress, this.asksCallbackId);
|
|
76
|
+
this.accountLoader.removeAccount(this.bidsAddress, this.bidsCallbackId);
|
|
77
|
+
|
|
78
|
+
this.subscribed = false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Connection, PublicKey } from '@solana/web3.js';
|
|
2
|
+
import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
|
|
3
|
+
|
|
4
|
+
export type SerumMarketSubscriberConfig = {
|
|
5
|
+
connection: Connection;
|
|
6
|
+
programId: PublicKey;
|
|
7
|
+
marketAddress: PublicKey;
|
|
8
|
+
accountSubscription: {
|
|
9
|
+
// enables use to add web sockets in the future
|
|
10
|
+
type: 'polling';
|
|
11
|
+
accountLoader: BulkAccountLoader;
|
|
12
|
+
};
|
|
13
|
+
};
|