跳转到帖子
View in the app

A better way to browse. Learn more.

WEB3论坛社区

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

recommended_posts

发布于
  • 管理员

HelloWorld

Solana为了初学者可以快速入门,提供了一个 Playground 服务。 可以在不需要本地环境的情况下,进行DApp的开发。

创建项目

在界面中,我们点击 "Create a New Project"

playground_create_hellowrld.png

创建完项目后。在Explorer里可以看到文件列表,主要有:

Copysrc/
    lib.rs

client/
    clinet.ts

test/
    native.test.ts

playground_explorer.png

对应的位置有

  • "Build":构建合约

  • "Run" : 运行客户端端,调试合约。

  • "Test": 执行测试代码

链接钱包

在界面的左下角有个"unconnect"的提示,点击后,弹出:

playground_import_wallet.png

在这里选择我们之前生成的"~/.config/solana/id.json"秘钥文件。导入后,可以看到钱包详情playground_wallet.png

开发合约

打开这里的lib.rs,在里面贴上:

Copyuse solana_program::{
    account_info::AccountInfo,
    entrypoint,
    entrypoint::ProgramResult,
    pubkey::Pubkey,
    msg,
};


// Declare and export the program's entrypoint
entrypoint!(process_instruction);

// Program entrypoint's implementation
pub fn process_instruction(
    _program_id: &Pubkey, // Public key of the account the hello world program was loaded into
    _accounts: &[AccountInfo], // The account to say hello to
    _instruction_data: &[u8], // Ignored, all helloworld instructions are hellos
) -> ProgramResult {
    msg!("Hello World Rust program entrypoint");

    Ok(())
}

然后点击"Build",在下面的界面可以看到build的结果:

CopyBuilding...
Build successful. Completed in 0.60s.

然后点击左侧的 锤子+扳手的图标,点击"Deploy":

playground_build_deplay.png

发布成功,可以在界面上看到发布后的合约地址。在发布过一次后,这里的"Deploy"就会变成"Update"

如果我们的合约有修改,只要在这里构建后点击"Update"就可以进行更新了。

客户端开发

回到Explorer界面,打开client.ts,在里面贴上:

Copy// Client
console.log("My address:", pg.wallet.publicKey.toString());
const balance = await pg.connection.getBalance(pg.wallet.publicKey);
console.log(`My balance: ${balance / web3.LAMPORTS_PER_SOL} SOL`);

// create an empty transaction
const transaction = new web3.Transaction();

// add a hello world program instruction to the transaction
transaction.add(
new web3.TransactionInstruction({
    keys: [],
    programId: new web3.PublicKey(pg.PROGRAM_ID),
}),
);

console.log("Sending transaction...");
const txHash = await web3.sendAndConfirmTransaction(
    pg.connection,
    transaction,
    [pg.wallet.keypair],
);
console.log("Transaction sent with hash:", txHash);

这里,不需要过多的import,IDE已经帮忙们做了import。可以直接使用web3。其中"pg.wallet" 就是我们的钱包,其publicKey属性就是钱包的地址。而pg.connection就如同我们用web3创建 的connection对象,这里共用playground的设置里面的RPC地址。

pb.PROGRAM_ID 就是我们要刚刚构建并发布的合约的地址。

点击"Run"我们可以在日志里面看到:

CopyRunning client...
client.ts:
My address: 5pWae6RxD3zrYzBmPTMYo1LZ5vef3vfWH6iV3s8n6ZRG
My balance: 4.27388232 SOL
Sending transaction...
Transaction sent with hash: 3XcaF6zpXthBQ2mih7DdVBuvwXi7L6Wy4rCHzgwHEaNsqMDNRGC5yppG9xKP9g9hYfT6wPEw127mxgYWBTouS5gz

打开solana浏览器,贴上这里的hash地址。可以看到。在日志部分,执行了我们前面合约代码里面的"HelloWorld"。

playground_helloworld_explorer.png

创建帐户或登录后发表意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.