Skip to content

Commit

Permalink
withMinimumReceive()
Browse files Browse the repository at this point in the history
  • Loading branch information
Sluder committed Feb 28, 2024
1 parent 252e8df commit 6d7cbe0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
22 changes: 18 additions & 4 deletions docs/requests/swap-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,13 @@ dexter.newSwapRequest()
<br>

<details>
<summary><code>flip(): SwapRequest</code> Flip your swap in and swap out token.</summary>

Flipping will only affect the swap in & swap out token if the swap in token was set beforehand.
<summary><code>withMinimumReceive(bigint): SwapRequest</code> Set the minimum you want to receive.</summary>

##### Using

```js
dexter.newSwapRequest()
.flip()
.withMinimumReceive(10_000000n)
...
```
</details>
Expand All @@ -113,6 +111,22 @@ dexter.newSwapRequest()

<br>

<details>
<summary><code>flip(): SwapRequest</code> Flip your swap in and swap out token.</summary>

Flipping will only affect the swap in & swap out token if the swap in token was set beforehand.

##### Using

```js
dexter.newSwapRequest()
.flip()
...
```
</details>

<br>

<details>
<summary><code>getEstimatedReceive(LiquidityPool?): bigint</code> Get the <i>estimated</i> receive for your swap.</summary>

Expand Down
19 changes: 19 additions & 0 deletions src/requests/swap-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ export class SwapRequest {
return this;
}

public withMinimumReceive(minReceive: bigint): SwapRequest {
if (minReceive <= 0n) {
this._swapInAmount = 0n;
}
if (! this._liquidityPool) {
throw new Error('Liquidity pool must be set before setting a swap out amount.');
}

this._swapInAmount = this._dexter.availableDexs[this._liquidityPool.dex].estimatedGive(
this._liquidityPool,
this._swapOutToken,
BigInt(
Math.ceil(Number(minReceive) * (1 + (this._slippagePercent / 100)))
),
);

return this;
}

public withSlippagePercent(slippagePercent: number): SwapRequest {
if (slippagePercent < 0) {
throw new Error('Slippage percent must be zero or above.');
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "tests", "build"],
}
}

0 comments on commit 6d7cbe0

Please sign in to comment.