-
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 #49 from bleu/withdraw-gas-limit-fix
Withdraw gas limit fix
- Loading branch information
Showing
10 changed files
with
123 additions
and
94 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
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
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
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,12 +1,17 @@ | ||
import type { SupportedChainId } from "@cowprotocol/cow-sdk"; | ||
|
||
import { usePools } from "@bleu/cow-hooks-ui"; | ||
import { BigNumber } from "ethers"; | ||
import type { Address } from "viem"; | ||
|
||
export function useUserPools(chainId?: SupportedChainId, user?: Address) { | ||
return usePools( | ||
const useSwrData = usePools( | ||
{ poolTypeIn: ["COW_AMM"], userAddress: user }, | ||
chainId, | ||
"userbalanceUsd", | ||
); | ||
const data = useSwrData.data?.filter((pool) => | ||
BigNumber.from(pool.userBalance.walletBalance).gt(BigNumber.from("10")), | ||
); | ||
return { ...useSwrData, data }; | ||
} |
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,28 @@ | ||
import { COW_PROTOCOL_SETTLEMENT_CONTRACT_ADDRESS } from "@cowprotocol/cow-sdk"; | ||
import type { CowHook } from "@cowprotocol/hook-dapp-lib"; | ||
import { useCallback } from "react"; | ||
import type { Address } from "viem"; | ||
import { useIFrameContext } from "../context/iframe"; | ||
import { retryAsync } from "../utils/retry"; | ||
|
||
export function useEstimateGas() { | ||
const { context, actions, publicClient } = useIFrameContext(); | ||
return useCallback( | ||
async (hook: Omit<CowHook, "gasLimit" | "dappId">) => { | ||
if (!context || !actions || !publicClient) | ||
throw new Error("Missing context"); | ||
|
||
return await retryAsync<bigint>(() => { | ||
return publicClient.estimateGas({ | ||
account: COW_PROTOCOL_SETTLEMENT_CONTRACT_ADDRESS[ | ||
context.chainId | ||
] as `0x${string}`, | ||
to: hook.target as Address, | ||
value: BigInt("0"), | ||
data: hook.callData as `0x${string}`, | ||
}); | ||
}); | ||
}, | ||
[context, actions, publicClient], | ||
); | ||
} |
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,40 @@ | ||
type RetryOptions = { | ||
maxAttempts?: number; | ||
delayMs?: number; | ||
onRetry?: (error: Error, attempt: number) => void; | ||
}; | ||
|
||
export async function retryAsync<T>( | ||
fn: () => Promise<T>, | ||
options: RetryOptions = {}, | ||
): Promise<T> { | ||
const { | ||
maxAttempts = 5, | ||
delayMs = 1000, | ||
onRetry = (error, attempt) => | ||
console.warn( | ||
`Attempt ${attempt} failed with error: ${error.message}. Retrying...`, | ||
), | ||
} = options; | ||
|
||
let lastError: Error | null = null; | ||
|
||
for (let attempt = 1; attempt <= maxAttempts; attempt++) { | ||
try { | ||
return await fn(); | ||
} catch (error) { | ||
lastError = error instanceof Error ? error : new Error(String(error)); | ||
|
||
if (attempt === maxAttempts) { | ||
break; | ||
} | ||
|
||
onRetry(lastError, attempt); | ||
await new Promise((resolve) => setTimeout(resolve, delayMs)); | ||
} | ||
} | ||
|
||
throw new Error( | ||
`Failed after ${maxAttempts} attempts. Last error: ${lastError?.message}`, | ||
); | ||
} |