Explorer: Fix domain table and bump @bonfida/spl-name-service to 0.1.30 (#24312)

* migrate performReverseLookup from bonfida

* Fix display name fetching

Co-authored-by: Justin Starry <justin@solana.com>
This commit is contained in:
Dennis Antela Martinez 2022-04-15 01:32:10 -07:00 committed by GitHub
parent 50fba01842
commit 47e1c9107d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1177 additions and 996 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@
"dependencies": { "dependencies": {
"@blockworks-foundation/mango-client": "^3.2.16", "@blockworks-foundation/mango-client": "^3.2.16",
"@bonfida/bot": "^0.5.3", "@bonfida/bot": "^0.5.3",
"@bonfida/spl-name-service": "^0.1.22", "@bonfida/spl-name-service": "^0.1.30",
"@cloudflare/stream-react": "^1.2.0", "@cloudflare/stream-react": "^1.2.0",
"@metamask/jazzicon": "^2.0.0", "@metamask/jazzicon": "^2.0.0",
"@metaplex/js": "4.12.0", "@metaplex/js": "4.12.0",

View File

@ -21,15 +21,14 @@ export function DomainsCard({ pubkey }: { pubkey: PublicKey }) {
return ( return (
<div className="card"> <div className="card">
<div className="card-header align-items-center"> <div className="card-header align-items-center">
<h3 className="card-header-title">Domain Names Owned</h3> <h3 className="card-header-title">Owned Domain Names</h3>
</div> </div>
<div className="table-responsive mb-0"> <div className="table-responsive mb-0">
<table className="table table-sm table-nowrap card-table"> <table className="table table-sm table-nowrap card-table">
<thead> <thead>
<tr> <tr>
<th className="text-muted">Domain name</th> <th className="text-muted">Domain Name</th>
<th className="text-muted">Domain Address</th> <th className="text-muted">Name Service Account</th>
<th className="text-muted">Domain Class Address</th>
</tr> </tr>
</thead> </thead>
<tbody className="list"> <tbody className="list">
@ -53,9 +52,6 @@ function RenderDomainRow({ domainInfo }: { domainInfo: DomainInfo }) {
<td> <td>
<Address pubkey={domainInfo.address} link /> <Address pubkey={domainInfo.address} link />
</td> </td>
<td>
<Address pubkey={domainInfo.class} link />
</td>
</tr> </tr>
); );
} }

View File

@ -1,49 +1,38 @@
import { PublicKey, Connection } from "@solana/web3.js"; import { PublicKey, Connection } from "@solana/web3.js";
import { import {
getHashedName,
getNameAccountKey,
NameRegistryState,
getFilteredProgramAccounts, getFilteredProgramAccounts,
NAME_PROGRAM_ID, NAME_PROGRAM_ID,
performReverseLookup,
} from "@bonfida/spl-name-service"; } from "@bonfida/spl-name-service";
import BN from "bn.js";
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { Cluster, useCluster } from "providers/cluster"; import { Cluster, useCluster } from "providers/cluster";
// Name auctionning Program ID // Address of the SOL TLD
export const PROGRAM_ID = new PublicKey( const SOL_TLD_AUTHORITY = new PublicKey(
"jCebN34bUfdeUYJT13J1yG16XWQpt5PDx6Mse9GUqhR" "58PwtjSDuFHuUkYjH9BYnnQKHfwo9reZhC2zMJv9JPkx"
); );
export interface DomainInfo { export interface DomainInfo {
name: string; name: string;
address: PublicKey; address: PublicKey;
class: PublicKey;
} }
async function getDomainKey( async function getUserDomainAddresses(
name: string,
nameClass?: PublicKey,
nameParent?: PublicKey
) {
const hashedDomainName = await getHashedName(name);
const nameKey = await getNameAccountKey(
hashedDomainName,
nameClass,
nameParent
);
return nameKey;
}
export async function findOwnedNameAccountsForUser(
connection: Connection, connection: Connection,
userAccount: PublicKey userAddress: PublicKey
): Promise<PublicKey[]> { ): Promise<PublicKey[]> {
const filters = [ const filters = [
// parent
{
memcmp: {
offset: 0,
bytes: SOL_TLD_AUTHORITY.toBase58(),
},
},
// owner
{ {
memcmp: { memcmp: {
offset: 32, offset: 32,
bytes: userAccount.toBase58(), bytes: userAddress.toBase58(),
}, },
}, },
]; ];
@ -55,41 +44,8 @@ export async function findOwnedNameAccountsForUser(
return accounts.map((a) => a.publicKey); return accounts.map((a) => a.publicKey);
} }
export async function performReverseLookup(
connection: Connection,
nameAccounts: PublicKey[]
): Promise<DomainInfo[]> {
let [centralState] = await PublicKey.findProgramAddress(
[PROGRAM_ID.toBuffer()],
PROGRAM_ID
);
const reverseLookupAccounts = await Promise.all(
nameAccounts.map((name) => getDomainKey(name.toBase58(), centralState))
);
let names = await NameRegistryState.retrieveBatch(
connection,
reverseLookupAccounts
);
return names
.map((name) => {
if (!name?.data) {
return undefined;
}
const nameLength = new BN(name!.data.slice(0, 4), "le").toNumber();
return {
name: name.data.slice(4, 4 + nameLength).toString() + ".sol",
address: name.address,
class: name.class,
};
})
.filter((e) => !!e) as DomainInfo[];
}
export const useUserDomains = ( export const useUserDomains = (
address: PublicKey userAddress: PublicKey
): [DomainInfo[] | null, boolean] => { ): [DomainInfo[] | null, boolean] => {
const { url, cluster } = useCluster(); const { url, cluster } = useCluster();
const [result, setResult] = useState<DomainInfo[] | null>(null); const [result, setResult] = useState<DomainInfo[] | null>(null);
@ -102,12 +58,21 @@ export const useUserDomains = (
const connection = new Connection(url, "confirmed"); const connection = new Connection(url, "confirmed");
try { try {
setLoading(true); setLoading(true);
const domains = await findOwnedNameAccountsForUser(connection, address); const userDomainAddresses = await getUserDomainAddresses(
let names = await performReverseLookup(connection, domains); connection,
names.sort((a, b) => { userAddress
return a.name.localeCompare(b.name); );
}); const userDomains = await Promise.all(
setResult(names); userDomainAddresses.map(async (address) => {
const domainName = await performReverseLookup(connection, address);
return {
name: `${domainName}.sol`,
address,
};
})
);
userDomains.sort((a, b) => a.name.localeCompare(b.name));
setResult(userDomains);
} catch (err) { } catch (err) {
console.log(`Error fetching user domains ${err}`); console.log(`Error fetching user domains ${err}`);
} finally { } finally {
@ -115,7 +80,7 @@ export const useUserDomains = (
} }
}; };
resolve(); resolve();
}, [address, url]); // eslint-disable-line react-hooks/exhaustive-deps }, [userAddress, url]); // eslint-disable-line react-hooks/exhaustive-deps
return [result, loading]; return [result, loading];
}; };