Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MaryemHadjWannes committed Dec 31, 2024
1 parent e901f71 commit c6cbf03
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
27 changes: 27 additions & 0 deletions challenge1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Connection, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";

const publicKeyString = "GghvcjRjgFUzkzd7kq45cdmCjxKGaobdetiY99hG4crM"; // Example address

// Check if the public key is valid
try {
const publicKey = new PublicKey("GgJJRwLg9NzFQ97o1CJLGLp1KLSUMBwFc6eQNVEr4fbW");
if (!publicKey.toString()) {
throw new Error("Invalid public key format");
}

//const connection = new Connection("https://api.devnet.solana.com", "confirmed");


// Connect to the Solana mainnet
const connection = new Connection("https://api.mainnet-beta.solana.com", "confirmed");

// Fetch balance in lamports (smallest unit of SOL)
const balanceInLamports = await connection.getBalance(publicKey);

// Convert lamports to SOL
const balanceInSOL = balanceInLamports / LAMPORTS_PER_SOL;

console.log(`💰 Finished! The balance for the wallet at address ${publicKey} is ${balanceInSOL} SOL.`);
} catch (error) {
console.error("Error:", error.message);
}
13 changes: 13 additions & 0 deletions check-balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Connection, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";

const publicKey = new PublicKey("GghvcjRjgFUzkzd7kq45cdmCjxKGaobdetiY99hG4crM");

const connection = new Connection("https://api.devnet.solana.com", "confirmed");

const balanceInLamports = await connection.getBalance(publicKey);

const balanceInSOL = balanceInLamports / LAMPORTS_PER_SOL;

console.log(
`💰 Finished! The balance for the wallet at address ${publicKey} is ${balanceInSOL}!`,
);
14 changes: 14 additions & 0 deletions example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
Connection,
PublicKey,
clusterApiUrl,
LAMPORTS_PER_SOL,
} from "@solana/web3.js";

const connection = new Connection(clusterApiUrl("devnet"));
const address = new PublicKey("CenYq6bDRB7p73EjsPEpiYN7uveyPUTdXkDkgUduboaN");
const balance = await connection.getBalance(address);
const balanceInSol = balance / LAMPORTS_PER_SOL;

console.log(`The balance of the account at ${address} is ${balanceInSol} SOL`);
console.log(`✅ Finished!`);
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"@solana-developers/helpers": "^2.5.6",
"@solana/spl-name-service": "^0.2.1",
"@solana/web3.js": "^1.98.0",
"esrun": "^3.2.26",
"typescript": "^5.7.2"
Expand Down

0 comments on commit c6cbf03

Please sign in to comment.