Last active
October 20, 2022 20:48
-
-
Save aeither/1ba13d7a819a6de03afbc3913eaf1229 to your computer and use it in GitHub Desktop.
Thirdweb and Anchor Initiate Solana Program
This file contains hidden or 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
/* Initiate Solana Program with React */ | |
/* With Thirdweb */ | |
const [program, setProgram] = useState<Program<Dungeon3>>(); | |
// TypeError: Class extends value undefined is not a constructor or null with Vite. Requires Polyfills, check Metaplex examples. | |
const sdk = useMemo(() => { | |
if (wallet.connected) { | |
const sdk = ThirdwebSDK.fromNetwork("devnet"); | |
sdk.wallet.connect(wallet); | |
return sdk; | |
} | |
}, [wallet]); | |
useEffect(() => { | |
load(); | |
async function load() { | |
if (sdk) { | |
const { program }: { program: Program<Dungeon3> } = | |
(await sdk.getProgram(PROGRAM_ID.toBase58(), IDL)) as any; | |
setProgram(program); | |
} | |
} | |
}, [sdk]); | |
/* With Anchor */ | |
const { connection } = useConnection(); | |
const provider = useMemo(() => { | |
const aWallet = wallet as any as Wallet; | |
const provider = new AnchorProvider(connection, aWallet, {}); | |
return provider; | |
}, [wallet, connection]); | |
const program: Program<Dungeon3> | undefined = useMemo( | |
() => new Program(IDL as Idl, PROGRAM_ID, provider) as any, | |
[wallet, connection] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment