Why call eth_accounts twice?

Open
#413 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Refactor
Clarity
Needs clarification
Activity status
Stale
Tech stack
javascript
Domain
frontend

Research direction

Start by tracing initializeProvider and setActiveProviderDetail, then compare each eth_accounts request and its call to handleNewAccounts. Determine whether the repeated request is necessary and document or remove the duplication without changing account initialization behavior.

Written by the indexing model from the issue text.

Description

const initializeProvider = async () => {
  initializeContracts();
  updateFormElements();

  if (isMetaMaskInstalled()) {
    globalContext.provider.autoRefreshOnNetworkChange = false;
    await getNetworkAndChainId();

    globalContext.provider.on('chainChanged', handleNewChain);
    globalContext.provider.on('chainChanged', handleEIP1559Support);
    globalContext.provider.on('networkChanged', handleNewNetwork);
    globalContext.provider.on('accountsChanged', handleNewAccounts);
    globalContext.provider.on('accountsChanged', handleEIP1559Support);

    try {
      const newAccounts = await globalContext.provider.request({
        method: 'eth_accounts',
      });
      handleNewAccounts(newAccounts);
    } catch (err) {
      console.error('Error on init when getting accounts', err);
    }
  } else {
    handleScrollTo();
  }
};

export const setActiveProviderDetail = async (providerDetail) => {
  closeProvider();
  console.log(providerDetail);
  // When the extension is not installed the providerDetails comes in undefined
  // but because the SDK is already init the window.ethereum has been injected
  // this doesn't mean we can refer to it directly as the connection may have
  // not been approved which is there uuid comes in as empty
  if (!providerDetail || providerDetail.info.uuid === '') {
    return;
  }
  globalContext.provider = providerDetail.provider;
  await initializeProvider();

  try {
    const newAccounts = await globalContext.provider.request({
      method: 'eth_accounts',
    });
    handleNewAccounts(newAccounts);
  } catch (err) {
    console.error('Error on init when getting accounts', err);
  }

  const { uuid, name, icon } = providerDetail.info;
  activeProviderUUIDResult.innerText = uuid;
  activeProviderNameResult.innerText = name;
  activeProviderIconResult.innerHTML = icon
    ? `<img src="${icon}" height="90" width="90" />`
    : '';
  updateFormElements();
};

    const newAccounts = await globalContext.provider.request({
      method: 'eth_accounts',
    });
    handleNewAccounts(newAccounts);

Is this code duplicated?Why call eth_accounts twice?thank you~~

Dominant language
JavaScript
Stars
642
Forks
369
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from MetaMask/test-dapp

All issues in MetaMask/test-dapp

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.