How to fetch the .sol names owned by a wallet address

I am trying to integrate the name service in my dapp , such that , whenever an user connects the wallet , their .sol name is shown if they own any .sol name

I tried to go through the

here is the code snippet

Screenshot 2022-04-13 at 7.20.43 PM

1 Like

The code snippet here GitHub - Bonfida/solana-name-service-guide: Small guide to the Solana name service works great. I assume you are encountering issues while trying to the code you posted? Mind posting the error you are getting? Also can you copy past your code instead of posting a screenshot

import { NAME_PROGRAM_ID, performReverseLookup } from "@bonfida/spl-name-service";
import * as solanaWeb3 from "@solana/web3.js";
import retry from "async-retry";
// Public key of bonfida.sol

export const solName = async ({ setSolName, nonEVMUser }) => {
  let connection = new solanaWeb3.Connection(
    "https://solana-api.syndica.io/access-token/<access-token>",
    "confirmed"
  );

  console.log({ connection });

  const filters = [
    {
      memcmp: {
        offset: 32,
        bytes: nonEVMUser.addr,
      },
    },
  ];

  try {
    const accounts = await connection.getProgramAccounts(NAME_PROGRAM_ID, {
      filters,
    });

    const result = accounts.map((a) => a.pubkey);
    console.log({ result });

    console.log({ result: result[0].toBase58() });

    if (!result[0]) return;
    const domainKey = new solanaWeb3.PublicKey(result[0]);

    console.log("fetchiung sol name");
    console.log({ domainKey });

    console.log("retrying");

    const domainName = await performReverseLookup(connection, domainKey);
    console.log({ domainName });
    setSolName(domainName);
    return domainName;
  } catch (e) {
    console.error(e);
  }
};

It works fine sometime , but sometime it throws error attached below

Error

Screenshot 2022-04-15 at 2.43.25 PM

The getProgramAccounts RPC call returns all the account that are program owned. If for some reason a user has a program owned account that is not a .sol domain name, the deserialization method will throw an error. You can just ignore these cases

got it, but in my case, for the same wallet address, the same code works and sometimes throws the attached error,

Also is there anyway, one can distinguish the program account if it is .sol or not before passing it as a parameter to performReverseLookup function

Yes, you can check that the parent of the domain name is .sol i.e 58PwtjSDuFHuUkYjH9BYnnQKHfwo9reZhC2zMJv9JPkx

For that you need to retrieve the data of all domain keys, you can use NameRegistry.retrieveBatch and then filter on the array of NameRegistry you get before calling performReverseLookup