-
Notifications
You must be signed in to change notification settings - Fork 113
Feat/delta slippage param #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xNazarii
wants to merge
20
commits into
master
Choose a base branch
from
feat/deltaSlippageParam
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c2dc249
add `slippage` param for delta build/submit methods
0xNazarii 6607730
remove SourceEth error case
0xNazarii b4532fc
remove bigint literals
0xNazarii be1a8d8
remove SourceEth error case
0xNazarii ee41a1d
add `side` to discrimination types
0xNazarii ec785b9
lint
0xNazarii 8b1c630
pass api from env
0xNazarii 7caf94f
fix tests
0xNazarii bd9a3fb
temporarily skip `/swap` tests
0xNazarii 168b62c
Revert "temporarily skip `/swap` tests"
0xNazarii 5ae4415
use strict compare to narrow types
0xNazarii 12d018c
validate slippage param
0xNazarii 03e7871
strict check
0xNazarii 313ab85
ignore build artifacts
0xNazarii 2d5e3fc
remove tests for limit orders and nft orders
0xNazarii 8f66540
`applySlippage` to take input object as param
0xNazarii 0ba6036
Revert "ignore build artifacts"
0xNazarii d80d459
add `outDir` compiler option
0xNazarii 90755d1
Revert "add `outDir` compiler option"
0xNazarii fc8f6ec
use `assert` for input validation
0xNazarii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ export type { SignableDeltaOrderData } from './helpers/buildDeltaOrderData'; | |
|
|
||
| export type SwapSideUnion = EnumerateLiteral<typeof SwapSide>; | ||
|
|
||
| export type BuildDeltaOrderDataParams = { | ||
| type BuildDeltaOrderDataParamsBase = { | ||
| /** @description The address of the order owner */ | ||
| owner: string; | ||
| /** @description The address of the order beneficiary */ | ||
|
|
@@ -28,10 +28,6 @@ export type BuildDeltaOrderDataParams = { | |
| srcToken: string; // lowercase | ||
| /** @description The address of the dest token. For Crosschain Order - destination token on the destination chain */ | ||
| destToken: string; // lowercase | ||
| /** @description The amount of src token to swap */ | ||
| srcAmount: string; // wei | ||
| /** @description The minimum amount of dest token to receive */ | ||
| destAmount: string; // wei, deltaPrice.destAmount - slippage | ||
| /** @description The deadline for the order */ | ||
| deadline?: number; // seconds | ||
| /** @description The nonce of the order */ | ||
|
|
@@ -68,12 +64,44 @@ export type BuildDeltaOrderDataParams = { | |
| /** @description A boolean indicating whether the surplus should be capped. True by default */ | ||
| capSurplus?: boolean; | ||
|
|
||
| /** @description The side of the order. Default is SELL */ | ||
| side?: SwapSideUnion; | ||
| /** @description Metadata for the order, hex string */ | ||
| metadata?: string; | ||
| }; | ||
|
|
||
| /** @description SELL with slippage: srcAmount provided, destAmount auto-computed from deltaPrice.destAmount */ | ||
| type DeltaAmountsSellSlippage = { | ||
| /** @description Slippage in basis points (bps). 10000 = 100%, 50 = 0.5% */ | ||
| slippage: number; | ||
| /** @description The amount of src token to swap */ | ||
| srcAmount: string; | ||
| destAmount?: never; | ||
| /** @description The side of the order */ | ||
| side?: 'SELL'; | ||
| }; | ||
| /** @description BUY with slippage: destAmount provided, srcAmount auto-computed from deltaPrice.srcAmount */ | ||
| type DeltaAmountsBuySlippage = { | ||
| /** @description Slippage in basis points (bps). 10000 = 100%, 50 = 0.5% */ | ||
| slippage: number; | ||
| /** @description The minimum amount of dest token to receive */ | ||
| destAmount: string; | ||
| srcAmount?: never; | ||
| /** @description The side of the order */ | ||
| side?: 'BUY'; | ||
| }; | ||
| /** @description Explicit amounts, no slippage (backward-compatible) */ | ||
| type DeltaAmountsExplicit = { | ||
| slippage?: never; | ||
| /** @description The amount of src token to swap */ | ||
| srcAmount: string; | ||
| /** @description The minimum amount of dest token to receive */ | ||
| destAmount: string; | ||
| /** @description The side of the order. Default is SELL */ | ||
| side?: SwapSideUnion; | ||
| }; | ||
|
|
||
| export type BuildDeltaOrderDataParams = BuildDeltaOrderDataParamsBase & | ||
| (DeltaAmountsSellSlippage | DeltaAmountsBuySlippage | DeltaAmountsExplicit); | ||
|
|
||
| type BuildDeltaOrder = ( | ||
| buildOrderParams: BuildDeltaOrderDataParams, | ||
| requestParams?: RequestParameters | ||
|
|
@@ -138,7 +166,40 @@ export const constructBuildDeltaOrder = ( | |
| partnerFeeBps = partnerFeeBps ?? 0; | ||
| partnerTakesSurplus = partnerTakesSurplus ?? false; | ||
|
|
||
| const swapSide = options.side ?? SwapSide.SELL; | ||
| // Resolve srcAmount, destAmount and side. | ||
| // When slippage is used, side is inferred from which amount is present. | ||
| let srcAmount: string; | ||
| let destAmount: string; | ||
|
|
||
| const swapSide: SwapSideUnion = | ||
| options.slippage !== undefined | ||
| ? options.srcAmount | ||
| ? SwapSide.SELL | ||
| : SwapSide.BUY | ||
| : options.side ?? SwapSide.SELL; | ||
|
|
||
| if (options.slippage !== undefined) { | ||
| if (options.srcAmount !== undefined) { | ||
| // SELL with slippage: destAmount auto-computed | ||
| srcAmount = options.srcAmount; | ||
| destAmount = applySlippage( | ||
| options.deltaPrice.destAmount, | ||
| options.slippage, | ||
| false | ||
| ); | ||
| } else { | ||
| // BUY with slippage: srcAmount auto-computed | ||
| destAmount = options.destAmount; | ||
| srcAmount = applySlippage( | ||
| options.deltaPrice.srcAmount, | ||
| options.slippage, | ||
| true | ||
| ); | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| srcAmount = options.srcAmount; | ||
| destAmount = options.destAmount; | ||
| } | ||
|
|
||
| const expectedAmount = | ||
| swapSide === SwapSide.SELL | ||
|
|
@@ -152,8 +213,8 @@ export const constructBuildDeltaOrder = ( | |
| // for some cases of WETH->ETH crosschain swaps, the destToken is changed to WETH or ETH, | ||
| // this is already reflected in deltaPrice | ||
| destToken: options.deltaPrice.destToken, | ||
| srcAmount: options.srcAmount, | ||
| destAmount: options.destAmount, | ||
| srcAmount, | ||
| destAmount, | ||
| expectedAmount, | ||
| deadline: options.deadline, | ||
| nonce: options.nonce?.toString(10), | ||
|
|
@@ -179,3 +240,25 @@ export const constructBuildDeltaOrder = ( | |
| buildDeltaOrder, | ||
| }; | ||
| }; | ||
|
|
||
| function applySlippage( | ||
| amount: string, | ||
| slippageBps: number, | ||
| increase: boolean | ||
| ): string { | ||
|
||
| if ( | ||
| !Number.isInteger(slippageBps) || | ||
| slippageBps < 0 || | ||
| slippageBps >= 10_000 | ||
| ) { | ||
| throw new Error('slippageBps must be an integer between 0 and 10_000'); | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const BPS_BASE = 10_000n; | ||
| const amt = BigInt(amount); | ||
| const bps = BigInt(slippageBps); | ||
|
|
||
| return increase | ||
| ? ((amt * (BPS_BASE + bps)) / BPS_BASE).toString(10) | ||
| : ((amt * (BPS_BASE - bps)) / BPS_BASE).toString(10); | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.