-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from nifty-oss/febo/nifty-asset
Add nifty asset pages
- Loading branch information
Showing
11 changed files
with
1,213 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,98 @@ | ||
<script lang="ts"> | ||
import type { UINiftyAsset, UITokenMetadata } from "$lib/types"; | ||
import { SOL } from "$lib/xray"; | ||
import { page } from "$app/stores"; | ||
import { trpcWithQuery } from "$lib/trpc/client"; | ||
import IntersectionObserver from "svelte-intersection-observer"; | ||
import { ExtensionType, getExtension } from "@nifty-oss/asset"; | ||
export let address: string | undefined = undefined; | ||
export let asset: UINiftyAsset | undefined = undefined; | ||
export let status: { isLoading: boolean; isError: boolean } = { | ||
isError: false, | ||
isLoading: true, | ||
}; | ||
let intersecting = false; | ||
const params = new URLSearchParams(window.location.search); | ||
const network = params.get("network"); | ||
const isMainnetValue = network !== "devnet"; | ||
const client = trpcWithQuery($page); | ||
let account: any | undefined; | ||
if (address) { | ||
account = client.niftyAsset.createQuery([address, isMainnetValue], { | ||
refetchOnMount: false, | ||
refetchOnWindowFocus: false, | ||
}); | ||
} | ||
$: if ($account && $account.data) { | ||
status = { isError: $account.isError, isLoading: $account.isLoading }; | ||
updateAsset($account.data[0]); | ||
} | ||
const updateAsset = (data: UINiftyAsset) => { | ||
asset = data; | ||
if (asset) { | ||
const metadata = getExtension(asset, ExtensionType.Metadata); | ||
if (metadata && metadata.uri) { | ||
(async () => { | ||
try { | ||
asset.json = await fetchJsonMetadata(metadata.uri); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error("Error in fetchJsonMetadata:", error); | ||
} | ||
})(); | ||
} | ||
} | ||
}; | ||
const fetchJsonMetadata = async (jsonUri: string) => { | ||
try { | ||
const response = await fetch(jsonUri); | ||
if (!response.ok) { | ||
throw new Error(`Status ${response.status}`); | ||
} | ||
const contentType = response.headers.get("content-type"); | ||
if (!contentType || !contentType.includes("application/json")) { | ||
throw new TypeError("Received non-JSON content type"); | ||
} | ||
return await response.json(); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error("Error fetching or parsing JSON metadata:", error); | ||
return ""; | ||
} | ||
}; | ||
let element: HTMLDivElement; | ||
$: loading = address !== SOL && status.isLoading; | ||
$: failed = status.isError; | ||
</script> | ||
|
||
<div> | ||
<IntersectionObserver | ||
once={true} | ||
{element} | ||
bind:intersecting | ||
> | ||
<div bind:this={element} /> | ||
|
||
{#if intersecting} | ||
<slot | ||
{asset} | ||
{loading} | ||
{failed} | ||
/> | ||
{/if} | ||
</IntersectionObserver> | ||
</div> |
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,32 @@ | ||
import { z } from "zod"; | ||
|
||
import { t } from "$lib/trpc/t"; | ||
|
||
import { connect } from "$lib/xray"; | ||
import { LAMPORTS_PER_SOL, PublicKey, Connection } from "@solana/web3.js"; | ||
import { getRPCUrl } from "$lib/util/get-rpc-url"; | ||
|
||
import { HELIUS_API_KEY } from "$env/static/private"; | ||
import { getAssetAccountDataSerializer } from "@nifty-oss/asset"; | ||
|
||
export const niftyAsset = t.procedure | ||
.input(z.tuple([z.string(), z.boolean()])) | ||
.query(async ({ input }) => { | ||
const [address, isMainnet] = input; | ||
const connection = new Connection( | ||
getRPCUrl(`?api-key=${HELIUS_API_KEY}`, isMainnet), | ||
"confirmed" | ||
); | ||
|
||
const pubKey = new PublicKey(address); | ||
|
||
const accountInfo = await connection.getAccountInfo(pubKey); | ||
|
||
if (accountInfo) { | ||
return getAssetAccountDataSerializer().deserialize( | ||
accountInfo.data | ||
); | ||
} | ||
|
||
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
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,4 @@ | ||
import type { UINiftyAsset } from "$lib/types"; | ||
import { writable } from "svelte/store"; | ||
|
||
export const niftyAssetStore = writable<UINiftyAsset | null>(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
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.