Skip to content

Commit

Permalink
[CoW AMM Deployer] Fix decimals on min traded token 0 filling
Browse files Browse the repository at this point in the history
  • Loading branch information
yvesfracari committed Apr 17, 2024
1 parent 77a7304 commit 14afd4c
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions apps/cow-amm-deployer/src/app/new/(components)/AmmForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { InfoCircledIcon } from "@radix-ui/react-icons";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { FieldValues, useForm, UseFormReturn } from "react-hook-form";
import { formatUnits, parseUnits } from "viem";

import { Button } from "#/components";
import { Input } from "#/components/Input";
Expand Down Expand Up @@ -42,6 +43,15 @@ const getNewMinTradeToken0 = (newToken0: IToken, chainId: ChainId) => {
chainId,
})
.then((price) => 10 / price)
.then((amount) =>
// Format and parse to round on the right number of decimals
Number(
formatUnits(
parseUnits(String(amount), newToken0.decimals),
newToken0.decimals
)
)
)
.catch(() => 0);
};

Expand Down Expand Up @@ -128,7 +138,7 @@ export function AmmForm({
});
setValue(
"minTradedToken0",
await getNewMinTradeToken0(token, chainId as ChainId),
await getNewMinTradeToken0(token, chainId as ChainId)
);
}}
selectedToken={formData?.token0 ?? undefined}
Expand Down Expand Up @@ -173,7 +183,7 @@ export function AmmForm({
<AccordionTrigger
className={cn(
errors.minTradedToken0 ? "text-destructive" : "",
"pt-0",
"pt-0"
)}
>
Advanced Options
Expand Down Expand Up @@ -312,7 +322,7 @@ function PriceOracleFields({
getUniswapV2PairAddress(
chainId,
tokenAddresses[0],
tokenAddresses[1],
tokenAddresses[1]
)
.then((address) => {
setValue("uniswapV2Pair", address);
Expand Down Expand Up @@ -369,7 +379,7 @@ async function getBalancerPoolId(chainId: number, tokens: Address[]) {
async function getUniswapV2PairAddress(
chainId: number,
token0: Address,
token1: Address,
token1: Address
) {
if (token0 === token1) throw new Error("Invalid tokens");
const pairsData = await pairs.gql(String(chainId) || "1").pairsWhereTokens({
Expand Down

0 comments on commit 14afd4c

Please sign in to comment.