-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
274 additions
and
5 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,4 @@ | ||
import { writable } from "svelte/store"; | ||
import type { Idl } from "@coral-xyz/anchor"; | ||
|
||
export const idlStore = writable<Idl | 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* eslint-disable no-console */ | ||
/* eslint-disable this/no-this */ | ||
import { writable, type Writable } from "svelte/store"; | ||
import { PublicKey, Keypair, Connection } from "@solana/web3.js"; | ||
import * as anchor from "@coral-xyz/anchor"; | ||
import { getRPCUrl } from "../get-rpc-url"; | ||
|
||
// Define the internal state type | ||
type IdlState = anchor.Idl | null; | ||
|
||
class IdlStore { | ||
private store: Writable<IdlState>; | ||
|
||
constructor() { | ||
this.store = writable<IdlState>(null); | ||
} | ||
|
||
// Method to get the store's subscription method | ||
subscribe(run: (value: IdlState) => void) { | ||
return this.store.subscribe(run); | ||
} | ||
|
||
// Method to manually set the store's state | ||
set(idl: IdlState) { | ||
this.store.set(idl); | ||
} | ||
|
||
// Method to update the store's state | ||
update(updater: (value: IdlState) => IdlState) { | ||
this.store.update(updater); | ||
} | ||
} | ||
|
||
// Create an instance of the custom store | ||
export const idlStore = new IdlStore(); | ||
|
||
// Function to fetch IDL and update the store | ||
export async function grabIdl( | ||
accountAddress: string, | ||
isMainnetValue: boolean, | ||
apiKey: string | ||
) { | ||
try { | ||
const connection = new Connection( | ||
getRPCUrl(`?api-key=${apiKey}`, isMainnetValue), | ||
"confirmed" | ||
); | ||
const aWallet = new anchor.Wallet(Keypair.generate()); | ||
const provider = new anchor.AnchorProvider( | ||
connection, | ||
aWallet, | ||
anchor.AnchorProvider.defaultOptions() | ||
); | ||
|
||
const accountPubkey = new PublicKey(accountAddress); | ||
const idl = await anchor.Program.fetchIdl(accountPubkey, provider); | ||
|
||
// Directly use the idlStore instance to set the IDL | ||
idlStore.set(idl); | ||
} catch (error) { | ||
console.error("Error fetching IDL:", error); | ||
idlStore.set(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,27 @@ | ||
<script lang="ts"> | ||
import { idlStore } from "$lib/util/stores/idl"; | ||
// @ts-ignore | ||
import formatHighlight from "json-format-highlight"; | ||
let idl: string; | ||
let idlData = $idlStore; | ||
$: if (idlData) { | ||
idl = formatHighlight(JSON.stringify(idlData, null, 2), { | ||
keyColor: "#a5a3a", | ||
numberColor: "#e8a034", | ||
stringColor: "#24ae67", | ||
}); | ||
} | ||
</script> | ||
|
||
<div> | ||
<div class="relative mx-auto w-full max-w-2xl pb-32"> | ||
{#if idlData} | ||
<div class="code col-span-12 w-full overflow-hidden px-3"> | ||
<pre><code class="text-xs">{@html idl}</code></pre> | ||
</div> | ||
{/if} | ||
</div> | ||
</div> |