-
Notifications
You must be signed in to change notification settings - Fork 135
/
index.ts
199 lines (188 loc) · 7.36 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { Address } from "@coral-xyz/anchor";
import { Percentage, TransactionBuilder } from "@orca-so/common-sdk";
import { AddressLookupTableAccount } from "@solana/web3.js";
import BN from "bn.js";
import { WhirlpoolAccountFetchOptions } from "../../network/public/fetcher";
import { SwapQuote } from "../../quotes/public";
import { Path } from "../../utils/public";
import { AtaAccountInfo, RouteSelectOptions } from "./router-utils";
export * from "./router-builder";
export * from "./router-utils";
/**
* A Trade type that represents a trade between two tokens
*
* @category Router
* @param tokenIn The token that is being traded in
* @param tokenOut The token that is being traded out
* @param tradeAmount The amount of token being traded in or out
* @param amountSpecifiedIsInput Whether the trade amount is the amount being traded in or out
*/
export type Trade = {
tokenIn: Address;
tokenOut: Address;
tradeAmount: BN;
amountSpecifiedIsInput: boolean;
};
/**
* Options to configure the router.
*
* @category Router
* @param percentIncrement The percent increment to use when splitting a trade into multiple trades.
* @param numTopRoutes The number of top routes to return from the router.
* @param numTopPartialQuotes The number of top partial quotes to return from the router.
* @param maxSplits The maximum number of splits to perform on a trade.
*/
export type RoutingOptions = {
percentIncrement: number;
numTopRoutes: number;
numTopPartialQuotes: number;
maxSplits: number;
};
/**
* A trade route that is ready to execute.
* A trade can be broken into multiple sub-trades for potentially better trades.
*
* @category Router
* @param subRoutes
* The sub-routes that make up the trade route. The sum of all splitPercent should equal 100.
* @param totalAmountIn The total amount of token being traded in for this trade.
* @param totalAmountOut The total amount of token being traded out for this trade.
*/
export type TradeRoute = {
subRoutes: SubTradeRoute[];
totalAmountIn: BN;
totalAmountOut: BN;
};
/**
* Represents a fragment of a trade that was splitted into multiple trades for more efficient execution.
*
* @category Router
* @param path The path of pool addresses that make up this sub trade.
* @param splitPercent The percent of the trade that this sub trade represents.
* @param amountIn The amount of token being traded in within this sub-route.
* @param amountOut The amount of token being traded out within this sub-routes.
* @param hopQuotes The quotes for each hop in the path of this trade.
*/
export type SubTradeRoute = {
path: Path;
splitPercent: number;
amountIn: BN;
amountOut: BN;
hopQuotes: TradeHop[];
};
/**
* Represents a quote for a single hop in the path of a {@link SubTradeRoute}.
*
* @category Router
* @param amountIn The amount of token being traded in for this hop.
* @param amountOut The amount of token being traded out for this hop.
* @param whirlpool The address of the whirlpool that this hop is trading through.
* @param inputMint The address of the input token mint.
* @param outputMint The address of the output token mint.
* @param mintA The address of the first mint in the pool.
* @param mintB The address of the second mint in the pool.
* @param vaultA The address of the first vault in the pool.
* @param vaultB The address of the second vault in the pool.
* @param quote The {@link SwapQuote} for this hop.
* @param snapshot A snapshot of the whirlpool condition when this hop was made
*/
export type TradeHop = {
amountIn: BN;
amountOut: BN;
whirlpool: Address;
inputMint: Address;
outputMint: Address;
mintA: Address;
mintB: Address;
vaultA: Address;
vaultB: Address;
quote: SwapQuote;
snapshot: TradeHopSnapshot;
};
/**
* A snapshot of the whirlpool condition when a trade hop was made.
* @category Router
*/
export type TradeHopSnapshot = {
aToB: boolean;
sqrtPrice: BN;
feeRate: Percentage;
};
/**
* A trade route that is ready to execute.
* Contains the {@link TradeRoute} and a possible set of {@link AddressLookupTableAccount} that
* is needed to successfully execute the trade.
*
* If the lookup table accounts are undefined, then the trade can be executed with a legacy transaction.
*
* @category Router
*/
export type ExecutableRoute = readonly [TradeRoute, AddressLookupTableAccount[] | undefined];
/**
* Convienience class to find routes through a set of Whirlpools and execute a swap across them.
* The router only supports up to 2-hop trades between pools and does not support arbitrage trades
* between the same token.
*
* @category Router
*/
export interface WhirlpoolRouter {
/**
* Finds all possible routes for a trade, ordered by the best other token amount you would get from a trade.
* Use {@link RouterUtils.selectFirstExecutableRoute} to find the best executable route.
*
* @param trade
* The trade to find routes for.
* @param opts an {@link WhirlpoolAccountFetchOptions} object to define fetch and cache options when accessing on-chain accounts
* @param opts
* {@link RoutingOptions} to configure the router. Missing options will be filled with default values from
* {@link RouterUtils.getDefaultRoutingOptions}.
* @param fetchOpts
* {@link WhirlpoolAccountFetchOptions} to configure the fetching of on-chain data.
* @return A list of {@link TradeRoute} that can be used to execute a swap, ordered by the best other token amount.
*/
findAllRoutes(
trade: Trade,
opts?: Partial<RoutingOptions>,
fetchOpts?: WhirlpoolAccountFetchOptions
): Promise<TradeRoute[]>;
/**
* Finds all possible routes for a trade and select the best route that is executable
* under the current execution environment.
* @param trade
* The trade to find routes for.
* @param opts an {@link WhirlpoolAccountFetchOptions} object to define fetch and cache options when accessing on-chain accounts
* @param opts
* {@link RoutingOptions} to configure the router. Missing options will be filled with default values from
* {@link RouterUtils.getDefaultRoutingOptions}.
* @param selectionOpts
* {@link RouteSelectOptions} to configure the selection of the best route. Missing options
* will be filled with default values from {@link RouterUtils.getDefaultRouteSelectOptions}.
* @param fetchOpts
* {@link WhirlpoolAccountFetchOptions} to configure the fetching of on-chain data.
* @returns
* The best {@link ExecutableRoute} that can be used to execute a swap. If no executable route is found, null is returned.
*/
findBestRoute(
trade: Trade,
opts?: Partial<RoutingOptions>,
selectionOpts?: Partial<RouteSelectOptions>,
fetchOpts?: WhirlpoolAccountFetchOptions
): Promise<ExecutableRoute | null>;
/**
* Construct a {@link TransactionBuilder} to help execute a trade route.
* @param trade The trade route to execute.
* @param slippage The slippage tolerance for the trade.
* @param resolvedAtas
* The ATA accounts that the executing wallet owns / needed by the execution.
* If not provided, the router will attempt to resolve them.
* @returns
* A {@link TransactionBuilder}that can be used to execute the trade.
* If provvided from {@link ExecutableRoute}, plug the {@link AddressLookupTableAccount}s
* into builder to lower the transaction size.
*/
swap(
trade: TradeRoute,
slippage: Percentage,
resolvedAtas: AtaAccountInfo[] | null
): Promise<TransactionBuilder>;
}