-
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 #272 from helius-labs/feat/add-idls
[Feat] Implement IDL Fetch
- Loading branch information
Showing
8 changed files
with
336 additions
and
8 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,56 @@ | ||
import { | ||
PublicKey, | ||
Keypair, | ||
Connection, | ||
Transaction, | ||
VersionedTransaction, | ||
} from "@solana/web3.js"; | ||
import { AnchorProvider, Program, type Idl } from "@coral-xyz/anchor"; | ||
import { getRPCUrl } from "./get-rpc-url"; | ||
import { idlStore } from "./stores/idl"; | ||
|
||
type DummyTransaction = { dummy: true }; | ||
|
||
// 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" | ||
); | ||
|
||
// In the future, look into ambiguous export errors for Anchor's Wallet class | ||
// For now, we create a dummy wallet and use it with the provider | ||
// This is enough since we aren't actually using the wallet to sign anything | ||
const dummyKeypair = Keypair.generate(); | ||
const dummyWallet = { | ||
publicKey: dummyKeypair.publicKey, | ||
signAllTransactions: <T extends Transaction | VersionedTransaction>( | ||
txs: T[] | ||
): Promise<T[]> => Promise.resolve(txs), | ||
signTransaction: <T extends Transaction | VersionedTransaction>( | ||
tx: T | ||
): Promise<T> => Promise.resolve(tx), | ||
}; | ||
|
||
const provider = new AnchorProvider( | ||
connection, | ||
dummyWallet, | ||
AnchorProvider.defaultOptions() | ||
); | ||
|
||
const accountPubkey = new PublicKey(accountAddress); | ||
const idl = (await Program.fetchIdl( | ||
accountPubkey, | ||
provider | ||
)) as Idl | null; | ||
|
||
return idl; | ||
} catch (error) { | ||
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
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
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.