-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(twap): calculate and display TWAP orders execution info (#2764)
- Loading branch information
Showing
19 changed files
with
255 additions
and
103 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
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,73 @@ | ||
import { useAtomValue } from 'jotai' | ||
import { useMemo } from 'react' | ||
|
||
import { Order, OrderInfoApi } from 'legacy/state/orders/actions' | ||
import { useOrdersById } from 'legacy/state/orders/hooks' | ||
|
||
import { useWalletInfo } from 'modules/wallet' | ||
|
||
import { DEFAULT_TWAP_EXECUTION_INFO } from '../const' | ||
import { twapPartOrdersAtom } from '../state/twapPartOrdersAtom' | ||
import { TwapOrderExecutionInfo } from '../types' | ||
|
||
export type TwapOrdersExecutionMap = { [id: string]: TwapOrderExecutionInfo } | ||
|
||
type PartsIdsInfo = { | ||
sets: { [id: string]: Set<string> } | ||
ids: string[] | ||
} | ||
|
||
export function useTwapOrdersExecutions(ids: string[]): TwapOrdersExecutionMap { | ||
const { chainId } = useWalletInfo() | ||
const twapPartOrders = useAtomValue(twapPartOrdersAtom) | ||
|
||
const { sets: partSets, ids: allPartIds } = useMemo(() => { | ||
return Object.keys(twapPartOrders).reduce<PartsIdsInfo>( | ||
(acc, val) => { | ||
const { sets, ids } = acc | ||
|
||
const partsIds = (twapPartOrders[val] || []).map((item) => item.uid) | ||
|
||
sets[val] = new Set<string>(partsIds) | ||
ids.push(...partsIds) | ||
|
||
return acc | ||
}, | ||
{ sets: {}, ids: [] } | ||
) | ||
}, [twapPartOrders]) | ||
|
||
const allPartOrdersMap = useOrdersById({ chainId, ids: allPartIds }) | ||
|
||
const allPartOrders = useMemo(() => { | ||
if (!allPartOrdersMap) return [] | ||
|
||
return Object.values(allPartOrdersMap) | ||
}, [allPartOrdersMap]) | ||
|
||
return useMemo(() => { | ||
return ids.reduce<TwapOrdersExecutionMap>((acc, id) => { | ||
const childrenIds = partSets[id] | ||
|
||
if (childrenIds?.size > 0) { | ||
const children = allPartOrders.filter((order) => childrenIds.has(order.id)) | ||
|
||
const executedBuyAmount = sumChildrenAmount(children, 'executedBuyAmount').toString() | ||
const executedSellAmount = sumChildrenAmount(children, 'executedSellAmount').toString() | ||
const executedFeeAmount = sumChildrenAmount(children, 'executedFeeAmount').toString() | ||
|
||
acc[id] = { executedSellAmount, executedFeeAmount, executedBuyAmount } | ||
} else { | ||
acc[id] = DEFAULT_TWAP_EXECUTION_INFO | ||
} | ||
|
||
return acc | ||
}, {}) | ||
}, [ids, partSets, allPartOrders]) | ||
} | ||
|
||
function sumChildrenAmount(children: Order[], key: keyof OrderInfoApi): BigInt { | ||
return children.reduce((acc, order) => { | ||
return acc + BigInt((order.apiAdditionalInfo?.[key] || '0') as string) | ||
}, BigInt(0)) | ||
} |
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,45 @@ | ||
import { atom } from 'jotai' | ||
|
||
import { OrderStatus } from 'legacy/state/orders/actions' | ||
|
||
import { tokensByAddressAtom } from 'modules/tokensList/state/tokensListAtom' | ||
import { walletInfoAtom } from 'modules/wallet/api/state' | ||
|
||
import { OrderWithComposableCowInfo } from 'common/types' | ||
|
||
import { twapOrdersListAtom } from './twapOrdersListAtom' | ||
|
||
import { TwapOrderStatus } from '../types' | ||
import { emulateTwapAsOrder } from '../utils/emulateTwapAsOrder' | ||
|
||
const statusesMap: Record<TwapOrderStatus, OrderStatus> = { | ||
[TwapOrderStatus.Cancelled]: OrderStatus.CANCELLED, | ||
[TwapOrderStatus.Expired]: OrderStatus.EXPIRED, | ||
[TwapOrderStatus.Pending]: OrderStatus.PENDING, | ||
[TwapOrderStatus.Scheduled]: OrderStatus.SCHEDULED, | ||
[TwapOrderStatus.WaitSigning]: OrderStatus.PRESIGNATURE_PENDING, | ||
[TwapOrderStatus.Fulfilled]: OrderStatus.FULFILLED, | ||
} | ||
|
||
export const emulatedTwapOrdersAtom = atom((get) => { | ||
const { account, chainId } = get(walletInfoAtom) | ||
const tokens = get(tokensByAddressAtom) | ||
const orders = get(twapOrdersListAtom) | ||
const accountLowerCase = account?.toLowerCase() | ||
|
||
if (!accountLowerCase) return [] | ||
|
||
const orderWithComposableCowInfo: OrderWithComposableCowInfo[] = orders | ||
.filter((order) => order.chainId === chainId && order.safeAddress.toLowerCase() === accountLowerCase) | ||
.map((order) => { | ||
return { | ||
order: emulateTwapAsOrder(tokens, order), | ||
composableCowInfo: { | ||
id: order.id, | ||
status: statusesMap[order.status], | ||
}, | ||
} | ||
}) | ||
|
||
return orderWithComposableCowInfo | ||
}) |
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 |
---|---|---|
@@ -1,64 +1,42 @@ | ||
import { atom } from 'jotai' | ||
import { atomWithStorage } from 'jotai/utils' | ||
|
||
import { OrderStatus } from 'legacy/state/orders/actions' | ||
|
||
import { tokensByAddressAtom } from 'modules/tokensList/state/tokensListAtom' | ||
import { walletInfoAtom } from 'modules/wallet/api/state' | ||
|
||
import { OrderWithComposableCowInfo } from 'common/types' | ||
import { deepEqual } from 'utils/deepEqual' | ||
|
||
import { TwapOrderItem, TwapOrderStatus } from '../types' | ||
import { emulateTwapAsOrder } from '../utils/emulateTwapAsOrder' | ||
import { TwapOrderItem } from '../types' | ||
import { updateTwapOrdersList } from '../utils/updateTwapOrdersList' | ||
|
||
export type TwapOrdersList = { [key: string]: TwapOrderItem } | ||
|
||
export const twapOrdersListAtom = atomWithStorage<TwapOrdersList>('twap-orders-list:v4', {}) | ||
export const twapOrdersAtom = atomWithStorage<TwapOrdersList>('twap-orders-list:v5', {}) | ||
|
||
export const twapOrdersListAtom = atom<TwapOrderItem[]>((get) => { | ||
const { account, chainId } = get(walletInfoAtom) | ||
|
||
if (!account || !chainId) return [] | ||
|
||
const accountLowerCase = account.toLowerCase() | ||
|
||
const orders = Object.values(get(twapOrdersAtom)) | ||
|
||
return orders | ||
.flat() | ||
.filter((order) => order.safeAddress.toLowerCase() === accountLowerCase && order.chainId === chainId) | ||
}) | ||
|
||
export const updateTwapOrdersListAtom = atom(null, (get, set, nextState: TwapOrdersList) => { | ||
const currentState = get(twapOrdersListAtom) | ||
const currentState = get(twapOrdersAtom) | ||
const newState = updateTwapOrdersList(currentState, nextState) | ||
|
||
if (!deepEqual(currentState, newState)) { | ||
set(twapOrdersListAtom, newState) | ||
set(twapOrdersAtom, newState) | ||
} | ||
}) | ||
|
||
export const addTwapOrderToListAtom = atom(null, (get, set, order: TwapOrderItem) => { | ||
const currentState = get(twapOrdersListAtom) | ||
|
||
set(twapOrdersListAtom, { ...currentState, [order.id]: order }) | ||
}) | ||
|
||
const statusesMap: Record<TwapOrderStatus, OrderStatus> = { | ||
[TwapOrderStatus.Cancelled]: OrderStatus.CANCELLED, | ||
[TwapOrderStatus.Expired]: OrderStatus.EXPIRED, | ||
[TwapOrderStatus.Pending]: OrderStatus.PENDING, | ||
[TwapOrderStatus.Scheduled]: OrderStatus.SCHEDULED, | ||
[TwapOrderStatus.WaitSigning]: OrderStatus.PRESIGNATURE_PENDING, | ||
} | ||
|
||
export const emulatedTwapOrdersAtom = atom((get) => { | ||
const { account, chainId } = get(walletInfoAtom) | ||
const tokens = get(tokensByAddressAtom) | ||
const orders = Object.values(get(twapOrdersListAtom)) | ||
const accountLowerCase = account?.toLowerCase() | ||
|
||
if (!accountLowerCase) return [] | ||
|
||
const orderWithComposableCowInfo: OrderWithComposableCowInfo[] = orders | ||
.filter((order) => order.chainId === chainId && order.safeAddress.toLowerCase() === accountLowerCase) | ||
.map((order) => { | ||
return { | ||
order: emulateTwapAsOrder(tokens, order), | ||
composableCowInfo: { | ||
id: order.id, | ||
status: statusesMap[order.status], | ||
}, | ||
} | ||
}) | ||
const currentState = get(twapOrdersAtom) | ||
|
||
return orderWithComposableCowInfo | ||
set(twapOrdersAtom, { ...currentState, [order.id]: order }) | ||
}) |
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
Oops, something went wrong.