Connect to Galactica Snap

Before the Galactica Snap can be installed, we need to check that Metamask is available in the user's browser. This is similar to a standard Metamask connection.

If Metamask is not installed, you can forward the user with the connect button to the install page:

export const InstallMetamaskButton = () => (
  <Link href="https://metamask.io/" target="_blank">
    <FlaskFox />
    <ButtonText>Install MetaMask</ButtonText>
  </Link>
);

With Metamask present, the user can connect to the Galactica Snap and install it if necessary (missing or outdated) with the following function:

const defaultSnapOrigin = "npm:@galactica-corp/snap";

await window.ethereum.request({
  method: 'wallet_requestSnaps',
  params: {
    [defaultSnapOrigin]: {},
  },
});

You can verify if the Snap is installed:

/**
 * Get the snap from MetaMask.
 *
 * @returns The snap object returned by the extension.
 */
export const getSnap = async (): Promise<Snap | undefined> => {
  try {
    const snaps = await window.ethereum.request({
      method: 'wallet_getSnaps',
    });

    return Object.values(snaps).find(
      (snap) => snap.id === defaultSnapOrigin,
    );
  } catch (error) {
    console.log('Failed to obtain installed snap', error);
    return undefined;
  }
};

Now you should be able to use Galactica specific functions through the snap using the wallet_invokeSnap method. See the next page for an example

Last updated