@drift-labs/sdk 0.2.0-master.0 → 0.2.0-master.11
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/admin.d.ts +8 -5
- package/lib/admin.js +43 -11
- package/lib/clearingHouse.d.ts +28 -18
- package/lib/clearingHouse.js +366 -143
- package/lib/clearingHouseUser.d.ts +2 -2
- package/lib/clearingHouseUser.js +8 -17
- package/lib/config.js +1 -1
- package/lib/constants/banks.js +9 -2
- package/lib/constants/numericConstants.d.ts +2 -0
- package/lib/constants/numericConstants.js +3 -1
- package/lib/factory/bigNum.d.ts +8 -2
- package/lib/factory/bigNum.js +14 -6
- package/lib/idl/clearing_house.json +805 -202
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +3 -2
- package/lib/index.js +7 -2
- package/lib/math/amm.d.ts +6 -1
- package/lib/math/amm.js +124 -41
- package/lib/math/auction.js +4 -1
- package/lib/math/orders.d.ts +2 -2
- package/lib/math/orders.js +18 -11
- package/lib/math/position.js +3 -1
- package/lib/math/repeg.js +1 -1
- 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/orders.d.ts +1 -2
- package/lib/orders.js +6 -85
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +7 -5
- package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +41 -40
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +137 -14
- package/lib/types.js +53 -1
- package/lib/util/computeUnits.js +1 -1
- package/package.json +3 -3
- package/src/accounts/bulkAccountLoader.js +197 -0
- package/src/accounts/bulkUserSubscription.js +33 -0
- package/src/accounts/fetch.js +29 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
- package/src/accounts/pollingOracleSubscriber.js +93 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
- package/src/accounts/pollingUserAccountSubscriber.js +132 -0
- package/src/accounts/types.js +10 -0
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +93 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
- package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/pda.js +104 -0
- package/src/admin.ts +66 -14
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +558 -230
- package/src/clearingHouseUser.ts +12 -23
- package/src/config.ts +1 -1
- package/src/constants/banks.ts +9 -2
- package/src/constants/numericConstants.ts +2 -0
- package/src/events/eventList.js +77 -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.js +20 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/factory/bigNum.js +364 -0
- package/src/factory/bigNum.ts +26 -9
- package/src/factory/oracleClient.js +20 -0
- package/src/idl/clearing_house.json +805 -202
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.js +100 -0
- package/src/index.ts +3 -2
- package/src/math/amm.js +369 -0
- package/src/math/amm.ts +207 -52
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +5 -1
- package/src/math/bankBalance.js +75 -0
- package/src/math/conversion.js +11 -0
- package/src/math/funding.js +248 -0
- package/src/math/market.js +57 -0
- package/src/math/oracles.js +26 -0
- package/src/math/orders.js +110 -0
- package/src/math/orders.ts +17 -13
- package/src/math/position.js +140 -0
- package/src/math/position.ts +5 -1
- package/src/math/repeg.js +128 -0
- package/src/math/repeg.ts +2 -1
- 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/mockUSDCFaucet.js +280 -0
- 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/orders.js +134 -0
- package/src/orders.ts +7 -131
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +48 -59
- package/src/tx/retryTxSender.js +188 -0
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.js +114 -0
- package/src/types.ts +132 -16
- package/src/userName.js +20 -0
- package/src/util/computeUnits.ts +1 -1
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/src/util/computeUnits.js +0 -17
- package/src/util/computeUnits.js.map +0 -1
package/lib/orderParams.js
CHANGED
|
@@ -2,107 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return {
|
|
8
|
-
orderType: types_1.OrderType.LIMIT,
|
|
9
|
-
userOrderId,
|
|
10
|
-
marketIndex,
|
|
11
|
-
direction,
|
|
12
|
-
quoteAssetAmount: numericConstants_1.ZERO,
|
|
13
|
-
baseAssetAmount,
|
|
14
|
-
price,
|
|
15
|
-
reduceOnly,
|
|
16
|
-
postOnly,
|
|
17
|
-
immediateOrCancel,
|
|
18
|
-
positionLimit: numericConstants_1.ZERO,
|
|
19
|
-
padding0: true,
|
|
20
|
-
padding1: numericConstants_1.ZERO,
|
|
21
|
-
optionalAccounts: {
|
|
22
|
-
discountToken,
|
|
23
|
-
referrer,
|
|
24
|
-
},
|
|
25
|
-
triggerCondition: types_1.OrderTriggerCondition.ABOVE,
|
|
26
|
-
triggerPrice: numericConstants_1.ZERO,
|
|
27
|
-
oraclePriceOffset,
|
|
28
|
-
};
|
|
5
|
+
function getLimitOrderParams(params) {
|
|
6
|
+
return Object.assign({}, params, { orderType: types_1.OrderType.LIMIT });
|
|
29
7
|
}
|
|
30
8
|
exports.getLimitOrderParams = getLimitOrderParams;
|
|
31
|
-
function getTriggerMarketOrderParams(
|
|
32
|
-
return {
|
|
33
|
-
orderType: types_1.OrderType.TRIGGER_MARKET,
|
|
34
|
-
userOrderId,
|
|
35
|
-
marketIndex,
|
|
36
|
-
direction,
|
|
37
|
-
quoteAssetAmount: numericConstants_1.ZERO,
|
|
38
|
-
baseAssetAmount,
|
|
39
|
-
price: numericConstants_1.ZERO,
|
|
40
|
-
reduceOnly,
|
|
41
|
-
postOnly: false,
|
|
42
|
-
immediateOrCancel: false,
|
|
43
|
-
positionLimit: numericConstants_1.ZERO,
|
|
44
|
-
padding0: true,
|
|
45
|
-
padding1: numericConstants_1.ZERO,
|
|
46
|
-
optionalAccounts: {
|
|
47
|
-
discountToken,
|
|
48
|
-
referrer,
|
|
49
|
-
},
|
|
50
|
-
triggerCondition,
|
|
51
|
-
triggerPrice,
|
|
52
|
-
oraclePriceOffset: numericConstants_1.ZERO,
|
|
53
|
-
};
|
|
9
|
+
function getTriggerMarketOrderParams(params) {
|
|
10
|
+
return Object.assign({}, params, { orderType: types_1.OrderType.TRIGGER_MARKET });
|
|
54
11
|
}
|
|
55
12
|
exports.getTriggerMarketOrderParams = getTriggerMarketOrderParams;
|
|
56
|
-
function getTriggerLimitOrderParams(
|
|
57
|
-
return {
|
|
58
|
-
orderType: types_1.OrderType.TRIGGER_LIMIT,
|
|
59
|
-
userOrderId,
|
|
60
|
-
marketIndex,
|
|
61
|
-
direction,
|
|
62
|
-
quoteAssetAmount: numericConstants_1.ZERO,
|
|
63
|
-
baseAssetAmount,
|
|
64
|
-
price,
|
|
65
|
-
reduceOnly,
|
|
66
|
-
postOnly: false,
|
|
67
|
-
immediateOrCancel: false,
|
|
68
|
-
positionLimit: numericConstants_1.ZERO,
|
|
69
|
-
padding0: true,
|
|
70
|
-
padding1: numericConstants_1.ZERO,
|
|
71
|
-
optionalAccounts: {
|
|
72
|
-
discountToken,
|
|
73
|
-
referrer,
|
|
74
|
-
},
|
|
75
|
-
triggerCondition,
|
|
76
|
-
triggerPrice,
|
|
77
|
-
oraclePriceOffset: numericConstants_1.ZERO,
|
|
78
|
-
};
|
|
13
|
+
function getTriggerLimitOrderParams(params) {
|
|
14
|
+
return Object.assign({}, params, { orderType: types_1.OrderType.TRIGGER_LIMIT });
|
|
79
15
|
}
|
|
80
16
|
exports.getTriggerLimitOrderParams = getTriggerLimitOrderParams;
|
|
81
|
-
function getMarketOrderParams(
|
|
82
|
-
|
|
83
|
-
throw Error('baseAssetAmount or quoteAssetAmount must be zero');
|
|
84
|
-
}
|
|
85
|
-
return {
|
|
86
|
-
orderType: types_1.OrderType.MARKET,
|
|
87
|
-
userOrderId: 0,
|
|
88
|
-
marketIndex,
|
|
89
|
-
direction,
|
|
90
|
-
quoteAssetAmount,
|
|
91
|
-
baseAssetAmount,
|
|
92
|
-
price,
|
|
93
|
-
reduceOnly,
|
|
94
|
-
postOnly: false,
|
|
95
|
-
immediateOrCancel: false,
|
|
96
|
-
positionLimit: numericConstants_1.ZERO,
|
|
97
|
-
padding0: true,
|
|
98
|
-
padding1: numericConstants_1.ZERO,
|
|
99
|
-
optionalAccounts: {
|
|
100
|
-
discountToken,
|
|
101
|
-
referrer,
|
|
102
|
-
},
|
|
103
|
-
triggerCondition: types_1.OrderTriggerCondition.ABOVE,
|
|
104
|
-
triggerPrice: numericConstants_1.ZERO,
|
|
105
|
-
oraclePriceOffset: numericConstants_1.ZERO,
|
|
106
|
-
};
|
|
17
|
+
function getMarketOrderParams(params) {
|
|
18
|
+
return Object.assign({}, params, { orderType: types_1.OrderType.MARKET });
|
|
107
19
|
}
|
|
108
20
|
exports.getMarketOrderParams = getMarketOrderParams;
|
package/lib/orders.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { MarketAccount, Order, UserAccount, UserPosition } from './types';
|
|
3
|
-
import { BN
|
|
3
|
+
import { BN } from '.';
|
|
4
4
|
import { OraclePriceData } from '.';
|
|
5
5
|
export declare function calculateNewStateAfterOrder(userAccount: UserAccount, userPosition: UserPosition, market: MarketAccount, order: Order): [UserAccount, UserPosition, MarketAccount] | null;
|
|
6
6
|
export declare function calculateBaseAssetAmountMarketCanExecute(market: MarketAccount, order: Order, oraclePriceData?: OraclePriceData): BN;
|
|
7
7
|
export declare function calculateAmountToTradeForLimit(market: MarketAccount, order: Order, oraclePriceData?: OraclePriceData): BN;
|
|
8
8
|
export declare function calculateAmountToTradeForTriggerLimit(market: MarketAccount, order: Order): BN;
|
|
9
|
-
export declare function calculateBaseAssetAmountUserCanExecute(market: MarketAccount, order: Order, user: ClearingHouseUser, oraclePriceData?: OraclePriceData): BN;
|
package/lib/orders.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.calculateAmountToTradeForTriggerLimit = exports.calculateAmountToTradeForLimit = exports.calculateBaseAssetAmountMarketCanExecute = exports.calculateNewStateAfterOrder = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
5
|
const _1 = require(".");
|
|
6
6
|
const market_1 = require("./math/market");
|
|
@@ -88,7 +88,6 @@ function calculateBaseAssetAmountMarketCanExecute(market, order, oraclePriceData
|
|
|
88
88
|
return calculateAmountToTradeForTriggerLimit(market, order);
|
|
89
89
|
}
|
|
90
90
|
else if ((0, types_1.isVariant)(order.orderType, 'market')) {
|
|
91
|
-
// should never be a market order queued
|
|
92
91
|
return numericConstants_1.ZERO;
|
|
93
92
|
}
|
|
94
93
|
else {
|
|
@@ -102,15 +101,7 @@ function calculateAmountToTradeForLimit(market, order, oraclePriceData) {
|
|
|
102
101
|
if (!oraclePriceData) {
|
|
103
102
|
throw Error('Cant calculate limit price for oracle offset oracle without OraclePriceData');
|
|
104
103
|
}
|
|
105
|
-
|
|
106
|
-
if (order.postOnly) {
|
|
107
|
-
limitPrice = (0, types_1.isVariant)(order.direction, 'long')
|
|
108
|
-
? _1.BN.min(order.price, floatingPrice)
|
|
109
|
-
: _1.BN.max(order.price, floatingPrice);
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
limitPrice = floatingPrice;
|
|
113
|
-
}
|
|
104
|
+
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
114
105
|
}
|
|
115
106
|
const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(market.amm, limitPrice, order.direction);
|
|
116
107
|
const baseAssetAmount = (0, _1.standardizeBaseAssetAmount)(maxAmountToTrade, market.amm.baseAssetAmountStepSize);
|
|
@@ -125,11 +116,8 @@ function calculateAmountToTradeForLimit(market, order, oraclePriceData) {
|
|
|
125
116
|
}
|
|
126
117
|
exports.calculateAmountToTradeForLimit = calculateAmountToTradeForLimit;
|
|
127
118
|
function calculateAmountToTradeForTriggerLimit(market, order) {
|
|
128
|
-
if (order.
|
|
129
|
-
|
|
130
|
-
if (baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
131
|
-
return numericConstants_1.ZERO;
|
|
132
|
-
}
|
|
119
|
+
if (!order.triggered) {
|
|
120
|
+
return numericConstants_1.ZERO;
|
|
133
121
|
}
|
|
134
122
|
return calculateAmountToTradeForLimit(market, order);
|
|
135
123
|
}
|
|
@@ -139,75 +127,8 @@ function isSameDirection(firstDirection, secondDirection) {
|
|
|
139
127
|
((0, types_1.isVariant)(firstDirection, 'short') && (0, types_1.isVariant)(secondDirection, 'short')));
|
|
140
128
|
}
|
|
141
129
|
function calculateAmountToTradeForTriggerMarket(market, order) {
|
|
142
|
-
|
|
143
|
-
? order.baseAssetAmount
|
|
144
|
-
: numericConstants_1.ZERO;
|
|
145
|
-
}
|
|
146
|
-
function isTriggerConditionSatisfied(market, order, oraclePriceData) {
|
|
147
|
-
const markPrice = (0, market_1.calculateMarkPrice)(market, oraclePriceData);
|
|
148
|
-
if ((0, types_1.isVariant)(order.triggerCondition, 'above')) {
|
|
149
|
-
return markPrice.gt(order.triggerPrice);
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
return markPrice.lt(order.triggerPrice);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
function calculateBaseAssetAmountUserCanExecute(market, order, user, oraclePriceData) {
|
|
156
|
-
const maxLeverage = user.getMaxLeverage(order.marketIndex, 'Initial');
|
|
157
|
-
const freeCollateral = user.getFreeCollateral();
|
|
158
|
-
let quoteAssetAmount;
|
|
159
|
-
if ((0, _1.isOrderRiskIncreasingInSameDirection)(user, order)) {
|
|
160
|
-
quoteAssetAmount = freeCollateral.mul(maxLeverage).div(_1.TEN_THOUSAND);
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
const position = user.getUserPosition(order.marketIndex) ||
|
|
164
|
-
user.getEmptyPosition(order.marketIndex);
|
|
165
|
-
const positionValue = (0, _1.calculateBaseAssetValue)(market, position, oraclePriceData);
|
|
166
|
-
quoteAssetAmount = freeCollateral
|
|
167
|
-
.mul(maxLeverage)
|
|
168
|
-
.div(_1.TEN_THOUSAND)
|
|
169
|
-
.add(positionValue.mul(numericConstants_1.TWO));
|
|
170
|
-
}
|
|
171
|
-
if (quoteAssetAmount.lte(numericConstants_1.ZERO)) {
|
|
130
|
+
if (!order.triggered) {
|
|
172
131
|
return numericConstants_1.ZERO;
|
|
173
132
|
}
|
|
174
|
-
|
|
175
|
-
? types_1.SwapDirection.ADD
|
|
176
|
-
: types_1.SwapDirection.REMOVE;
|
|
177
|
-
const useSpread = !order.postOnly;
|
|
178
|
-
let amm;
|
|
179
|
-
if (useSpread) {
|
|
180
|
-
const { baseAssetReserve, quoteAssetReserve } = (0, _1.calculateSpreadReserves)(market.amm, order.direction, oraclePriceData);
|
|
181
|
-
amm = {
|
|
182
|
-
baseAssetReserve,
|
|
183
|
-
quoteAssetReserve,
|
|
184
|
-
sqrtK: market.amm.sqrtK,
|
|
185
|
-
pegMultiplier: market.amm.pegMultiplier,
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
else {
|
|
189
|
-
amm = market.amm;
|
|
190
|
-
}
|
|
191
|
-
const baseAssetReservesBefore = amm.baseAssetReserve;
|
|
192
|
-
const [_, baseAssetReservesAfter] = (0, _1.calculateAmmReservesAfterSwap)(amm, 'quote', quoteAssetAmount, swapDirection);
|
|
193
|
-
let baseAssetAmount = baseAssetReservesBefore
|
|
194
|
-
.sub(baseAssetReservesAfter)
|
|
195
|
-
.abs();
|
|
196
|
-
if (order.reduceOnly) {
|
|
197
|
-
const position = user.getUserPosition(order.marketIndex) ||
|
|
198
|
-
user.getEmptyPosition(order.marketIndex);
|
|
199
|
-
if ((0, types_1.isVariant)(order.direction, 'long') &&
|
|
200
|
-
position.baseAssetAmount.gte(numericConstants_1.ZERO)) {
|
|
201
|
-
baseAssetAmount = numericConstants_1.ZERO;
|
|
202
|
-
}
|
|
203
|
-
else if ((0, types_1.isVariant)(order.direction, 'short') &&
|
|
204
|
-
position.baseAssetAmount.lte(numericConstants_1.ZERO)) {
|
|
205
|
-
baseAssetAmount = numericConstants_1.ZERO;
|
|
206
|
-
}
|
|
207
|
-
else {
|
|
208
|
-
_1.BN.min(baseAssetAmount, position.baseAssetAmount.abs());
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
return baseAssetAmount;
|
|
133
|
+
return order.baseAssetAmount;
|
|
212
134
|
}
|
|
213
|
-
exports.calculateBaseAssetAmountUserCanExecute = calculateBaseAssetAmountUserCanExecute;
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { Connection } from '@solana/web3.js';
|
|
3
|
+
import { EventEmitter } from 'events';
|
|
4
|
+
import StrictEventEmitter from 'strict-event-emitter-types/types/src';
|
|
2
5
|
declare type SlotSubscriberConfig = {};
|
|
6
|
+
export interface SlotSubscriberEvents {
|
|
7
|
+
newSlot: (newSlot: number) => void;
|
|
8
|
+
}
|
|
3
9
|
export declare class SlotSubscriber {
|
|
4
10
|
private connection;
|
|
5
11
|
currentSlot: number;
|
|
6
12
|
subscriptionId: number;
|
|
13
|
+
eventEmitter: StrictEventEmitter<EventEmitter, SlotSubscriberEvents>;
|
|
7
14
|
constructor(connection: Connection, _config?: SlotSubscriberConfig);
|
|
8
15
|
subscribe(): Promise<void>;
|
|
9
16
|
getSlot(): number;
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SlotSubscriber = void 0;
|
|
4
|
+
const events_1 = require("events");
|
|
4
5
|
class SlotSubscriber {
|
|
5
6
|
constructor(connection, _config) {
|
|
6
7
|
this.connection = connection;
|
|
8
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
7
9
|
}
|
|
8
10
|
async subscribe() {
|
|
9
11
|
this.currentSlot = await this.connection.getSlot('confirmed');
|
|
10
12
|
this.subscriptionId = this.connection.onSlotChange((slotInfo) => {
|
|
11
13
|
this.currentSlot = slotInfo.slot;
|
|
14
|
+
this.eventEmitter.emit('newSlot', slotInfo.slot);
|
|
12
15
|
});
|
|
13
16
|
}
|
|
14
17
|
getSlot() {
|
|
@@ -5,22 +5,24 @@ import { AccountInfo } from '@solana/spl-token';
|
|
|
5
5
|
import { ConfirmOptions, Connection, PublicKey, TransactionInstruction, TransactionSignature } from '@solana/web3.js';
|
|
6
6
|
import { BN } from '.';
|
|
7
7
|
import { IWallet } from './types';
|
|
8
|
-
export declare class
|
|
8
|
+
export declare class TokenFaucet {
|
|
9
9
|
connection: Connection;
|
|
10
10
|
wallet: IWallet;
|
|
11
11
|
program: Program;
|
|
12
12
|
provider: AnchorProvider;
|
|
13
|
+
mint: PublicKey;
|
|
13
14
|
opts?: ConfirmOptions;
|
|
14
|
-
constructor(connection: Connection, wallet: IWallet, programId: PublicKey, opts?: ConfirmOptions);
|
|
15
|
-
|
|
15
|
+
constructor(connection: Connection, wallet: IWallet, programId: PublicKey, mint: PublicKey, opts?: ConfirmOptions);
|
|
16
|
+
getFaucetConfigPublicKeyAndNonce(): Promise<[
|
|
16
17
|
PublicKey,
|
|
17
18
|
number
|
|
18
19
|
]>;
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
getMintAuthority(): Promise<PublicKey>;
|
|
21
|
+
getFaucetConfigPublicKey(): Promise<PublicKey>;
|
|
21
22
|
initialize(): Promise<TransactionSignature>;
|
|
22
23
|
fetchState(): Promise<any>;
|
|
23
24
|
mintToUser(userTokenAccount: PublicKey, amount: BN): Promise<TransactionSignature>;
|
|
25
|
+
transferMintAuthority(): Promise<TransactionSignature>;
|
|
24
26
|
createAssociatedTokenAccountAndMintTo(userPublicKey: PublicKey, amount: BN): Promise<[PublicKey, TransactionSignature]>;
|
|
25
27
|
createAssociatedTokenAccountAndMintToInstructions(userPublicKey: PublicKey, amount: BN): Promise<[PublicKey, TransactionInstruction, TransactionInstruction]>;
|
|
26
28
|
getAssosciatedMockUSDMintAddress(props: {
|
|
@@ -26,74 +26,75 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.
|
|
29
|
+
exports.TokenFaucet = void 0;
|
|
30
30
|
const anchor = __importStar(require("@project-serum/anchor"));
|
|
31
31
|
const anchor_1 = require("@project-serum/anchor");
|
|
32
32
|
const spl_token_1 = require("@solana/spl-token");
|
|
33
33
|
const web3_js_1 = require("@solana/web3.js");
|
|
34
|
-
const
|
|
35
|
-
class
|
|
36
|
-
constructor(connection, wallet, programId, opts) {
|
|
34
|
+
const token_faucet_json_1 = __importDefault(require("./idl/token_faucet.json"));
|
|
35
|
+
class TokenFaucet {
|
|
36
|
+
constructor(connection, wallet, programId, mint, opts) {
|
|
37
37
|
this.connection = connection;
|
|
38
38
|
this.wallet = wallet;
|
|
39
39
|
this.opts = opts || anchor_1.AnchorProvider.defaultOptions();
|
|
40
40
|
const provider = new anchor_1.AnchorProvider(connection, wallet, this.opts);
|
|
41
41
|
this.provider = provider;
|
|
42
|
-
this.program = new anchor_1.Program(
|
|
42
|
+
this.program = new anchor_1.Program(token_faucet_json_1.default, programId, provider);
|
|
43
|
+
this.mint = mint;
|
|
43
44
|
}
|
|
44
|
-
async
|
|
45
|
-
return anchor.web3.PublicKey.findProgramAddress([
|
|
45
|
+
async getFaucetConfigPublicKeyAndNonce() {
|
|
46
|
+
return anchor.web3.PublicKey.findProgramAddress([
|
|
47
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('faucet_config')),
|
|
48
|
+
this.mint.toBuffer(),
|
|
49
|
+
], this.program.programId);
|
|
46
50
|
}
|
|
47
|
-
async
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
async getMintAuthority() {
|
|
52
|
+
return (await anchor.web3.PublicKey.findProgramAddress([
|
|
53
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('mint_authority')),
|
|
54
|
+
this.mint.toBuffer(),
|
|
55
|
+
], this.program.programId))[0];
|
|
56
|
+
}
|
|
57
|
+
async getFaucetConfigPublicKey() {
|
|
58
|
+
return (await this.getFaucetConfigPublicKeyAndNonce())[0];
|
|
53
59
|
}
|
|
54
60
|
async initialize() {
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
throw new Error('Faucet already initialized');
|
|
58
|
-
}
|
|
59
|
-
const fakeUSDCMint = anchor.web3.Keypair.generate();
|
|
60
|
-
const createUSDCMintAccountIx = web3_js_1.SystemProgram.createAccount({
|
|
61
|
-
fromPubkey: this.wallet.publicKey,
|
|
62
|
-
newAccountPubkey: fakeUSDCMint.publicKey,
|
|
63
|
-
lamports: await spl_token_1.Token.getMinBalanceRentForExemptMint(this.connection),
|
|
64
|
-
space: spl_token_1.MintLayout.span,
|
|
65
|
-
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
|
66
|
-
});
|
|
67
|
-
const [mintAuthority, _mintAuthorityNonce] = await web3_js_1.PublicKey.findProgramAddress([fakeUSDCMint.publicKey.toBuffer()], this.program.programId);
|
|
68
|
-
const initUSDCMintIx = spl_token_1.Token.createInitMintInstruction(spl_token_1.TOKEN_PROGRAM_ID, fakeUSDCMint.publicKey, 6, mintAuthority, null);
|
|
69
|
-
const [mockUSDCFaucetStatePublicKey, mockUSDCFaucetStateNonce] = await this.getMockUSDCFaucetStatePublicKeyAndNonce();
|
|
70
|
-
return await this.program.rpc.initialize(mockUSDCFaucetStateNonce, {
|
|
61
|
+
const [faucetConfigPublicKey] = await this.getFaucetConfigPublicKeyAndNonce();
|
|
62
|
+
return await this.program.rpc.initialize({
|
|
71
63
|
accounts: {
|
|
72
|
-
|
|
64
|
+
faucetConfig: faucetConfigPublicKey,
|
|
73
65
|
admin: this.wallet.publicKey,
|
|
74
|
-
mintAccount:
|
|
66
|
+
mintAccount: this.mint,
|
|
75
67
|
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
76
68
|
systemProgram: anchor.web3.SystemProgram.programId,
|
|
69
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
77
70
|
},
|
|
78
|
-
instructions: [createUSDCMintAccountIx, initUSDCMintIx],
|
|
79
|
-
signers: [fakeUSDCMint],
|
|
80
71
|
});
|
|
81
72
|
}
|
|
82
73
|
async fetchState() {
|
|
83
|
-
return await this.program.account.
|
|
74
|
+
return await this.program.account.faucetConfig.fetch(await this.getFaucetConfigPublicKey());
|
|
84
75
|
}
|
|
85
76
|
async mintToUser(userTokenAccount, amount) {
|
|
86
|
-
const state = await this.fetchState();
|
|
87
77
|
return await this.program.rpc.mintToUser(amount, {
|
|
88
78
|
accounts: {
|
|
89
|
-
|
|
90
|
-
mintAccount:
|
|
79
|
+
faucetConfig: await this.getFaucetConfigPublicKey(),
|
|
80
|
+
mintAccount: this.mint,
|
|
91
81
|
userTokenAccount,
|
|
92
|
-
mintAuthority:
|
|
82
|
+
mintAuthority: await this.getMintAuthority(),
|
|
93
83
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
94
84
|
},
|
|
95
85
|
});
|
|
96
86
|
}
|
|
87
|
+
async transferMintAuthority() {
|
|
88
|
+
return await this.program.rpc.transferMintAuthority({
|
|
89
|
+
accounts: {
|
|
90
|
+
faucetConfig: await this.getFaucetConfigPublicKey(),
|
|
91
|
+
mintAccount: this.mint,
|
|
92
|
+
mintAuthority: await this.getMintAuthority(),
|
|
93
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
94
|
+
admin: this.wallet.publicKey,
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
|
97
98
|
async createAssociatedTokenAccountAndMintTo(userPublicKey, amount) {
|
|
98
99
|
const [associatedTokenPublicKey, createAssociatedAccountIx, mintToTx] = await this.createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount);
|
|
99
100
|
const tx = new web3_js_1.Transaction().add(createAssociatedAccountIx).add(mintToTx);
|
|
@@ -106,7 +107,7 @@ class MockUSDCFaucet {
|
|
|
106
107
|
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);
|
|
107
108
|
const mintToIx = await this.program.instruction.mintToUser(amount, {
|
|
108
109
|
accounts: {
|
|
109
|
-
|
|
110
|
+
faucetConfig: await this.getFaucetConfigPublicKey(),
|
|
110
111
|
mintAccount: state.mint,
|
|
111
112
|
userTokenAccount: associateTokenPublicKey,
|
|
112
113
|
mintAuthority: state.mintAuthority,
|
|
@@ -143,4 +144,4 @@ class MockUSDCFaucet {
|
|
|
143
144
|
}
|
|
144
145
|
}
|
|
145
146
|
}
|
|
146
|
-
exports.
|
|
147
|
+
exports.TokenFaucet = TokenFaucet;
|
package/lib/tx/utils.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.wrapInTx = void 0;
|
|
4
4
|
const web3_js_1 = require("@solana/web3.js");
|
|
5
5
|
const COMPUTE_UNITS_DEFAULT = 200000;
|
|
6
|
-
function wrapInTx(instruction, computeUnits =
|
|
6
|
+
function wrapInTx(instruction, computeUnits = 600000 // TODO, requires less code change
|
|
7
7
|
) {
|
|
8
8
|
const tx = new web3_js_1.Transaction();
|
|
9
9
|
if (computeUnits != COMPUTE_UNITS_DEFAULT) {
|