@drift-labs/sdk 2.22.0-beta.0 → 2.22.0-beta.2
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/constants/perpMarkets.js +20 -0
- package/lib/driftClient.d.ts +5 -3
- package/lib/driftClient.js +12 -5
- package/lib/idl/drift.json +1 -1
- package/lib/math/market.js +2 -1
- package/lib/math/position.js +1 -2
- package/lib/math/spotBalance.js +2 -6
- package/package.json +2 -2
- package/src/assert/assert.js +9 -0
- package/src/constants/perpMarkets.ts +20 -0
- package/src/driftClient.ts +23 -12
- package/src/idl/drift.json +2 -2
- package/src/math/market.ts +6 -1
- package/src/math/position.ts +1 -3
- package/src/math/spotBalance.ts +2 -10
- package/src/token/index.js +38 -0
- package/src/util/computeUnits.js +27 -0
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
|
@@ -64,6 +64,16 @@ exports.DevnetPerpMarkets = [
|
|
|
64
64
|
launchTs: 1677690149000,
|
|
65
65
|
oracleSource: __1.OracleSource.PYTH,
|
|
66
66
|
},
|
|
67
|
+
{
|
|
68
|
+
fullName: 'Arbitrum',
|
|
69
|
+
category: ['L2', 'Infra'],
|
|
70
|
+
symbol: 'ARB-PERP',
|
|
71
|
+
baseAssetSymbol: 'ARB',
|
|
72
|
+
marketIndex: 6,
|
|
73
|
+
oracle: new web3_js_1.PublicKey('4mRGHzjGerQNWKXyQAmr9kWqb9saPPHKqo1xziXGQ5Dh'),
|
|
74
|
+
launchTs: 1679501812000,
|
|
75
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
76
|
+
},
|
|
67
77
|
];
|
|
68
78
|
exports.MainnetPerpMarkets = [
|
|
69
79
|
{
|
|
@@ -126,6 +136,16 @@ exports.MainnetPerpMarkets = [
|
|
|
126
136
|
launchTs: 1677690149000,
|
|
127
137
|
oracleSource: __1.OracleSource.PYTH,
|
|
128
138
|
},
|
|
139
|
+
{
|
|
140
|
+
fullName: 'Arbitrum',
|
|
141
|
+
category: ['L2', 'Infra'],
|
|
142
|
+
symbol: 'ARB-PERP',
|
|
143
|
+
baseAssetSymbol: 'ARB',
|
|
144
|
+
marketIndex: 6,
|
|
145
|
+
oracle: new web3_js_1.PublicKey('5HRrdmghsnU3i2u5StaKaydS7eq3vnKVKwXMzCNKsc4C'),
|
|
146
|
+
launchTs: 1679501812000,
|
|
147
|
+
oracleSource: __1.OracleSource.PYTH,
|
|
148
|
+
},
|
|
129
149
|
];
|
|
130
150
|
exports.PerpMarkets = {
|
|
131
151
|
devnet: exports.DevnetPerpMarkets,
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -91,7 +91,7 @@ export declare class DriftClient {
|
|
|
91
91
|
* @param activeSubAccountId
|
|
92
92
|
*/
|
|
93
93
|
updateWallet(newWallet: IWallet, subAccountIds?: number[], activeSubAccountId?: number): Promise<void>;
|
|
94
|
-
switchActiveUser(subAccountId: number):
|
|
94
|
+
switchActiveUser(subAccountId: number): void;
|
|
95
95
|
addUser(subAccountId: number): Promise<void>;
|
|
96
96
|
initializeUserAccount(subAccountId?: number, name?: string, referrerInfo?: ReferrerInfo): Promise<[TransactionSignature, PublicKey]>;
|
|
97
97
|
getInitializeUserInstructions(subAccountId?: number, name?: string, referrerInfo?: ReferrerInfo): Promise<[PublicKey, TransactionInstruction]>;
|
|
@@ -170,9 +170,11 @@ export declare class DriftClient {
|
|
|
170
170
|
* @param userAccountPublicKey
|
|
171
171
|
* @param userAccount
|
|
172
172
|
* @param makerInfo
|
|
173
|
+
* @param txParams
|
|
174
|
+
* @param bracketOrdersParams
|
|
173
175
|
* @returns
|
|
174
176
|
*/
|
|
175
|
-
sendMarketOrderAndGetSignedFillTx(orderParams: OptionalOrderParams, userAccountPublicKey: PublicKey, userAccount: UserAccount, makerInfo?: MakerInfo, txParams?: TxParams): Promise<{
|
|
177
|
+
sendMarketOrderAndGetSignedFillTx(orderParams: OptionalOrderParams, userAccountPublicKey: PublicKey, userAccount: UserAccount, makerInfo?: MakerInfo | MakerInfo[], txParams?: TxParams, bracketOrdersParams?: OptionalOrderParams[], referrerInfo?: ReferrerInfo): Promise<{
|
|
176
178
|
txSig: TransactionSignature;
|
|
177
179
|
signedFillTx: Transaction;
|
|
178
180
|
}>;
|
|
@@ -296,7 +298,7 @@ export declare class DriftClient {
|
|
|
296
298
|
settlePNLs(users: {
|
|
297
299
|
settleeUserAccountPublicKey: PublicKey;
|
|
298
300
|
settleeUserAccount: UserAccount;
|
|
299
|
-
}[],
|
|
301
|
+
}[], marketIndexes: number[]): Promise<TransactionSignature>;
|
|
300
302
|
settlePNL(settleeUserAccountPublicKey: PublicKey, settleeUserAccount: UserAccount, marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
301
303
|
settlePNLIx(settleeUserAccountPublicKey: PublicKey, settleeUserAccount: UserAccount, marketIndex: number): Promise<TransactionInstruction>;
|
|
302
304
|
liquidatePerp(userAccountPublicKey: PublicKey, userAccount: UserAccount, marketIndex: number, maxBaseAssetAmount: BN, limitPrice?: BN, txParams?: TxParams): Promise<TransactionSignature>;
|
package/lib/driftClient.js
CHANGED
|
@@ -284,7 +284,7 @@ class DriftClient {
|
|
|
284
284
|
this.activeSubAccountId = activeSubAccountId;
|
|
285
285
|
this.userStatsAccountPublicKey = undefined;
|
|
286
286
|
}
|
|
287
|
-
|
|
287
|
+
switchActiveUser(subAccountId) {
|
|
288
288
|
this.activeSubAccountId = subAccountId;
|
|
289
289
|
}
|
|
290
290
|
async addUser(subAccountId) {
|
|
@@ -1109,16 +1109,21 @@ class DriftClient {
|
|
|
1109
1109
|
* @param userAccountPublicKey
|
|
1110
1110
|
* @param userAccount
|
|
1111
1111
|
* @param makerInfo
|
|
1112
|
+
* @param txParams
|
|
1113
|
+
* @param bracketOrdersParams
|
|
1112
1114
|
* @returns
|
|
1113
1115
|
*/
|
|
1114
|
-
async sendMarketOrderAndGetSignedFillTx(orderParams, userAccountPublicKey, userAccount, makerInfo, txParams) {
|
|
1116
|
+
async sendMarketOrderAndGetSignedFillTx(orderParams, userAccountPublicKey, userAccount, makerInfo, txParams, bracketOrdersParams = new Array(), referrerInfo) {
|
|
1115
1117
|
const marketIndex = orderParams.marketIndex;
|
|
1116
1118
|
const orderId = userAccount.nextOrderId;
|
|
1117
1119
|
const marketOrderTx = (0, utils_1.wrapInTx)(await this.getPlacePerpOrderIx(orderParams), txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice);
|
|
1120
|
+
for (const bracketOrderParams of bracketOrdersParams) {
|
|
1121
|
+
marketOrderTx.add(await this.getPlacePerpOrderIx(bracketOrderParams));
|
|
1122
|
+
}
|
|
1118
1123
|
const fillTx = (0, utils_1.wrapInTx)(await this.getFillPerpOrderIx(userAccountPublicKey, userAccount, {
|
|
1119
1124
|
orderId,
|
|
1120
1125
|
marketIndex,
|
|
1121
|
-
}, makerInfo), txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice);
|
|
1126
|
+
}, makerInfo, referrerInfo), txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice);
|
|
1122
1127
|
// Apply the latest blockhash to the txs so that we can sign before sending them
|
|
1123
1128
|
const currentBlockHash = (await this.connection.getLatestBlockhash('finalized')).blockhash;
|
|
1124
1129
|
marketOrderTx.recentBlockhash = currentBlockHash;
|
|
@@ -2050,10 +2055,12 @@ class DriftClient {
|
|
|
2050
2055
|
}
|
|
2051
2056
|
return txSig;
|
|
2052
2057
|
}
|
|
2053
|
-
async settlePNLs(users,
|
|
2058
|
+
async settlePNLs(users, marketIndexes) {
|
|
2054
2059
|
const ixs = [];
|
|
2055
2060
|
for (const { settleeUserAccountPublicKey, settleeUserAccount } of users) {
|
|
2056
|
-
|
|
2061
|
+
for (const marketIndex of marketIndexes) {
|
|
2062
|
+
ixs.push(await this.settlePNLIx(settleeUserAccountPublicKey, settleeUserAccount, marketIndex));
|
|
2063
|
+
}
|
|
2057
2064
|
}
|
|
2058
2065
|
const tx = new web3_js_1.Transaction()
|
|
2059
2066
|
.add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
|
package/lib/idl/drift.json
CHANGED
package/lib/math/market.js
CHANGED
|
@@ -122,7 +122,8 @@ exports.calculateNetUserPnl = calculateNetUserPnl;
|
|
|
122
122
|
function calculateNetUserPnlImbalance(perpMarket, spotMarket, oraclePriceData) {
|
|
123
123
|
const netUserPnl = calculateNetUserPnl(perpMarket, oraclePriceData);
|
|
124
124
|
const pnlPool = (0, spotBalance_1.getTokenAmount)(perpMarket.pnlPool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
125
|
-
const
|
|
125
|
+
const feePool = (0, spotBalance_1.getTokenAmount)(perpMarket.amm.feePool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
126
|
+
const imbalance = netUserPnl.sub(pnlPool.add(feePool));
|
|
126
127
|
return imbalance;
|
|
127
128
|
}
|
|
128
129
|
exports.calculateNetUserPnlImbalance = calculateNetUserPnlImbalance;
|
package/lib/math/position.js
CHANGED
|
@@ -83,8 +83,7 @@ function calculatePositionPNL(market, perpPosition, withFunding = false, oracleP
|
|
|
83
83
|
exports.calculatePositionPNL = calculatePositionPNL;
|
|
84
84
|
function calculateClaimablePnl(market, spotMarket, perpPosition, oraclePriceData) {
|
|
85
85
|
const unrealizedPnl = calculatePositionPNL(market, perpPosition, true, oraclePriceData);
|
|
86
|
-
|
|
87
|
-
let unsettledPnl = unrealizedPnl.add(fundingPnL);
|
|
86
|
+
let unsettledPnl = unrealizedPnl;
|
|
88
87
|
if (unrealizedPnl.gt(numericConstants_1.ZERO)) {
|
|
89
88
|
const excessPnlPool = __1.BN.max(numericConstants_1.ZERO, (0, market_1.calculateNetUserPnlImbalance)(market, spotMarket, oraclePriceData).mul(new __1.BN(-1)));
|
|
90
89
|
const maxPositivePnl = __1.BN.max(perpPosition.quoteAssetAmount.sub(perpPosition.quoteEntryAmount), numericConstants_1.ZERO).add(excessPnlPool);
|
package/lib/math/spotBalance.js
CHANGED
|
@@ -206,14 +206,10 @@ function calculateWithdrawLimit(spotMarket, now) {
|
|
|
206
206
|
.add(marketDepositTokenAmount.mul(sinceLast))
|
|
207
207
|
.div(sinceLast.add(sinceStart));
|
|
208
208
|
const maxBorrowTokens = anchor_1.BN.max(spotMarket.withdrawGuardThreshold, anchor_1.BN.min(anchor_1.BN.max(marketDepositTokenAmount.div(new anchor_1.BN(6)), borrowTokenTwapLive.add(marketDepositTokenAmount.div(new anchor_1.BN(10)))), marketDepositTokenAmount.sub(marketDepositTokenAmount.div(new anchor_1.BN(5))))); // between ~15-80% utilization with friction on twap
|
|
209
|
-
const minDepositTokens = depositTokenTwapLive.sub(anchor_1.BN.max(depositTokenTwapLive.div(new anchor_1.BN(
|
|
210
|
-
|
|
209
|
+
const minDepositTokens = depositTokenTwapLive.sub(anchor_1.BN.max(depositTokenTwapLive.div(new anchor_1.BN(4)), anchor_1.BN.min(spotMarket.withdrawGuardThreshold, depositTokenTwapLive)));
|
|
210
|
+
const withdrawLimit = anchor_1.BN.max(marketDepositTokenAmount.sub(minDepositTokens), numericConstants_1.ZERO);
|
|
211
211
|
let borrowLimit = maxBorrowTokens.sub(marketBorrowTokenAmount);
|
|
212
|
-
borrowLimit = anchor_1.BN.min(borrowLimit, marketDepositTokenAmount.sub(minDepositTokens));
|
|
213
212
|
borrowLimit = anchor_1.BN.min(borrowLimit, marketDepositTokenAmount.sub(marketBorrowTokenAmount));
|
|
214
|
-
if (borrowLimit.eq(numericConstants_1.ZERO)) {
|
|
215
|
-
withdrawLimit = numericConstants_1.ZERO;
|
|
216
|
-
}
|
|
217
213
|
if (withdrawLimit.eq(numericConstants_1.ZERO)) {
|
|
218
214
|
borrowLimit = numericConstants_1.ZERO;
|
|
219
215
|
}
|
package/package.json
CHANGED
|
@@ -74,6 +74,16 @@ export const DevnetPerpMarkets: PerpMarketConfig[] = [
|
|
|
74
74
|
launchTs: 1677690149000, //todo
|
|
75
75
|
oracleSource: OracleSource.PYTH,
|
|
76
76
|
},
|
|
77
|
+
{
|
|
78
|
+
fullName: 'Arbitrum',
|
|
79
|
+
category: ['L2', 'Infra'],
|
|
80
|
+
symbol: 'ARB-PERP',
|
|
81
|
+
baseAssetSymbol: 'ARB',
|
|
82
|
+
marketIndex: 6,
|
|
83
|
+
oracle: new PublicKey('4mRGHzjGerQNWKXyQAmr9kWqb9saPPHKqo1xziXGQ5Dh'),
|
|
84
|
+
launchTs: 1679501812000, //todo
|
|
85
|
+
oracleSource: OracleSource.PYTH,
|
|
86
|
+
},
|
|
77
87
|
];
|
|
78
88
|
|
|
79
89
|
export const MainnetPerpMarkets: PerpMarketConfig[] = [
|
|
@@ -137,6 +147,16 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
|
|
|
137
147
|
launchTs: 1677690149000, //todo
|
|
138
148
|
oracleSource: OracleSource.PYTH,
|
|
139
149
|
},
|
|
150
|
+
{
|
|
151
|
+
fullName: 'Arbitrum',
|
|
152
|
+
category: ['L2', 'Infra'],
|
|
153
|
+
symbol: 'ARB-PERP',
|
|
154
|
+
baseAssetSymbol: 'ARB',
|
|
155
|
+
marketIndex: 6,
|
|
156
|
+
oracle: new PublicKey('5HRrdmghsnU3i2u5StaKaydS7eq3vnKVKwXMzCNKsc4C'),
|
|
157
|
+
launchTs: 1679501812000, //todo
|
|
158
|
+
oracleSource: OracleSource.PYTH,
|
|
159
|
+
},
|
|
140
160
|
];
|
|
141
161
|
|
|
142
162
|
export const PerpMarkets: { [key in DriftEnv]: PerpMarketConfig[] } = {
|
package/src/driftClient.ts
CHANGED
|
@@ -479,7 +479,7 @@ export class DriftClient {
|
|
|
479
479
|
this.userStatsAccountPublicKey = undefined;
|
|
480
480
|
}
|
|
481
481
|
|
|
482
|
-
public
|
|
482
|
+
public switchActiveUser(subAccountId: number) {
|
|
483
483
|
this.activeSubAccountId = subAccountId;
|
|
484
484
|
}
|
|
485
485
|
|
|
@@ -1946,14 +1946,18 @@ export class DriftClient {
|
|
|
1946
1946
|
* @param userAccountPublicKey
|
|
1947
1947
|
* @param userAccount
|
|
1948
1948
|
* @param makerInfo
|
|
1949
|
+
* @param txParams
|
|
1950
|
+
* @param bracketOrdersParams
|
|
1949
1951
|
* @returns
|
|
1950
1952
|
*/
|
|
1951
1953
|
public async sendMarketOrderAndGetSignedFillTx(
|
|
1952
1954
|
orderParams: OptionalOrderParams,
|
|
1953
1955
|
userAccountPublicKey: PublicKey,
|
|
1954
1956
|
userAccount: UserAccount,
|
|
1955
|
-
makerInfo?: MakerInfo,
|
|
1956
|
-
txParams?: TxParams
|
|
1957
|
+
makerInfo?: MakerInfo | MakerInfo[],
|
|
1958
|
+
txParams?: TxParams,
|
|
1959
|
+
bracketOrdersParams = new Array<OptionalOrderParams>(),
|
|
1960
|
+
referrerInfo?: ReferrerInfo
|
|
1957
1961
|
): Promise<{ txSig: TransactionSignature; signedFillTx: Transaction }> {
|
|
1958
1962
|
const marketIndex = orderParams.marketIndex;
|
|
1959
1963
|
const orderId = userAccount.nextOrderId;
|
|
@@ -1963,6 +1967,10 @@ export class DriftClient {
|
|
|
1963
1967
|
txParams?.computeUnits,
|
|
1964
1968
|
txParams?.computeUnitsPrice
|
|
1965
1969
|
);
|
|
1970
|
+
for (const bracketOrderParams of bracketOrdersParams) {
|
|
1971
|
+
marketOrderTx.add(await this.getPlacePerpOrderIx(bracketOrderParams));
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1966
1974
|
const fillTx = wrapInTx(
|
|
1967
1975
|
await this.getFillPerpOrderIx(
|
|
1968
1976
|
userAccountPublicKey,
|
|
@@ -1971,7 +1979,8 @@ export class DriftClient {
|
|
|
1971
1979
|
orderId,
|
|
1972
1980
|
marketIndex,
|
|
1973
1981
|
},
|
|
1974
|
-
makerInfo
|
|
1982
|
+
makerInfo,
|
|
1983
|
+
referrerInfo
|
|
1975
1984
|
),
|
|
1976
1985
|
txParams?.computeUnits,
|
|
1977
1986
|
txParams?.computeUnitsPrice
|
|
@@ -3487,17 +3496,19 @@ export class DriftClient {
|
|
|
3487
3496
|
settleeUserAccountPublicKey: PublicKey;
|
|
3488
3497
|
settleeUserAccount: UserAccount;
|
|
3489
3498
|
}[],
|
|
3490
|
-
|
|
3499
|
+
marketIndexes: number[]
|
|
3491
3500
|
): Promise<TransactionSignature> {
|
|
3492
3501
|
const ixs = [];
|
|
3493
3502
|
for (const { settleeUserAccountPublicKey, settleeUserAccount } of users) {
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
|
|
3503
|
+
for (const marketIndex of marketIndexes) {
|
|
3504
|
+
ixs.push(
|
|
3505
|
+
await this.settlePNLIx(
|
|
3506
|
+
settleeUserAccountPublicKey,
|
|
3507
|
+
settleeUserAccount,
|
|
3508
|
+
marketIndex
|
|
3509
|
+
)
|
|
3510
|
+
);
|
|
3511
|
+
}
|
|
3501
3512
|
}
|
|
3502
3513
|
|
|
3503
3514
|
const tx = new Transaction()
|
package/src/idl/drift.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.22.0-beta.
|
|
2
|
+
"version": "2.22.0-beta.2",
|
|
3
3
|
"name": "drift",
|
|
4
4
|
"instructions": [
|
|
5
5
|
{
|
|
@@ -5119,7 +5119,7 @@
|
|
|
5119
5119
|
"fields": [
|
|
5120
5120
|
{
|
|
5121
5121
|
"name": "revenueWithdrawSinceLastSettle",
|
|
5122
|
-
"type": "
|
|
5122
|
+
"type": "i64"
|
|
5123
5123
|
},
|
|
5124
5124
|
{
|
|
5125
5125
|
"name": "maxRevenueWithdrawPerPeriod",
|
package/src/math/market.ts
CHANGED
|
@@ -253,8 +253,13 @@ export function calculateNetUserPnlImbalance(
|
|
|
253
253
|
spotMarket,
|
|
254
254
|
SpotBalanceType.DEPOSIT
|
|
255
255
|
);
|
|
256
|
+
const feePool = getTokenAmount(
|
|
257
|
+
perpMarket.amm.feePool.scaledBalance,
|
|
258
|
+
spotMarket,
|
|
259
|
+
SpotBalanceType.DEPOSIT
|
|
260
|
+
);
|
|
256
261
|
|
|
257
|
-
const imbalance = netUserPnl.sub(pnlPool);
|
|
262
|
+
const imbalance = netUserPnl.sub(pnlPool.add(feePool));
|
|
258
263
|
|
|
259
264
|
return imbalance;
|
|
260
265
|
}
|
package/src/math/position.ts
CHANGED
|
@@ -139,9 +139,7 @@ export function calculateClaimablePnl(
|
|
|
139
139
|
oraclePriceData
|
|
140
140
|
);
|
|
141
141
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
let unsettledPnl = unrealizedPnl.add(fundingPnL);
|
|
142
|
+
let unsettledPnl = unrealizedPnl;
|
|
145
143
|
if (unrealizedPnl.gt(ZERO)) {
|
|
146
144
|
const excessPnlPool = BN.max(
|
|
147
145
|
ZERO,
|
package/src/math/spotBalance.ts
CHANGED
|
@@ -345,30 +345,22 @@ export function calculateWithdrawLimit(
|
|
|
345
345
|
|
|
346
346
|
const minDepositTokens = depositTokenTwapLive.sub(
|
|
347
347
|
BN.max(
|
|
348
|
-
depositTokenTwapLive.div(new BN(
|
|
348
|
+
depositTokenTwapLive.div(new BN(4)),
|
|
349
349
|
BN.min(spotMarket.withdrawGuardThreshold, depositTokenTwapLive)
|
|
350
350
|
)
|
|
351
351
|
);
|
|
352
352
|
|
|
353
|
-
|
|
353
|
+
const withdrawLimit = BN.max(
|
|
354
354
|
marketDepositTokenAmount.sub(minDepositTokens),
|
|
355
355
|
ZERO
|
|
356
356
|
);
|
|
357
357
|
|
|
358
358
|
let borrowLimit = maxBorrowTokens.sub(marketBorrowTokenAmount);
|
|
359
|
-
borrowLimit = BN.min(
|
|
360
|
-
borrowLimit,
|
|
361
|
-
marketDepositTokenAmount.sub(minDepositTokens)
|
|
362
|
-
);
|
|
363
359
|
borrowLimit = BN.min(
|
|
364
360
|
borrowLimit,
|
|
365
361
|
marketDepositTokenAmount.sub(marketBorrowTokenAmount)
|
|
366
362
|
);
|
|
367
363
|
|
|
368
|
-
if (borrowLimit.eq(ZERO)) {
|
|
369
|
-
withdrawLimit = ZERO;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
364
|
if (withdrawLimit.eq(ZERO)) {
|
|
373
365
|
borrowLimit = ZERO;
|
|
374
366
|
}
|
|
@@ -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,27 @@
|
|
|
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.findComputeUnitConsumption = void 0;
|
|
13
|
+
function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
const tx = yield connection.getTransaction(txSignature, { commitment });
|
|
16
|
+
const computeUnits = [];
|
|
17
|
+
const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`);
|
|
18
|
+
tx.meta.logMessages.forEach((logMessage) => {
|
|
19
|
+
const match = logMessage.match(regex);
|
|
20
|
+
if (match && match[1]) {
|
|
21
|
+
computeUnits.push(match[1]);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return computeUnits;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.findComputeUnitConsumption = findComputeUnitConsumption;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTokenAddress = void 0;
|
|
4
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
const getTokenAddress = (mintAddress, userPubKey) => {
|
|
7
|
+
return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
|
|
8
|
+
};
|
|
9
|
+
exports.getTokenAddress = getTokenAddress;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promiseTimeout = void 0;
|
|
4
|
+
function promiseTimeout(promise, timeoutMs) {
|
|
5
|
+
let timeoutId;
|
|
6
|
+
const timeoutPromise = new Promise((resolve) => {
|
|
7
|
+
timeoutId = setTimeout(() => resolve(null), timeoutMs);
|
|
8
|
+
});
|
|
9
|
+
return Promise.race([promise, timeoutPromise]).then((result) => {
|
|
10
|
+
clearTimeout(timeoutId);
|
|
11
|
+
return result;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
exports.promiseTimeout = promiseTimeout;
|
package/src/util/tps.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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.estimateTps = void 0;
|
|
13
|
+
function estimateTps(programId, connection, failed) {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
let signatures = yield connection.getSignaturesForAddress(programId, undefined, 'finalized');
|
|
16
|
+
if (failed) {
|
|
17
|
+
signatures = signatures.filter((signature) => signature.err);
|
|
18
|
+
}
|
|
19
|
+
const numberOfSignatures = signatures.length;
|
|
20
|
+
if (numberOfSignatures === 0) {
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
|
+
return (numberOfSignatures /
|
|
24
|
+
(signatures[0].blockTime - signatures[numberOfSignatures - 1].blockTime));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.estimateTps = estimateTps;
|