-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor search to remove legacy web3js dependency (#275)
This PR refactors the search bar to remove the client-side legacy web3js dependency. This means that the core app layout, shared by every page, no longer pulls in legacy web3js - The client-side call to `getDomainInfo` is replaced with a GET request to a new API `/api/domain-info/[domain]` - This API just calls `getDomainInfo` and returns the response as JSON. This function and everything else in `utils/name-service` that can be run on the server are extracted to a new `utils/domain-info`. This is necessary because `utils/domain-info` has `use client` (needed for its client-side hooks) - The API has a 24h cache-response header. I haven't done any manual client-side caching, Next's fetch de-dupe should be sufficient. - For now I haven't changed how we get the domain info. Longer term I'd like to support multiple domains: #271 - I've extracted everything used by the search bar from `utils/tx` to a new `utils/programs`. I've removed the legacy web3js dependency, which was just used to pull in program public keys The result of this is that some pages load ~100kb less JS. I'm not exactly sure how some of these numbers work though, eg the homepage is already not including web3js despite including the search bar. <img width="1366" alt="Screenshot 2023-07-18 at 15 23 19" src="https://github.com/solana-labs/explorer/assets/1711350/a1cc43d8-1f76-44ca-a558-1f9f8a8d2f3d"> We can also inspect the bundle before and after this change, this is the app/layout endpoint. On master we have the legacy web3js <img width="2387" alt="Screenshot 2023-07-18 at 15 01 10" src="https://github.com/solana-labs/explorer/assets/1711350/fd0d4a6e-4e47-400a-b45f-fed2719415ef"> After we've stripped many of these dependencies out: <img width="2388" alt="Screenshot 2023-07-18 at 15 02 33" src="https://github.com/solana-labs/explorer/assets/1711350/04294a1c-03d7-4b67-8fcd-549cbc5a6215"> Mostly it shows that we should stop including that token list though... #201
- Loading branch information
1 parent
7e0e619
commit 47006f0
Showing
7 changed files
with
526 additions
and
514 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Connection } from "@solana/web3.js" | ||
import { NextResponse } from "next/server" | ||
|
||
import { MAINNET_BETA_URL } from "@/app/utils/cluster" | ||
import { getDomainInfo } from "@/app/utils/domain-info" | ||
|
||
type Params = { | ||
params: { | ||
domain: string | ||
} | ||
} | ||
|
||
export type FetchedDomainInfo = Awaited<ReturnType<typeof getDomainInfo>>; | ||
|
||
export async function GET( | ||
_request: Request, | ||
{ params: { domain } }: Params | ||
) { | ||
// Intentionally using legacy web3js for compatibility with bonfida library | ||
// This is an API route so won't affect client bundle | ||
// We only fetch domains on mainnet | ||
const connection = new Connection(MAINNET_BETA_URL); | ||
const domainInfo = await getDomainInfo(domain, connection); | ||
|
||
return NextResponse.json(domainInfo, { | ||
headers: { | ||
// 24 hours | ||
"Cache-Control": "max-age=86400" | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { getHashedName, getNameAccountKey, getNameOwner } from "@bonfida/spl-name-service"; | ||
import { Connection, PublicKey } from "@solana/web3.js"; | ||
|
||
// Address of the SOL TLD | ||
export const SOL_TLD_AUTHORITY = new PublicKey('58PwtjSDuFHuUkYjH9BYnnQKHfwo9reZhC2zMJv9JPkx'); | ||
|
||
async function getDomainKey(name: string, nameClass?: PublicKey, nameParent?: PublicKey) { | ||
const hashedDomainName = await getHashedName(name); | ||
const nameKey = await getNameAccountKey(hashedDomainName, nameClass, nameParent); | ||
return nameKey; | ||
} | ||
|
||
export interface DomainInfo { | ||
name: string; | ||
address: PublicKey; | ||
} | ||
|
||
export const hasDomainSyntax = (value: string) => { | ||
return value.length > 4 && value.substring(value.length - 4) === '.sol'; | ||
}; | ||
|
||
// returns non empty wallet string if a given .sol domain is owned by a wallet | ||
export async function getDomainInfo(domain: string, connection: Connection) { | ||
const domainKey = await getDomainKey( | ||
domain.slice(0, -4), // remove .sol | ||
undefined, | ||
SOL_TLD_AUTHORITY | ||
); | ||
try { | ||
const registry = await getNameOwner(connection, domainKey); | ||
return registry && registry.registry.owner | ||
? { | ||
address: domainKey.toString(), | ||
owner: registry.registry.owner.toString(), | ||
} | ||
: null; | ||
} catch { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
47006f0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
explorer – ./
explorer.solana.com
explorer-git-master-solana-labs.vercel.app
explorer-solana-labs.vercel.app