Bonfida / sns-sdk

Solana Name Service SDKs monorepo
https://sns.id/
MIT License
34 stars 28 forks source link

useSubdomains() method return data undefined result #60

Closed alexandr-kazakov closed 7 months ago

alexandr-kazakov commented 7 months ago
import { useSubdomains } from '@bonfida/sns-react';

const { connection } = useConnection();
const subDomains = useSubdomains(connection, 'verified-iban');

console.log("subDomains: ", subDomains.data); // undefined

As I understand the data in subDomain fails to load sometimes and I get undefined instead of the array. How I can around of this?

If the data has had time to load, I get the array in the data:

console.log("subDomains: ", subDomains.data); 
[
    "ch5204835012345671000",
    "ch9430700114900892402",
    "ch7200700114900892402",
    "li2430172110007062984"
]
dr497 commented 7 months ago

Hi @alexandr-kazakov ! I tried to replicate the issue and it loaded very quickly for me. I suspect that might be due to the RPC you are using. Are you using a private RPC or a public one?

alexandr-kazakov commented 7 months ago

I'm using public RPC and I tried different public RPCs(for example helius & ironforge) and for all of them I have this issue. I don't know how to get around it.

letehaha commented 7 months ago

@alexandr-kazakov tbh I understood your point a bit differently. Seems like you mean that initially the result is undefined and only after data is loaded the result becomes an array, and that bothers you. If that is correct, have you tried using it like that?

const { data: subdomains = [] } = useSubdomains(connection, 'verified-iban');

console.log("subDomains: ", subdomains); // this way you will always have an array here
alexandr-kazakov commented 7 months ago

@letehaha thank you for your reply, but you just change syntax, it doesn't help me.

My problem is next: sometimes the data has undefined value, or it can be a empty array it's doesn't matter, how I can check and track is useSubdomains(connection, 'verified-iban'); fully load list of subdomains?

import { useSubdomains } from '@bonfida/sns-react';

const { connection } = useConnection();
const subDomains = useSubdomains(connection, 'verified-iban');

console.log("subDomains: ", subDomains.data); // undefined

How I can around it?

@dr497 answer is good, maybe it's RPC issue, but does this means I can't use public RPC with your lib or do you know another way to around this problem?

The useSubdomains() method is not return Promise, so i can't understand how to check is useSubdomains fully load the data or not and how I can work with it.

Thanks

dr497 commented 7 months ago

@alexandr-kazakov Public RPC on Solana sometimes disable certain methods (e.g getProgramAccounts) or have very strict rate limits which could explain why sometimes the data does not load. If you are considering building an app I would strongly recommend you to get your own private RPC, Helius is very good and has an affordable pricing

alexandr-kazakov commented 7 months ago

It will be good somehow to wrap useSubdomains() method to Promise, like this:

new Promise((resolve,reject) => {
   return resolve(useSubdomains(connection, 'verified-iban'))
}).then((res) => {console.log('The data is fully loaded', res.data});

@dr497 can I make something like this? In this case I'll be know for sure that the data array list of subdomains is loaded

letehaha commented 7 months ago

@alexandr-kazakov actually you can simply check the implementation of useSubdomains here.

import { getDomainKeySync, findSubdomains } from "@bonfida/spl-name-service";

const domain = 'verified-iban';
const pubKey = getDomainKeySync(domain).pubkey;
const subdomains = pubKey ? await findSubdomains(connection, pubKey) : undefined;

@bonfida/spl-name-service is already a dependency so using it won't cause any bundle increase.

alexandr-kazakov commented 7 months ago

@letehaha thank you it's working for me well.

But anyway, please think about how to update useSubdomains() method so the users can work with it and public RPCs and not have a problem like mine. Thanks