@drift-labs/sdk 2.30.0-beta.0 → 2.30.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/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 +51 -2
- package/lib/driftClient.js +162 -13
- package/lib/events/types.d.ts +3 -2
- package/lib/events/types.js +1 -0
- package/lib/examples/makeTradeExample.js +1 -1
- package/lib/idl/drift.json +279 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/jupiter/jupiterClient.d.ts +86 -0
- package/lib/jupiter/jupiterClient.js +109 -0
- 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 +29 -1
- package/lib/types.js +6 -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/assert/assert.js +9 -0
- package/src/constants/spotMarkets.ts +11 -0
- package/src/dlob/DLOB.ts +75 -0
- package/src/driftClient.ts +288 -37
- package/src/events/types.ts +5 -1
- package/src/examples/makeTradeExample.ts +2 -4
- package/src/idl/drift.json +279 -2
- package/src/index.ts +1 -0
- package/src/jupiter/jupiterClient.ts +214 -0
- package/src/math/spotBalance.ts +41 -0
- package/src/token/index.js +38 -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 +32 -1
- package/src/util/computeUnits.js +27 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.ts +34 -12
- package/tests/dlob/helpers.ts +9 -0
- package/tests/dlob/test.ts +218 -40
- package/dlob_read.ts +0 -155
- package/lib/util/getTokenAddress.d.ts +0 -2
- package/lib/util/getTokenAddress.js +0 -9
- package/src/util/getTokenAddress.ts +0 -18
package/src/idl/drift.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.30.0
|
|
2
|
+
"version": "2.30.0",
|
|
3
3
|
"name": "drift",
|
|
4
4
|
"instructions": [
|
|
5
5
|
{
|
|
@@ -710,6 +710,170 @@
|
|
|
710
710
|
}
|
|
711
711
|
]
|
|
712
712
|
},
|
|
713
|
+
{
|
|
714
|
+
"name": "beginSwap",
|
|
715
|
+
"accounts": [
|
|
716
|
+
{
|
|
717
|
+
"name": "state",
|
|
718
|
+
"isMut": false,
|
|
719
|
+
"isSigner": false
|
|
720
|
+
},
|
|
721
|
+
{
|
|
722
|
+
"name": "user",
|
|
723
|
+
"isMut": true,
|
|
724
|
+
"isSigner": false
|
|
725
|
+
},
|
|
726
|
+
{
|
|
727
|
+
"name": "userStats",
|
|
728
|
+
"isMut": true,
|
|
729
|
+
"isSigner": false
|
|
730
|
+
},
|
|
731
|
+
{
|
|
732
|
+
"name": "authority",
|
|
733
|
+
"isMut": false,
|
|
734
|
+
"isSigner": true
|
|
735
|
+
},
|
|
736
|
+
{
|
|
737
|
+
"name": "outSpotMarketVault",
|
|
738
|
+
"isMut": true,
|
|
739
|
+
"isSigner": false
|
|
740
|
+
},
|
|
741
|
+
{
|
|
742
|
+
"name": "inSpotMarketVault",
|
|
743
|
+
"isMut": true,
|
|
744
|
+
"isSigner": false
|
|
745
|
+
},
|
|
746
|
+
{
|
|
747
|
+
"name": "outTokenAccount",
|
|
748
|
+
"isMut": true,
|
|
749
|
+
"isSigner": false
|
|
750
|
+
},
|
|
751
|
+
{
|
|
752
|
+
"name": "inTokenAccount",
|
|
753
|
+
"isMut": true,
|
|
754
|
+
"isSigner": false
|
|
755
|
+
},
|
|
756
|
+
{
|
|
757
|
+
"name": "tokenProgram",
|
|
758
|
+
"isMut": false,
|
|
759
|
+
"isSigner": false
|
|
760
|
+
},
|
|
761
|
+
{
|
|
762
|
+
"name": "driftSigner",
|
|
763
|
+
"isMut": false,
|
|
764
|
+
"isSigner": false
|
|
765
|
+
},
|
|
766
|
+
{
|
|
767
|
+
"name": "instructions",
|
|
768
|
+
"isMut": false,
|
|
769
|
+
"isSigner": false,
|
|
770
|
+
"docs": [
|
|
771
|
+
"Instructions Sysvar for instruction introspection"
|
|
772
|
+
]
|
|
773
|
+
}
|
|
774
|
+
],
|
|
775
|
+
"args": [
|
|
776
|
+
{
|
|
777
|
+
"name": "inMarketIndex",
|
|
778
|
+
"type": "u16"
|
|
779
|
+
},
|
|
780
|
+
{
|
|
781
|
+
"name": "outMarketIndex",
|
|
782
|
+
"type": "u16"
|
|
783
|
+
},
|
|
784
|
+
{
|
|
785
|
+
"name": "amountIn",
|
|
786
|
+
"type": "u64"
|
|
787
|
+
}
|
|
788
|
+
]
|
|
789
|
+
},
|
|
790
|
+
{
|
|
791
|
+
"name": "endSwap",
|
|
792
|
+
"accounts": [
|
|
793
|
+
{
|
|
794
|
+
"name": "state",
|
|
795
|
+
"isMut": false,
|
|
796
|
+
"isSigner": false
|
|
797
|
+
},
|
|
798
|
+
{
|
|
799
|
+
"name": "user",
|
|
800
|
+
"isMut": true,
|
|
801
|
+
"isSigner": false
|
|
802
|
+
},
|
|
803
|
+
{
|
|
804
|
+
"name": "userStats",
|
|
805
|
+
"isMut": true,
|
|
806
|
+
"isSigner": false
|
|
807
|
+
},
|
|
808
|
+
{
|
|
809
|
+
"name": "authority",
|
|
810
|
+
"isMut": false,
|
|
811
|
+
"isSigner": true
|
|
812
|
+
},
|
|
813
|
+
{
|
|
814
|
+
"name": "outSpotMarketVault",
|
|
815
|
+
"isMut": true,
|
|
816
|
+
"isSigner": false
|
|
817
|
+
},
|
|
818
|
+
{
|
|
819
|
+
"name": "inSpotMarketVault",
|
|
820
|
+
"isMut": true,
|
|
821
|
+
"isSigner": false
|
|
822
|
+
},
|
|
823
|
+
{
|
|
824
|
+
"name": "outTokenAccount",
|
|
825
|
+
"isMut": true,
|
|
826
|
+
"isSigner": false
|
|
827
|
+
},
|
|
828
|
+
{
|
|
829
|
+
"name": "inTokenAccount",
|
|
830
|
+
"isMut": true,
|
|
831
|
+
"isSigner": false
|
|
832
|
+
},
|
|
833
|
+
{
|
|
834
|
+
"name": "tokenProgram",
|
|
835
|
+
"isMut": false,
|
|
836
|
+
"isSigner": false
|
|
837
|
+
},
|
|
838
|
+
{
|
|
839
|
+
"name": "driftSigner",
|
|
840
|
+
"isMut": false,
|
|
841
|
+
"isSigner": false
|
|
842
|
+
},
|
|
843
|
+
{
|
|
844
|
+
"name": "instructions",
|
|
845
|
+
"isMut": false,
|
|
846
|
+
"isSigner": false,
|
|
847
|
+
"docs": [
|
|
848
|
+
"Instructions Sysvar for instruction introspection"
|
|
849
|
+
]
|
|
850
|
+
}
|
|
851
|
+
],
|
|
852
|
+
"args": [
|
|
853
|
+
{
|
|
854
|
+
"name": "inMarketIndex",
|
|
855
|
+
"type": "u16"
|
|
856
|
+
},
|
|
857
|
+
{
|
|
858
|
+
"name": "outMarketIndex",
|
|
859
|
+
"type": "u16"
|
|
860
|
+
},
|
|
861
|
+
{
|
|
862
|
+
"name": "limitPrice",
|
|
863
|
+
"type": {
|
|
864
|
+
"option": "u64"
|
|
865
|
+
}
|
|
866
|
+
},
|
|
867
|
+
{
|
|
868
|
+
"name": "reduceOnly",
|
|
869
|
+
"type": {
|
|
870
|
+
"option": {
|
|
871
|
+
"defined": "SwapReduceOnly"
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
]
|
|
876
|
+
},
|
|
713
877
|
{
|
|
714
878
|
"name": "addPerpLpShares",
|
|
715
879
|
"accounts": [
|
|
@@ -4895,12 +5059,46 @@
|
|
|
4895
5059
|
"defined": "AssetTier"
|
|
4896
5060
|
}
|
|
4897
5061
|
},
|
|
5062
|
+
{
|
|
5063
|
+
"name": "padding1",
|
|
5064
|
+
"type": {
|
|
5065
|
+
"array": [
|
|
5066
|
+
"u8",
|
|
5067
|
+
6
|
|
5068
|
+
]
|
|
5069
|
+
}
|
|
5070
|
+
},
|
|
5071
|
+
{
|
|
5072
|
+
"name": "flashLoanAmount",
|
|
5073
|
+
"docs": [
|
|
5074
|
+
"For swaps, the amount of token loaned out in the begin_swap ix",
|
|
5075
|
+
"precision: token mint precision"
|
|
5076
|
+
],
|
|
5077
|
+
"type": "u64"
|
|
5078
|
+
},
|
|
5079
|
+
{
|
|
5080
|
+
"name": "flashLoanInitialTokenAmount",
|
|
5081
|
+
"docs": [
|
|
5082
|
+
"For swaps, the amount in the users token account in the begin_swap ix",
|
|
5083
|
+
"Used to calculate how much of the token left the system in end_swap ix",
|
|
5084
|
+
"precision: token mint precision"
|
|
5085
|
+
],
|
|
5086
|
+
"type": "u64"
|
|
5087
|
+
},
|
|
5088
|
+
{
|
|
5089
|
+
"name": "totalSwapFee",
|
|
5090
|
+
"docs": [
|
|
5091
|
+
"The total fees received from swaps",
|
|
5092
|
+
"precision: token mint precision"
|
|
5093
|
+
],
|
|
5094
|
+
"type": "u64"
|
|
5095
|
+
},
|
|
4898
5096
|
{
|
|
4899
5097
|
"name": "padding",
|
|
4900
5098
|
"type": {
|
|
4901
5099
|
"array": [
|
|
4902
5100
|
"u8",
|
|
4903
|
-
|
|
5101
|
+
56
|
|
4904
5102
|
]
|
|
4905
5103
|
}
|
|
4906
5104
|
}
|
|
@@ -7259,6 +7457,20 @@
|
|
|
7259
7457
|
]
|
|
7260
7458
|
}
|
|
7261
7459
|
},
|
|
7460
|
+
{
|
|
7461
|
+
"name": "SwapReduceOnly",
|
|
7462
|
+
"type": {
|
|
7463
|
+
"kind": "enum",
|
|
7464
|
+
"variants": [
|
|
7465
|
+
{
|
|
7466
|
+
"name": "In"
|
|
7467
|
+
},
|
|
7468
|
+
{
|
|
7469
|
+
"name": "Out"
|
|
7470
|
+
}
|
|
7471
|
+
]
|
|
7472
|
+
}
|
|
7473
|
+
},
|
|
7262
7474
|
{
|
|
7263
7475
|
"name": "TwapPeriod",
|
|
7264
7476
|
"type": {
|
|
@@ -8845,6 +9057,56 @@
|
|
|
8845
9057
|
"index": false
|
|
8846
9058
|
}
|
|
8847
9059
|
]
|
|
9060
|
+
},
|
|
9061
|
+
{
|
|
9062
|
+
"name": "SwapRecord",
|
|
9063
|
+
"fields": [
|
|
9064
|
+
{
|
|
9065
|
+
"name": "ts",
|
|
9066
|
+
"type": "i64",
|
|
9067
|
+
"index": false
|
|
9068
|
+
},
|
|
9069
|
+
{
|
|
9070
|
+
"name": "user",
|
|
9071
|
+
"type": "publicKey",
|
|
9072
|
+
"index": false
|
|
9073
|
+
},
|
|
9074
|
+
{
|
|
9075
|
+
"name": "amountOut",
|
|
9076
|
+
"type": "u64",
|
|
9077
|
+
"index": false
|
|
9078
|
+
},
|
|
9079
|
+
{
|
|
9080
|
+
"name": "amountIn",
|
|
9081
|
+
"type": "u64",
|
|
9082
|
+
"index": false
|
|
9083
|
+
},
|
|
9084
|
+
{
|
|
9085
|
+
"name": "outMarketIndex",
|
|
9086
|
+
"type": "u16",
|
|
9087
|
+
"index": false
|
|
9088
|
+
},
|
|
9089
|
+
{
|
|
9090
|
+
"name": "inMarketIndex",
|
|
9091
|
+
"type": "u16",
|
|
9092
|
+
"index": false
|
|
9093
|
+
},
|
|
9094
|
+
{
|
|
9095
|
+
"name": "outOraclePrice",
|
|
9096
|
+
"type": "i64",
|
|
9097
|
+
"index": false
|
|
9098
|
+
},
|
|
9099
|
+
{
|
|
9100
|
+
"name": "inOraclePrice",
|
|
9101
|
+
"type": "i64",
|
|
9102
|
+
"index": false
|
|
9103
|
+
},
|
|
9104
|
+
{
|
|
9105
|
+
"name": "fee",
|
|
9106
|
+
"type": "u64",
|
|
9107
|
+
"index": false
|
|
9108
|
+
}
|
|
9109
|
+
]
|
|
8848
9110
|
}
|
|
8849
9111
|
],
|
|
8850
9112
|
"errors": [
|
|
@@ -10087,6 +10349,21 @@
|
|
|
10087
10349
|
"code": 6247,
|
|
10088
10350
|
"name": "InvalidPhoenixMarket",
|
|
10089
10351
|
"msg": "InvalidPhoenixMarket"
|
|
10352
|
+
},
|
|
10353
|
+
{
|
|
10354
|
+
"code": 6248,
|
|
10355
|
+
"name": "InvalidSwap",
|
|
10356
|
+
"msg": "InvalidSwap"
|
|
10357
|
+
},
|
|
10358
|
+
{
|
|
10359
|
+
"code": 6249,
|
|
10360
|
+
"name": "SwapLimitPriceBreached",
|
|
10361
|
+
"msg": "SwapLimitPriceBreached"
|
|
10362
|
+
},
|
|
10363
|
+
{
|
|
10364
|
+
"code": 6250,
|
|
10365
|
+
"name": "SpotMarketReduceOnly",
|
|
10366
|
+
"msg": "SpotMarketReduceOnly"
|
|
10090
10367
|
}
|
|
10091
10368
|
]
|
|
10092
10369
|
}
|
package/src/index.ts
CHANGED
|
@@ -32,6 +32,7 @@ export * from './factory/bigNum';
|
|
|
32
32
|
export * from './events/types';
|
|
33
33
|
export * from './events/eventSubscriber';
|
|
34
34
|
export * from './events/fetchLogs';
|
|
35
|
+
export * from './jupiter/jupiterClient';
|
|
35
36
|
export * from './math/auction';
|
|
36
37
|
export * from './math/spotMarket';
|
|
37
38
|
export * from './math/conversion';
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AddressLookupTableAccount,
|
|
3
|
+
Connection,
|
|
4
|
+
PublicKey,
|
|
5
|
+
TransactionInstruction,
|
|
6
|
+
TransactionMessage,
|
|
7
|
+
VersionedTransaction,
|
|
8
|
+
} from '@solana/web3.js';
|
|
9
|
+
import fetch from 'node-fetch';
|
|
10
|
+
import { BN } from '@coral-xyz/anchor';
|
|
11
|
+
|
|
12
|
+
export type SwapMode = 'ExactIn' | 'ExactOut';
|
|
13
|
+
|
|
14
|
+
export interface MarketInfo {
|
|
15
|
+
id: string;
|
|
16
|
+
inAmount: number;
|
|
17
|
+
inputMint: string;
|
|
18
|
+
label: string;
|
|
19
|
+
lpFee: Fee;
|
|
20
|
+
notEnoughLiquidity: boolean;
|
|
21
|
+
outAmount: number;
|
|
22
|
+
outputMint: string;
|
|
23
|
+
platformFee: Fee;
|
|
24
|
+
priceImpactPct: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface Fee {
|
|
28
|
+
amount: number;
|
|
29
|
+
mint: string;
|
|
30
|
+
pct: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface Route {
|
|
34
|
+
amount: number;
|
|
35
|
+
inAmount: number;
|
|
36
|
+
marketInfos: MarketInfo[];
|
|
37
|
+
otherAmountThreshold: number;
|
|
38
|
+
outAmount: number;
|
|
39
|
+
priceImpactPct: number;
|
|
40
|
+
slippageBps: number;
|
|
41
|
+
swapMode: SwapMode;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class JupiterClient {
|
|
45
|
+
url = 'https://quote-api.jup.ag/v4';
|
|
46
|
+
connection: Connection;
|
|
47
|
+
lookupTableCahce = new Map<string, AddressLookupTableAccount>();
|
|
48
|
+
|
|
49
|
+
constructor({ connection }: { connection: Connection }) {
|
|
50
|
+
this.connection = connection;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get routes for a swap
|
|
55
|
+
* @param inputMint the mint of the input token
|
|
56
|
+
* @param outputMint the mint of the output token
|
|
57
|
+
* @param amount the amount of the input token
|
|
58
|
+
* @param slippageBps the slippage tolerance in basis points
|
|
59
|
+
* @param swapMode the swap mode (ExactIn or ExactOut)
|
|
60
|
+
*/
|
|
61
|
+
public async getRoutes({
|
|
62
|
+
inputMint,
|
|
63
|
+
outputMint,
|
|
64
|
+
amount,
|
|
65
|
+
slippageBps = 50,
|
|
66
|
+
swapMode = 'ExactIn',
|
|
67
|
+
}: {
|
|
68
|
+
inputMint: PublicKey;
|
|
69
|
+
outputMint: PublicKey;
|
|
70
|
+
amount: BN;
|
|
71
|
+
slippageBps?: number;
|
|
72
|
+
swapMode?: SwapMode;
|
|
73
|
+
}): Promise<Route[]> {
|
|
74
|
+
const params = new URLSearchParams({
|
|
75
|
+
inputMint: inputMint.toString(),
|
|
76
|
+
outputMint: outputMint.toString(),
|
|
77
|
+
amount: amount.toString(),
|
|
78
|
+
slippageBps: slippageBps.toString(),
|
|
79
|
+
swapMode,
|
|
80
|
+
}).toString();
|
|
81
|
+
|
|
82
|
+
const { data: routes } = await (
|
|
83
|
+
await fetch(`https://quote-api.jup.ag/v4/quote?${params}`)
|
|
84
|
+
).json();
|
|
85
|
+
|
|
86
|
+
return routes;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Get a swap transaction for a route
|
|
91
|
+
* @param route the route to perform swap
|
|
92
|
+
* @param userPublicKey the signer's wallet public key
|
|
93
|
+
* @param slippageBps the slippage tolerance in basis points
|
|
94
|
+
*/
|
|
95
|
+
public async getSwapTransaction({
|
|
96
|
+
route,
|
|
97
|
+
userPublicKey,
|
|
98
|
+
slippageBps = 50,
|
|
99
|
+
}: {
|
|
100
|
+
route: Route;
|
|
101
|
+
userPublicKey: PublicKey;
|
|
102
|
+
slippageBps?: number;
|
|
103
|
+
}): Promise<VersionedTransaction> {
|
|
104
|
+
const resp = await (
|
|
105
|
+
await fetch(`${this.url}/swap`, {
|
|
106
|
+
method: 'POST',
|
|
107
|
+
headers: {
|
|
108
|
+
'Content-Type': 'application/json',
|
|
109
|
+
},
|
|
110
|
+
body: JSON.stringify({
|
|
111
|
+
route,
|
|
112
|
+
userPublicKey,
|
|
113
|
+
slippageBps,
|
|
114
|
+
}),
|
|
115
|
+
})
|
|
116
|
+
).json();
|
|
117
|
+
|
|
118
|
+
const { swapTransaction } = resp;
|
|
119
|
+
|
|
120
|
+
const swapTransactionBuf = Buffer.from(swapTransaction, 'base64');
|
|
121
|
+
return VersionedTransaction.deserialize(swapTransactionBuf);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Get the transaction message and lookup tables for a transaction
|
|
126
|
+
* @param transaction
|
|
127
|
+
*/
|
|
128
|
+
public async getTransactionMessageAndLookupTables({
|
|
129
|
+
transaction,
|
|
130
|
+
}: {
|
|
131
|
+
transaction: VersionedTransaction;
|
|
132
|
+
}): Promise<{
|
|
133
|
+
transactionMessage: TransactionMessage;
|
|
134
|
+
lookupTables: AddressLookupTableAccount[];
|
|
135
|
+
}> {
|
|
136
|
+
const message = transaction.message;
|
|
137
|
+
|
|
138
|
+
const lookupTables = (
|
|
139
|
+
await Promise.all(
|
|
140
|
+
message.addressTableLookups.map(async (lookup) => {
|
|
141
|
+
return await this.getLookupTable(lookup.accountKey);
|
|
142
|
+
})
|
|
143
|
+
)
|
|
144
|
+
).filter((lookup) => lookup);
|
|
145
|
+
|
|
146
|
+
const transactionMessage = TransactionMessage.decompile(message, {
|
|
147
|
+
addressLookupTableAccounts: lookupTables,
|
|
148
|
+
});
|
|
149
|
+
return {
|
|
150
|
+
transactionMessage,
|
|
151
|
+
lookupTables,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async getLookupTable(
|
|
156
|
+
accountKey: PublicKey
|
|
157
|
+
): Promise<AddressLookupTableAccount> {
|
|
158
|
+
if (this.lookupTableCahce.has(accountKey.toString())) {
|
|
159
|
+
return this.lookupTableCahce.get(accountKey.toString());
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return (await this.connection.getAddressLookupTable(accountKey)).value;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Get the jupiter instructions from transaction by filtering out instructions to compute budget and associated token programs
|
|
167
|
+
* @param transactionMessage the transaction message
|
|
168
|
+
* @param inputMint the input mint
|
|
169
|
+
* @param outputMint the output mint
|
|
170
|
+
*/
|
|
171
|
+
public getJupiterInstructions({
|
|
172
|
+
transactionMessage,
|
|
173
|
+
inputMint,
|
|
174
|
+
outputMint,
|
|
175
|
+
}: {
|
|
176
|
+
transactionMessage: TransactionMessage;
|
|
177
|
+
inputMint: PublicKey;
|
|
178
|
+
outputMint: PublicKey;
|
|
179
|
+
}): TransactionInstruction[] {
|
|
180
|
+
return transactionMessage.instructions.filter((instruction) => {
|
|
181
|
+
if (
|
|
182
|
+
instruction.programId.toString() ===
|
|
183
|
+
'ComputeBudget111111111111111111111111111111'
|
|
184
|
+
) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (
|
|
189
|
+
instruction.programId.toString() ===
|
|
190
|
+
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'
|
|
191
|
+
) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (
|
|
196
|
+
instruction.programId.toString() === '11111111111111111111111111111111'
|
|
197
|
+
) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (
|
|
202
|
+
instruction.programId.toString() ===
|
|
203
|
+
'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL'
|
|
204
|
+
) {
|
|
205
|
+
const mint = instruction.keys[3].pubkey;
|
|
206
|
+
if (mint.equals(inputMint) || mint.equals(outputMint)) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return true;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
package/src/math/spotBalance.ts
CHANGED
|
@@ -23,6 +23,15 @@ import { OraclePriceData } from '../oracles/types';
|
|
|
23
23
|
import { PERCENTAGE_PRECISION } from '../constants/numericConstants';
|
|
24
24
|
import { divCeil } from './utils';
|
|
25
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Calculates the balance of a given token amount including any accumulated interest. This
|
|
28
|
+
* is the same as `SpotPosition.scaledBalance`.
|
|
29
|
+
*
|
|
30
|
+
* @param {BN} tokenAmount - the amount of tokens
|
|
31
|
+
* @param {SpotMarketAccount} spotMarket - the spot market account
|
|
32
|
+
* @param {SpotBalanceType} balanceType - the balance type ('deposit' or 'borrow')
|
|
33
|
+
* @return {BN} the calculated balance, scaled by `SPOT_MARKET_BALANCE_PRECISION`
|
|
34
|
+
*/
|
|
26
35
|
export function getBalance(
|
|
27
36
|
tokenAmount: BN,
|
|
28
37
|
spotMarket: SpotMarketAccount,
|
|
@@ -43,6 +52,14 @@ export function getBalance(
|
|
|
43
52
|
return balance;
|
|
44
53
|
}
|
|
45
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Calculates the spot token amount including any accumulated interest.
|
|
57
|
+
*
|
|
58
|
+
* @param {BN} balanceAmount - The balance amount, typically from `SpotPosition.scaledBalance`
|
|
59
|
+
* @param {SpotMarketAccount} spotMarket - The spot market account details
|
|
60
|
+
* @param {SpotBalanceType} balanceType - The balance type to be used for calculation
|
|
61
|
+
* @returns {BN} The calculated token amount, scaled by `SpotMarketConfig.precision`
|
|
62
|
+
*/
|
|
46
63
|
export function getTokenAmount(
|
|
47
64
|
balanceAmount: BN,
|
|
48
65
|
spotMarket: SpotMarketAccount,
|
|
@@ -62,6 +79,13 @@ export function getTokenAmount(
|
|
|
62
79
|
}
|
|
63
80
|
}
|
|
64
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Returns the signed (positive for deposit,negative for borrow) token amount based on the balance type.
|
|
84
|
+
*
|
|
85
|
+
* @param {BN} tokenAmount - The token amount to convert (from `getTokenAmount`)
|
|
86
|
+
* @param {SpotBalanceType} balanceType - The balance type to determine the sign of the token amount.
|
|
87
|
+
* @returns {BN} - The signed token amount, scaled by `SpotMarketConfig.precision`
|
|
88
|
+
*/
|
|
65
89
|
export function getSignedTokenAmount(
|
|
66
90
|
tokenAmount: BN,
|
|
67
91
|
balanceType: SpotBalanceType
|
|
@@ -73,6 +97,15 @@ export function getSignedTokenAmount(
|
|
|
73
97
|
}
|
|
74
98
|
}
|
|
75
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Calculates the value of a given token amount using the worst of the provided oracle price and its TWAP.
|
|
102
|
+
*
|
|
103
|
+
* @param {BN} tokenAmount - The amount of tokens to calculate the value for (from `getTokenAmount`)
|
|
104
|
+
* @param {number} spotDecimals - The number of decimals in the token.
|
|
105
|
+
* @param {OraclePriceData} oraclePriceData - The oracle price data (typically a token/USD oracle).
|
|
106
|
+
* @param {BN} oraclePriceTwap - The Time-Weighted Average Price of the oracle.
|
|
107
|
+
* @return {BN} The calculated value of the given token amount, scaled by `PRICE_PRECISION`
|
|
108
|
+
*/
|
|
76
109
|
export function getStrictTokenValue(
|
|
77
110
|
tokenAmount: BN,
|
|
78
111
|
spotDecimals: number,
|
|
@@ -95,6 +128,14 @@ export function getStrictTokenValue(
|
|
|
95
128
|
return tokenAmount.mul(price).div(precisionDecrease);
|
|
96
129
|
}
|
|
97
130
|
|
|
131
|
+
/**
|
|
132
|
+
* Calculates the value of a given token amount in relation to an oracle price data
|
|
133
|
+
*
|
|
134
|
+
* @param {BN} tokenAmount - The amount of tokens to calculate the value for (from `getTokenAmount`)
|
|
135
|
+
* @param {number} spotDecimals - The number of decimal places of the token.
|
|
136
|
+
* @param {OraclePriceData} oraclePriceData - The oracle price data (typically a token/USD oracle).
|
|
137
|
+
* @return {BN} The value of the token based on the oracle, scaled by `PRICE_PRECISION`
|
|
138
|
+
*/
|
|
98
139
|
export function getTokenValue(
|
|
99
140
|
tokenAmount: BN,
|
|
100
141
|
spotDecimals: number,
|
|
@@ -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;
|
package/src/token/index.ts
CHANGED
|
@@ -1,37 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { PublicKey } from '@solana/web3.js';
|
|
3
|
-
|
|
4
|
-
export function parseTokenAccount(data: Buffer):
|
|
5
|
-
|
|
6
|
-
accountInfo
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
accountInfo.delegatedAmount = u64.fromBuffer(Buffer.from('0'));
|
|
14
|
-
} else {
|
|
15
|
-
accountInfo.delegate = new PublicKey(accountInfo.delegate);
|
|
16
|
-
accountInfo.delegatedAmount = u64.fromBuffer(accountInfo.delegatedAmount);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
accountInfo.isInitialized = accountInfo.state !== 0;
|
|
20
|
-
accountInfo.isFrozen = accountInfo.state === 2;
|
|
21
|
-
|
|
22
|
-
if (accountInfo.isNativeOption === 1) {
|
|
23
|
-
accountInfo.rentExemptReserve = u64.fromBuffer(accountInfo.isNative);
|
|
24
|
-
accountInfo.isNative = true;
|
|
25
|
-
} else {
|
|
26
|
-
accountInfo.rentExemptReserve = null;
|
|
27
|
-
accountInfo.isNative = false;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (accountInfo.closeAuthorityOption === 0) {
|
|
31
|
-
accountInfo.closeAuthority = null;
|
|
32
|
-
} else {
|
|
33
|
-
accountInfo.closeAuthority = new PublicKey(accountInfo.closeAuthority);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return accountInfo;
|
|
1
|
+
import { Account, TOKEN_PROGRAM_ID, unpackAccount } from '@solana/spl-token';
|
|
2
|
+
import { PublicKey, AccountInfo } from '@solana/web3.js';
|
|
3
|
+
|
|
4
|
+
export function parseTokenAccount(data: Buffer, pubkey: PublicKey): Account {
|
|
5
|
+
// mock AccountInfo so unpackAccount can be used
|
|
6
|
+
const accountInfo: AccountInfo<Buffer> = {
|
|
7
|
+
data,
|
|
8
|
+
owner: TOKEN_PROGRAM_ID,
|
|
9
|
+
executable: false,
|
|
10
|
+
lamports: 0,
|
|
11
|
+
};
|
|
12
|
+
return unpackAccount(pubkey, accountInfo, TOKEN_PROGRAM_ID);
|
|
37
13
|
}
|