Skip to content

Commit

Permalink
[MilkmApp] Change hardcoded cow url on order fetching (#572)
Browse files Browse the repository at this point in the history
* fix: change hardcoded cow url on order fetching

* chore: run lint
  • Loading branch information
yvesfracari authored Jan 16, 2024
1 parent 1c01602 commit 516ead0
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
32 changes: 2 additions & 30 deletions apps/cow-tools/src/hooks/useUserMilkmanTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand All @@ -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[][],
Expand Down Expand Up @@ -159,7 +139,7 @@ async function getProcessedMilkmanTransactions({
orderContractsBySwap.map((orderContract) =>
retryAsyncOperation<ICowOrder[]>(
async () => {
return getCowOrders(orderContract as Address);
return getCowOrders(orderContract as Address, chainId);
},
5,
1000,
Expand Down Expand Up @@ -346,11 +326,3 @@ export async function getTokenBalance(
args: [userAddress],
});
}

export async function getCowOrders(userAddress: Address): Promise<ICowOrder[]> {
return fetch(`${cowApiUrl}/api/v1/account/${userAddress}/orders`, {
headers: {
Accept: "application/json",
},
}).then((response) => response.json() as Promise<ICowOrder[]>);
}
38 changes: 38 additions & 0 deletions apps/cow-tools/src/lib/cow/fetchCowOrder.ts
Original file line number Diff line number Diff line change
@@ -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<ICowOrder[]> {
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<ICowOrder[]>);
}
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 8 additions & 0 deletions apps/cow-tools/src/lib/cow/index.ts
Original file line number Diff line number Diff line change
@@ -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",
};
2 changes: 1 addition & 1 deletion apps/cow-tools/src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 516ead0

Please sign in to comment.