Skip to content

Commit

Permalink
Fix pool init by manually creating pool state
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Nov 29, 2024
1 parent 926adc6 commit 1b88f59
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 14 deletions.
14 changes: 13 additions & 1 deletion packages/nextjs/app/v3/_components/PoolDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ function DetailSection({ title, isValid, isEmpty, isPreview, content }: DetailSe

function TokenDetails({ token }: { token: TokenConfig }) {
const { poolType } = usePoolCreationStore();
const { targetNetwork } = useTargetNetwork();

const { data: boostableWhitelist } = useBoostableWhitelist();

Expand All @@ -225,7 +226,18 @@ function TokenDetails({ token }: { token: TokenConfig }) {
{token.rateProvider && token.rateProvider !== zeroAddress && (
<div className="flex gap-2 mt-1">
<i>Rate Provider:</i>
<div>{abbreviateAddress(token.rateProvider)}</div>
<div>
{" "}
<a
href={getBlockExplorerAddressLink(targetNetwork, token.rateProvider)}
target="_blank"
rel="noopener noreferrer"
className="flex items-center gap-2 text-info hover:underline"
>
{abbreviateAddress(token.rateProvider)}
<ArrowTopRightOnSquareIcon className="w-4 h-4" />
</a>
</div>
</div>
)}
</div>
Expand Down
56 changes: 45 additions & 11 deletions packages/nextjs/hooks/v3/useInitializePool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { BALANCER_ROUTER, InitPool, InitPoolDataProvider, InitPoolInput, balancerRouterAbi } from "@balancer/sdk";
import {
BALANCER_ROUTER,
InitPool,
InitPoolInput,
PoolState,
VAULT_V3,
balancerRouterAbi,
vaultExtensionAbi_V3,
} from "@balancer/sdk";
import { useMutation } from "@tanstack/react-query";
import { getContract, parseUnits } from "viem";
import { Address, getContract, parseUnits } from "viem";
import { usePublicClient, useWalletClient } from "wagmi";
import { useTransactor } from "~~/hooks/scaffold-eth";
import { useBoostableWhitelist, usePoolCreationStore } from "~~/hooks/v3";
Expand All @@ -23,17 +31,43 @@ export const useInitializePool = () => {
if (!poolType) throw new Error("Pool type missing");
if (!walletClient) throw new Error("Wallet client missing");

console.log("chainId", chainId);
console.log("rpcUrl", rpcUrl);
// const initPoolDataProvider = new InitPoolDataProvider(chainId, rpcUrl);
// const poolState = await initPoolDataProvider.getInitPoolData(poolAddress, poolType, protocolVersion);

const initPoolDataProvider = new InitPoolDataProvider(chainId, rpcUrl);

console.log("initPoolDataProvider", initPoolDataProvider);
console.log("poolAddress", poolAddress);
console.log("poolType", poolType);
console.log("protocolVersion", protocolVersion);
// TEMPORARILY MANUAL FETCH OF POOLSTATE
const vaultV3 = getContract({
abi: vaultExtensionAbi_V3,
address: VAULT_V3[chainId],
client: publicClient,
});
const poolTokens = (await vaultV3.read.getPoolTokens([poolAddress])) as Address[];

const tokens = poolTokens.map((token, idx) => {
const tokenConfig = tokenConfigs.find(t => {
const tokenConfigAddress = t.useBoostedVariant ? boostableWhitelist?.[t.address]?.address : t.address;
if (!tokenConfigAddress) throw new Error(`Problem with boosted variant for ${t.address}`);
return tokenConfigAddress.toLowerCase() === token.toLowerCase();
});
const decimals = tokenConfig?.tokenInfo?.decimals;
if (!decimals)
throw new Error(
`Missing decimals for token ${token}. Available tokens: ${tokenConfigs.map(t => t.address).join(", ")}`,
);
return {
address: token as `0x${string}`,
decimals,
index: idx,
};
});

const poolState = await initPoolDataProvider.getInitPoolData(poolAddress, poolType, protocolVersion);
const poolState: PoolState = {
id: poolAddress as `0x${string}`,
address: poolAddress as `0x${string}`,
type: poolType,
tokens,
protocolVersion,
};
// END TEMPORARY MANUAL FETCH OF POOLSTATE

const initPool = new InitPool();

Expand Down
8 changes: 6 additions & 2 deletions packages/nextjs/hooks/v3/useMultiSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,14 @@ export const useMultiSwap = () => {
});

console.log("logs", logs);

logs.forEach((log, idx) => {
// mintedShares is the amount, underlyingToken is an address
const { mintedShares, underlyingToken } = log.args;
const boostedToken = boostableWhitelist?.[underlyingToken.toLowerCase()];
const { mintedShares, wrappedToken } = log.args;
console.log("wrappedToken", wrappedToken);
const boostedToken = Object.values(boostableWhitelist ?? {}).find(
token => token.address.toLowerCase() === wrappedToken.toLowerCase(),
);
if (!boostedToken) throw new Error("Boosted token not found");
const amount = formatUnits(mintedShares, boostedToken?.decimals);
updateTokenConfig(idx, { amount });
Expand Down

0 comments on commit 1b88f59

Please sign in to comment.