NEAR Guest Wallet Widget

Increase user engagement with this React component that sets up a NEAR guest wallet for your visitors on their first load.

Give your visitors a frictionless experience on performing on-chain transactions and using smart-contracts without the need of their own wallet. Perfect for beginners and for new product launches! 👌

Getting Started

Follow the Project Setup instructions to start the project in your local machine and try the recipes yourself. You can also clone or navigate the repo to get just what you need.

Creating the Guest Wallet

This code needs to be executed on the server-side of your application, since creating new NEAR sub-accounts require you to provide the private keys of a parent account.

The clue is to return a JSON object that mimics the Wallet Selector localStorage format so that it would pick the expected guest-wallet when a user enters.

If the new account will interact with an NEP141 fungible token, you'll need to register the account and transfer the minimum NEAR to activate the account with the source NEP141 contract.

Create a Guest Wallet Selector Module

When you setup the NEAR Wallet Selector modules, you'll want to insert your own instance of a guest-wallet module so that you have control on the ID and the way it connects your guest user.

See Calling the API Endpoint for full implementation.

const walletSelector = await setupWalletSelector({
          network,
          modules: [
            setupGuestWallet(), <== custom module here
            setupNearWallet(),
            setupMyNearWallet(),
            setupNearFi(),
            setupHereWallet(),
            setupMathWallet(),
            setupSender(),
            setupNightly(),
            setupMeteorWallet(),
            setupLedger(),
            setupCoin98Wallet(),
          ],
        });

Calling the API endpoint

The next step is to GET the endpoint JSON object and parse it to set the localStorage properties, see line 188+ and how it waits for selector to exist first. Then call initGuestConnection to initialize a new NEAR Wallet Connection.

This new Wallet connection will be stored in the WalletStateContextController, so that it can be used in your components:

Using the Guest Wallet Connection Instance

Now that a React context state has a wallet instance, you can use it to display values on a widget component.

See WalletStateContextController.tsx to learn how to use this React context pattern.

Last updated