Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/bridge #213

Merged
merged 18 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 2 additions & 51 deletions apps/bridge/src/components/Faq/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const faqList: Array<AccordionItemType> = [
testnet. It provides a safe environment to try out the bridge
functionality without any real token transactions.
<a
href="https://bridge.testnet.mantle.xyz"
href="https://bridge.sepolia.mantle.xyz"
target="__blank"
rel="noreferrer"
className="underline block mt-4"
Expand Down Expand Up @@ -179,55 +179,6 @@ const faqList: Array<AccordionItemType> = [
),
value: "faq-5",
},
{
trigger: (
<Typography
className="text-type-primary max-w-[90%]"
variant="microBody14"
>
How are withdrawal fees determined?
</Typography>
),
content: (
<div className="text-type-secondary max-w-[90%]">
<Typography className="mb-4">
The cost to claim on the bridge is dependent on the gas costs,
measured in Gwei, at the time of your transaction. The estimated
formula to determine this cost is:
</Typography>
<Typography className="mb-4">Cost (ETH)=600,000×Gwei</Typography>
<Typography>For example:</Typography>
<ul>
<li className="flex">
<span className="mr-4">•</span>
<Typography className="text-type-secondary">
At 15 Gwei, the cost is approximately 0.009 ETH.
</Typography>
</li>
<li className="flex">
<span className="mr-4">•</span>
<Typography className="text-type-secondary">
At 30 Gwei, the cost is approximately 0.018 ETH.
</Typography>
</li>
</ul>
<Typography>
To minimize your costs, you can opt to claim during a period when the
Gwei is low. For current gas prices, you can check the{" "}
<a
href="https://etherscan.io/gastracker"
target="__blank"
rel="noreferrer"
className="underline text-md"
>
Etherscan Gas Tracker
</a>
.
</Typography>
</div>
),
value: "faq-6",
},
{
trigger: (
<Typography
Expand All @@ -254,7 +205,7 @@ const faqList: Array<AccordionItemType> = [
</Typography>
</div>
),
value: "faq-7",
value: "faq-6",
},
];

Expand Down
50 changes: 50 additions & 0 deletions apps/bridge/src/components/bridge/CTA.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import { useIsWalletMultisig } from "@hooks/useIsWalletMultisig";
import Link from "next/link";
import Image from "next/image";

const tokensToRevoke = [
"0xdac17f958d2ee523a2206206994597c13d831ec7",
// "0xa1cd76d0a3e122f689e6bec548155e842c4a06a1",
// sepolia testing
];

// Contains a button & a modal to control allowance and deposits/withdrawals
export default function CTA({
selected,
Expand Down Expand Up @@ -117,6 +123,40 @@ export default function CTA({
)
) {
text = "Insufficient balance";
} else if (
tokensToRevoke.includes(selected.address.toLocaleLowerCase()) &&
direction === Direction.Deposit &&
fixDecimals(allowance).gt(0) &&
fixDecimals(allowance).lt(fixDecimals(destinationTokenAmount || "0"))
) {
text = (
<div className="flex flex-row gap-4 items-center mx-auto w-fit">
<span>{approvalStatus || "Reset allowance"}</span>
{approvalStatus ? (
<div role="status">
<svg
aria-hidden="true"
className="w-8 h-8 mr-2 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
) : (
""
)}
</div>
);
} else if (
fixDecimals(allowance || "-1").lt(
fixDecimals(destinationTokenAmount || "0")
Expand Down Expand Up @@ -173,6 +213,7 @@ export default function CTA({
chainId,
approvalStatus,
fixDecimals,
selected,
]);

// set wagmi address to address for ssr
Expand Down Expand Up @@ -233,6 +274,15 @@ export default function CTA({
onClick={() => {
if (!isChainID) {
switchToNetwork(chainId);
} else if (
selected.address &&
tokensToRevoke.includes(selected.address.toLocaleLowerCase()) &&
fixDecimals(allowance).gt(0) &&
fixDecimals(allowance).lt(
fixDecimals(destinationTokenAmount || "0")
)
) {
approve("0");
} else if (
fixDecimals(allowance || "-1").lt(
fixDecimals(destinationTokenAmount || "0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function useAllowanceCheck(
resetAllowance,
} as {
allowance: string;
resetAllowance: () => void;
resetAllowance: () => Promise<unknown>;
};
}

Expand Down
104 changes: 59 additions & 45 deletions apps/bridge/src/hooks/web3/bridge/write/useCallApprove.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,68 @@ export function useCallApprove(selected: Token) {
});

// construct a method to call and await an approval call to allocate allowance to the bridge
const approve = useCallback(async () => {
try {
// mark as waiting...
setApprovalStatus("Waiting for tx approval...");
// perform the tx call
const txRes = await writeApprove?.({
args: [
bridgeAddress as `0x${string}`,
BigInt(
parseUnits(
destinationTokenAmount || "1",
selected.decimals
).toString()
),
],
}).catch((e) => {
throw e;
});
// mark approval...
setApprovalStatus("Tx approved, waiting for confirmation...");
// wait for one confirmation
await provider
.waitForTransaction(txRes?.hash || "", 1)
.catch((e: any) => {
const approve = useCallback(
async (overwriteAmount?: string) => {
try {
// mark as waiting...
setApprovalStatus("Waiting for tx approval...");
// perform the tx call
const txRes = await writeApprove?.({
args: [
bridgeAddress as `0x${string}`,
BigInt(
parseUnits(
overwriteAmount || destinationTokenAmount || "1",
selected.decimals
).toString()
),
],
}).catch((e) => {
throw e;
});
// final update
setApprovalStatus("Tx settled");
} catch {
// log the approval was cancelled
setApprovalStatus("Approval cancelled");
} finally {
// call this to reset the allowance in the ui
resetAllowance();
// stop awaiting
setApprovalStatus(false);
}
// mark approval...
setApprovalStatus("Tx approved, waiting for confirmation...");
// wait for one confirmation
await provider
.waitForTransaction(txRes?.hash || "", 1)
.catch((e: any) => {
throw e;
});

// token is now approved
return true;
}, [
bridgeAddress,
destinationTokenAmount,
provider,
selected.decimals,
resetAllowance,
writeApprove,
]);
// final update
setApprovalStatus("Tx settled");

// delay for rpc update
await new Promise((resolve: (value: unknown) => void) => {
setTimeout(() => {
resolve(true);
}, 6000);
});
} catch {
// log the approval was cancelled
setApprovalStatus("Approval cancelled");
} finally {
// call this to reset the allowance in the ui
console.log("reseting allowance");
await resetAllowance();
console.log("reset allowance done");

// stop awaiting
setApprovalStatus(false);
}

// token is now approved
return true;
},
[
bridgeAddress,
destinationTokenAmount,
provider,
selected.decimals,
resetAllowance,
writeApprove,
]
);

return {
approvalStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export function useWaitForRelay({ direction }: { direction: Direction }) {
// if we got a receipt then we can reset everything and return
if (receipt.status) {
// call this to reset the allowance and balances in the ui
resetAllowance();
await resetAllowance();
resetBalances();
}

Expand Down
17 changes: 12 additions & 5 deletions apps/bridge/src/providers/stateContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export type StateProps = {
}) => void;
setSafeChains: (chains: number[]) => void;
resetBalances: () => void;
resetAllowance: () => void;
resetAllowance: () => Promise<unknown>;
refetchWithdrawals: () => void;
refetchDeposits: () => void;
loadMoreWithdrawals: () => void;
Expand Down Expand Up @@ -381,6 +381,10 @@ export function StateProvider({ children }: { children: React.ReactNode }) {
provider
);

useEffect(() => {
console.log({ allowance });
}, [allowance]);

// fetch the gas estimate for the selected operation on in the selected direction
const { actualGasFee, resetGasEstimate } = useGasEstimate(
chainId,
Expand Down Expand Up @@ -471,10 +475,13 @@ export function StateProvider({ children }: { children: React.ReactNode }) {
// reset the allowances and balances once we gather enough intel to make the calls
useEffect(
() => {
resetAllowance();
resetBalances();
refetchL1FeeData();
refetchL2FeeData();
const reset = async () => {
await resetAllowance();
resetBalances();
refetchL1FeeData();
refetchL2FeeData();
};
reset();
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[chainId, client?.address, selectedToken, multicall, bridgeAddress]
Expand Down
Loading