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

Add support for custom chains in changeNetwork function #425

Open
wants to merge 10 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
8 changes: 8 additions & 0 deletions .changeset/large-meals-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@aptos-labs/wallet-adapter-react": minor
"@aptos-labs/wallet-adapter-core": minor
"@aptos-labs/wallet-adapter-vue": minor
"@aptos-labs/wallet-adapter-nextjs-example": minor
---

Support for custom chains in changeNetwork
35 changes: 33 additions & 2 deletions apps/nextjs-example/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from "@aptos-labs/wallet-adapter-react";
import { AlertCircle } from "lucide-react";
import Image from "next/image";
import { useState } from "react";

// Imports for registering a browser extension wallet plugin on page load
import { MyWallet } from "@/utils/standardWallet";
Expand Down Expand Up @@ -144,7 +145,7 @@ interface WalletConnectionProps {
account: AccountInfo | null;
network: NetworkInfo | null;
wallet: WalletInfo | null;
changeNetwork: (network: Network) => Promise<AptosChangeNetworkOutput>;
changeNetwork: (network: Network, chainId?: string) => Promise<AptosChangeNetworkOutput>;
}

function WalletConnection({
Expand All @@ -162,6 +163,8 @@ function WalletConnection({
return true;
};

const [customChainId, setCustomChainId] = useState<string>("");

// TODO: Do a proper check for network change support
const isNetworkChangeSupported = wallet?.name === "Nightly";

Expand Down Expand Up @@ -292,7 +295,17 @@ function WalletConnection({
value={network?.name}
orientation="horizontal"
className="flex gap-6"
onValueChange={(value: Network) => changeNetwork(value)}
onValueChange={(value: Network) => {
if (value === Network.CUSTOM && customChainId && Number(customChainId) > 0) {
changeNetwork(value, customChainId);
} else if (value !== Network.CUSTOM) {
changeNetwork(value);
} else {
console.warn("Invalid chainId");
}
}}


disabled={!isNetworkChangeSupported}
>
<div className="flex items-center space-x-2">
Expand All @@ -307,6 +320,24 @@ function WalletConnection({
<RadioGroupItem value={Network.MAINNET} id="mainnet-radio" />
<Label htmlFor="mainnet-radio">Mainnet</Label>
</div>

<div className="flex items-center space-x-2">
<RadioGroupItem
value={Network.CUSTOM}
id="custom-radio"
disabled={!customChainId || Number(customChainId) <= 0}
/>

<Label htmlFor="custom-radio">Custom</Label>

<input
type="number"
className="ml-2 border rounded px-2 py-1"
value={customChainId}
onChange={(e) => setCustomChainId(e.target.value)}
placeholder="Enter chainId"
/>
</div>
</RadioGroup>
{!isNetworkChangeSupported && (
<div className="text-sm text-red-600 dark:text-red-400">
Expand Down
8 changes: 6 additions & 2 deletions packages/wallet-adapter-core/src/WalletCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1161,17 +1161,21 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
* @param network
* @returns AptosChangeNetworkOutput
*/
async changeNetwork(network: Network): Promise<AptosChangeNetworkOutput> {
async changeNetwork(
network: Network,
customChainId?: string
): Promise<AptosChangeNetworkOutput> {
try {
this.ensureWalletExists(this._wallet);
this.recordEvent("change_network_request", {
from: this._network?.name,
to: network,
});

const chainId =
network === Network.DEVNET
? await fetchDevnetChainId()
: NetworkToChainId[network];
: (network === Network.CUSTOM && customChainId) ? Number(customChainId) : NetworkToChainId[network];
if (this._wallet.changeNetwork) {
const networkInfo: StandardNetworkInfo = {
name: network,
Expand Down
7 changes: 4 additions & 3 deletions packages/wallet-adapter-react/src/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,19 @@ export const AptosWalletAdapterProvider: FC<AptosWalletProviderProps> = ({
}
};

const changeNetwork = async (network: Network) => {
const changeNetwork = async (network: Network, chainId?: string) => {
if (!walletCore) {
throw new Error("WalletCore is not initialized");
}

try {
return await walletCore?.changeNetwork(network);
return await walletCore.changeNetwork(network, chainId);
} catch (error: any) {
if (onError) onError(error);
return Promise.reject(error);
}
};

useEffect(() => {
if (autoConnect) {
if (localStorage.getItem("AptosWalletName") && !connected) {
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet-adapter-react/src/useWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface WalletContextState {
): Promise<PendingTransactionResponse>;
signMessage(message: SignMessagePayload): Promise<SignMessageResponse>;
signMessageAndVerify(message: SignMessagePayload): Promise<boolean>;
changeNetwork(network: Network): Promise<AptosChangeNetworkOutput>;
changeNetwork(network: Network, chainId?: string): Promise<AptosChangeNetworkOutput>;
}

const DEFAULT_CONTEXT = {
Expand Down
7 changes: 4 additions & 3 deletions packages/wallet-adapter-vue/src/composables/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface WalletContextState {
): Promise<PendingTransactionResponse>;
signMessage(message: SignMessagePayload): Promise<SignMessageResponse>;
signMessageAndVerify(message: SignMessagePayload): Promise<boolean>;
changeNetwork(network: Network): Promise<AptosChangeNetworkOutput>;
changeNetwork(network: Network, chainId?: string): Promise<AptosChangeNetworkOutput>;
}

export interface AptosWalletProviderProps {
Expand Down Expand Up @@ -197,9 +197,10 @@ export function useWallet(
}
};

const changeNetwork = async (network: Network) => {
const changeNetwork = async (network: Network, chainId?: string) => {
try {
return await walletCoreInstance.changeNetwork(network);
// Call changeNetwork with chainId if provided, otherwise call without it
await walletCoreInstance.changeNetwork(network, chainId)
} catch (error: any) {
if (onError) onError(error);
return Promise.reject(error);
Expand Down
Loading