-
Notifications
You must be signed in to change notification settings - Fork 114
Feat/twap #231
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
Merged
Merged
Feat/twap #231
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ebd55bb
twap types and some global exports
andriy-shymkiv 109eee6
export more types
andriy-shymkiv a609486
TWAP core
andriy-shymkiv 373bb4e
PostTWAPDeltaOrderParams/degenMode + prettify
andriy-shymkiv e01cd72
export more twap types
andriy-shymkiv f3fcdd1
export DeltaOrderToPost from index
andriy-shymkiv 1092d27
buildTWAPDeltaOrder/maxSrcAmount for BUY slippage
andriy-shymkiv f09df90
constructPostTWAPDeltaOrder/consistend posting
andriy-shymkiv 872b5fa
Release 9.4.1-dev.1
andriy-shymkiv 3bdbcf4
constructBuildTWAPDeltaOrder/ nullify bridge.protocolData by default
andriy-shymkiv 5ea2eb1
add comment
andriy-shymkiv 389e3d4
Release 9.4.1-dev.2
andriy-shymkiv bc559db
twap/clarifying comments
andriy-shymkiv ed86d9a
constructSubmitTWAPDeltaOrder/degenMode
andriy-shymkiv aa6ac67
rename types
andriy-shymkiv 2de995d
merge with master
andriy-shymkiv b711a70
buildTWAPSignableOrderData/default deadline enough for all slices to …
andriy-shymkiv 36a6a86
constructDeltaTokenModule/cancelTWAPAndWithdraw & cancelTWAPBuyAndWit…
andriy-shymkiv 667cb80
delta/fix some tests
andriy-shymkiv 320c2ba
Release 9.4.3-dev.1
andriy-shymkiv bf20619
constructDeltaTokenModule/sanitizeOrderData
andriy-shymkiv 95e4c07
get rid of ethersV5 dependency
andriy-shymkiv dc32f22
Release 9.5.0
andriy-shymkiv 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import type { ConstructFetchInput, RequestParameters } from '../../types'; | ||
| import { constructGetDeltaContract } from './getDeltaContract'; | ||
| import type { BridgePrice } from './getDeltaPrice'; | ||
| import { constructGetPartnerFee } from './getPartnerFee'; | ||
| import { | ||
| buildTWAPSignableOrderData, | ||
| type BuildTWAPOrderDataInput, | ||
| type SignableTWAPOrderData, | ||
| } from './helpers/buildTWAPOrderData'; | ||
| import { applySlippage, resolvePartnerFee } from './helpers/misc'; | ||
| import type { MarkOptional } from 'ts-essentials'; | ||
| export type { SignableTWAPOrderData } from './helpers/buildTWAPOrderData'; | ||
|
|
||
| type BuildTWAPDeltaOrderParamsBase = { | ||
| /** @description The address of the order owner */ | ||
| owner: string; | ||
| /** @description The address of the order beneficiary */ | ||
| beneficiary?: string; | ||
| /** @description The address of the src token */ | ||
| srcToken: string; | ||
| /** @description The address of the dest token. For Crosschain Order - destination token on the destination chain */ | ||
| destToken: string; | ||
| /** @description The deadline for the order */ | ||
| deadline?: number; | ||
| /** @description The nonce of the order */ | ||
| nonce?: number | string; | ||
| /** @description Optional permit signature for the src token */ | ||
| permit?: string; | ||
| /** @description Partner string */ | ||
| partner?: string; | ||
| /** @description Destination Chain ID for Crosschain Orders */ | ||
| destChainId?: number; | ||
| /** @description Seconds between slice executions (min 60) */ | ||
| interval: number; | ||
| /** @description Number of slices (min 2) */ | ||
| numSlices: number; | ||
| /** @description Slippage in basis points (bps). 10000 = 100%, 50 = 0.5% */ | ||
| slippage?: number; | ||
|
|
||
| /** @description price response received from /delta/prices (getDeltaPrice method) for a single slice */ | ||
| deltaPrice: MarkOptional< | ||
| Pick< | ||
| BridgePrice, | ||
| | 'destAmount' | ||
| | 'partner' | ||
| | 'partnerFee' | ||
| | 'destToken' | ||
| | 'srcAmount' | ||
| | 'bridge' | ||
| >, | ||
| 'partner' | 'partnerFee' | ||
| >; | ||
|
|
||
| /** @description partner fee in basis points (bps), 50bps=0.5% */ | ||
| partnerFeeBps?: number; | ||
| /** @description partner address */ | ||
| partnerAddress?: string; | ||
| /** @description take surplus */ | ||
| partnerTakesSurplus?: boolean; | ||
| /** @description A boolean indicating whether the surplus should be capped. True by default */ | ||
| capSurplus?: boolean; | ||
| /** @description Metadata for the order, hex string */ | ||
| metadata?: string; | ||
| }; | ||
|
|
||
| export type BuildTWAPSellOrderParams = BuildTWAPDeltaOrderParamsBase & { | ||
| /** @description Must be "TWAPOrder" for sell orders */ | ||
| onChainOrderType: 'TWAPOrder'; | ||
| /** @description Total source token amount across all slices */ | ||
| totalSrcAmount: string; | ||
| }; | ||
|
|
||
| export type BuildTWAPBuyOrderParams = BuildTWAPDeltaOrderParamsBase & { | ||
| /** @description Must be "TWAPBuyOrder" for buy orders */ | ||
| onChainOrderType: 'TWAPBuyOrder'; | ||
| /** @description Total destination token amount to buy across all slices */ | ||
| totalDestAmount: string; | ||
| /** @description Maximum source token amount willing to spend */ | ||
| maxSrcAmount: string; | ||
| }; | ||
|
|
||
| export type BuildTWAPDeltaOrderParams = | ||
| | BuildTWAPSellOrderParams | ||
| | BuildTWAPBuyOrderParams; | ||
|
|
||
| type BuildTWAPDeltaOrder = ( | ||
| buildOrderParams: BuildTWAPDeltaOrderParams, | ||
| requestParams?: RequestParameters | ||
| ) => Promise<SignableTWAPOrderData>; | ||
|
|
||
| export type BuildTWAPDeltaOrderFunctions = { | ||
| /** @description Build TWAP Orders (sell or buy) to be posted to Delta API for execution */ | ||
| buildTWAPDeltaOrder: BuildTWAPDeltaOrder; | ||
| }; | ||
|
|
||
| export const constructBuildTWAPDeltaOrder = ( | ||
| options: ConstructFetchInput | ||
| ): BuildTWAPDeltaOrderFunctions => { | ||
| const { chainId } = options; | ||
|
|
||
| const { getDeltaContract } = constructGetDeltaContract(options); | ||
| const { getPartnerFee } = constructGetPartnerFee(options); | ||
|
|
||
| const buildTWAPDeltaOrder: BuildTWAPDeltaOrder = async ( | ||
| params, | ||
| requestParams | ||
| ) => { | ||
| const ParaswapDelta = await getDeltaContract(requestParams); | ||
| if (!ParaswapDelta) { | ||
| throw new Error(`Delta is not available on chain ${chainId}`); | ||
| } | ||
|
|
||
| const { partnerAddress, partnerFeeBps, partnerTakesSurplus } = | ||
| await resolvePartnerFee(params, getPartnerFee, requestParams); | ||
|
|
||
| const commonInput = { | ||
| owner: params.owner, | ||
| beneficiary: params.beneficiary, | ||
| srcToken: params.srcToken, | ||
| destToken: params.deltaPrice.destToken, | ||
| deadline: params.deadline, | ||
| nonce: params.nonce?.toString(10), | ||
| permit: params.permit, | ||
| metadata: params.metadata, | ||
| interval: params.interval, | ||
| numSlices: params.numSlices, | ||
|
andriy-shymkiv marked this conversation as resolved.
|
||
| bridge: params.deltaPrice.bridge, | ||
|
|
||
| chainId, | ||
| paraswapDeltaAddress: ParaswapDelta, | ||
| partnerAddress, | ||
| partnerTakesSurplus, | ||
| partnerFeeBps, | ||
| capSurplus: params.capSurplus, | ||
| }; | ||
|
|
||
| let input: BuildTWAPOrderDataInput; | ||
|
|
||
| if (params.onChainOrderType === 'TWAPOrder') { | ||
| const slippage = params.slippage ?? 0; | ||
| const destAmountPerSlice = | ||
| slippage > 0 | ||
| ? applySlippage({ | ||
| amount: params.deltaPrice.destAmount, | ||
| slippageBps: slippage, | ||
| increase: false, | ||
| }) | ||
| : params.deltaPrice.destAmount; | ||
|
|
||
| input = { | ||
| ...commonInput, | ||
| onChainOrderType: 'TWAPOrder', | ||
| destAmountPerSlice, | ||
| totalSrcAmount: params.totalSrcAmount, | ||
| }; | ||
| } else { | ||
| const slippage = params.slippage ?? 0; | ||
| const maxSrcAmount = | ||
| slippage > 0 | ||
| ? applySlippage({ | ||
| amount: params.deltaPrice.srcAmount, | ||
| slippageBps: slippage, | ||
| increase: true, | ||
| }) | ||
| : params.maxSrcAmount; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| input = { | ||
| ...commonInput, | ||
| onChainOrderType: 'TWAPBuyOrder', | ||
| totalDestAmount: params.totalDestAmount, | ||
| maxSrcAmount, | ||
| }; | ||
| } | ||
|
|
||
| return buildTWAPSignableOrderData(input); | ||
| }; | ||
|
|
||
| return { | ||
| buildTWAPDeltaOrder, | ||
| }; | ||
| }; | ||
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.