Skip to content

Commit

Permalink
run lint
Browse files Browse the repository at this point in the history
  • Loading branch information
yvesfracari committed Oct 31, 2024
1 parent 6a29713 commit 5692817
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 53 deletions.
9 changes: 4 additions & 5 deletions apps/deposit-pool/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import {
Spinner,
useIFrameContext,
} from "@bleu/cow-hooks-ui";
import { Address, ALL_SUPPORTED_CHAIN_IDS } from "@cowprotocol/cow-sdk";
import { ALL_SUPPORTED_CHAIN_IDS, type Address } from "@cowprotocol/cow-sdk";
import { useCallback, useState } from "react";
import { useFormContext } from "react-hook-form";
import { PoolForm } from "#/components/PoolForm";
import { PoolItemInfo } from "#/components/PoolItemInfo";
import { useSelectedPool } from "#/hooks/useSelectedPool";
import { useTokenBalanceAfterSwap } from "#/hooks/useTokenBalanceAfterSwap";
import { useTokenBuyPools } from "#/hooks/useTokenBuyPools";
import type { FormType } from "#/types";
import { decodeCalldata } from "#/utils/decodeCalldata";
import { useSwapAmount } from "#/hooks/useSwapAmount";
import { useTokenBalanceAfterSwap } from "#/hooks/useTokenBalanceAfterSwap";

export default function Page() {
const { context } = useIFrameContext();
Expand All @@ -25,15 +24,15 @@ export default function Page() {

const { setValue } = useFormContext<FormType>();
const sellTokenAmountAfterSwap = useTokenBalanceAfterSwap(
context?.orderParams?.sellTokenAddress as Address
context?.orderParams?.sellTokenAddress as Address,
);

const selectedPool = useSelectedPool();

const loadHookInfo = useCallback(() => {
if (!context?.hookToEdit || !context.account || !isEditHookLoading) return;
const data = decodeCalldata(
context?.hookToEdit?.hook.callData as `0x${string}`
context?.hookToEdit?.hook.callData as `0x${string}`,
);
if (data) {
setValue("poolId", data.poolId);
Expand Down
16 changes: 10 additions & 6 deletions apps/deposit-pool/src/components/FormButton.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { useTokensAfterSwap } from "#/hooks/useTokensAfterSwap";
import { FormType } from "#/types";
import { ButtonPrimary, IBalance, useIFrameContext } from "@bleu/cow-hooks-ui";
import {
ButtonPrimary,
type IBalance,
useIFrameContext,
} from "@bleu/cow-hooks-ui";
import { useMemo } from "react";
import { useFormContext, useFormState, useWatch } from "react-hook-form";
import { Address } from "viem";
import type { Address } from "viem";
import { useTokensAfterSwap } from "#/hooks/useTokensAfterSwap";
import type { FormType } from "#/types";

export function FormButton({ poolBalances }: { poolBalances: IBalance[] }) {
const { context } = useIFrameContext();
const tokenAddresses = useMemo(
() =>
poolBalances.map(
(poolBalance) => poolBalance.token.address.toLowerCase() as Address
(poolBalance) => poolBalance.token.address.toLowerCase() as Address,
),
[poolBalances]
[poolBalances],
);
const poolTokens = useTokensAfterSwap(tokenAddresses);

Expand Down
18 changes: 9 additions & 9 deletions apps/deposit-pool/src/components/PoolForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Info,
type IPool,
Info,
Spinner,
useIFrameContext,
} from "@bleu/cow-hooks-ui";
Expand All @@ -9,24 +9,24 @@ import { useCallback, useEffect, useMemo } from "react";
import { useFormContext, useWatch } from "react-hook-form";
import { type Address, formatUnits } from "viem";
import { usePoolBalance } from "#/hooks/usePoolBalance";
import { useSwapAmount } from "#/hooks/useSwapAmount";
import { useTokenBalanceAfterSwap } from "#/hooks/useTokenBalanceAfterSwap";
import type { FormType } from "#/types";
import { calculateProportionalTokenAmounts, getTokenPrice } from "#/utils/math";
import { AmountFromAccountCheckbox } from "./AmountFromAccountCheckbox";
import { AmountFromSwapCheckbox } from "./AmountFromSwapCheckbox";
import { AmountFromUserInputCheckbox } from "./AmountFromUserInputCheckbox";
import { TokenAmountInput } from "./TokenAmountInput";
import { useSwapAmount } from "#/hooks/useSwapAmount";
import { useTokenBalanceAfterSwap } from "#/hooks/useTokenBalanceAfterSwap";
import { FormButton } from "./FormButton";
import { InfoContent } from "./InfoContent";
import { TokenAmountInput } from "./TokenAmountInput";

export function PoolForm({ pool }: { pool: IPool | undefined }) {
const { context } = useIFrameContext();
const { control, setValue } = useFormContext<FormType>();

const { buyAmount } = useSwapAmount();
const buyAmountAfterSwap = useTokenBalanceAfterSwap(
context?.orderParams?.buyTokenAddress as Address
context?.orderParams?.buyTokenAddress as Address,
);

const { data: poolBalances, isLoading: isBalanceLoading } = usePoolBalance({
Expand All @@ -46,7 +46,7 @@ export function PoolForm({ pool }: { pool: IPool | undefined }) {

const tokenPrices = useMemo(
() => poolBalances?.map((poolBalance) => getTokenPrice(poolBalance)),
[poolBalances]
[poolBalances],
);

const totalUsd = useMemo(() => {
Expand Down Expand Up @@ -82,14 +82,14 @@ export function PoolForm({ pool }: { pool: IPool | undefined }) {
const tokenAmountKey = `amounts.${tokenAmountAddress}` as const;
const calculatedAmount = formatUnits(
tokenAmount.rawAmount,
tokenAmount.decimals
tokenAmount.decimals,
);
setValue(tokenAmountKey, calculatedAmount);
}

setValue("referenceTokenAddress", address);
},
[poolBalances, tokenPrices, pool, setValue]
[poolBalances, tokenPrices, pool, setValue],
);

useEffect(() => {
Expand All @@ -116,7 +116,7 @@ export function PoolForm({ pool }: { pool: IPool | undefined }) {
}
}, [
context?.orderParams,
amountFromSwap,
amountFromAccount,
setValue,
updateTokenAmounts,
buyAmountAfterSwap,
Expand Down
12 changes: 6 additions & 6 deletions apps/deposit-pool/src/components/TokenAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Button, Input, formatNumber } from "@bleu/ui";
import { useCallback, useMemo } from "react";
import { useFormContext, useWatch } from "react-hook-form";
import type { Address } from "viem";
import { useTokenBalanceAfterSwap } from "#/hooks/useTokenBalanceAfterSwap";
import type { FormType } from "#/types";
import { constraintStringToBeNumeric } from "#/utils/constraintStringToBeNumeric";
import { useTokenBalanceAfterSwap } from "#/hooks/useTokenBalanceAfterSwap";

export function TokenAmountInput({
poolBalance,
Expand All @@ -32,7 +32,7 @@ export function TokenAmountInput({
});

const tokenBalanceAfterSwap = useTokenBalanceAfterSwap(
poolBalance.token.address
poolBalance.token.address,
);

const amountUsd = useMemo(() => {
Expand All @@ -46,11 +46,11 @@ export function TokenAmountInput({
if (updateTokenAmounts) {
updateTokenAmounts(
constraintStringToBeNumeric(amount),
poolBalance.token.address as Address
poolBalance.token.address as Address,
);
}
},
[updateTokenAmounts, poolBalance.token.address]
[updateTokenAmounts, poolBalance.token.address],
);

const disabled = amountFromSwap || amountFromAccount;
Expand All @@ -77,7 +77,7 @@ export function TokenAmountInput({
onChange(e.target.value);
setValue(
`amounts.${poolBalance.token.address.toLowerCase()}`,
constraintStringToBeNumeric(e.target.value)
constraintStringToBeNumeric(e.target.value),
);
},
})}
Expand All @@ -102,7 +102,7 @@ export function TokenAmountInput({
onClick={() => {
setValue(
`amounts.${poolBalance.token.address.toLowerCase()}`,
tokenBalanceAfterSwap
tokenBalanceAfterSwap,
);
onChange(tokenBalanceAfterSwap);
}}
Expand Down
4 changes: 2 additions & 2 deletions apps/deposit-pool/src/hooks/useSwapAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export function useSwapAmount() {
if (!context?.orderParams?.buyAmount || !tokenBuyDecimals) return;
return formatUnits(
BigInt(context.orderParams.buyAmount),
tokenBuyDecimals
tokenBuyDecimals,
) as `${number}`;
}, [context?.orderParams?.buyAmount, tokenBuyDecimals]);

const sellAmount = useMemo(() => {
if (!context?.orderParams?.sellAmount || !tokenSellDecimals) return;
return formatUnits(
BigInt(context.orderParams.sellAmount),
tokenSellDecimals
tokenSellDecimals,
) as `${number}`;
}, [context?.orderParams?.sellAmount, tokenSellDecimals]);

Expand Down
8 changes: 4 additions & 4 deletions apps/deposit-pool/src/hooks/useTokenBalanceAfterSwap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Address, formatUnits, parseUnits } from "viem";
import { useSwapAmount } from "./useSwapAmount";
import { useIFrameContext, useReadTokenContract } from "@bleu/cow-hooks-ui";
import { useMemo } from "react";
import { type Address, formatUnits } from "viem";
import { updateTokenBalanceAfterSwap } from "#/utils/math";
import { useSwapAmount } from "./useSwapAmount";

export function useTokenBalanceAfterSwap(address: string) {
const { context } = useIFrameContext();
Expand All @@ -16,7 +16,7 @@ export function useTokenBalanceAfterSwap(address: string) {
return updateTokenBalanceAfterSwap({
userBalance: formatUnits(
userBalance || BigInt(0),
tokenDecimals
tokenDecimals,
) as `${number}`,
tokenAddress: address as Address,
tokenDecimals: tokenDecimals,
Expand All @@ -25,5 +25,5 @@ export function useTokenBalanceAfterSwap(address: string) {
tokenBuyAddress: context?.orderParams?.buyTokenAddress as Address,
tokenSellAddress: context?.orderParams?.sellTokenAddress as Address,
});
}, [buyAmount, sellAmount, tokenDecimals, userBalance]);
}, [buyAmount, sellAmount, tokenDecimals, userBalance, context, address]);
}
12 changes: 7 additions & 5 deletions apps/deposit-pool/src/hooks/useTokens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readTokenContract, useIFrameContext } from "@bleu/cow-hooks-ui";
import { useCallback, useEffect, useState } from "react";
import { Address, formatUnits } from "viem";
import { type Address, formatUnits } from "viem";

export function useTokens(tokens: Address[]) {
const { context, publicClient } = useIFrameContext();
Expand All @@ -15,20 +15,22 @@ export function useTokens(tokens: Address[]) {
return readTokenContract(
token,
publicClient,
context.account as Address
context.account as Address,
);
})
}),
);

const newTokensBalance = tokenReadInfo.reduce(
(acc, tokenInfo, index) => {
// biome-ignore lint:
if (!tokenInfo.decimals.result) return { ...acc };
return {
// biome-ignore lint:
...acc,
[tokens[index]]: {
balance: formatUnits(
tokenInfo?.balance?.result || BigInt(0),
tokenInfo.decimals.result
tokenInfo.decimals.result,
) as `${number}`,
decimals: tokenInfo.decimals.result,
symbol: tokenInfo.symbol.result || "",
Expand All @@ -38,7 +40,7 @@ export function useTokens(tokens: Address[]) {
{} as Record<
string,
{ balance: `${number}`; decimals: number; symbol: string }
>
>,
);

setTokensBalance(newTokensBalance);
Expand Down
14 changes: 8 additions & 6 deletions apps/deposit-pool/src/hooks/useTokensAfterSwap.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useIFrameContext } from "@bleu/cow-hooks-ui";
import { useTokens } from "./useTokens";
import { useSwapAmount } from "./useSwapAmount";
import { useMemo } from "react";
import { Address } from "viem";
import type { Address } from "viem";
import { updateTokenBalanceAfterSwap } from "#/utils/math";
import { useSwapAmount } from "./useSwapAmount";
import { useTokens } from "./useTokens";

export function useTokensAfterSwap(
tokens: Address[]
tokens: Address[],
): Record<string, { balance: `${number}`; decimals: number; symbol: string }> {
const { context } = useIFrameContext();
const balancesBeforeSwap = useTokens(tokens);
Expand All @@ -16,9 +16,11 @@ export function useTokensAfterSwap(
return balancesBeforeSwap;

return Object.keys(balancesBeforeSwap).reduce(
(acc, token, index) => {
(acc, token, _index) => {
// biome-ignore lint:
if (!balancesBeforeSwap[token]) return { ...acc };
return {
// biome-ignore lint:
...acc,
[token]: {
balance: updateTokenBalanceAfterSwap({
Expand All @@ -39,7 +41,7 @@ export function useTokensAfterSwap(
{} as Record<
string,
{ balance: `${number}`; decimals: number; symbol: string }
>
>,
);
}, [balancesBeforeSwap, buyAmount, sellAmount, context?.orderParams]);
}
12 changes: 6 additions & 6 deletions apps/deposit-pool/src/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function calculateProportionalTokenAmounts({
}) {
const referenceToken = poolBalances.find(
(balance) =>
balance.token.address.toLowerCase() === tokenAddress.toLowerCase()
balance.token.address.toLowerCase() === tokenAddress.toLowerCase(),
);

if (!referenceToken) {
Expand All @@ -34,13 +34,13 @@ export function calculateProportionalTokenAmounts({
address: pool.address,
totalShares: formatUnits(
BigInt(pool.dynamicData.totalShares.toString()),
pool.decimals
pool.decimals,
) as `${number}`,
tokens: poolBalances.map((balance) => ({
address: balance.token.address.toLowerCase() as Address,
balance: formatUnits(
BigInt(balance.balance.toString()),
balance.token.decimals
balance.token.decimals,
) as `${number}`,
decimals: balance.token.decimals,
})),
Expand All @@ -49,7 +49,7 @@ export function calculateProportionalTokenAmounts({
address: referenceToken.token.address.toLowerCase() as Address,
decimals: referenceToken.token.decimals,
rawAmount: parseUnits(tokenAmount, referenceToken.token.decimals),
}
},
);
}

Expand Down Expand Up @@ -78,7 +78,7 @@ export function updateTokenBalanceAfterSwap({
) {
return formatUnits(
balance - parseUnits(sellAmount, tokenDecimals),
tokenDecimals
tokenDecimals,
) as `${number}`;
}

Expand All @@ -88,7 +88,7 @@ export function updateTokenBalanceAfterSwap({
) {
return formatUnits(
balance + parseUnits(buyAmount, tokenDecimals),
tokenDecimals
tokenDecimals,
) as `${number}`;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cow-hooks-ui/src/ButtonPrimary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const ButtonPrimary: React.FC<
<button
className={cn(
"flex flex-row flex-wrap w-full items-center justify-center text-sm font-semibold border-none shadow-none rounded-2xl relative min-h-[58px] xsm:text-lg disabled:border-transparent bg-color-primary text-color-button-text transition-colors duration-200 ease-in-out focus:shadow-none focus:transform-none focus:bg-color-primary-lighter hover:shadow-none hover:transform-none hover:bg-color-primary-lighter active:shadow-none active:transform-none active:bg-color-primary-lighter disabled:outline-none disabled:cursor-auto disabled:shadow-none disabled:bg-color-paper-darker disabled:text-color-button-text-disabled mb-4",
className
className,
)}
disabled={disabled}
{...props}
Expand Down
Loading

0 comments on commit 5692817

Please sign in to comment.