@drift-labs/sdk 0.2.0-master.2 → 0.2.0-master.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/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 +727 -197
- package/lib/clearingHouseConfig.d.ts +1 -0
- package/lib/clearingHouseUser.d.ts +17 -17
- package/lib/clearingHouseUser.js +186 -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 +1609 -388
- 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 +5 -2
- package/lib/math/orders.js +53 -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 +233 -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 +1224 -319
- package/src/clearingHouseConfig.ts +1 -0
- package/src/clearingHouseUser.ts +311 -148
- 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/fetchLogs.js +50 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/sort.js +44 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/types.ts +6 -0
- package/src/events/webSocketLogProvider.js +41 -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 +1609 -388
- 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 +112 -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 +236 -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
|
@@ -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;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
31
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
32
|
+
};
|
|
33
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
+
exports.TokenFaucet = void 0;
|
|
35
|
+
const anchor = __importStar(require("@project-serum/anchor"));
|
|
36
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
37
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
38
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
39
|
+
const token_faucet_json_1 = __importDefault(require("./idl/token_faucet.json"));
|
|
40
|
+
class TokenFaucet {
|
|
41
|
+
constructor(connection, wallet, programId, mint, opts) {
|
|
42
|
+
this.connection = connection;
|
|
43
|
+
this.wallet = wallet;
|
|
44
|
+
this.opts = opts || anchor_1.AnchorProvider.defaultOptions();
|
|
45
|
+
const provider = new anchor_1.AnchorProvider(connection, wallet, this.opts);
|
|
46
|
+
this.provider = provider;
|
|
47
|
+
this.program = new anchor_1.Program(token_faucet_json_1.default, programId, provider);
|
|
48
|
+
this.mint = mint;
|
|
49
|
+
}
|
|
50
|
+
getFaucetConfigPublicKeyAndNonce() {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
return anchor.web3.PublicKey.findProgramAddress([
|
|
53
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('faucet_config')),
|
|
54
|
+
this.mint.toBuffer(),
|
|
55
|
+
], this.program.programId);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
getMintAuthority() {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
return (yield anchor.web3.PublicKey.findProgramAddress([
|
|
61
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('mint_authority')),
|
|
62
|
+
this.mint.toBuffer(),
|
|
63
|
+
], this.program.programId))[0];
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
getFaucetConfigPublicKey() {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
return (yield this.getFaucetConfigPublicKeyAndNonce())[0];
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
initialize() {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
const [faucetConfigPublicKey] = yield this.getFaucetConfigPublicKeyAndNonce();
|
|
74
|
+
return yield this.program.rpc.initialize({
|
|
75
|
+
accounts: {
|
|
76
|
+
faucetConfig: faucetConfigPublicKey,
|
|
77
|
+
admin: this.wallet.publicKey,
|
|
78
|
+
mintAccount: this.mint,
|
|
79
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
80
|
+
systemProgram: anchor.web3.SystemProgram.programId,
|
|
81
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
fetchState() {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
return yield this.program.account.faucetConfig.fetch(yield this.getFaucetConfigPublicKey());
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
mintToUserIx(userTokenAccount, amount) {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
return this.program.instruction.mintToUser(amount, {
|
|
94
|
+
accounts: {
|
|
95
|
+
faucetConfig: yield this.getFaucetConfigPublicKey(),
|
|
96
|
+
mintAccount: this.mint,
|
|
97
|
+
userTokenAccount,
|
|
98
|
+
mintAuthority: yield this.getMintAuthority(),
|
|
99
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
mintToUser(userTokenAccount, amount) {
|
|
105
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
+
const mintIx = yield this.mintToUserIx(userTokenAccount, amount);
|
|
107
|
+
const tx = new web3_js_1.Transaction().add(mintIx);
|
|
108
|
+
const txSig = yield this.program.provider.sendAndConfirm(tx, [], this.opts);
|
|
109
|
+
return txSig;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
transferMintAuthority() {
|
|
113
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
114
|
+
return yield this.program.rpc.transferMintAuthority({
|
|
115
|
+
accounts: {
|
|
116
|
+
faucetConfig: yield this.getFaucetConfigPublicKey(),
|
|
117
|
+
mintAccount: this.mint,
|
|
118
|
+
mintAuthority: yield this.getMintAuthority(),
|
|
119
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
120
|
+
admin: this.wallet.publicKey,
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
createAssociatedTokenAccountAndMintTo(userPublicKey, amount) {
|
|
126
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
127
|
+
const tx = new web3_js_1.Transaction();
|
|
128
|
+
const [associatedTokenPublicKey, createAssociatedAccountIx, mintToTx] = yield this.createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount);
|
|
129
|
+
let associatedTokenAccountExists = false;
|
|
130
|
+
try {
|
|
131
|
+
const assosciatedTokenAccount = yield this.connection.getAccountInfo(associatedTokenPublicKey);
|
|
132
|
+
associatedTokenAccountExists = !!assosciatedTokenAccount;
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
// token account doesn't exist
|
|
136
|
+
associatedTokenAccountExists = false;
|
|
137
|
+
}
|
|
138
|
+
const skipAccountCreation = associatedTokenAccountExists;
|
|
139
|
+
if (!skipAccountCreation)
|
|
140
|
+
tx.add(createAssociatedAccountIx);
|
|
141
|
+
tx.add(mintToTx);
|
|
142
|
+
const txSig = yield this.program.provider.sendAndConfirm(tx, [], this.opts);
|
|
143
|
+
return [associatedTokenPublicKey, txSig];
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount) {
|
|
147
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
148
|
+
const state = yield this.fetchState();
|
|
149
|
+
const associateTokenPublicKey = yield this.getAssosciatedMockUSDMintAddress({ userPubKey: userPublicKey });
|
|
150
|
+
const createAssociatedAccountIx = spl_token_1.Token.createAssociatedTokenAccountInstruction(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, associateTokenPublicKey, userPublicKey, this.wallet.publicKey);
|
|
151
|
+
const mintToIx = yield this.mintToUserIx(associateTokenPublicKey, amount);
|
|
152
|
+
return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
getAssosciatedMockUSDMintAddress(props) {
|
|
156
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
const state = yield this.fetchState();
|
|
158
|
+
return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, props.userPubKey);
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
getTokenAccountInfo(props) {
|
|
162
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
163
|
+
const assosciatedKey = yield this.getAssosciatedMockUSDMintAddress(props);
|
|
164
|
+
const state = yield this.fetchState();
|
|
165
|
+
const token = new spl_token_1.Token(this.connection, state.mint, spl_token_1.TOKEN_PROGRAM_ID,
|
|
166
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
this.provider.payer);
|
|
169
|
+
return yield token.getAccountInfo(assosciatedKey);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
subscribeToTokenAccount(props) {
|
|
173
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
try {
|
|
175
|
+
const tokenAccountKey = yield this.getAssosciatedMockUSDMintAddress(props);
|
|
176
|
+
props.callback(yield this.getTokenAccountInfo(props));
|
|
177
|
+
// Couldn't find a way to do it using anchor framework subscription, someone on serum discord recommended this way
|
|
178
|
+
this.connection.onAccountChange(tokenAccountKey, (_accountInfo /* accountInfo is a buffer which we don't know how to deserialize */) => __awaiter(this, void 0, void 0, function* () {
|
|
179
|
+
props.callback(yield this.getTokenAccountInfo(props));
|
|
180
|
+
}));
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
catch (e) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
exports.TokenFaucet = TokenFaucet;
|