Skip to content

Instantly share code, notes, and snippets.

@zurgl
Last active January 5, 2022 05:48
Show Gist options
  • Save zurgl/68ca5745e2f618d905f7d6117183c19e to your computer and use it in GitHub Desktop.
Save zurgl/68ca5745e2f618d905f7d6117183c19e to your computer and use it in GitHub Desktop.
test solana service
// 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,
);
// ------------------------------------
})();
@zurgl
Copy link
Author

zurgl commented Dec 15, 2021

If you liked these snippets of code and like to know more about them:
Try the Solana pathway published by figment learn
Or Join our community on discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment