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

feat: ability to switch chains in the swap component #15

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 13 additions & 9 deletions src/components/Swap/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ interface SwapLayoutProps {
sendDisabled: boolean;
isSending: boolean;
sendButtonText: string;
switchToRightChain: () => any;
isSwitchingChainsPending: boolean;
fadeev marked this conversation as resolved.
Show resolved Hide resolved
}

const SwapLayout: React.FC<SwapLayoutProps> = ({
Expand Down Expand Up @@ -93,6 +95,8 @@ const SwapLayout: React.FC<SwapLayoutProps> = ({
sendDisabled,
isSending,
sendButtonText,
switchToRightChain,
isSwitchingChainsPending,
}) => {
const [sourceTokenOpen, setSourceTokenOpen] = useState(false);
const [destinationTokenOpen, setDestinationTokenOpen] = useState(false);
Expand Down Expand Up @@ -368,17 +372,17 @@ const SwapLayout: React.FC<SwapLayoutProps> = ({
) : (
<Button
variant="outline"
disabled={true}
// disabled={
// isLoading && pendingChainId === sourceTokenSelected.chain_id
// }
onClick={switchToRightChain}
disabled={isSwitchingChainsPending}
>
{/* {isLoading && pendingChainId === sourceTokenSelected.chain_id ? (
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
{isSwitchingChainsPending ? (
<>
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
<span>Switching to {sourceTokenSelected.chain_name}</span>
</>
) : (
<RefreshCcw className="h-4 w-4 mr-2" />
)} */}
Switch Network
"Switch Network"
)}
</Button>
)}
</div>
Expand Down
10 changes: 9 additions & 1 deletion src/components/Swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { computeSendType, sendTypeDetails } from "./hooks/computeSendType";
import useSwapErrors from "./hooks/useSwapErrors";
import useTokenSelection from "./hooks/useTokenSelection";
import { formatAddress } from "@/lib/utils";
import { useAccount } from "wagmi";
import { useAccount, useSwitchChain } from "wagmi";
import { useZetaChainClient } from "@/providers/UniversalKitProvider";
import { useBitcoinWallet } from "@/index";

Expand All @@ -30,6 +30,7 @@ export const Swap: React.FC<SwapProps> = ({
config,
}) => {
const { address, chainId } = useAccount();
const { switchChain, isPending: isSwitchingChainsPending } = useSwitchChain();
const { address: bitcoin } = useBitcoinWallet();
const client = useZetaChainClient();

Expand Down Expand Up @@ -80,6 +81,11 @@ export const Swap: React.FC<SwapProps> = ({
client
);

const switchToRightChain = () => {
sourceTokenSelected &&
switchChain({ chainId: Number(sourceTokenSelected.chain_id) });
};

const { isAmountGTFee, isAmountLTBalance } = useAmountValidation(
sourceTokenSelected,
destinationTokenSelected,
Expand Down Expand Up @@ -195,6 +201,8 @@ export const Swap: React.FC<SwapProps> = ({
sendDisabled={sendDisabled}
isSending={isSending}
sendButtonText={sendButtonText}
switchToRightChain={switchToRightChain}
isSwitchingChainsPending={isSwitchingChainsPending}
/>
</div>
);
Expand Down
16 changes: 14 additions & 2 deletions src/wagmi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { getDefaultConfig } from "@rainbow-me/rainbowkit";
import { bscTestnet, sepolia, zetachainAthensTestnet } from "wagmi/chains";
import {
bscTestnet,
sepolia,
zetachainAthensTestnet,
baseSepolia,
polygonAmoy,
} from "wagmi/chains";

export const config = getDefaultConfig({
appName: "RainbowKit demo",
projectId: "YOUR_PROJECT_ID",
chains: [sepolia, bscTestnet, zetachainAthensTestnet],
chains: [
sepolia,
bscTestnet,
zetachainAthensTestnet,
baseSepolia,
polygonAmoy,
],
ssr: false,
});
Loading