@drift-labs/sdk 2.30.0-beta.1 → 2.31.0-beta.0
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/pollingTokenAccountSubscriber.d.ts +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +5 -2
- package/lib/accounts/types.d.ts +3 -3
- package/lib/adminClient.d.ts +1 -0
- package/lib/adminClient.js +9 -0
- package/lib/constants/spotMarkets.js +10 -0
- package/lib/dlob/DLOB.d.ts +20 -1
- package/lib/dlob/DLOB.js +39 -0
- package/lib/driftClient.d.ts +13 -8
- package/lib/driftClient.js +32 -27
- package/lib/examples/makeTradeExample.js +1 -1
- package/lib/idl/drift.json +106 -15
- package/lib/jupiter/jupiterClient.d.ts +1 -1
- package/lib/jupiter/jupiterClient.js +7 -2
- package/lib/math/spotBalance.d.ts +41 -0
- package/lib/math/spotBalance.js +41 -0
- package/lib/token/index.d.ts +3 -2
- package/lib/token/index.js +9 -32
- package/lib/tokenFaucet.d.ts +3 -3
- package/lib/tokenFaucet.js +4 -9
- package/lib/tx/retryTxSender.js +3 -0
- package/lib/types.d.ts +22 -2
- package/lib/types.js +12 -1
- package/lib/wallet.d.ts +5 -3
- package/lib/wallet.js +19 -7
- package/package.json +2 -2
- package/src/accounts/pollingTokenAccountSubscriber.ts +8 -5
- package/src/accounts/types.ts +3 -3
- package/src/adminClient.ts +19 -0
- package/src/constants/spotMarkets.ts +11 -0
- package/src/dlob/DLOB.ts +75 -0
- package/src/driftClient.ts +57 -53
- package/src/examples/makeTradeExample.ts +2 -4
- package/src/idl/drift.json +106 -15
- package/src/jupiter/jupiterClient.ts +10 -2
- package/src/math/spotBalance.ts +41 -0
- package/src/token/index.ts +12 -36
- package/src/tokenFaucet.ts +15 -34
- package/src/tx/retryTxSender.ts +4 -0
- package/src/types.ts +23 -2
- package/src/wallet.ts +34 -12
- package/tests/dlob/helpers.ts +1 -0
- package/tests/dlob/test.ts +218 -40
- package/lib/util/getTokenAddress.d.ts +0 -2
- package/lib/util/getTokenAddress.js +0 -9
- package/src/util/getTokenAddress.ts +0 -18
package/lib/idl/drift.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.
|
|
2
|
+
"version": "2.31.0-beta.0",
|
|
3
3
|
"name": "drift",
|
|
4
4
|
"instructions": [
|
|
5
5
|
{
|
|
@@ -863,6 +863,14 @@
|
|
|
863
863
|
"type": {
|
|
864
864
|
"option": "u64"
|
|
865
865
|
}
|
|
866
|
+
},
|
|
867
|
+
{
|
|
868
|
+
"name": "reduceOnly",
|
|
869
|
+
"type": {
|
|
870
|
+
"option": {
|
|
871
|
+
"defined": "SwapReduceOnly"
|
|
872
|
+
}
|
|
873
|
+
}
|
|
866
874
|
}
|
|
867
875
|
]
|
|
868
876
|
},
|
|
@@ -3636,6 +3644,32 @@
|
|
|
3636
3644
|
}
|
|
3637
3645
|
]
|
|
3638
3646
|
},
|
|
3647
|
+
{
|
|
3648
|
+
"name": "updatePerpMarketTargetBaseAssetAmountPerLp",
|
|
3649
|
+
"accounts": [
|
|
3650
|
+
{
|
|
3651
|
+
"name": "admin",
|
|
3652
|
+
"isMut": false,
|
|
3653
|
+
"isSigner": true
|
|
3654
|
+
},
|
|
3655
|
+
{
|
|
3656
|
+
"name": "state",
|
|
3657
|
+
"isMut": false,
|
|
3658
|
+
"isSigner": false
|
|
3659
|
+
},
|
|
3660
|
+
{
|
|
3661
|
+
"name": "perpMarket",
|
|
3662
|
+
"isMut": true,
|
|
3663
|
+
"isSigner": false
|
|
3664
|
+
}
|
|
3665
|
+
],
|
|
3666
|
+
"args": [
|
|
3667
|
+
{
|
|
3668
|
+
"name": "targetBaseAssetAmountPerLp",
|
|
3669
|
+
"type": "i32"
|
|
3670
|
+
}
|
|
3671
|
+
]
|
|
3672
|
+
},
|
|
3639
3673
|
{
|
|
3640
3674
|
"name": "updateLpCooldownTime",
|
|
3641
3675
|
"accounts": [
|
|
@@ -6441,7 +6475,8 @@
|
|
|
6441
6475
|
{
|
|
6442
6476
|
"name": "lastOracleNormalisedPrice",
|
|
6443
6477
|
"docs": [
|
|
6444
|
-
"the last seen oracle price partially shrunk toward the amm reserve price"
|
|
6478
|
+
"the last seen oracle price partially shrunk toward the amm reserve price",
|
|
6479
|
+
"precision: PRICE_PRECISION"
|
|
6445
6480
|
],
|
|
6446
6481
|
"type": "i64"
|
|
6447
6482
|
},
|
|
@@ -6455,21 +6490,24 @@
|
|
|
6455
6490
|
{
|
|
6456
6491
|
"name": "lastBidPriceTwap",
|
|
6457
6492
|
"docs": [
|
|
6458
|
-
"average estimate of bid price over funding_period"
|
|
6493
|
+
"average estimate of bid price over funding_period",
|
|
6494
|
+
"precision: PRICE_PRECISION"
|
|
6459
6495
|
],
|
|
6460
6496
|
"type": "u64"
|
|
6461
6497
|
},
|
|
6462
6498
|
{
|
|
6463
6499
|
"name": "lastAskPriceTwap",
|
|
6464
6500
|
"docs": [
|
|
6465
|
-
"average estimate of ask price over funding_period"
|
|
6501
|
+
"average estimate of ask price over funding_period",
|
|
6502
|
+
"precision: PRICE_PRECISION"
|
|
6466
6503
|
],
|
|
6467
6504
|
"type": "u64"
|
|
6468
6505
|
},
|
|
6469
6506
|
{
|
|
6470
6507
|
"name": "lastMarkPriceTwap",
|
|
6471
6508
|
"docs": [
|
|
6472
|
-
"average estimate of (bid+ask)/2 price over funding_period"
|
|
6509
|
+
"average estimate of (bid+ask)/2 price over funding_period",
|
|
6510
|
+
"precision: PRICE_PRECISION"
|
|
6473
6511
|
],
|
|
6474
6512
|
"type": "u64"
|
|
6475
6513
|
},
|
|
@@ -6491,14 +6529,15 @@
|
|
|
6491
6529
|
"name": "lastOracleConfPct",
|
|
6492
6530
|
"docs": [
|
|
6493
6531
|
"the pct size of the oracle confidence interval",
|
|
6494
|
-
"PERCENTAGE_PRECISION"
|
|
6532
|
+
"precision: PERCENTAGE_PRECISION"
|
|
6495
6533
|
],
|
|
6496
6534
|
"type": "u64"
|
|
6497
6535
|
},
|
|
6498
6536
|
{
|
|
6499
6537
|
"name": "netRevenueSinceLastFunding",
|
|
6500
6538
|
"docs": [
|
|
6501
|
-
"the total_fee_minus_distribution change since the last funding update"
|
|
6539
|
+
"the total_fee_minus_distribution change since the last funding update",
|
|
6540
|
+
"precision: QUOTE_PRECISION"
|
|
6502
6541
|
],
|
|
6503
6542
|
"type": "i64"
|
|
6504
6543
|
},
|
|
@@ -6519,28 +6558,32 @@
|
|
|
6519
6558
|
{
|
|
6520
6559
|
"name": "orderStepSize",
|
|
6521
6560
|
"docs": [
|
|
6522
|
-
"the base step size (increment) of orders"
|
|
6561
|
+
"the base step size (increment) of orders",
|
|
6562
|
+
"precision: BASE_PRECISION"
|
|
6523
6563
|
],
|
|
6524
6564
|
"type": "u64"
|
|
6525
6565
|
},
|
|
6526
6566
|
{
|
|
6527
6567
|
"name": "orderTickSize",
|
|
6528
6568
|
"docs": [
|
|
6529
|
-
"the price tick size of orders"
|
|
6569
|
+
"the price tick size of orders",
|
|
6570
|
+
"precision: PRICE_PRECISION"
|
|
6530
6571
|
],
|
|
6531
6572
|
"type": "u64"
|
|
6532
6573
|
},
|
|
6533
6574
|
{
|
|
6534
6575
|
"name": "minOrderSize",
|
|
6535
6576
|
"docs": [
|
|
6536
|
-
"the minimum base size of an order"
|
|
6577
|
+
"the minimum base size of an order",
|
|
6578
|
+
"precision: BASE_PRECISION"
|
|
6537
6579
|
],
|
|
6538
6580
|
"type": "u64"
|
|
6539
6581
|
},
|
|
6540
6582
|
{
|
|
6541
6583
|
"name": "maxPositionSize",
|
|
6542
6584
|
"docs": [
|
|
6543
|
-
"the max base size a single user can have"
|
|
6585
|
+
"the max base size a single user can have",
|
|
6586
|
+
"precision: BASE_PRECISION"
|
|
6544
6587
|
],
|
|
6545
6588
|
"type": "u64"
|
|
6546
6589
|
},
|
|
@@ -6576,14 +6619,16 @@
|
|
|
6576
6619
|
{
|
|
6577
6620
|
"name": "markStd",
|
|
6578
6621
|
"docs": [
|
|
6579
|
-
"estimate of standard deviation of the fill (mark) prices"
|
|
6622
|
+
"estimate of standard deviation of the fill (mark) prices",
|
|
6623
|
+
"precision: PRICE_PRECISION"
|
|
6580
6624
|
],
|
|
6581
6625
|
"type": "u64"
|
|
6582
6626
|
},
|
|
6583
6627
|
{
|
|
6584
6628
|
"name": "oracleStd",
|
|
6585
6629
|
"docs": [
|
|
6586
|
-
"estimate of standard deviation of the oracle price at each update"
|
|
6630
|
+
"estimate of standard deviation of the oracle price at each update",
|
|
6631
|
+
"precision: PRICE_PRECISION"
|
|
6587
6632
|
],
|
|
6588
6633
|
"type": "u64"
|
|
6589
6634
|
},
|
|
@@ -6660,7 +6705,8 @@
|
|
|
6660
6705
|
{
|
|
6661
6706
|
"name": "ammJitIntensity",
|
|
6662
6707
|
"docs": [
|
|
6663
|
-
"the jit intensity of AMM. larger intensity means larger participation in jit. 0 means no jit participation."
|
|
6708
|
+
"the jit intensity of AMM. larger intensity means larger participation in jit. 0 means no jit participation.",
|
|
6709
|
+
"(0, 100] is intensity for protocol-owned AMM. (100, 200] is intensity for user LP-owned AMM."
|
|
6664
6710
|
],
|
|
6665
6711
|
"type": "u8"
|
|
6666
6712
|
},
|
|
@@ -6680,12 +6726,20 @@
|
|
|
6680
6726
|
],
|
|
6681
6727
|
"type": "bool"
|
|
6682
6728
|
},
|
|
6729
|
+
{
|
|
6730
|
+
"name": "targetBaseAssetAmountPerLp",
|
|
6731
|
+
"docs": [
|
|
6732
|
+
"the target value for `base_asset_amount_per_lp`, used during AMM JIT with LP split",
|
|
6733
|
+
"precision: BASE_PRECISION"
|
|
6734
|
+
],
|
|
6735
|
+
"type": "i32"
|
|
6736
|
+
},
|
|
6683
6737
|
{
|
|
6684
6738
|
"name": "padding",
|
|
6685
6739
|
"type": {
|
|
6686
6740
|
"array": [
|
|
6687
6741
|
"u8",
|
|
6688
|
-
|
|
6742
|
+
44
|
|
6689
6743
|
]
|
|
6690
6744
|
}
|
|
6691
6745
|
}
|
|
@@ -7449,6 +7503,20 @@
|
|
|
7449
7503
|
]
|
|
7450
7504
|
}
|
|
7451
7505
|
},
|
|
7506
|
+
{
|
|
7507
|
+
"name": "SwapReduceOnly",
|
|
7508
|
+
"type": {
|
|
7509
|
+
"kind": "enum",
|
|
7510
|
+
"variants": [
|
|
7511
|
+
{
|
|
7512
|
+
"name": "In"
|
|
7513
|
+
},
|
|
7514
|
+
{
|
|
7515
|
+
"name": "Out"
|
|
7516
|
+
}
|
|
7517
|
+
]
|
|
7518
|
+
}
|
|
7519
|
+
},
|
|
7452
7520
|
{
|
|
7453
7521
|
"name": "TwapPeriod",
|
|
7454
7522
|
"type": {
|
|
@@ -7681,6 +7749,12 @@
|
|
|
7681
7749
|
},
|
|
7682
7750
|
{
|
|
7683
7751
|
"name": "OrderFillWithPhoenix"
|
|
7752
|
+
},
|
|
7753
|
+
{
|
|
7754
|
+
"name": "OrderFilledWithAMMJitLPSplit"
|
|
7755
|
+
},
|
|
7756
|
+
{
|
|
7757
|
+
"name": "OrderFilledWithLPJit"
|
|
7684
7758
|
}
|
|
7685
7759
|
]
|
|
7686
7760
|
}
|
|
@@ -7897,6 +7971,23 @@
|
|
|
7897
7971
|
]
|
|
7898
7972
|
}
|
|
7899
7973
|
},
|
|
7974
|
+
{
|
|
7975
|
+
"name": "AMMLiquiditySplit",
|
|
7976
|
+
"type": {
|
|
7977
|
+
"kind": "enum",
|
|
7978
|
+
"variants": [
|
|
7979
|
+
{
|
|
7980
|
+
"name": "ProtocolOwned"
|
|
7981
|
+
},
|
|
7982
|
+
{
|
|
7983
|
+
"name": "LPOwned"
|
|
7984
|
+
},
|
|
7985
|
+
{
|
|
7986
|
+
"name": "Shared"
|
|
7987
|
+
}
|
|
7988
|
+
]
|
|
7989
|
+
}
|
|
7990
|
+
},
|
|
7900
7991
|
{
|
|
7901
7992
|
"name": "SpotBalanceType",
|
|
7902
7993
|
"type": {
|
|
@@ -53,7 +53,7 @@ export declare class JupiterClient {
|
|
|
53
53
|
/**
|
|
54
54
|
* Get a swap transaction for a route
|
|
55
55
|
* @param route the route to perform swap
|
|
56
|
-
* @param userPublicKey the
|
|
56
|
+
* @param userPublicKey the signer's wallet public key
|
|
57
57
|
* @param slippageBps the slippage tolerance in basis points
|
|
58
58
|
*/
|
|
59
59
|
getSwapTransaction({ route, userPublicKey, slippageBps, }: {
|
|
@@ -34,7 +34,7 @@ class JupiterClient {
|
|
|
34
34
|
/**
|
|
35
35
|
* Get a swap transaction for a route
|
|
36
36
|
* @param route the route to perform swap
|
|
37
|
-
* @param userPublicKey the
|
|
37
|
+
* @param userPublicKey the signer's wallet public key
|
|
38
38
|
* @param slippageBps the slippage tolerance in basis points
|
|
39
39
|
*/
|
|
40
40
|
async getSwapTransaction({ route, userPublicKey, slippageBps = 50, }) {
|
|
@@ -62,7 +62,9 @@ class JupiterClient {
|
|
|
62
62
|
const lookupTables = (await Promise.all(message.addressTableLookups.map(async (lookup) => {
|
|
63
63
|
return await this.getLookupTable(lookup.accountKey);
|
|
64
64
|
}))).filter((lookup) => lookup);
|
|
65
|
-
const transactionMessage = web3_js_1.TransactionMessage.decompile(message
|
|
65
|
+
const transactionMessage = web3_js_1.TransactionMessage.decompile(message, {
|
|
66
|
+
addressLookupTableAccounts: lookupTables,
|
|
67
|
+
});
|
|
66
68
|
return {
|
|
67
69
|
transactionMessage,
|
|
68
70
|
lookupTables,
|
|
@@ -90,6 +92,9 @@ class JupiterClient {
|
|
|
90
92
|
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA') {
|
|
91
93
|
return false;
|
|
92
94
|
}
|
|
95
|
+
if (instruction.programId.toString() === '11111111111111111111111111111111') {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
93
98
|
if (instruction.programId.toString() ===
|
|
94
99
|
'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL') {
|
|
95
100
|
const mint = instruction.keys[3].pubkey;
|
|
@@ -1,10 +1,51 @@
|
|
|
1
1
|
import { SpotMarketAccount, SpotBalanceType, MarginCategory } from '../types';
|
|
2
2
|
import { BN } from '@coral-xyz/anchor';
|
|
3
3
|
import { OraclePriceData } from '../oracles/types';
|
|
4
|
+
/**
|
|
5
|
+
* Calculates the balance of a given token amount including any accumulated interest. This
|
|
6
|
+
* is the same as `SpotPosition.scaledBalance`.
|
|
7
|
+
*
|
|
8
|
+
* @param {BN} tokenAmount - the amount of tokens
|
|
9
|
+
* @param {SpotMarketAccount} spotMarket - the spot market account
|
|
10
|
+
* @param {SpotBalanceType} balanceType - the balance type ('deposit' or 'borrow')
|
|
11
|
+
* @return {BN} the calculated balance, scaled by `SPOT_MARKET_BALANCE_PRECISION`
|
|
12
|
+
*/
|
|
4
13
|
export declare function getBalance(tokenAmount: BN, spotMarket: SpotMarketAccount, balanceType: SpotBalanceType): BN;
|
|
14
|
+
/**
|
|
15
|
+
* Calculates the spot token amount including any accumulated interest.
|
|
16
|
+
*
|
|
17
|
+
* @param {BN} balanceAmount - The balance amount, typically from `SpotPosition.scaledBalance`
|
|
18
|
+
* @param {SpotMarketAccount} spotMarket - The spot market account details
|
|
19
|
+
* @param {SpotBalanceType} balanceType - The balance type to be used for calculation
|
|
20
|
+
* @returns {BN} The calculated token amount, scaled by `SpotMarketConfig.precision`
|
|
21
|
+
*/
|
|
5
22
|
export declare function getTokenAmount(balanceAmount: BN, spotMarket: SpotMarketAccount, balanceType: SpotBalanceType): BN;
|
|
23
|
+
/**
|
|
24
|
+
* Returns the signed (positive for deposit,negative for borrow) token amount based on the balance type.
|
|
25
|
+
*
|
|
26
|
+
* @param {BN} tokenAmount - The token amount to convert (from `getTokenAmount`)
|
|
27
|
+
* @param {SpotBalanceType} balanceType - The balance type to determine the sign of the token amount.
|
|
28
|
+
* @returns {BN} - The signed token amount, scaled by `SpotMarketConfig.precision`
|
|
29
|
+
*/
|
|
6
30
|
export declare function getSignedTokenAmount(tokenAmount: BN, balanceType: SpotBalanceType): BN;
|
|
31
|
+
/**
|
|
32
|
+
* Calculates the value of a given token amount using the worst of the provided oracle price and its TWAP.
|
|
33
|
+
*
|
|
34
|
+
* @param {BN} tokenAmount - The amount of tokens to calculate the value for (from `getTokenAmount`)
|
|
35
|
+
* @param {number} spotDecimals - The number of decimals in the token.
|
|
36
|
+
* @param {OraclePriceData} oraclePriceData - The oracle price data (typically a token/USD oracle).
|
|
37
|
+
* @param {BN} oraclePriceTwap - The Time-Weighted Average Price of the oracle.
|
|
38
|
+
* @return {BN} The calculated value of the given token amount, scaled by `PRICE_PRECISION`
|
|
39
|
+
*/
|
|
7
40
|
export declare function getStrictTokenValue(tokenAmount: BN, spotDecimals: number, oraclePriceData: OraclePriceData, oraclePriceTwap: BN): BN;
|
|
41
|
+
/**
|
|
42
|
+
* Calculates the value of a given token amount in relation to an oracle price data
|
|
43
|
+
*
|
|
44
|
+
* @param {BN} tokenAmount - The amount of tokens to calculate the value for (from `getTokenAmount`)
|
|
45
|
+
* @param {number} spotDecimals - The number of decimal places of the token.
|
|
46
|
+
* @param {OraclePriceData} oraclePriceData - The oracle price data (typically a token/USD oracle).
|
|
47
|
+
* @return {BN} The value of the token based on the oracle, scaled by `PRICE_PRECISION`
|
|
48
|
+
*/
|
|
8
49
|
export declare function getTokenValue(tokenAmount: BN, spotDecimals: number, oraclePriceData: OraclePriceData): BN;
|
|
9
50
|
export declare function calculateAssetWeight(balanceAmount: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
|
|
10
51
|
export declare function calculateLiabilityWeight(size: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
|
package/lib/math/spotBalance.js
CHANGED
|
@@ -7,6 +7,15 @@ const numericConstants_1 = require("../constants/numericConstants");
|
|
|
7
7
|
const margin_1 = require("./margin");
|
|
8
8
|
const numericConstants_2 = require("../constants/numericConstants");
|
|
9
9
|
const utils_1 = require("./utils");
|
|
10
|
+
/**
|
|
11
|
+
* Calculates the balance of a given token amount including any accumulated interest. This
|
|
12
|
+
* is the same as `SpotPosition.scaledBalance`.
|
|
13
|
+
*
|
|
14
|
+
* @param {BN} tokenAmount - the amount of tokens
|
|
15
|
+
* @param {SpotMarketAccount} spotMarket - the spot market account
|
|
16
|
+
* @param {SpotBalanceType} balanceType - the balance type ('deposit' or 'borrow')
|
|
17
|
+
* @return {BN} the calculated balance, scaled by `SPOT_MARKET_BALANCE_PRECISION`
|
|
18
|
+
*/
|
|
10
19
|
function getBalance(tokenAmount, spotMarket, balanceType) {
|
|
11
20
|
const precisionIncrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
|
|
12
21
|
const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
|
|
@@ -19,6 +28,14 @@ function getBalance(tokenAmount, spotMarket, balanceType) {
|
|
|
19
28
|
return balance;
|
|
20
29
|
}
|
|
21
30
|
exports.getBalance = getBalance;
|
|
31
|
+
/**
|
|
32
|
+
* Calculates the spot token amount including any accumulated interest.
|
|
33
|
+
*
|
|
34
|
+
* @param {BN} balanceAmount - The balance amount, typically from `SpotPosition.scaledBalance`
|
|
35
|
+
* @param {SpotMarketAccount} spotMarket - The spot market account details
|
|
36
|
+
* @param {SpotBalanceType} balanceType - The balance type to be used for calculation
|
|
37
|
+
* @returns {BN} The calculated token amount, scaled by `SpotMarketConfig.precision`
|
|
38
|
+
*/
|
|
22
39
|
function getTokenAmount(balanceAmount, spotMarket, balanceType) {
|
|
23
40
|
const precisionDecrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
|
|
24
41
|
if ((0, types_1.isVariant)(balanceType, 'deposit')) {
|
|
@@ -31,6 +48,13 @@ function getTokenAmount(balanceAmount, spotMarket, balanceType) {
|
|
|
31
48
|
}
|
|
32
49
|
}
|
|
33
50
|
exports.getTokenAmount = getTokenAmount;
|
|
51
|
+
/**
|
|
52
|
+
* Returns the signed (positive for deposit,negative for borrow) token amount based on the balance type.
|
|
53
|
+
*
|
|
54
|
+
* @param {BN} tokenAmount - The token amount to convert (from `getTokenAmount`)
|
|
55
|
+
* @param {SpotBalanceType} balanceType - The balance type to determine the sign of the token amount.
|
|
56
|
+
* @returns {BN} - The signed token amount, scaled by `SpotMarketConfig.precision`
|
|
57
|
+
*/
|
|
34
58
|
function getSignedTokenAmount(tokenAmount, balanceType) {
|
|
35
59
|
if ((0, types_1.isVariant)(balanceType, 'deposit')) {
|
|
36
60
|
return tokenAmount;
|
|
@@ -40,6 +64,15 @@ function getSignedTokenAmount(tokenAmount, balanceType) {
|
|
|
40
64
|
}
|
|
41
65
|
}
|
|
42
66
|
exports.getSignedTokenAmount = getSignedTokenAmount;
|
|
67
|
+
/**
|
|
68
|
+
* Calculates the value of a given token amount using the worst of the provided oracle price and its TWAP.
|
|
69
|
+
*
|
|
70
|
+
* @param {BN} tokenAmount - The amount of tokens to calculate the value for (from `getTokenAmount`)
|
|
71
|
+
* @param {number} spotDecimals - The number of decimals in the token.
|
|
72
|
+
* @param {OraclePriceData} oraclePriceData - The oracle price data (typically a token/USD oracle).
|
|
73
|
+
* @param {BN} oraclePriceTwap - The Time-Weighted Average Price of the oracle.
|
|
74
|
+
* @return {BN} The calculated value of the given token amount, scaled by `PRICE_PRECISION`
|
|
75
|
+
*/
|
|
43
76
|
function getStrictTokenValue(tokenAmount, spotDecimals, oraclePriceData, oraclePriceTwap) {
|
|
44
77
|
if (tokenAmount.eq(numericConstants_1.ZERO)) {
|
|
45
78
|
return numericConstants_1.ZERO;
|
|
@@ -55,6 +88,14 @@ function getStrictTokenValue(tokenAmount, spotDecimals, oraclePriceData, oracleP
|
|
|
55
88
|
return tokenAmount.mul(price).div(precisionDecrease);
|
|
56
89
|
}
|
|
57
90
|
exports.getStrictTokenValue = getStrictTokenValue;
|
|
91
|
+
/**
|
|
92
|
+
* Calculates the value of a given token amount in relation to an oracle price data
|
|
93
|
+
*
|
|
94
|
+
* @param {BN} tokenAmount - The amount of tokens to calculate the value for (from `getTokenAmount`)
|
|
95
|
+
* @param {number} spotDecimals - The number of decimal places of the token.
|
|
96
|
+
* @param {OraclePriceData} oraclePriceData - The oracle price data (typically a token/USD oracle).
|
|
97
|
+
* @return {BN} The value of the token based on the oracle, scaled by `PRICE_PRECISION`
|
|
98
|
+
*/
|
|
58
99
|
function getTokenValue(tokenAmount, spotDecimals, oraclePriceData) {
|
|
59
100
|
if (tokenAmount.eq(numericConstants_1.ZERO)) {
|
|
60
101
|
return numericConstants_1.ZERO;
|
package/lib/token/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import { Account } from '@solana/spl-token';
|
|
3
|
+
import { PublicKey } from '@solana/web3.js';
|
|
4
|
+
export declare function parseTokenAccount(data: Buffer, pubkey: PublicKey): Account;
|
package/lib/token/index.js
CHANGED
|
@@ -2,37 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseTokenAccount = void 0;
|
|
4
4
|
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const accountInfo =
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
accountInfo.delegatedAmount = spl_token_1.u64.fromBuffer(Buffer.from('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;
|
|
5
|
+
function parseTokenAccount(data, pubkey) {
|
|
6
|
+
// mock AccountInfo so unpackAccount can be used
|
|
7
|
+
const accountInfo = {
|
|
8
|
+
data,
|
|
9
|
+
owner: spl_token_1.TOKEN_PROGRAM_ID,
|
|
10
|
+
executable: false,
|
|
11
|
+
lamports: 0,
|
|
12
|
+
};
|
|
13
|
+
return (0, spl_token_1.unpackAccount)(pubkey, accountInfo, spl_token_1.TOKEN_PROGRAM_ID);
|
|
37
14
|
}
|
|
38
15
|
exports.parseTokenAccount = parseTokenAccount;
|
package/lib/tokenFaucet.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as anchor from '@coral-xyz/anchor';
|
|
2
2
|
import { AnchorProvider, Program } from '@coral-xyz/anchor';
|
|
3
|
-
import {
|
|
3
|
+
import { Account } from '@solana/spl-token';
|
|
4
4
|
import { ConfirmOptions, Connection, PublicKey, TransactionInstruction, TransactionSignature } from '@solana/web3.js';
|
|
5
5
|
import { BN } from '.';
|
|
6
6
|
import { IWallet } from './types';
|
|
@@ -30,9 +30,9 @@ export declare class TokenFaucet {
|
|
|
30
30
|
}): Promise<anchor.web3.PublicKey>;
|
|
31
31
|
getTokenAccountInfo(props: {
|
|
32
32
|
userPubKey: PublicKey;
|
|
33
|
-
}): Promise<
|
|
33
|
+
}): Promise<Account>;
|
|
34
34
|
subscribeToTokenAccount(props: {
|
|
35
35
|
userPubKey: PublicKey;
|
|
36
|
-
callback: (accountInfo:
|
|
36
|
+
callback: (accountInfo: Account) => void;
|
|
37
37
|
}): Promise<boolean>;
|
|
38
38
|
}
|
package/lib/tokenFaucet.js
CHANGED
|
@@ -123,22 +123,17 @@ class TokenFaucet {
|
|
|
123
123
|
async createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount) {
|
|
124
124
|
const state = await this.fetchState();
|
|
125
125
|
const associateTokenPublicKey = await this.getAssosciatedMockUSDMintAddress({ userPubKey: userPublicKey });
|
|
126
|
-
const createAssociatedAccountIx = spl_token_1.
|
|
126
|
+
const createAssociatedAccountIx = (0, spl_token_1.createAssociatedTokenAccountInstruction)(this.wallet.publicKey, associateTokenPublicKey, userPublicKey, state.mint);
|
|
127
127
|
const mintToIx = await this.mintToUserIx(associateTokenPublicKey, amount);
|
|
128
128
|
return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
|
|
129
129
|
}
|
|
130
130
|
async getAssosciatedMockUSDMintAddress(props) {
|
|
131
131
|
const state = await this.fetchState();
|
|
132
|
-
return
|
|
132
|
+
return (0, spl_token_1.getAssociatedTokenAddress)(state.mint, props.userPubKey);
|
|
133
133
|
}
|
|
134
134
|
async getTokenAccountInfo(props) {
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
const token = new spl_token_1.Token(this.connection, state.mint, spl_token_1.TOKEN_PROGRAM_ID,
|
|
138
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
139
|
-
// @ts-ignore
|
|
140
|
-
this.provider.payer);
|
|
141
|
-
return await token.getAccountInfo(assosciatedKey);
|
|
135
|
+
const associatedKey = await this.getAssosciatedMockUSDMintAddress(props);
|
|
136
|
+
return await (0, spl_token_1.getAccount)(this.connection, associatedKey);
|
|
142
137
|
}
|
|
143
138
|
async subscribeToTokenAccount(props) {
|
|
144
139
|
try {
|
package/lib/tx/retryTxSender.js
CHANGED
|
@@ -57,6 +57,9 @@ class RetryTxSender {
|
|
|
57
57
|
async sendVersionedTransaction(tx, additionalSigners, opts) {
|
|
58
58
|
// @ts-ignore
|
|
59
59
|
tx.sign((additionalSigners !== null && additionalSigners !== void 0 ? additionalSigners : []).concat(this.provider.wallet.payer));
|
|
60
|
+
if (opts === undefined) {
|
|
61
|
+
opts = this.provider.opts;
|
|
62
|
+
}
|
|
60
63
|
return this.sendRawTransaction(tx.serialize(), opts);
|
|
61
64
|
}
|
|
62
65
|
async sendRawTransaction(rawTransaction, opts) {
|
package/lib/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PublicKey, Transaction } from '@solana/web3.js';
|
|
1
|
+
import { PublicKey, Transaction, VersionedTransaction } from '@solana/web3.js';
|
|
2
2
|
import { BN } from '.';
|
|
3
3
|
export declare enum ExchangeStatus {
|
|
4
4
|
ACTIVE = 0,
|
|
@@ -218,6 +218,12 @@ export declare class OrderActionExplanation {
|
|
|
218
218
|
static readonly ORDER_FILLED_WITH_AMM_JIT: {
|
|
219
219
|
orderFilledWithAmmJit: {};
|
|
220
220
|
};
|
|
221
|
+
static readonly ORDER_FILLED_WITH_AMM_JIT_LP_SPLIT: {
|
|
222
|
+
orderFilledWithAmmJitLpSplit: {};
|
|
223
|
+
};
|
|
224
|
+
static readonly ORDER_FILLED_WITH_LP_JIT: {
|
|
225
|
+
orderFilledWithAmmJitLpSplit: {};
|
|
226
|
+
};
|
|
221
227
|
static readonly ORDER_FILLED_WITH_MATCH: {
|
|
222
228
|
orderFilledWithMatch: {};
|
|
223
229
|
};
|
|
@@ -585,7 +591,7 @@ export type OrderActionRecord = {
|
|
|
585
591
|
oraclePrice: BN;
|
|
586
592
|
};
|
|
587
593
|
export type SwapRecord = {
|
|
588
|
-
ts:
|
|
594
|
+
ts: BN;
|
|
589
595
|
user: PublicKey;
|
|
590
596
|
amountOut: BN;
|
|
591
597
|
amountIn: BN;
|
|
@@ -786,6 +792,7 @@ export type AMM = {
|
|
|
786
792
|
maxSpread: number;
|
|
787
793
|
baseAssetAmountPerLp: BN;
|
|
788
794
|
quoteAssetAmountPerLp: BN;
|
|
795
|
+
targetBaseAssetAmountPerLp: number;
|
|
789
796
|
ammJitIntensity: number;
|
|
790
797
|
maxOpenInterest: BN;
|
|
791
798
|
maxBaseAssetReserve: BN;
|
|
@@ -979,11 +986,24 @@ export type TxParams = {
|
|
|
979
986
|
computeUnits?: number;
|
|
980
987
|
computeUnitsPrice?: number;
|
|
981
988
|
};
|
|
989
|
+
export declare class SwapReduceOnly {
|
|
990
|
+
static readonly In: {
|
|
991
|
+
in: {};
|
|
992
|
+
};
|
|
993
|
+
static readonly Out: {
|
|
994
|
+
out: {};
|
|
995
|
+
};
|
|
996
|
+
}
|
|
982
997
|
export interface IWallet {
|
|
983
998
|
signTransaction(tx: Transaction): Promise<Transaction>;
|
|
984
999
|
signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;
|
|
985
1000
|
publicKey: PublicKey;
|
|
986
1001
|
}
|
|
1002
|
+
export interface IVersionedWallet {
|
|
1003
|
+
signVersionedTransaction(tx: VersionedTransaction): Promise<VersionedTransaction>;
|
|
1004
|
+
signAllVersionedTransactions(txs: VersionedTransaction[]): Promise<VersionedTransaction[]>;
|
|
1005
|
+
publicKey: PublicKey;
|
|
1006
|
+
}
|
|
987
1007
|
export type FeeStructure = {
|
|
988
1008
|
feeTiers: FeeTier[];
|
|
989
1009
|
makerRebateNumerator: BN;
|
package/lib/types.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DefaultOrderParams = exports.ModifyOrderPolicy = exports.PostOnlyParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.StakeAction = exports.SpotFulfillmentConfigStatus = exports.SettlePnlExplanation = exports.DepositExplanation = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.UserStatus = exports.MarketStatus = exports.ExchangeStatus = void 0;
|
|
3
|
+
exports.SwapReduceOnly = exports.DefaultOrderParams = exports.ModifyOrderPolicy = exports.PostOnlyParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.getVariant = exports.isOneOfVariant = exports.isVariant = exports.StakeAction = exports.SpotFulfillmentConfigStatus = exports.SettlePnlExplanation = exports.DepositExplanation = exports.SpotFulfillmentStatus = exports.SpotFulfillmentType = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderStatus = exports.MarketType = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.SpotBalanceType = exports.SwapDirection = exports.AssetTier = exports.ContractTier = exports.ContractType = exports.UserStatus = exports.MarketStatus = exports.ExchangeStatus = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
// # Utility Types / Enums / Constants
|
|
6
6
|
var ExchangeStatus;
|
|
@@ -134,6 +134,12 @@ OrderActionExplanation.ORDER_FILLED_WITH_AMM = {
|
|
|
134
134
|
OrderActionExplanation.ORDER_FILLED_WITH_AMM_JIT = {
|
|
135
135
|
orderFilledWithAmmJit: {},
|
|
136
136
|
};
|
|
137
|
+
OrderActionExplanation.ORDER_FILLED_WITH_AMM_JIT_LP_SPLIT = {
|
|
138
|
+
orderFilledWithAmmJitLpSplit: {},
|
|
139
|
+
};
|
|
140
|
+
OrderActionExplanation.ORDER_FILLED_WITH_LP_JIT = {
|
|
141
|
+
orderFilledWithAmmJitLpSplit: {},
|
|
142
|
+
};
|
|
137
143
|
OrderActionExplanation.ORDER_FILLED_WITH_MATCH = {
|
|
138
144
|
orderFilledWithMatch: {},
|
|
139
145
|
};
|
|
@@ -266,3 +272,8 @@ exports.DefaultOrderParams = {
|
|
|
266
272
|
auctionStartPrice: null,
|
|
267
273
|
auctionEndPrice: null,
|
|
268
274
|
};
|
|
275
|
+
class SwapReduceOnly {
|
|
276
|
+
}
|
|
277
|
+
exports.SwapReduceOnly = SwapReduceOnly;
|
|
278
|
+
SwapReduceOnly.In = { in: {} };
|
|
279
|
+
SwapReduceOnly.Out = { out: {} };
|
package/lib/wallet.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { Keypair, PublicKey, Transaction } from '@solana/web3.js';
|
|
2
|
-
import { IWallet } from './types';
|
|
3
|
-
export declare class Wallet implements IWallet {
|
|
1
|
+
import { Keypair, PublicKey, Transaction, VersionedTransaction } from '@solana/web3.js';
|
|
2
|
+
import { IWallet, IVersionedWallet } from './types';
|
|
3
|
+
export declare class Wallet implements IWallet, IVersionedWallet {
|
|
4
4
|
readonly payer: Keypair;
|
|
5
5
|
constructor(payer: Keypair);
|
|
6
6
|
signTransaction(tx: Transaction): Promise<Transaction>;
|
|
7
|
+
signVersionedTransaction(tx: VersionedTransaction): Promise<VersionedTransaction>;
|
|
7
8
|
signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;
|
|
9
|
+
signAllVersionedTransactions(txs: VersionedTransaction[]): Promise<VersionedTransaction[]>;
|
|
8
10
|
get publicKey(): PublicKey;
|
|
9
11
|
}
|
|
10
12
|
export declare function loadKeypair(privateKey: string): Keypair;
|