Created
January 14, 2022 02:35
-
-
Save topherPedersen/b8188c1e61b827f50f9bd6aa03e0a2d2 to your computer and use it in GitHub Desktop.
Solana 101: Check Deployed Solana Program from Client (From Figment.io's Solana 101 Course)
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
import type {NextApiRequest, NextApiResponse} from 'next'; | |
import {Connection, PublicKey} from '@solana/web3.js'; | |
import {getNodeURL} from '@figment-solana/lib'; | |
import path from 'path'; | |
import fs from 'mz/fs'; | |
const PROGRAM_PATH = path.resolve('dist/solana/program'); | |
const PROGRAM_SO_PATH = path.join(PROGRAM_PATH, 'helloworld.so'); | |
export default async function deploy( | |
req: NextApiRequest, | |
res: NextApiResponse<string | boolean>, | |
) { | |
try { | |
const {network, programId} = req.body; | |
const url = getNodeURL(network); | |
const connection = new Connection(url, 'confirmed'); | |
// Re-create publicKeys from params | |
const publicKey = new PublicKey(programId); | |
const programInfo = await connection.getAccountInfo(publicKey); | |
if (programInfo === null) { | |
if (fs.existsSync(PROGRAM_SO_PATH)) { | |
throw new Error( | |
'Program needs to be deployed with `solana program deploy`', | |
); | |
} else { | |
throw new Error('Program needs to be built and deployed'); | |
} | |
} else if (!programInfo.executable) { | |
throw new Error(`Program is not executable`); | |
} | |
res.status(200).json(true); | |
} catch (error) { | |
let errorMessage = error instanceof Error ? error.message : 'Unknown Error'; | |
res.status(500).json(errorMessage); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment