-
-
Save lakshmankashyap/1d2c20109c3e705d62c6f621bb757a73 to your computer and use it in GitHub Desktop.
test solana service
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
// first install @solana "@solana/web3.js" | |
// yarn add @solana/web3.js | |
import { | |
Connection, | |
clusterApiUrl, | |
Keypair, | |
LAMPORTS_PER_SOL, | |
PublicKey, | |
} from '@solana/web3.js'; | |
const API_KEY = 'xxx'; | |
const DATAHUB_URL = 'https://solana--devnet--rpc.datahub.figment.io/apikey/'; | |
const test_connection = (url, msg) => async () => { | |
try { | |
const connection = new Connection(url, 'confirmed'); | |
const version = await connection.getVersion(); | |
console.log(msg, version?.['solana-core']); | |
} catch (error) { | |
console.log(error.message); | |
} | |
}; | |
const test_airdrop = (url, msg) => async () => { | |
try { | |
const connection = new Connection(url, 'confirmed'); | |
const keypair = Keypair.generate(); | |
const hash = await connection.requestAirdrop( | |
keypair.publicKey, | |
LAMPORTS_PER_SOL, | |
); | |
await connection.confirmTransaction(hash); | |
const balance = await connection.getBalance(keypair.publicKey); | |
console.log(msg, keypair.publicKey.toBase58(), balance); | |
} catch (error) { | |
console.log(error.message); | |
} | |
}; | |
const test_balance = (url, msg, address) => async () => { | |
try { | |
const connection = new Connection(url, 'confirmed'); | |
const publicKey = new PublicKey(address); | |
const balance = await connection.getBalance(publicKey); | |
console.log(msg, balance); | |
} catch (error) { | |
console.log(error.message); | |
} | |
}; | |
(async () => { | |
const devnet_url = clusterApiUrl('devnet'); | |
const datahub_url = `${DATAHUB_URL}${API_KEY}`; | |
const address = 'CBQACgyxWq9ndXPvQ2SJLLAp6ukhgrZBi6okHWyPGVZC'; | |
// test connection -------------------- | |
await test_connection(devnet_url, 'test devnet connection')().catch( | |
(error) => error.message, | |
); | |
await test_connection(datahub_url, 'test datahub connection')().catch( | |
(error) => error.message, | |
); | |
// ------------------------------------ | |
// test airdrop -------------------- | |
await test_airdrop(devnet_url, 'test devnet airdrop')().catch( | |
(error) => error.message, | |
); | |
await test_airdrop(datahub_url, 'test datahub aridrop')().catch( | |
(error) => error.message, | |
); | |
// ------------------------------------ | |
// test balance -------------------- | |
await test_balance(devnet_url, 'test devnet balance', address)().catch( | |
(error) => error.message, | |
); | |
await test_balance(datahub_url, 'test datahub balance', address)().catch( | |
(error) => error.message, | |
); | |
// ------------------------------------ | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment