-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from balancer/improve-safe-support
Improve safe support
- Loading branch information
Showing
23 changed files
with
499 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
export * from "./types"; | ||
export * from "./useReadPool"; | ||
export * from "./useCheckIfPoolExists"; | ||
export * from "./useCreatePool"; | ||
export * from "./useFinalizePool"; | ||
export * from "./useBindToken"; | ||
export * from "./useSetSwapFee"; | ||
export * from "./getPoolUrl"; | ||
export * from "./usePoolCreationStore"; | ||
export * from "./useFetchPoolAddress"; | ||
export * from "./useCreatePool"; | ||
export * from "./useCreatePoolTxHash"; | ||
export * from "./useApproveTokenTxHash"; | ||
export * from "./useBindTokenTxHash"; | ||
export * from "./useSetSwapFeeTxHash"; | ||
export * from "./useFinalizePoolTxHash"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { erc20Abi } from "@balancer/sdk"; | ||
import { useSafeAppsSDK } from "@safe-global/safe-apps-react-sdk"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { parseEventLogs, parseUnits } from "viem"; | ||
import { usePublicClient } from "wagmi"; | ||
import { usePoolCreationStore } from "~~/hooks/cow/usePoolCreationStore"; | ||
import { useIsSafeWallet } from "~~/hooks/safe/useIsSafeWallet"; | ||
import { pollSafeTxStatus } from "~~/utils/safe"; | ||
|
||
interface UseApproveTokenTxHashProps { | ||
tokenNumber: 1 | 2; | ||
} | ||
|
||
export function useApproveTokenTxHash({ tokenNumber }: UseApproveTokenTxHashProps) { | ||
const publicClient = usePublicClient(); | ||
const { sdk } = useSafeAppsSDK(); | ||
const isSafeWallet = useIsSafeWallet(); | ||
|
||
const { poolCreation, updatePoolCreation } = usePoolCreationStore(); | ||
const txKey = `approveToken${tokenNumber}Tx` as const; | ||
const { safeHash, wagmiHash, isSuccess } = poolCreation?.[txKey] || {}; | ||
|
||
return useQuery({ | ||
queryKey: [`approveToken${tokenNumber}TxHash`, safeHash, wagmiHash, isSuccess], | ||
queryFn: async () => { | ||
if (!publicClient) throw new Error("No public client for fetching pool address"); | ||
|
||
if (isSafeWallet && safeHash && !wagmiHash) { | ||
const hash = await pollSafeTxStatus(sdk, safeHash); | ||
updatePoolCreation({ [txKey]: { safeHash, wagmiHash: hash, isSuccess: false } }); | ||
return null; | ||
} | ||
|
||
if (!wagmiHash) return null; | ||
|
||
const txReceipt = await publicClient.waitForTransactionReceipt({ hash: wagmiHash }); | ||
|
||
if (txReceipt.status === "success") { | ||
const amount = poolCreation?.[`token${tokenNumber}Amount`]; | ||
const decimals = poolCreation?.[`token${tokenNumber}`].decimals; | ||
if (!amount || !decimals) throw new Error(`Missing info for token ${tokenNumber}`); | ||
|
||
const rawAmount = parseUnits(amount, decimals); | ||
const logs = parseEventLogs({ | ||
abi: erc20Abi, | ||
logs: txReceipt.logs, | ||
}); | ||
|
||
const approvalEvent = logs.find(log => log.eventName === "Approval"); | ||
if (!approvalEvent) throw new Error("No Approval event found in logs"); | ||
|
||
const newAllowance = approvalEvent.args.value; | ||
if (newAllowance < rawAmount) throw new Error(`Approval amount for token ${tokenNumber} is less than required`); | ||
|
||
updatePoolCreation({ | ||
[txKey]: { safeHash, wagmiHash, isSuccess: true }, | ||
step: poolCreation.step + 1, | ||
}); | ||
return { isSuccess: true }; | ||
} else { | ||
throw new Error("Approve token transaction reverted"); | ||
} | ||
}, | ||
enabled: Boolean(!isSuccess && (safeHash || wagmiHash)), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useSafeAppsSDK } from "@safe-global/safe-apps-react-sdk"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { usePublicClient } from "wagmi"; | ||
import { usePoolCreationStore } from "~~/hooks/cow/usePoolCreationStore"; | ||
import { useIsSafeWallet } from "~~/hooks/safe/useIsSafeWallet"; | ||
import { pollSafeTxStatus } from "~~/utils/safe"; | ||
|
||
interface UseBindTokenTxHashProps { | ||
tokenNumber: 1 | 2; | ||
} | ||
|
||
export function useBindTokenTxHash({ tokenNumber }: UseBindTokenTxHashProps) { | ||
const publicClient = usePublicClient(); | ||
const { sdk } = useSafeAppsSDK(); | ||
const isSafeWallet = useIsSafeWallet(); | ||
|
||
const { poolCreation, updatePoolCreation } = usePoolCreationStore(); | ||
const txKey = `bindToken${tokenNumber}Tx` as const; | ||
const { safeHash, wagmiHash, isSuccess } = poolCreation?.[txKey] || {}; | ||
|
||
return useQuery({ | ||
queryKey: [`bindToken${tokenNumber}TxHash`, safeHash, wagmiHash, isSuccess], | ||
queryFn: async () => { | ||
if (!publicClient) throw new Error("No public client for fetching pool address"); | ||
|
||
if (isSafeWallet && safeHash && !wagmiHash) { | ||
const hash = await pollSafeTxStatus(sdk, safeHash); | ||
updatePoolCreation({ [txKey]: { safeHash, wagmiHash: hash, isSuccess: false } }); | ||
return null; | ||
} | ||
|
||
if (!wagmiHash) return null; | ||
|
||
const txReceipt = await publicClient.waitForTransactionReceipt({ hash: wagmiHash }); | ||
|
||
if (txReceipt.status === "success") { | ||
if (!poolCreation?.step) throw new Error("Missing pool creation step"); | ||
|
||
updatePoolCreation({ | ||
[txKey]: { safeHash, wagmiHash, isSuccess: true }, | ||
step: poolCreation.step + 1, | ||
}); | ||
return { isSuccess: true }; | ||
} else { | ||
throw new Error("Bind token transaction reverted"); | ||
} | ||
}, | ||
enabled: Boolean(!isSuccess && (safeHash || wagmiHash)), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useSafeAppsSDK } from "@safe-global/safe-apps-react-sdk"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { parseEventLogs } from "viem"; | ||
import { usePublicClient } from "wagmi"; | ||
import { abis } from "~~/contracts/abis"; | ||
import { usePoolCreationStore } from "~~/hooks/cow/usePoolCreationStore"; | ||
import { useIsSafeWallet } from "~~/hooks/safe/useIsSafeWallet"; | ||
import { pollSafeTxStatus } from "~~/utils/safe"; | ||
|
||
export function useCreatePoolTxHash() { | ||
const isSafeWallet = useIsSafeWallet(); | ||
const publicClient = usePublicClient(); | ||
const { sdk } = useSafeAppsSDK(); | ||
const { poolCreation, updatePoolCreation } = usePoolCreationStore(); | ||
const { createPoolTx, poolAddress } = poolCreation || {}; | ||
const { safeHash, wagmiHash, isSuccess } = createPoolTx || {}; | ||
|
||
return useQuery({ | ||
queryKey: ["cowPoolAddress", safeHash, wagmiHash, isSuccess], | ||
queryFn: async () => { | ||
if (!publicClient) throw new Error("No public client for fetching pool address"); | ||
|
||
// If safe wallet, poll for safe tx status to update createPoolTxHash | ||
if (isSafeWallet && safeHash && !wagmiHash) { | ||
const hash = await pollSafeTxStatus(sdk, safeHash); | ||
updatePoolCreation({ createPoolTx: { safeHash, wagmiHash: hash, isSuccess: false } }); | ||
return null; // Trigger a re-query with the new createPoolTxHash | ||
} | ||
|
||
if (!wagmiHash) return null; | ||
|
||
const txReceipt = await publicClient.waitForTransactionReceipt({ hash: wagmiHash }); | ||
|
||
if (txReceipt.status === "success") { | ||
const logs = parseEventLogs({ | ||
abi: abis.CoW.BCoWFactory, | ||
logs: txReceipt.logs, | ||
}); | ||
|
||
const newPoolAddress = (logs[0].args as { caller: string; bPool: string }).bPool; | ||
if (!newPoolAddress) throw new Error("No new pool address from pool creation tx receipt"); | ||
|
||
updatePoolCreation({ | ||
createPoolTx: { safeHash, wagmiHash, isSuccess: true }, | ||
poolAddress: newPoolAddress, | ||
step: 2, | ||
}); | ||
return newPoolAddress; | ||
} else { | ||
throw new Error("Create pool transaction reverted"); | ||
} | ||
}, | ||
enabled: Boolean(!poolAddress && (safeHash || wagmiHash)), | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.