Created
February 17, 2023 09:35
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 dotenv from "dotenv"; | |
dotenv.config(); | |
import assert from "assert"; | |
import * as web3 from "@solana/web3.js"; | |
import * as fs from "fs"; | |
/** | |
* Constants | |
*/ | |
const PING_PROGRAM_ID = new web3.PublicKey( | |
"ChT1B39WKLS8qUrkLvFDXMhEJ4F1XZzwUNHUt4AU9aVa" | |
); | |
const PING_PROGRAM_DATA_PUBLIC_KEY = new web3.PublicKey( | |
"Ah9K7dQ8EHaZqcAsgBW8w37yN2eAy3koFmUn4x3CJtod" | |
); | |
/** | |
* Main | |
*/ | |
async function main() { | |
const keyPair = initializeKeyPair(); | |
console.log("public key:", keyPair.publicKey.toBase58()); | |
const connection = new web3.Connection(web3.clusterApiUrl("devnet")); | |
const balanceLamport = await airdrop(connection, keyPair.publicKey, 0.1, { | |
maxBalance: 0.5, | |
}); | |
console.log( | |
"current balance:", | |
balanceLamport / web3.LAMPORTS_PER_SOL, | |
"SOL" | |
); | |
await pingProgram(connection, keyPair); | |
const transferRecipient = new web3.PublicKey( | |
"CmYpdcRKreR7HLU7MdQngGri2XvWVoeVydnieYdmxKoH" | |
); | |
await trasnferSol(connection, { | |
recipient: transferRecipient, | |
payer: keyPair, | |
amountLamports: 0.025 * web3.LAMPORTS_PER_SOL, | |
}); | |
console.log( | |
"transfered", | |
0.025, | |
"SOL from", | |
keyPair.publicKey.toBase58(), | |
"to", | |
transferRecipient.toBase58() | |
); | |
console.log( | |
"sender balance", | |
await getSolBalence(connection, keyPair.publicKey) | |
); | |
console.log( | |
"receiver balance", | |
await getSolBalence(connection, transferRecipient) | |
); | |
} | |
/** | |
* Functions | |
*/ | |
async function getSolBalence( | |
connection: web3.Connection, | |
accountAddress: web3.PublicKey | |
) { | |
const lamportBalance = await connection.getBalance(accountAddress); | |
return lamportBalance / web3.LAMPORTS_PER_SOL; | |
} | |
function initializeKeyPair(): web3.Keypair { | |
if (!process.env.SECRET_KEY) { | |
const keyPair = web3.Keypair.generate(); | |
fs.writeFileSync("./.env", `SECRET_KEY=[${keyPair.secretKey.toString()}]`); | |
return keyPair; | |
} | |
const sercretKey = JSON.parse(process.env.SECRET_KEY) as number[]; | |
assert(Array.isArray(sercretKey), "SECRET_KEY should be an array"); | |
return web3.Keypair.fromSecretKey(Uint8Array.from(sercretKey)); | |
} | |
async function airdrop( | |
connection: web3.Connection, | |
receiver: web3.PublicKey, | |
amountSol = 0.1, | |
opts: { maxBalance?: number } = {} | |
) { | |
if (opts.maxBalance) { | |
const currentBalanceLamport = await connection.getBalance( | |
receiver, | |
"confirmed" | |
); | |
if (currentBalanceLamport >= opts.maxBalance * web3.LAMPORTS_PER_SOL) { | |
console.log("enough SOL, no more airdrops"); | |
return currentBalanceLamport; | |
} | |
} | |
const signature = await connection.requestAirdrop( | |
receiver, | |
amountSol * web3.LAMPORTS_PER_SOL | |
); | |
const lastesBlockhash = await connection.getLatestBlockhash(); | |
await connection.confirmTransaction( | |
{ | |
blockhash: lastesBlockhash.blockhash, | |
lastValidBlockHeight: lastesBlockhash.lastValidBlockHeight, | |
signature, | |
}, | |
"finalized" | |
); | |
console.log("airdroped", amountSol, "SOL"); | |
return connection.getBalance(receiver, "confirmed"); | |
} | |
async function pingProgram(connection: web3.Connection, payer: web3.Keypair) { | |
const ixs = new web3.TransactionInstruction({ | |
keys: [ | |
{ | |
pubkey: PING_PROGRAM_DATA_PUBLIC_KEY, | |
isSigner: false, | |
isWritable: true, | |
}, | |
], | |
programId: PING_PROGRAM_ID, | |
}); | |
const tx = new web3.Transaction(); | |
tx.add(ixs); | |
const txSig = await web3.sendAndConfirmTransaction(connection, tx, [payer], { | |
commitment: "finalized", | |
}); | |
console.log( | |
`[PING] Transaction https://explorer.solana.com/tx/${txSig}?cluster=devnet` | |
); | |
} | |
async function trasnferSol( | |
connection: web3.Connection, | |
params: { | |
recipient: web3.PublicKey; | |
payer: web3.Keypair; | |
amountLamports: number; | |
} | |
) { | |
const tx = new web3.Transaction(); | |
tx.add( | |
web3.SystemProgram.transfer({ | |
fromPubkey: params.payer.publicKey, | |
toPubkey: params.recipient, | |
lamports: params.amountLamports, | |
}) | |
); | |
const txSig = await web3.sendAndConfirmTransaction( | |
connection, | |
tx, | |
[params.payer], | |
{ commitment: "finalized" } | |
); | |
console.log( | |
`[TRANSFER] Transaction https://explorer.solana.com/tx/${txSig}?cluster=devnet` | |
); | |
} | |
/** | |
* Execution | |
*/ | |
main() | |
.then(() => { | |
console.log("Finished successfully"); | |
process.exit(0); | |
}) | |
.catch((error) => { | |
console.log(error); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment