Solana CLI 基础

Solana 配置

查看当前配置:

Terminal
$
solana config get

您将看到如下输出:

Config File: /Users/test/.config/solana/cli/config.yml
RPC URL: https://api.mainnet-beta.solana.com
WebSocket URL: wss://api.mainnet-beta.solana.com/ (computed)
Keypair Path: /Users/test/.config/solana/id.json
Commitment: confirmed

RPC URL 和 Websocket URL 指定了 CLI 请求的 Solana 集群。

您可以使用以下命令更新 Solana CLI 集群:

Terminal
$
solana config set --url mainnet-beta
$
solana config set --url devnet
$
solana config set --url localhost
$
solana config set --url testnet

您还可以使用以下简写选项:

Terminal
$
solana config set -um # For mainnet-beta
$
solana config set -ud # For devnet
$
solana config set -ul # For localhost
$
solana config set -ut # For testnet

Keypair Path 指向 Solana CLI 默认使用的 Solana 钱包(keypair),用于支付交易费用和部署程序。默认情况下,此文件存储在 ~/.config/solana/id.json

创建钱包

要使用 Solana CLI 发送交易,您需要一个已注资 SOL 的 Solana 钱包。

要在默认的 Keypair Path 生成一个 keypair,请运行以下命令:

Terminal
$
solana-keygen new

您将看到如下输出:

Generating a new keypair
For added security, enter a BIP39 passphrase
NOTE! This passphrase improves security of the recovery seed phrase NOT the
keypair file itself, which is stored as insecure plain text
BIP39 Passphrase (empty for none):
Wrote new keypair to /Users/test/.config/solana/id.json
===========================================================================
pubkey: 8dBTPrjnkXyuQK3KDt9wrZBfizEZijmmUQXVHpFbVwGT
===========================================================================
Save this seed phrase and your BIP39 passphrase to recover your new keypair:
cream bleak tortoise ocean nasty game gift forget fancy salon mimic amazing
===========================================================================

如果您已经在默认位置保存了一个文件系统钱包,此命令不会覆盖它,除非您明确使用 --force 标志强制覆盖。

要查看您的钱包地址(公钥),请运行:

Terminal
$
solana address

空投 SOL

请求向您的钱包空投 SOL,以支付交易和程序部署费用。

将您的集群设置为 devnet:

Terminal
$
solana config set -ud

然后请求空投 devnet SOL:

Terminal
$
solana airdrop 2

Devnet 空投每次请求限制为 5 SOL。如果您遇到速率限制或错误,请尝试使用 Web Faucet

要检查您的钱包 SOL 余额,请运行以下命令:

Terminal
$
solana balance

运行本地验证器

Solana CLI 包含一个内置的测试验证器,用于本地开发。

在一个单独的终端中,运行以下命令以启动本地验证器:

Terminal
$
solana-test-validator

在运行 Solana CLI 命令之前,请记得将您的 CLI 更新为使用 localhost:

Terminal
$
solana config set -ul

Is this page helpful?

Table of Contents

Edit Page