diff --git a/apps/cow-tools/src/app/milkman/(components)/OrderTable/TableRowTransaction.tsx b/apps/cow-tools/src/app/milkman/(components)/OrderTable/TableRowTransaction.tsx index f21abf1e0..bd04e4d8a 100644 --- a/apps/cow-tools/src/app/milkman/(components)/OrderTable/TableRowTransaction.tsx +++ b/apps/cow-tools/src/app/milkman/(components)/OrderTable/TableRowTransaction.tsx @@ -10,10 +10,8 @@ import { useState } from "react"; import { formatUnits } from "viem"; import Table from "#/components/Table"; -import { - ICowOrder, - IUserMilkmanTransaction, -} from "#/hooks/useUserMilkmanTransactions"; +import { IUserMilkmanTransaction } from "#/hooks/useUserMilkmanTransactions"; +import { ICowOrder } from "#/lib/cow/fetchCowOrder"; import { cowTokenList } from "#/utils/cowTokenList"; import { SwapStatus, TransactionStatus } from "../../utils/type"; diff --git a/apps/cow-tools/src/app/milkman/(components)/SingleOrderForm/PriceChecker.tsx b/apps/cow-tools/src/app/milkman/(components)/SingleOrderForm/PriceChecker.tsx index 54314951a..aa5c6e2cc 100644 --- a/apps/cow-tools/src/app/milkman/(components)/SingleOrderForm/PriceChecker.tsx +++ b/apps/cow-tools/src/app/milkman/(components)/SingleOrderForm/PriceChecker.tsx @@ -22,9 +22,9 @@ import Table from "#/components/Table"; import { Tooltip } from "#/components/Tooltip"; import { Form, FormMessage } from "#/components/ui/form"; import { Label } from "#/components/ui/label"; +import { fetchCowQuote } from "#/lib/cow/fetchCowQuote"; import { getPriceCheckerFromAddressAndChain } from "#/lib/decode"; import { encodeExpectedOutArguments } from "#/lib/encode"; -import { fetchCowQuote } from "#/lib/fetchCowQuote"; import { deployedPriceCheckersByChain, expectedOutCalculatorAddressesMapping, diff --git a/apps/cow-tools/src/app/milkman/(components)/SingleOrderForm/Twap.tsx b/apps/cow-tools/src/app/milkman/(components)/SingleOrderForm/Twap.tsx index b9693656a..a508659f8 100644 --- a/apps/cow-tools/src/app/milkman/(components)/SingleOrderForm/Twap.tsx +++ b/apps/cow-tools/src/app/milkman/(components)/SingleOrderForm/Twap.tsx @@ -8,7 +8,7 @@ import { Checkbox } from "#/components/Checkbox"; import { Input } from "#/components/Input"; import { Select, SelectItem } from "#/components/Select"; import { Form } from "#/components/ui/form"; -import { fetchCowQuote } from "#/lib/fetchCowQuote"; +import { fetchCowQuote } from "#/lib/cow/fetchCowQuote"; import { fetchTokenUsdPrice } from "#/lib/fetchTokenUsdPrice"; import { orderTwapSchema } from "#/lib/schema"; import { ChainId } from "#/utils/chainsPublicClients"; diff --git a/apps/cow-tools/src/hooks/useUserMilkmanTransactions.ts b/apps/cow-tools/src/hooks/useUserMilkmanTransactions.ts index 539a6215c..c504dd656 100644 --- a/apps/cow-tools/src/hooks/useUserMilkmanTransactions.ts +++ b/apps/cow-tools/src/hooks/useUserMilkmanTransactions.ts @@ -11,6 +11,7 @@ import { gql } from "graphql-tag"; import { useEffect, useState } from "react"; import { PublicClient } from "viem"; +import { getCowOrders, ICowOrder } from "#/lib/cow/fetchCowOrder"; import { fetchTokenInfo } from "#/lib/fetchTokenInfo"; import { AllTransactionFromUserQuery } from "#/lib/gql/generated"; import { milkmanSubgraph } from "#/lib/gql/sdk"; @@ -54,25 +55,6 @@ gql(` } `); -export interface ICowOrder { - sellToken: Address; - buyToken: Address; - receiver: Address; - sellAmount: number; - buyAmount: number; - validTo: number; - feeAmount: number; - kind: string; - partiallyFillable: boolean; - sellTokenBalance: string; - buyTokenBalance: string; - from: Address; - executedSellAmount?: number; - executedSellAmountBeforeFees?: number; - executedBuyAmount?: number; - executedFeeAmount?: number; - status: string; -} export interface IMilkmanOrder { cowOrders: ICowOrder[]; hasToken: boolean; @@ -86,8 +68,6 @@ export interface IUserMilkmanTransaction { processed: boolean; } -const cowApiUrl = "https://api.cow.fi/goerli"; - function structureMilkmanTransaction( transaction: AllTransactionFromUserQuery["users"][0]["transactions"][0], cowOrder: ICowOrder[][], @@ -159,7 +139,7 @@ async function getProcessedMilkmanTransactions({ orderContractsBySwap.map((orderContract) => retryAsyncOperation( async () => { - return getCowOrders(orderContract as Address); + return getCowOrders(orderContract as Address, chainId); }, 5, 1000, @@ -346,11 +326,3 @@ export async function getTokenBalance( args: [userAddress], }); } - -export async function getCowOrders(userAddress: Address): Promise { - return fetch(`${cowApiUrl}/api/v1/account/${userAddress}/orders`, { - headers: { - Accept: "application/json", - }, - }).then((response) => response.json() as Promise); -} diff --git a/apps/cow-tools/src/lib/cow/fetchCowOrder.ts b/apps/cow-tools/src/lib/cow/fetchCowOrder.ts new file mode 100644 index 000000000..08cae6cac --- /dev/null +++ b/apps/cow-tools/src/lib/cow/fetchCowOrder.ts @@ -0,0 +1,38 @@ +import { Address } from "@bleu-fi/utils"; + +import { ChainId } from "#/utils/chainsPublicClients"; + +import { COW_API_URL_BY_CHAIN_ID } from "."; + +export interface ICowOrder { + sellToken: Address; + buyToken: Address; + receiver: Address; + sellAmount: number; + buyAmount: number; + validTo: number; + feeAmount: number; + kind: string; + partiallyFillable: boolean; + sellTokenBalance: string; + buyTokenBalance: string; + from: Address; + executedSellAmount?: number; + executedSellAmountBeforeFees?: number; + executedBuyAmount?: number; + executedFeeAmount?: number; + status: string; +} + +export async function getCowOrders( + userAddress: Address, + chainId: ChainId, +): Promise { + const url = COW_API_URL_BY_CHAIN_ID[chainId]; + + return fetch(`${url}/api/v1/account/${userAddress}/orders`, { + headers: { + Accept: "application/json", + }, + }).then((response) => response.json() as Promise); +} diff --git a/apps/cow-tools/src/lib/fetchCowQuote.ts b/apps/cow-tools/src/lib/cow/fetchCowQuote.ts similarity index 84% rename from apps/cow-tools/src/lib/fetchCowQuote.ts rename to apps/cow-tools/src/lib/cow/fetchCowQuote.ts index 7aef0f210..bb7e9c012 100644 --- a/apps/cow-tools/src/lib/fetchCowQuote.ts +++ b/apps/cow-tools/src/lib/cow/fetchCowQuote.ts @@ -1,13 +1,6 @@ -import { gnosis, goerli } from "viem/chains"; - import { ChainId } from "#/utils/chainsPublicClients"; -const COW_API_BASE_URL = "https://api.cow.fi/"; - -const COW_API_URL_BY_CHAIN_ID = { - [gnosis.id]: COW_API_BASE_URL + "xdai", - [goerli.id]: COW_API_BASE_URL + "goerli", -}; +import { COW_API_URL_BY_CHAIN_ID } from "."; export async function fetchCowQuote({ tokenIn, diff --git a/apps/cow-tools/src/lib/cow/index.ts b/apps/cow-tools/src/lib/cow/index.ts new file mode 100644 index 000000000..b5ae23599 --- /dev/null +++ b/apps/cow-tools/src/lib/cow/index.ts @@ -0,0 +1,8 @@ +import { gnosis, goerli } from "viem/chains"; + +const COW_API_BASE_URL = "https://api.cow.fi/"; + +export const COW_API_URL_BY_CHAIN_ID = { + [gnosis.id]: COW_API_BASE_URL + "xdai", + [goerli.id]: COW_API_BASE_URL + "goerli", +}; diff --git a/apps/cow-tools/src/lib/schema.ts b/apps/cow-tools/src/lib/schema.ts index 9edf871a3..b3a7f108f 100644 --- a/apps/cow-tools/src/lib/schema.ts +++ b/apps/cow-tools/src/lib/schema.ts @@ -2,7 +2,7 @@ import { Address, capitalize } from "@bleu-fi/utils"; import { isAddress, PublicClient } from "viem"; import { z } from "zod"; -import { fetchCowQuote } from "#/lib/fetchCowQuote"; +import { fetchCowQuote } from "#/lib/cow/fetchCowQuote"; import { ChainId } from "#/utils/chainsPublicClients"; import { dynamicSlippagePriceCheckerAbi } from "./abis/dynamicSlippagePriceChecker";