This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add utils fn decodeFullAppData * feat: add getUiOrderType - different than CoW Swap's
- Loading branch information
1 parent
637155e
commit 6d3eb8c
Showing
4 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,28 @@ | ||
import { AnyAppDataDocVersion } from '@cowprotocol/app-data' | ||
|
||
/** | ||
* Decode appData from a string to a AnyAppDataDocVersion instance | ||
* Keep in mind it can be a valid JSON but not necessarily a valid AppDataDoc | ||
* | ||
* Returns undefined if the given appData is not a valid JSON | ||
* When `throwOnError` is true, it will throw an error if the given appData is not a valid JSON | ||
*/ | ||
export function decodeFullAppData( | ||
appData: string | null | undefined, | ||
throwOnError?: true, | ||
): AnyAppDataDocVersion | undefined { | ||
if (!appData) { | ||
return undefined | ||
} | ||
|
||
try { | ||
return JSON.parse(appData) | ||
} catch (e) { | ||
if (throwOnError) { | ||
throw e | ||
} | ||
|
||
console.info('[decodeFullAppData] given appData is not a valid JSON', appData) | ||
return undefined | ||
} | ||
} |
This file contains 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,43 @@ | ||
import { latest } from '@cowprotocol/app-data' | ||
import { OrderClass } from '@cowprotocol/cow-sdk' | ||
import { Order } from 'api/operator' | ||
import { decodeFullAppData } from 'utils/decodeFullAppData' | ||
|
||
/** | ||
* UiOrderType based on appData, falling back to backend order class. | ||
* | ||
* Similar to CoW Swap, but not exactly like it. | ||
* | ||
* Here, MARKET remains as MARKET, while on CoW Swap it's translated to SWAP. | ||
* Also, we keep the LIQUIDITY order type as it, while there it's translated to LIMIT. | ||
* | ||
* In summary, it matches 1:1 appData.metadata.orderClass.orderClass enum | ||
*/ | ||
export enum UiOrderType { | ||
MARKET = 'MARKET', | ||
LIMIT = 'LIMIT', | ||
LIQUIDITY = 'LIQUIDITY', | ||
TWAP = 'TWAP', | ||
} | ||
|
||
const API_ORDER_CLASS_TO_UI_ORDER_TYPE_MAP: Record<OrderClass, UiOrderType> = { | ||
[OrderClass.MARKET]: UiOrderType.MARKET, | ||
[OrderClass.LIMIT]: UiOrderType.LIMIT, | ||
[OrderClass.LIQUIDITY]: UiOrderType.LIQUIDITY, | ||
} | ||
|
||
export function getUiOrderType({ fullAppData, class: orderClass }: Order): UiOrderType { | ||
const appData = decodeFullAppData(fullAppData) | ||
|
||
const appDataOrderClass = appData?.metadata?.orderClass as latest.OrderClass | undefined | ||
const typeFromAppData = UiOrderType[appDataOrderClass?.orderClass.toUpperCase() || ''] | ||
|
||
// 1. AppData info has priority as it's what's more precise | ||
if (typeFromAppData) { | ||
return typeFromAppData | ||
} | ||
|
||
// 3. Fallback to API classification. | ||
// Least precise as it doesn't distinguish twap type and uses backend logic which doesn't match frontend's classification | ||
return API_ORDER_CLASS_TO_UI_ORDER_TYPE_MAP[orderClass] | ||
} |