Skip to content

Commit

Permalink
Merge pull request #60 from bleu/pedro/cow-418-create-balancer-v2-wit…
Browse files Browse the repository at this point in the history
…hdraw-based-on-cow-amm-withdraw-jean

Create Uni V2 Withdraw Hook
  • Loading branch information
yvesfracari authored Dec 4, 2024
2 parents 13d54ed + 1e52b8a commit 211b269
Show file tree
Hide file tree
Showing 145 changed files with 3,148 additions and 1,965 deletions.
3 changes: 2 additions & 1 deletion apps/deposit-pool/src/components/PoolItemInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { formatNumber } from "@bleu.builders/ui";
import type { IPool } from "@bleu/cow-hooks-ui";

export function PoolItemInfo({ pool }: { pool: IPool }) {
if (!pool.dynamicData) return null;
const aprSumPct =
pool.dynamicData.aprItems.reduce((acc, { apr }) => acc + apr, 0) * 100;

return (
<div className="flex text-right items-end flex-col gap-1 text-xs">
<i>TVL: ${formatNumber(pool.dynamicData.totalLiquidity, 2)}</i>
<i>TVL: ${formatNumber(pool.dynamicData?.totalLiquidity || 0, 2)}</i>
<i>APR: {formatNumber(aprSumPct, 2)}%</i>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions apps/deposit-pool/src/hooks/useCowAmmPools.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useIFrameContext, usePools } from "@bleu/cow-hooks-ui";
import { useBalancerPools, useIFrameContext } from "@bleu/cow-hooks-ui";

export function useCowAmmPools() {
const { context } = useIFrameContext();

return usePools(
return useBalancerPools(
{
poolTypeIn: ["COW_AMM"],
},
Expand Down
4 changes: 2 additions & 2 deletions apps/deposit-pool/src/hooks/useTokenBuyPools.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useIFrameContext, usePools } from "@bleu/cow-hooks-ui";
import { useBalancerPools, useIFrameContext } from "@bleu/cow-hooks-ui";
import type { Address } from "viem";

export function useTokenBuyPools() {
const { context } = useIFrameContext();

return usePools(
return useBalancerPools(
{
poolTypeIn: ["COW_AMM"],
tokensIn: context?.orderParams?.buyTokenAddress
Expand Down
2 changes: 1 addition & 1 deletion apps/deposit-pool/src/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function calculateProportionalTokenAmounts({
{
address: pool.address,
totalShares: formatUnits(
BigInt(pool.dynamicData.totalShares.toString()),
BigInt(pool.dynamicData?.totalShares.toString() || "0"),
pool.decimals,
) as `${number}`,
tokens: poolBalances.map((balance) => ({
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@bleu/withdraw-pool",
"name": "@bleu/withdraw-cow-amm",
"version": "0.0.0",
"private": true,
"scripts": {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"use client";

import { RootLayout } from "@bleu/cow-hooks-ui";
import { UserPoolContextProvider } from "#/context/userPools";
import { WithdrawFormContextProvider } from "#/context/withdrawHookForm";
import "@bleu/cow-hooks-ui/global.css";
import Head from "next/head";

import type * as React from "react";
import { FormContextProvider } from "#/context/form";

export default function Layout({ children }: { children: React.ReactNode }) {
return (
Expand All @@ -15,9 +14,9 @@ export default function Layout({ children }: { children: React.ReactNode }) {
<link rel="manifest" href="/manifest.json" />
</Head>
<RootLayout>
<UserPoolContextProvider>
<FormContextProvider>{children}</FormContextProvider>
</UserPoolContextProvider>
<WithdrawFormContextProvider poolTypeIn="COW_AMM">
{children}
</WithdrawFormContextProvider>
</RootLayout>
</html>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

import {
type IPool,
PoolForm,
PoolItemInfo,
PoolsDropdownMenu,
Spinner,
useBalancerUserPools,
useIFrameContext,
} from "@bleu/cow-hooks-ui";
import {
type WithdrawSchemaType,
decodeExitPoolHookCalldata,
} from "@bleu/utils";
import { ALL_SUPPORTED_CHAIN_IDS } from "@cowprotocol/cow-sdk";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useFormContext, useWatch } from "react-hook-form";
import { PoolForm } from "#/components/PoolForm";
import { PoolItemInfo } from "#/components/PoolItemInfo";
import { useUserPoolContext } from "#/context/userPools";
import { decodeExitPoolHookCalldata } from "#/utils/decodeExitPoolHookCalldata";
import type { WithdrawSchemaType } from "#/utils/schema";

export default function Page() {
const [isEditHookLoading, setIsEditHookLoading] = useState(true);
const { context } = useIFrameContext();
const {
userPoolSwr: { data: pools, isLoading: isLoadingPools },
} = useUserPoolContext();
const { data: pools, isLoading: isLoadingPools } =
useBalancerUserPools("COW_AMM");

const { setValue, control } = useFormContext<WithdrawSchemaType>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {
useHandleTokenAllowance,
useSubmitHook,
} from "@bleu/cow-hooks-ui";
import type { WithdrawSchemaType } from "@bleu/utils";
import { BigNumber, type BigNumberish } from "ethers";
import { useRouter } from "next/navigation";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useFormContext } from "react-hook-form";
import type { Address } from "viem";
import type { WithdrawSchemaType } from "#/utils/schema";

export default function Page() {
const [currentStepIndex, setCurrentStepIndex] = useState(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
"use client";

import { type PropsWithChildren, useCallback, useEffect, useMemo } from "react";
import { useBalancerUserPools } from "@bleu/cow-hooks-ui";
import { type WithdrawSchemaType, withdrawSchema } from "@bleu/utils";
import { useCallback, useEffect, useMemo } from "react";

import { Form } from "@bleu.builders/ui";
import { useIFrameContext } from "@bleu/cow-hooks-ui";
import { zodResolver } from "@hookform/resolvers/zod";
import { useRouter } from "next/navigation";
import { useForm, useWatch } from "react-hook-form";
import { useGetHookInfo } from "#/hooks/useGetHookInfo";
import { type WithdrawSchemaType, withdrawSchema } from "#/utils/schema";
import { useUserPoolContext } from "./userPools";

export function FormContextProvider({ children }: PropsWithChildren) {
export function WithdrawFormContextProvider({
children,
poolTypeIn,
}: {
children: React.ReactNode;
poolTypeIn: "COW_AMM" | "WEIGHTED";
}) {
const getHookInfo = useGetHookInfo();
const { context, setHookInfo } = useIFrameContext();

const form = useForm<WithdrawSchemaType>({
Expand All @@ -24,9 +31,7 @@ export function FormContextProvider({ children }: PropsWithChildren) {

const { control, handleSubmit, setValue } = form;

const {
userPoolSwr: { data: pools },
} = useUserPoolContext();
const { data: pools } = useBalancerUserPools(poolTypeIn);

const poolId = useWatch({ control, name: "poolId" });

Expand All @@ -36,18 +41,17 @@ export function FormContextProvider({ children }: PropsWithChildren) {
);
}, [pools, poolId]);

const getHooksTransactions = useGetHookInfo(selectedPool);

const router = useRouter();

const onSubmitCallback = useCallback(
async (data: WithdrawSchemaType) => {
const hookInfo = await getHooksTransactions(data.withdrawPct);
if (!selectedPool) return;
const hookInfo = await getHookInfo(selectedPool, data.withdrawPct);
if (!hookInfo) return;
setHookInfo(hookInfo);
router.push("/signing");
},
[getHooksTransactions, setHookInfo, router],
[getHookInfo, setHookInfo, router, selectedPool],
);

// biome-ignore lint:
Expand Down
49 changes: 49 additions & 0 deletions apps/withdraw-cow-amm/src/hooks/useGetHookInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { IHooksInfo, IPool } from "@bleu/cow-hooks-ui";
import { multiplyValueByPct } from "@bleu/utils";
import {
TRANSACTION_TYPES,
TransactionFactory,
} from "@bleu/utils/transactionFactory";
import { useCallback } from "react";
import { useGetPoolWithdrawArgs } from "./useGetPoolWithdrawArgs";

export function useGetHookInfo() {
const getPoolWithdrawArgs = useGetPoolWithdrawArgs();

return useCallback(
async (
pool: IPool,
withdrawPct: number,
): Promise<IHooksInfo | undefined> => {
if (!pool) return;

const bptAmount = multiplyValueByPct(
pool.userBalance.walletBalance,
withdrawPct,
);
const poolWithdrawArgs = await getPoolWithdrawArgs(pool, bptAmount);
if (!poolWithdrawArgs) return;

const txs = await Promise.all(
poolWithdrawArgs.map((arg) => {
return TransactionFactory.createRawTx(arg.type, arg);
}),
);

const permitData = poolWithdrawArgs
.filter((arg) => arg.type === TRANSACTION_TYPES.ERC20_TRANSFER_FROM)
.map((arg) => {
return {
tokenAddress: arg.token,
amount: arg.amount,
tokenSymbol: arg.symbol,
};
});
return {
txs,
permitData: permitData,
};
},
[getPoolWithdrawArgs],
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import {
import { BigNumber } from "ethers";
import { useCallback } from "react";

export function useGetPoolWithdrawArgs(
pool?: IPool,
): (
export function useGetPoolWithdrawArgs(): (
pool: IPool,
bptAMount: BigNumber,
) => Promise<
| (
Expand All @@ -27,7 +26,7 @@ export function useGetPoolWithdrawArgs(
const { context, cowShedProxy } = useIFrameContext();

return useCallback(
async (bptAmount: BigNumber) => {
async (pool: IPool, bptAmount: BigNumber) => {
if (!context?.account || !cowShedProxy || !pool) return;
const poolState = await fetchPoolState(pool.id, context.chainId);
const bptWalletAmount = bptAmount.gte(pool.userBalance.walletBalance)
Expand Down Expand Up @@ -78,6 +77,6 @@ export function useGetPoolWithdrawArgs(
| ERC20TransferFromAllWeirollArgs
)[];
},
[context, cowShedProxy, pool],
[context, cowShedProxy],
);
}
File renamed without changes.
File renamed without changes.
31 changes: 0 additions & 31 deletions apps/withdraw-pool/src/context/userPools.tsx

This file was deleted.

Loading

0 comments on commit 211b269

Please sign in to comment.