4

I have following code so far.

import { useSubstrateState } from './../substrate-lib'
import { web3FromSource } from '@polkadot/extension-dapp'

const { api, currentAccount } = useSubstrateState()    
const getFromAcct = async () => {
        const {
          address,
          meta: { source, isInjected },
        } = currentAccount
    
        if (!isInjected) {
          return [currentAccount]
        }
    
      
        const injector = await web3FromSource(source)
        return [address, { signer: injector.signer }]
      }

And transaction as:

const fromAcct = await getFromAcct()
const unsub = await txExecute
              .signAndSend(...fromAcct, ({ status, events }) => {
                txResHandler(status, events)
              })
              .catch(txErrHandler)

But it doesn't call the polkadot extension.
It gives error Cannot destructure property 'address' of 'currentAccount' as it is null.

2 Answers 2

11

You issue lies here -

const {
  address,
  meta: { source, isInjected },
} = currentAccount

As the error suggests, in this case currentAccount === null, so trying to extract address from it is invalid.

Either check for the null value before attempting to deconstruct, or don't allow null returns for currentAccount from useSubstrateState()

2
0

You should probably try the web3Accounts method first.

According to @polkadot/extension-dapp package's document, here are the differences

  • web3Accounts(): Promise<InjectedAccountWithMeta[]> - returns a list of all the injected accounts, accross all extensions (source in meta)
  • web3FromSource(name: string): Promise<InjectedExtension> - Retrieves a provider identified by the name

If you choose web3FromSource, make sure you pass to it the correct extension name.

In the case of Polkadot.js extension, the name should be polkadot-js:

const injector = await web3FromSource('polkadot-js');

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.