Skip to content

Commit

Permalink
fix tokens out of order issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yvesfracari committed Oct 22, 2024
1 parent 97bcd24 commit 8e5ea95
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 18 deletions.
4 changes: 2 additions & 2 deletions apps/deposit-pool/src/hooks/useGetHookInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import {
type IHooksInfo,
type IPool,
minimalPoolToPoolState,
fetchPoolState,
useIFrameContext,
useTokensAllowances,
} from "@bleu/cow-hooks-ui";
Expand Down Expand Up @@ -89,7 +89,7 @@ export function useGetHookInfo(pool?: IPool) {
async (params: FormType) => {
if (!pool || !context || !context.account || !cowShedProxy)
throw new Error("Missing context");
const poolState = minimalPoolToPoolState(pool);
const poolState = await fetchPoolState(pool.id, context.chainId);

const referenceTokenDecimals = pool.allTokens.find(
(token) =>
Expand Down
10 changes: 5 additions & 5 deletions apps/deposit-pool/src/hooks/usePoolBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { gql } from "graphql-request";
import useSWR from "swr";

import type { IBalance } from "@bleu/cow-hooks-ui";
import { BalancerChainName, GQL_CLIENT } from "@bleu/utils";
import { BALANCER_GQL_CLIENT, BalancerChainName } from "@bleu/utils";
import type { SupportedChainId } from "@cowprotocol/cow-sdk";
import { parseUnits } from "ethers/lib/utils";
import type { Address } from "viem";
Expand All @@ -24,7 +24,7 @@ interface IQuery {
totalBalance: `${number}`;
totalBalanceUsd: number;
};
poolTokens: {
allTokens: {
id: `0x${string}`;
address: Address;
name: string;
Expand Down Expand Up @@ -53,7 +53,7 @@ export const POOL_QUERY = gql`
totalBalance
totalBalanceUsd
}
poolTokens {
allTokens {
id
address
name
Expand All @@ -73,7 +73,7 @@ async function fetchPoolBalance(
): Promise<IBalance[]> {
if (!chainId || !poolId) return [];
const chainName = BalancerChainName[chainId];
const result = await GQL_CLIENT[chainId].request<{
const result = await BALANCER_GQL_CLIENT[chainId].request<{
pool: IQuery;
}>(POOL_QUERY, {
id: poolId,
Expand All @@ -83,7 +83,7 @@ async function fetchPoolBalance(
throw new Error("Pool not found");
}

const balances = result.pool.poolTokens.map((token) => {
const balances = result.pool.allTokens.map((token) => {
const balanceTotal = parseUnits(token.balance.toString(), token.decimals);

return {
Expand Down
2 changes: 1 addition & 1 deletion apps/withdraw-pool/src/hooks/useGetHookInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function useGetHookInfo(pool?: IPool) {
withdrawPct,
);
const balancerGaugeArgs = getBalancerGaugeArgs(bptAmount);
const poolWithdrawArgs = getPoolWithdrawArgs(bptAmount);
const poolWithdrawArgs = await getPoolWithdrawArgs(bptAmount);
if (!poolWithdrawArgs) return;

const argsArray = [...balancerGaugeArgs, ...poolWithdrawArgs];
Expand Down
8 changes: 4 additions & 4 deletions apps/withdraw-pool/src/hooks/useGetPoolWithdrawArgs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
type IPool,
minimalPoolToPoolState,
fetchPoolState,
useIFrameContext,
} from "@bleu/cow-hooks-ui";
import {
Expand All @@ -15,13 +15,13 @@ export function useGetPoolWithdrawArgs(
pool?: IPool,
): (
bptAMount: BigNumber,
) => (ERC20TransferFromArgs | BalancerWithdrawArgs)[] | undefined {
) => Promise<(ERC20TransferFromArgs | BalancerWithdrawArgs)[] | undefined> {
const { context, cowShedProxy } = useIFrameContext();

return useCallback(
(bptAmount: BigNumber) => {
async (bptAmount: BigNumber) => {
if (!context?.account || !cowShedProxy || !pool) return;
const poolState = minimalPoolToPoolState(pool);
const poolState = await fetchPoolState(pool.id, context.chainId);
const bptWalletAmount = bptAmount.gte(pool.userBalance.walletBalance)
? pool.userBalance.walletBalance
: bptAmount;
Expand Down
4 changes: 2 additions & 2 deletions apps/withdraw-pool/src/hooks/useUserPoolBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { gql } from "graphql-request";
import useSWR from "swr";

import type { IBalance } from "@bleu/cow-hooks-ui";
import { BalancerChainName, GQL_CLIENT } from "@bleu/utils";
import { BALANCER_GQL_CLIENT, BalancerChainName } from "@bleu/utils";
import type { SupportedChainId } from "@cowprotocol/cow-sdk";
import { formatUnits, parseUnits } from "ethers/lib/utils";
import type { Address } from "viem";
Expand Down Expand Up @@ -74,7 +74,7 @@ async function fetchUserPoolBalance(
): Promise<IBalance[]> {
if (!user || !chainId || !poolId) return [];
const chainName = BalancerChainName[chainId];
const result = await GQL_CLIENT[chainId].request<{
const result = await BALANCER_GQL_CLIENT[chainId].request<{
pool: IQuery;
}>(POOL_QUERY, {
id: poolId,
Expand Down
4 changes: 2 additions & 2 deletions packages/cow-hooks-ui/src/hooks/usePools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BalancerChainName, GQL_CLIENT } from "@bleu/utils";
import { BALANCER_GQL_CLIENT, BalancerChainName } from "@bleu/utils";
import type { SupportedChainId } from "@cowprotocol/cow-sdk";
import { gql } from "graphql-request";
import useSWR from "swr";
Expand Down Expand Up @@ -111,7 +111,7 @@ export function usePools(
async ([where, chainId]): Promise<IPool[]> => {
if (!chainId) return [];
const chainName = BalancerChainName[chainId];
return await GQL_CLIENT[chainId]
return await BALANCER_GQL_CLIENT[chainId]
.request<IQuery>(USER_POOLS_QUERY, {
where: {
...where,
Expand Down
15 changes: 14 additions & 1 deletion packages/cow-hooks-ui/src/utils/poolDataConverter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { PoolState } from "@balancer/sdk";
import { BalancerApi, type PoolState } from "@balancer/sdk";
import { BALANCER_API_URL, SUPPORTED_CHAIN_ID_TO_CHAIN_ID } from "@bleu/utils";
import type { SupportedChainId } from "@cowprotocol/cow-sdk";
import type { IPool } from "../types";

export function minimalPoolToPoolState(pool: IPool): PoolState {
Expand All @@ -15,3 +17,14 @@ export function minimalPoolToPoolState(pool: IPool): PoolState {
type: pool.type,
};
}

export function fetchPoolState(
poolId: string,
chainId: SupportedChainId,
): Promise<PoolState> {
const balancerApi = new BalancerApi(
BALANCER_API_URL[chainId],
SUPPORTED_CHAIN_ID_TO_CHAIN_ID[chainId],
);
return balancerApi.pools.fetchPoolState(poolId);
}
18 changes: 17 additions & 1 deletion packages/utils/balancerApi.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { ChainId } from "@balancer/sdk";
import { SupportedChainId } from "@cowprotocol/cow-sdk";

import { GraphQLClient } from "graphql-request";

const BASE_URL = "https://api-v3.balancer.fi/graphql";
const TEST_URL = "https://test-api-v3.balancer.fi/graphql";

export const GQL_CLIENT: Record<SupportedChainId, GraphQLClient> = {
export const BALANCER_API_URL: Record<SupportedChainId, string> = {
[SupportedChainId.MAINNET]: BASE_URL,
[SupportedChainId.SEPOLIA]: TEST_URL,
[SupportedChainId.ARBITRUM_ONE]: BASE_URL,
[SupportedChainId.GNOSIS_CHAIN]: BASE_URL,
};

export const BALANCER_GQL_CLIENT: Record<SupportedChainId, GraphQLClient> = {
[SupportedChainId.MAINNET]: new GraphQLClient(BASE_URL),
[SupportedChainId.SEPOLIA]: new GraphQLClient(TEST_URL),
[SupportedChainId.ARBITRUM_ONE]: new GraphQLClient(BASE_URL),
[SupportedChainId.GNOSIS_CHAIN]: new GraphQLClient(BASE_URL),
};

export const SUPPORTED_CHAIN_ID_TO_CHAIN_ID: Record<SupportedChainId, ChainId> =
{
[SupportedChainId.MAINNET]: ChainId.MAINNET,
[SupportedChainId.SEPOLIA]: ChainId.SEPOLIA,
[SupportedChainId.ARBITRUM_ONE]: ChainId.ARBITRUM_ONE,
[SupportedChainId.GNOSIS_CHAIN]: ChainId.GNOSIS_CHAIN,
};

export const BalancerChainName: Record<SupportedChainId, string> = {
[SupportedChainId.MAINNET]: "MAINNET",
[SupportedChainId.SEPOLIA]: "SEPOLIA",
Expand Down

0 comments on commit 8e5ea95

Please sign in to comment.