获取测试用SOL币

当你在本地工作时,你需要一些SOL才能进行交易。在非主网环境中,你可以通过将SOL空投到你的地址来接收SOL:

/* TS使用grill库 */
import {
  address,
  lamports,
  airdropFactory,
  createSolanaClient,
  LAMPORTS_PER_SOL,
} from "gill";

const { rpc, rpcSubscriptions, sendAndConfirmTransaction } = createSolanaClient(
  {
    urlOrMoniker: "devnet", // or `localnet`, etc
  },
);

const wallet = address("nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5");

const { value: initialBalance } = await rpc.getBalance(wallet).send();
console.log("Initial balance:", initialBalance);

/**
 * 注意:开发网devnet和测试网testnet有空投速率限制。
 * 强烈建议使用本地测试网localnet和本地测试验证器
 */
await airdropFactory({ rpc, rpcSubscriptions })({
  commitment: "confirmed",
  lamports: lamports(LAMPORTS_PER_SOL), // 申请1 SOL空投
  recipientAddress: wallet,
});

const { value: newBalance } = await rpc.getBalance(wallet).send();
console.log("New balance:", newBalance);

/* TS使用web3.js库 */
import { Connection, PublicKey, LAMPORTS_PER_SOL } from "@solana/web3.js";

const connection = new Connection("http://127.0.0.1:8899", "confirmed");

const wallet = new PublicKey("nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5");

/**
 * 注意:开发网devnet和测试网testnet有空投速率限制。
 * 强烈建议使用本地测试网localnet和本地测试验证器
 */
const signature = await connection.requestAirdrop(wallet, LAMPORTS_PER_SOL); // 申请1 SOL空投

const { blockhash, lastValidBlockHeight } =
  await connection.getLatestBlockhash();

// 注意:确认空投交易是非常重要的,用来确保你的钱包拿到空投
await connection.confirmTransaction({
  blockhash,
  lastValidBlockHeight,
  signature,
});

参考

Getting Test SOL

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注