0

GM! Im trying to initialice keyring, but for some reason its already inicialized.

This is my code:

"use client"
import React, { useState, useEffect, createContext, useContext } from "react";
import { ApiPromise, WsProvider } from "@polkadot/api";
import { web3Accounts, web3Enable, web3FromAddress, web3FromSource } from "@polkadot/extension-dapp";
import keyring from "@polkadot/ui-keyring";
import { stringToHex } from "@polkadot/util";

const PolkadotContext = createContext();

const APP_NAME = "MV_DAPP";

export const PolkadotProvider = ({ children }) => {
  const [api, setApi] = useState(null);
  const [accounts, setAccounts] = useState([]);
  const [selectedAccount, setSelectedAccount] = useState();
  const [isKeyringInitialized, setIsKeyringInitialized] = useState(false);

  const loadApi = async () => {
    if(api === null) {
      const wsProvider = new WsProvider(
        "wss://xxxxxxx-rpc.a.stagenet.tanssi.network"
      );
     
      const _api = ApiPromise.create({ provider: wsProvider }).then(() => {
        if (isKeyringInitialized === false) {
          keyring.loadAll({ ss58Format: 42, type: 'sr25519' })
          setIsKeyringInitialized(true)
        }  
      });
      setApi(_api);
    }
  }

  const handleConnect = async () => {
    const extensions = await web3Enable(APP_NAME);

    if (!extensions) {
      throw Error("No_Extension_Found")
    };

    const allAccounts = await web3Accounts();

    setAccounts(allAccounts);

    console.log(accounts);

    if (allAccounts === 1) {
      setSelectedAccount(allAccounts[0])
    }
    console.log(selectedAccount)
  }

  useEffect(() => {
    if(!api){
      loadApi();
    }
  })

  
  //NFT minting functions
  const handleCreateNFT = async () => {
    const adminAddress = selectedAccount.address;
    const injector = await web3FromAddress(adminAddress);
    const signer = await injector.signer;
    console.log(signer);
    console.log(injector)
    
    if (!api) {
      console.log("NO_API")
      loadApi();
      console.log(api)
    }

    if (!selectedAccount) {
      console.log("NO_SELECTED_ACCOUNT")
    } }

   
  
  

  const contextValue = { api, accounts, handleConnect, setSelectedAccount, selectedAccount, handleCreateNFT };

  return (
    <PolkadotContext.Provider value={contextValue}>
      {children}
    </PolkadotContext.Provider>
  );
};


export const usePolkadot = () => useContext(PolkadotContext);

And this is the error:

index.js:36 Uncaught (in promise) Error: Unable to initialise options more than once
2
  • Could you provide the full code? But it's clearly that you initialize the instances multiple times.
    – AurevoirXavier
    Commented May 6 at 10:30
  • Just added the full code in the question. Tks. Commented May 8 at 7:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.