Skip to content

Commit 7692e0b

Browse files
committed
feat: add --slippage param to spot swap (closes #5)
1 parent 5632bf0 commit 7692e0b

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

docs/spot.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ jup spot quote --from <mint> --to <mint> --raw-amount 1000000000
5454
```bash
5555
jup spot swap --from SOL --to USDC --amount 1
5656
jup spot swap --from SOL --to USDC --amount 1 --key mykey
57+
jup spot swap --from SOL --to USDC --amount 1 --slippage 50
5758
```
5859

5960
- `--key` overrides the active key for this transaction
61+
- `--slippage` sets max slippage in basis points (e.g. `50` = 0.5%). Recommended to be emtpy: Jupiter's Real-Time Slippage Estimation (RTSE) automatically picks an optimal value.
6062

6163
```js
6264
// Example JSON response:

src/clients/UltraClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type GetOrderRequest = {
88
outputMint: string;
99
amount: string;
1010
taker?: string | undefined;
11+
slippageBps?: string | undefined;
1112
};
1213

1314
export type GetOrderResponse = {

src/commands/SpotCommand.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export class SpotCommand {
5656
"Amount in on-chain units (no decimal conversion)"
5757
)
5858
.option("--key <name>", "Key to use for signing")
59+
.option(
60+
"--slippage <bps>",
61+
"Max slippage in basis points (leave empty to use Jupiter's Real-Time Slippage Estimation)"
62+
)
5963
.action((opts) => this.swap(opts));
6064
spot
6165
.command("portfolio")
@@ -228,9 +232,17 @@ export class SpotCommand {
228232
amount?: string;
229233
rawAmount?: string;
230234
key?: string;
235+
slippage?: string;
231236
}): Promise<void> {
232237
Swap.validateAmountOpts(opts);
233238

239+
if (opts.slippage !== undefined) {
240+
const bps = Number(opts.slippage);
241+
if (!Number.isInteger(bps) || bps < 0) {
242+
throw new Error("--slippage must be a non-negative integer (bps).");
243+
}
244+
}
245+
234246
const settings = Config.load();
235247
const signer = await Signer.load(opts.key ?? settings.activeKey);
236248
const [inputToken, outputToken] = await Promise.all([
@@ -244,6 +256,7 @@ export class SpotCommand {
244256
outputToken,
245257
amount: opts.amount,
246258
rawAmount: opts.rawAmount,
259+
slippageBps: opts.slippage,
247260
});
248261

249262
const networkFee = NumberConverter.fromChainAmount(

src/lib/Swap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export class Swap {
3939
outputToken: Token;
4040
amount?: string;
4141
rawAmount?: string;
42+
slippageBps?: string;
4243
}): Promise<SwapResult> {
4344
const { signer, inputToken, outputToken } = opts;
4445
const inputMultiplier = this.getScaledUiMultiplier(inputToken);
@@ -55,6 +56,7 @@ export class Swap {
5556
inputMultiplier
5657
),
5758
taker: signer.address,
59+
slippageBps: opts.slippageBps,
5860
});
5961

6062
if (order.error) {

0 commit comments

Comments
 (0)