-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(twap): generate all part orders (#2759)
- Loading branch information
Showing
9 changed files
with
143 additions
and
126 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 was deleted.
Oops, something went wrong.
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,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) => { | ||
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) | ||
|
||
const ids = await Promise.all(parts.map((part) => computeOrderUid(chainId, safeAddress, part as Order))) | ||
|
||
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 | ||
} |
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