Skip to content

Commit

Permalink
Merge pull request #15 from bleu-fi/luiza/cow-190-handle-executed-on-…
Browse files Browse the repository at this point in the history
…ponder

Get status from cow API
  • Loading branch information
luizakp authored Mar 11, 2024
2 parents 495a225 + 5a9f927 commit bc79903
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/app/history/(components)/OrderTable/TableRowOrder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { TrashIcon } from "@radix-ui/react-icons";
import Table from "#/components/Table";
import { TokenInfo } from "#/components/TokenInfo";
import { StopLossOrderType } from "#/hooks/useOrders";
import { formatDateToLocalDatetime } from "#/utils";
import { capitalize, formatDateToLocalDatetime } from "#/utils";

export function TableRowOrder({ order }: { order: StopLossOrderType }) {
const orderStatus = "Created";
return (
<>
<Table.BodyRow key={order?.id}>
Expand All @@ -30,7 +29,7 @@ export function TableRowOrder({ order }: { order: StopLossOrderType }) {
chainId={order?.chainId}
/>
</Table.BodyCell>
<Table.BodyCell>{orderStatus}</Table.BodyCell>
<Table.BodyCell>{capitalize(order?.status)}</Table.BodyCell>
<Table.BodyCell>
<button type="button" className="flex items-center" disabled={true}>
<TrashIcon className={"size-5 text-slate10"} />
Expand Down
2 changes: 1 addition & 1 deletion src/components/menus/StopLossConditionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Controller, FieldValues, useForm } from "react-hook-form";

import { stopLossConditionSchema } from "#/lib/schema";
import { IStopLossRecipeData, TIME_OPTIONS } from "#/lib/types";
import { buildBlockExplorerAddressURL } from "#/lib/utils";
import { buildBlockExplorerAddressURL } from "#/utils";

import Button from "../Button";
import { Input } from "../Input";
Expand Down
64 changes: 60 additions & 4 deletions src/hooks/useOrders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,53 @@ import { composableCowSubgraph } from "#/lib/gql/sdk";
import { ChainId } from "#/lib/publicClients";
import { ArrElement, GetDeepProp } from "#/utils";

export type StopLossOrderType = ArrElement<
type StopLossOrderTypeRaw = ArrElement<
GetDeepProp<UserStopLossOrdersQuery, "items">
>;

export interface StopLossOrderType extends StopLossOrderTypeRaw {
status: string;
}

export interface CowOrder {
appData: string
availableBalance: string
buyAmount: string
buyToken: string
buyTokenBalance: string
class: string
creationDate: string
executedBuyAmount:string
executedFeeAmount: string
executedSellAmount: string
executedSellAmountBeforeFees: string
executedSurplusFee: string
feeAmount: string
fullAppData: string
fullFeeAmount: string
interactions: {
pre: Array<string>
post: Array<string>
}
invalidated: boolean
isLiquidityOrder: boolean
kind: string
owner: string
partiallyFillable: boolean
receiver: string
sellAmount: string
sellToken: string
sellTokenBalance: string
settlementContract: string
signature: string
signingScheme: string
solverFee: string
status: string
uid: string
validTo: number
}


gql(
`query UserStopLossOrders($user: String!) {
orders(where: {stopLossParametersId_not: null, user_in: [$user]}) {
Expand Down Expand Up @@ -111,9 +154,22 @@ async function getProcessedStopLossOrders({
user: `${address}-${chainId}`,
});

//TODO: use this on COW-100
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const orderFromCowApi = await getCowOrders(address, chainId);

return rawOrdersData.orders.items;
const orderData = rawOrdersData.orders.items.map((order) => {
const match = orderFromCowApi.find((cowOrder: CowOrder) => cowOrder.appData === order.stopLossParameters?.appData);
if(match && match.status !== "expired") {
return {
...order,
status: match.status,
}
} else {
return {
...order,
status: "created",
}
}
})

return orderData;
}
3 changes: 2 additions & 1 deletion src/lib/cowApi/fetchCowOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Address } from "viem";
import { ChainId } from "../publicClients";
import { COW_API_URL_BY_CHAIN_ID } from "./api";
import { fetcher } from "#/utils/fetcher";
import { CowOrder } from "#/hooks/useOrders";

export async function getCowOrders(
userAddress: Address,
chainId: ChainId,
) {
const baseUrl = COW_API_URL_BY_CHAIN_ID[chainId];
const url = `${baseUrl}/api/v1/account/${userAddress}/orders`;
return await fetcher(url)
return await fetcher<CowOrder[]>(url)
}

0 comments on commit bc79903

Please sign in to comment.