-
Notifications
You must be signed in to change notification settings - Fork 89
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
[2] feat(twap): generate all part orders #2759
Changes from all commits
55f8487
0524638
182fc81
36fc2a3
b5af02b
ecf5c77
346e799
6579727
a737827
3f9c0a7
aa5b613
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { useAtomValue, useUpdateAtom } from 'jotai/utils' | ||
import { useEffect } from 'react' | ||
|
||
import { Order } from '@cowprotocol/contracts' | ||
import { OrderParameters, SupportedChainId } from '@cowprotocol/cow-sdk' | ||
|
||
import { isTruthy } from 'legacy/utils/misc' | ||
|
||
import { useWalletInfo } from 'modules/wallet' | ||
|
||
import { computeOrderUid } from 'utils/orderUtils/computeOrderUid' | ||
|
||
import { twapOrdersListAtom } from '../state/twapOrdersListAtom' | ||
import { TwapPartOrderItem, twapPartOrdersAtom } from '../state/twapPartOrdersAtom' | ||
import { TwapOrderItem } from '../types' | ||
|
||
export function PartOrdersUpdater() { | ||
const { chainId, account } = useWalletInfo() | ||
const twapOrdersList = useAtomValue(twapOrdersListAtom) | ||
const updateTwapPartOrders = useUpdateAtom(twapPartOrdersAtom) | ||
|
||
useEffect(() => { | ||
if (!chainId || !account) return | ||
|
||
const accountLowerCase = account.toLowerCase() | ||
const twapOrders = Object.values(twapOrdersList) | ||
|
||
const ordersParts$ = twapOrders.map((twapOrder) => { | ||
alfetopito marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return generateTwapOrderParts(twapOrder, accountLowerCase, chainId) | ||
}) | ||
|
||
Promise.all(ordersParts$).then((ordersParts) => { | ||
const ordersMap = ordersParts.reduce((acc, item) => { | ||
return { | ||
...acc, | ||
...item, | ||
} | ||
}, {}) | ||
|
||
updateTwapPartOrders(ordersMap) | ||
}) | ||
}, [chainId, account, twapOrdersList, updateTwapPartOrders]) | ||
|
||
return null | ||
} | ||
|
||
async function generateTwapOrderParts( | ||
twapOrder: TwapOrderItem, | ||
safeAddress: string, | ||
chainId: SupportedChainId | ||
): Promise<{ [id: string]: TwapPartOrderItem[] }> { | ||
const twapOrderId = twapOrder.id | ||
|
||
const parts = [...new Array(twapOrder.order.n)] | ||
.map((_, index) => createPartOrderFromParent(twapOrder, index)) | ||
.filter(isTruthy) | ||
alfetopito marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const ids = await Promise.all(parts.map((part) => computeOrderUid(chainId, safeAddress, part as Order))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we really need to cast the part into an order? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately yes. Because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately yes. Because |
||
|
||
return { | ||
[twapOrderId]: ids.map<TwapPartOrderItem>((uid, index) => { | ||
return { | ||
uid, | ||
index, | ||
twapOrderId, | ||
chainId, | ||
safeAddress, | ||
order: parts[index], | ||
} | ||
}), | ||
} | ||
} | ||
|
||
function createPartOrderFromParent(twapOrder: TwapOrderItem, index: number): OrderParameters | null { | ||
const executionDate = twapOrder.safeTxParams?.executionDate | ||
|
||
if (!executionDate) { | ||
return null | ||
} | ||
|
||
const blockTimestamp = new Date(executionDate) | ||
|
||
return { | ||
sellToken: twapOrder.order.sellToken, | ||
buyToken: twapOrder.order.buyToken, | ||
receiver: twapOrder.order.receiver, | ||
sellAmount: twapOrder.order.partSellAmount, | ||
buyAmount: twapOrder.order.minPartLimit, | ||
validTo: calculateValidTo({ | ||
part: index, | ||
startTime: Math.ceil(blockTimestamp.getTime() / 1000), | ||
span: twapOrder.order.span, | ||
frequency: twapOrder.order.t, | ||
}), | ||
appData: twapOrder.order.appData, | ||
feeAmount: '0', | ||
kind: 'sell', | ||
partiallyFillable: false, | ||
sellTokenBalance: 'erc20', | ||
buyTokenBalance: 'erc20', | ||
} as OrderParameters | ||
} | ||
|
||
function calculateValidTo({ | ||
part, | ||
startTime, | ||
frequency, | ||
span, | ||
}: { | ||
part: number | ||
startTime: number | ||
frequency: number | ||
span: number | ||
}): number { | ||
if (span === 0) { | ||
return startTime + (part + 1) * frequency - 1 | ||
} | ||
|
||
return startTime + part * frequency + span - 1 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is
order.safeAddress
always lowercase?