@drift-labs/sdk 0.2.0-master.2 → 0.2.0-master.22
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/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +14 -1
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/pda.d.ts +1 -0
- package/lib/addresses/pda.js +8 -1
- package/lib/admin.d.ts +9 -5
- package/lib/admin.js +52 -11
- package/lib/clearingHouse.d.ts +52 -23
- package/lib/clearingHouse.js +736 -200
- package/lib/clearingHouseConfig.d.ts +1 -0
- package/lib/clearingHouseUser.d.ts +24 -16
- package/lib/clearingHouseUser.js +223 -101
- package/lib/clearingHouseUserStats.d.ts +18 -0
- package/lib/clearingHouseUserStats.js +49 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/lib/clearingHouseUserStatsConfig.js +2 -0
- package/lib/config.js +1 -1
- package/lib/constants/banks.d.ts +2 -2
- package/lib/constants/banks.js +12 -4
- package/lib/constants/numericConstants.d.ts +5 -0
- package/lib/constants/numericConstants.js +8 -3
- package/lib/events/eventList.js +3 -0
- package/lib/events/types.d.ts +3 -1
- package/lib/events/types.js +2 -0
- package/lib/factory/bigNum.d.ts +1 -0
- package/lib/factory/bigNum.js +34 -10
- package/lib/idl/clearing_house.json +1603 -377
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +9 -3
- package/lib/index.js +13 -3
- package/lib/math/amm.d.ts +1 -0
- package/lib/math/amm.js +22 -38
- package/lib/math/auction.js +4 -1
- package/lib/math/bankBalance.d.ts +7 -1
- package/lib/math/bankBalance.js +77 -2
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +72 -0
- package/lib/math/market.d.ts +4 -1
- package/lib/math/market.js +35 -1
- package/lib/math/oracles.d.ts +3 -0
- package/lib/math/oracles.js +25 -5
- package/lib/math/orders.d.ts +6 -2
- package/lib/math/orders.js +62 -12
- package/lib/math/position.d.ts +8 -0
- package/lib/math/position.js +45 -12
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.js +7 -10
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
- package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
- package/lib/tx/retryTxSender.js +9 -2
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +236 -26
- package/lib/types.js +64 -1
- package/lib/util/computeUnits.js +1 -1
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +3 -3
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +18 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/pda.ts +13 -0
- package/src/admin.ts +82 -15
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +1232 -323
- package/src/clearingHouseConfig.ts +1 -0
- package/src/clearingHouseUser.ts +343 -155
- package/src/clearingHouseUserStats.ts +75 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +1 -1
- package/src/constants/banks.js +42 -0
- package/src/constants/banks.ts +14 -4
- package/src/constants/markets.js +42 -0
- package/src/constants/numericConstants.js +41 -0
- package/src/constants/numericConstants.ts +14 -2
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/sort.js +44 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/types.ts +6 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/factory/bigNum.js +390 -0
- package/src/factory/bigNum.ts +42 -13
- package/src/factory/oracleClient.js +20 -0
- package/src/idl/clearing_house.json +1603 -377
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +9 -3
- package/src/math/amm.ts +54 -55
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +5 -1
- package/src/math/bankBalance.ts +148 -2
- package/src/math/conversion.js +11 -0
- package/src/math/funding.js +248 -0
- package/src/math/margin.ts +124 -0
- package/src/math/market.ts +66 -1
- package/src/math/oracles.js +26 -0
- package/src/math/oracles.ts +42 -5
- package/src/math/orders.ts +129 -13
- package/src/math/position.ts +64 -9
- package/src/math/repeg.js +128 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/trade.ts +23 -25
- package/src/math/utils.js +0 -1
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/types.js +2 -0
- package/src/orderParams.js +20 -0
- package/src/orderParams.ts +20 -141
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/tokenFaucet.js +189 -0
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
- package/src/tx/retryTxSender.ts +11 -3
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +239 -27
- package/src/userName.js +20 -0
- package/src/util/computeUnits.js +21 -11
- package/src/util/computeUnits.ts +1 -1
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/getTokenAddress.ts +18 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/tests/bn/test.ts +10 -0
- package/lib/orders.d.ts +0 -8
- package/lib/orders.js +0 -142
- package/src/orders.ts +0 -251
- package/src/util/computeUnits.js.map +0 -1
package/src/math/trade.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MarketAccount, PositionDirection
|
|
1
|
+
import { MarketAccount, PositionDirection } from '../types';
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
3
|
import { assert } from '../assert/assert';
|
|
4
4
|
import {
|
|
@@ -78,28 +78,20 @@ export function calculateTradeSlippage(
|
|
|
78
78
|
if (amount.eq(ZERO)) {
|
|
79
79
|
return [ZERO, ZERO, oldPrice, oldPrice];
|
|
80
80
|
}
|
|
81
|
-
const [
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const swapDirection = isVariant(direction, 'long')
|
|
91
|
-
? SwapDirection.REMOVE
|
|
92
|
-
: SwapDirection.ADD;
|
|
93
|
-
const quoteAssetAmountAcquired = calculateQuoteAssetAmountSwapped(
|
|
94
|
-
acquiredQuote.abs(),
|
|
95
|
-
market.amm.pegMultiplier,
|
|
96
|
-
swapDirection
|
|
97
|
-
);
|
|
81
|
+
const [acquiredBaseReserve, acquiredQuoteReserve, acquiredQuoteAssetAmount] =
|
|
82
|
+
calculateTradeAcquiredAmounts(
|
|
83
|
+
direction,
|
|
84
|
+
amount,
|
|
85
|
+
market,
|
|
86
|
+
inputAssetType,
|
|
87
|
+
oraclePriceData,
|
|
88
|
+
useSpread
|
|
89
|
+
);
|
|
98
90
|
|
|
99
|
-
const entryPrice =
|
|
91
|
+
const entryPrice = acquiredQuoteAssetAmount
|
|
100
92
|
.mul(AMM_TO_QUOTE_PRECISION_RATIO)
|
|
101
93
|
.mul(MARK_PRICE_PRECISION)
|
|
102
|
-
.div(
|
|
94
|
+
.div(acquiredBaseReserve.abs());
|
|
103
95
|
|
|
104
96
|
let amm: Parameters<typeof calculateAmmReservesAfterSwap>[0];
|
|
105
97
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
@@ -116,8 +108,8 @@ export function calculateTradeSlippage(
|
|
|
116
108
|
}
|
|
117
109
|
|
|
118
110
|
const newPrice = calculatePrice(
|
|
119
|
-
amm.baseAssetReserve.sub(
|
|
120
|
-
amm.quoteAssetReserve.sub(
|
|
111
|
+
amm.baseAssetReserve.sub(acquiredBaseReserve),
|
|
112
|
+
amm.quoteAssetReserve.sub(acquiredQuoteReserve),
|
|
121
113
|
amm.pegMultiplier
|
|
122
114
|
);
|
|
123
115
|
|
|
@@ -159,9 +151,9 @@ export function calculateTradeAcquiredAmounts(
|
|
|
159
151
|
inputAssetType: AssetType = 'quote',
|
|
160
152
|
oraclePriceData: OraclePriceData,
|
|
161
153
|
useSpread = true
|
|
162
|
-
): [BN, BN] {
|
|
154
|
+
): [BN, BN, BN] {
|
|
163
155
|
if (amount.eq(ZERO)) {
|
|
164
|
-
return [ZERO, ZERO];
|
|
156
|
+
return [ZERO, ZERO, ZERO];
|
|
165
157
|
}
|
|
166
158
|
|
|
167
159
|
const swapDirection = getSwapDirection(inputAssetType, direction);
|
|
@@ -185,7 +177,13 @@ export function calculateTradeAcquiredAmounts(
|
|
|
185
177
|
|
|
186
178
|
const acquiredBase = amm.baseAssetReserve.sub(newBaseAssetReserve);
|
|
187
179
|
const acquiredQuote = amm.quoteAssetReserve.sub(newQuoteAssetReserve);
|
|
188
|
-
|
|
180
|
+
const acquiredQuoteAssetAmount = calculateQuoteAssetAmountSwapped(
|
|
181
|
+
acquiredQuote.abs(),
|
|
182
|
+
amm.pegMultiplier,
|
|
183
|
+
swapDirection
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
return [acquiredBase, acquiredQuote, acquiredQuoteAssetAmount];
|
|
189
187
|
}
|
|
190
188
|
|
|
191
189
|
/**
|
package/src/math/utils.js
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OracleClientCache = void 0;
|
|
4
|
+
const oracleClient_1 = require("../factory/oracleClient");
|
|
5
|
+
class OracleClientCache {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.cache = new Map();
|
|
8
|
+
}
|
|
9
|
+
get(oracleSource, connection) {
|
|
10
|
+
const key = Object.keys(oracleSource)[0];
|
|
11
|
+
if (this.cache.has(key)) {
|
|
12
|
+
return this.cache.get(key);
|
|
13
|
+
}
|
|
14
|
+
const client = oracleClient_1.getOracleClient(oracleSource, connection);
|
|
15
|
+
this.cache.set(key, client);
|
|
16
|
+
return client;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.OracleClientCache = OracleClientCache;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.convertPythPrice = exports.PythClient = void 0;
|
|
13
|
+
const client_1 = require("@pythnetwork/client");
|
|
14
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
15
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
16
|
+
class PythClient {
|
|
17
|
+
constructor(connection) {
|
|
18
|
+
this.connection = connection;
|
|
19
|
+
}
|
|
20
|
+
getOraclePriceData(pricePublicKey) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const accountInfo = yield this.connection.getAccountInfo(pricePublicKey);
|
|
23
|
+
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
getOraclePriceDataFromBuffer(buffer) {
|
|
27
|
+
const priceData = client_1.parsePriceData(buffer);
|
|
28
|
+
return {
|
|
29
|
+
price: convertPythPrice(priceData.aggregate.price, priceData.exponent),
|
|
30
|
+
slot: new anchor_1.BN(priceData.lastSlot.toString()),
|
|
31
|
+
confidence: convertPythPrice(priceData.confidence, priceData.exponent),
|
|
32
|
+
twap: convertPythPrice(priceData.twap.value, priceData.exponent),
|
|
33
|
+
twapConfidence: convertPythPrice(priceData.twac.value, priceData.exponent),
|
|
34
|
+
hasSufficientNumberOfDataPoints: true,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.PythClient = PythClient;
|
|
39
|
+
function convertPythPrice(price, exponent) {
|
|
40
|
+
exponent = Math.abs(exponent);
|
|
41
|
+
const pythPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(exponent).abs());
|
|
42
|
+
return new anchor_1.BN(price * Math.pow(10, exponent))
|
|
43
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
44
|
+
.div(pythPrecision);
|
|
45
|
+
}
|
|
46
|
+
exports.convertPythPrice = convertPythPrice;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.QuoteAssetOracleClient = exports.QUOTE_ORACLE_PRICE_DATA = void 0;
|
|
13
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
14
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
15
|
+
exports.QUOTE_ORACLE_PRICE_DATA = {
|
|
16
|
+
price: numericConstants_1.MARK_PRICE_PRECISION,
|
|
17
|
+
slot: new anchor_1.BN(0),
|
|
18
|
+
confidence: new anchor_1.BN(1),
|
|
19
|
+
hasSufficientNumberOfDataPoints: true,
|
|
20
|
+
};
|
|
21
|
+
class QuoteAssetOracleClient {
|
|
22
|
+
constructor() { }
|
|
23
|
+
getOraclePriceData(_pricePublicKey) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
return Promise.resolve(exports.QUOTE_ORACLE_PRICE_DATA);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
getOraclePriceDataFromBuffer(_buffer) {
|
|
29
|
+
return exports.QUOTE_ORACLE_PRICE_DATA;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.QuoteAssetOracleClient = QuoteAssetOracleClient;
|
|
@@ -0,0 +1,69 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.SwitchboardClient = void 0;
|
|
16
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
17
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
18
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
19
|
+
const wallet_1 = require("../wallet");
|
|
20
|
+
const switchboard_v2_json_1 = __importDefault(require("../idl/switchboard_v2.json"));
|
|
21
|
+
let program;
|
|
22
|
+
class SwitchboardClient {
|
|
23
|
+
constructor(connection) {
|
|
24
|
+
this.connection = connection;
|
|
25
|
+
}
|
|
26
|
+
getOraclePriceData(pricePublicKey) {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const accountInfo = yield this.connection.getAccountInfo(pricePublicKey);
|
|
29
|
+
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
getOraclePriceDataFromBuffer(buffer) {
|
|
33
|
+
const program = this.getProgram();
|
|
34
|
+
const aggregatorAccountData = program.account.aggregatorAccountData.coder.accounts.decode('AggregatorAccountData', buffer);
|
|
35
|
+
const price = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.result);
|
|
36
|
+
const confidence = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound
|
|
37
|
+
.stdDeviation);
|
|
38
|
+
const hasSufficientNumberOfDataPoints = aggregatorAccountData.latestConfirmedRound.numSuccess >=
|
|
39
|
+
aggregatorAccountData.minOracleResults;
|
|
40
|
+
const slot = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
|
|
41
|
+
return {
|
|
42
|
+
price,
|
|
43
|
+
slot,
|
|
44
|
+
confidence,
|
|
45
|
+
hasSufficientNumberOfDataPoints,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
getProgram() {
|
|
49
|
+
if (program) {
|
|
50
|
+
return program;
|
|
51
|
+
}
|
|
52
|
+
program = getSwitchboardProgram(this.connection);
|
|
53
|
+
return program;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.SwitchboardClient = SwitchboardClient;
|
|
57
|
+
function getSwitchboardProgram(connection) {
|
|
58
|
+
const DEFAULT_KEYPAIR = web3_js_1.Keypair.fromSeed(new Uint8Array(32).fill(1));
|
|
59
|
+
const programId = web3_js_1.PublicKey.default;
|
|
60
|
+
const wallet = new wallet_1.Wallet(DEFAULT_KEYPAIR);
|
|
61
|
+
const provider = new anchor_1.AnchorProvider(connection, wallet, {});
|
|
62
|
+
return new anchor_1.Program(switchboard_v2_json_1.default, programId, provider);
|
|
63
|
+
}
|
|
64
|
+
function convertSwitchboardDecimal(switchboardDecimal) {
|
|
65
|
+
const switchboardPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(switchboardDecimal.scale));
|
|
66
|
+
return switchboardDecimal.mantissa
|
|
67
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
68
|
+
.div(switchboardPrecision);
|
|
69
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
function getLimitOrderParams(params) {
|
|
6
|
+
return Object.assign({}, params, { orderType: types_1.OrderType.LIMIT });
|
|
7
|
+
}
|
|
8
|
+
exports.getLimitOrderParams = getLimitOrderParams;
|
|
9
|
+
function getTriggerMarketOrderParams(params) {
|
|
10
|
+
return Object.assign({}, params, { orderType: types_1.OrderType.TRIGGER_MARKET });
|
|
11
|
+
}
|
|
12
|
+
exports.getTriggerMarketOrderParams = getTriggerMarketOrderParams;
|
|
13
|
+
function getTriggerLimitOrderParams(params) {
|
|
14
|
+
return Object.assign({}, params, { orderType: types_1.OrderType.TRIGGER_LIMIT });
|
|
15
|
+
}
|
|
16
|
+
exports.getTriggerLimitOrderParams = getTriggerLimitOrderParams;
|
|
17
|
+
function getMarketOrderParams(params) {
|
|
18
|
+
return Object.assign({}, params, { orderType: types_1.OrderType.MARKET });
|
|
19
|
+
}
|
|
20
|
+
exports.getMarketOrderParams = getMarketOrderParams;
|
package/src/orderParams.ts
CHANGED
|
@@ -1,154 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
OrderParams,
|
|
3
|
-
OrderTriggerCondition,
|
|
4
|
-
OrderType,
|
|
5
|
-
PositionDirection,
|
|
6
|
-
} from './types';
|
|
1
|
+
import { OptionalOrderParams, OrderTriggerCondition, OrderType } from './types';
|
|
7
2
|
import { BN } from '@project-serum/anchor';
|
|
8
|
-
import { ZERO } from './constants/numericConstants';
|
|
9
3
|
|
|
10
4
|
export function getLimitOrderParams(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
price: BN,
|
|
15
|
-
reduceOnly: boolean,
|
|
16
|
-
discountToken = false,
|
|
17
|
-
referrer = false,
|
|
18
|
-
userOrderId = 0,
|
|
19
|
-
postOnly = false,
|
|
20
|
-
oraclePriceOffset = ZERO,
|
|
21
|
-
immediateOrCancel = false
|
|
22
|
-
): OrderParams {
|
|
23
|
-
return {
|
|
24
|
-
orderType: OrderType.LIMIT,
|
|
25
|
-
userOrderId,
|
|
26
|
-
marketIndex,
|
|
27
|
-
direction,
|
|
28
|
-
quoteAssetAmount: ZERO,
|
|
29
|
-
baseAssetAmount,
|
|
30
|
-
price,
|
|
31
|
-
reduceOnly,
|
|
32
|
-
postOnly,
|
|
33
|
-
immediateOrCancel,
|
|
34
|
-
positionLimit: ZERO,
|
|
35
|
-
padding0: true,
|
|
36
|
-
padding1: ZERO,
|
|
37
|
-
optionalAccounts: {
|
|
38
|
-
discountToken,
|
|
39
|
-
referrer,
|
|
40
|
-
},
|
|
41
|
-
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
42
|
-
triggerPrice: ZERO,
|
|
43
|
-
oraclePriceOffset,
|
|
44
|
-
};
|
|
5
|
+
params: Omit<OptionalOrderParams, 'orderType'> & { price: BN }
|
|
6
|
+
): OptionalOrderParams {
|
|
7
|
+
return Object.assign({}, params, { orderType: OrderType.LIMIT });
|
|
45
8
|
}
|
|
46
9
|
|
|
47
10
|
export function getTriggerMarketOrderParams(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
discountToken = false,
|
|
55
|
-
referrer = false,
|
|
56
|
-
userOrderId = 0
|
|
57
|
-
): OrderParams {
|
|
58
|
-
return {
|
|
59
|
-
orderType: OrderType.TRIGGER_MARKET,
|
|
60
|
-
userOrderId,
|
|
61
|
-
marketIndex,
|
|
62
|
-
direction,
|
|
63
|
-
quoteAssetAmount: ZERO,
|
|
64
|
-
baseAssetAmount,
|
|
65
|
-
price: ZERO,
|
|
66
|
-
reduceOnly,
|
|
67
|
-
postOnly: false,
|
|
68
|
-
immediateOrCancel: false,
|
|
69
|
-
positionLimit: ZERO,
|
|
70
|
-
padding0: true,
|
|
71
|
-
padding1: ZERO,
|
|
72
|
-
optionalAccounts: {
|
|
73
|
-
discountToken,
|
|
74
|
-
referrer,
|
|
75
|
-
},
|
|
76
|
-
triggerCondition,
|
|
77
|
-
triggerPrice,
|
|
78
|
-
oraclePriceOffset: ZERO,
|
|
79
|
-
};
|
|
11
|
+
params: Omit<OptionalOrderParams, 'orderType'> & {
|
|
12
|
+
triggerCondition: OrderTriggerCondition;
|
|
13
|
+
triggerPrice: BN;
|
|
14
|
+
}
|
|
15
|
+
): OptionalOrderParams {
|
|
16
|
+
return Object.assign({}, params, { orderType: OrderType.TRIGGER_MARKET });
|
|
80
17
|
}
|
|
81
18
|
|
|
82
19
|
export function getTriggerLimitOrderParams(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
discountToken = false,
|
|
91
|
-
referrer = false,
|
|
92
|
-
userOrderId = 0
|
|
93
|
-
): OrderParams {
|
|
94
|
-
return {
|
|
95
|
-
orderType: OrderType.TRIGGER_LIMIT,
|
|
96
|
-
userOrderId,
|
|
97
|
-
marketIndex,
|
|
98
|
-
direction,
|
|
99
|
-
quoteAssetAmount: ZERO,
|
|
100
|
-
baseAssetAmount,
|
|
101
|
-
price,
|
|
102
|
-
reduceOnly,
|
|
103
|
-
postOnly: false,
|
|
104
|
-
immediateOrCancel: false,
|
|
105
|
-
positionLimit: ZERO,
|
|
106
|
-
padding0: true,
|
|
107
|
-
padding1: ZERO,
|
|
108
|
-
optionalAccounts: {
|
|
109
|
-
discountToken,
|
|
110
|
-
referrer,
|
|
111
|
-
},
|
|
112
|
-
triggerCondition,
|
|
113
|
-
triggerPrice,
|
|
114
|
-
oraclePriceOffset: ZERO,
|
|
115
|
-
};
|
|
20
|
+
params: Omit<OptionalOrderParams, 'orderType'> & {
|
|
21
|
+
triggerCondition: OrderTriggerCondition;
|
|
22
|
+
triggerPrice: BN;
|
|
23
|
+
price: BN;
|
|
24
|
+
}
|
|
25
|
+
): OptionalOrderParams {
|
|
26
|
+
return Object.assign({}, params, { orderType: OrderType.TRIGGER_LIMIT });
|
|
116
27
|
}
|
|
117
28
|
|
|
118
29
|
export function getMarketOrderParams(
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
baseAssetAmount: BN,
|
|
123
|
-
reduceOnly: boolean,
|
|
124
|
-
price = ZERO,
|
|
125
|
-
discountToken = false,
|
|
126
|
-
referrer = false
|
|
127
|
-
): OrderParams {
|
|
128
|
-
if (baseAssetAmount.eq(ZERO) && quoteAssetAmount.eq(ZERO)) {
|
|
129
|
-
throw Error('baseAssetAmount or quoteAssetAmount must be zero');
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
orderType: OrderType.MARKET,
|
|
134
|
-
userOrderId: 0,
|
|
135
|
-
marketIndex,
|
|
136
|
-
direction,
|
|
137
|
-
quoteAssetAmount,
|
|
138
|
-
baseAssetAmount,
|
|
139
|
-
price,
|
|
140
|
-
reduceOnly,
|
|
141
|
-
postOnly: false,
|
|
142
|
-
immediateOrCancel: false,
|
|
143
|
-
positionLimit: ZERO,
|
|
144
|
-
padding0: true,
|
|
145
|
-
padding1: ZERO,
|
|
146
|
-
optionalAccounts: {
|
|
147
|
-
discountToken,
|
|
148
|
-
referrer,
|
|
149
|
-
},
|
|
150
|
-
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
151
|
-
triggerPrice: ZERO,
|
|
152
|
-
oraclePriceOffset: ZERO,
|
|
153
|
-
};
|
|
30
|
+
params: Omit<OptionalOrderParams, 'orderType'>
|
|
31
|
+
): OptionalOrderParams {
|
|
32
|
+
return Object.assign({}, params, { orderType: OrderType.MARKET });
|
|
154
33
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SlotSubscriber = void 0;
|
|
13
|
+
const events_1 = require("events");
|
|
14
|
+
class SlotSubscriber {
|
|
15
|
+
constructor(connection, _config) {
|
|
16
|
+
this.connection = connection;
|
|
17
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
18
|
+
}
|
|
19
|
+
subscribe() {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
this.currentSlot = yield this.connection.getSlot('confirmed');
|
|
22
|
+
this.subscriptionId = this.connection.onSlotChange((slotInfo) => {
|
|
23
|
+
this.currentSlot = slotInfo.slot;
|
|
24
|
+
this.eventEmitter.emit('newSlot', slotInfo.slot);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
getSlot() {
|
|
29
|
+
return this.currentSlot;
|
|
30
|
+
}
|
|
31
|
+
unsubscribe() {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
if (this.subscriptionId) {
|
|
34
|
+
yield this.connection.removeSlotChangeListener(this.subscriptionId);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.SlotSubscriber = SlotSubscriber;
|
|
@@ -1,22 +1,32 @@
|
|
|
1
1
|
import { Connection } from '@solana/web3.js';
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
import StrictEventEmitter from 'strict-event-emitter-types/types/src';
|
|
2
4
|
|
|
3
5
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
4
6
|
type SlotSubscriberConfig = {}; // for future customization
|
|
5
7
|
|
|
8
|
+
export interface SlotSubscriberEvents {
|
|
9
|
+
newSlot: (newSlot: number) => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
6
12
|
export class SlotSubscriber {
|
|
7
13
|
currentSlot: number;
|
|
8
14
|
subscriptionId: number;
|
|
15
|
+
eventEmitter: StrictEventEmitter<EventEmitter, SlotSubscriberEvents>;
|
|
9
16
|
|
|
10
17
|
public constructor(
|
|
11
18
|
private connection: Connection,
|
|
12
19
|
_config?: SlotSubscriberConfig
|
|
13
|
-
) {
|
|
20
|
+
) {
|
|
21
|
+
this.eventEmitter = new EventEmitter();
|
|
22
|
+
}
|
|
14
23
|
|
|
15
24
|
public async subscribe(): Promise<void> {
|
|
16
25
|
this.currentSlot = await this.connection.getSlot('confirmed');
|
|
17
26
|
|
|
18
27
|
this.subscriptionId = this.connection.onSlotChange((slotInfo) => {
|
|
19
28
|
this.currentSlot = slotInfo.slot;
|
|
29
|
+
this.eventEmitter.emit('newSlot', slotInfo.slot);
|
|
20
30
|
});
|
|
21
31
|
}
|
|
22
32
|
|
|
@@ -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;
|