Skip to content

Commit

Permalink
Merge pull request #4 from aurora-is-near/ycg/switch-chain-before-add…
Browse files Browse the repository at this point in the history
…ing-token-hide

feat: switch chain before changing token
  • Loading branch information
yellowCrimsonGator authored Oct 4, 2024
2 parents 54bd48f + 919725b commit cc6f6a1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/app/(index)/ConnectWalletButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { useCallback } from "react";
import Link from "next/link";
import Avatar, { ethWalletAvatarInputFormatter } from "@/components/Avatar";
import Button from "@/components/Button";
import { useWalletSelector } from "@/contexts/WalletSelectorContext";
import {
ETHEREUM_WALLETS_CONNECTOR,
useWalletSelector,
} from "@/contexts/WalletSelectorContext";
import midTruncate from "@/utils/midTruncate";
import { InjectedWalletBehaviour } from "@near-wallet-selector/core";
import { Menu, MenuButton, MenuItem, MenuItems } from "@headlessui/react";
Expand Down Expand Up @@ -33,7 +36,7 @@ const ConnectWalletButton = () => {

const connectWithEth = useCallback(async () => {
try {
const wallet = await selector.wallet("ethereum-wallets");
const wallet = await selector.wallet(ETHEREUM_WALLETS_CONNECTOR);
(wallet as InjectedWalletBehaviour).signIn({ contractId: "" });
} catch (e) {
console.error(e);
Expand Down
9 changes: 2 additions & 7 deletions src/app/account/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { useCallback, useEffect, useMemo, useState } from "react";
import { useCallback, useEffect, useState } from "react";
import {
AccessKeyInfoView,
FunctionCallPermissionView,
Expand All @@ -13,7 +13,7 @@ import { Card, CardPadding } from "@/components/Card";
import { InformationCircleIcon } from "@heroicons/react/20/solid";

const AccountPage = () => {
const { accountId, selector } = useWalletSelector();
const { accountId, selector, isEthereumWallet } = useWalletSelector();
const [dappAccessKeys, setDappAccessKeys] = useState<AccessKeyInfoView[]>([]);
const [onboardingAccessKeys, setOnboardingAccessKeys] = useState<
AccessKeyInfoView[]
Expand Down Expand Up @@ -41,11 +41,6 @@ const AccountPage = () => {
updateAccessKeys();
}, [updateAccessKeys]);

const isEthereumWallet = useMemo(
() => selector.store.getState().selectedWalletId === "ethereum-wallets",
[selector, accountId]
);

const onDeauthorize = useCallback(
async (publicKey: string) => {
if (!accountId) {
Expand Down
6 changes: 4 additions & 2 deletions src/components/AddTokenIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import Tooltip from "./Tooltip";
import { PlusCircleIcon } from "@heroicons/react/24/outline";
import { addTokenToWallet } from "@/utils/addTokenToWallet";
import { useWalletSelector } from "@/contexts/WalletSelectorContext";

export default function AddTokenIcon({
contract,
Expand All @@ -12,12 +13,13 @@ export default function AddTokenIcon({
symbol: string;
decimals: number;
}) {
return (
const { isEthereumWallet } = useWalletSelector();
return isEthereumWallet ? (
<Tooltip content="Add token to wallet" tooltipClassname="w-fit text-nowrap">
<PlusCircleIcon
className="h-5 w-5 text-sand-8 group-hover:text-sand-11"
onClick={() => addTokenToWallet(contract, symbol, decimals)}
/>
</Tooltip>
);
) : null;
}
6 changes: 6 additions & 0 deletions src/contexts/WalletSelectorContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ interface WalletSelectorContextValue {
accounts: Array<AccountState>;
accountId: string | null;
loading: boolean;
isEthereumWallet: boolean;
}

export const ETHEREUM_WALLETS_CONNECTOR = "ethereum-wallets";
const WalletSelectorContext =
React.createContext<WalletSelectorContextValue | null>(null);

Expand Down Expand Up @@ -207,6 +209,10 @@ export const WalletSelectorContextProvider: React.FC<{
account,
accountId,
loading,
isEthereumWallet: selector
? selector.store.getState().selectedWalletId ===
ETHEREUM_WALLETS_CONNECTOR
: false,
};
}, [selector, modal, accounts, loading]);

Expand Down
3 changes: 2 additions & 1 deletion src/utils/addTokenToWallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { keccak256, toHex } from "viem";
import { watchAsset } from "viem/actions";
import { watchAsset, switchChain } from "viem/actions";
import { getConnectorClient } from "@wagmi/core";
import { wagmiConfig } from "@/contexts/WalletSelectorContext";
import { getTokenMetaData } from "@/utils/near";
Expand All @@ -17,6 +17,7 @@ export async function addTokenToWallet(
}

const walletClient = await getConnectorClient(wagmiConfig);
await switchChain(walletClient, { id: Number(process.env.chainId) });
const address = playgroundComputeAddress(contractId);

watchAsset(walletClient, {
Expand Down

0 comments on commit cc6f6a1

Please sign in to comment.