跳转到帖子
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

发布于
  • 管理员

Anchor实践

我们将之前的我们的记事本合约改成Anchor工程。同时为了模拟PDA,我们将记事本所在地,按照用户 改成其PDA地址。

首先创建工程:

Copyanchor init note

设计指令

定义指令Account:

Copy#[derive(Accounts)]
pub struct Create<'info> {
    #[account(
        init,
        payer=user,
        space = 128,
        seeds = [user.key().as_ref()],
        bump
    )]
    pub note: Account<'info, Note>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}

其中State定义为:

Copy#[account]
pub struct Note {
    pub message: String
}

存储消息。

这里

Copy#[account(
        init,
        payer=user,
        space = 128,
        seeds = [user.key().as_ref()],
        bump
    )]

会新创建一个Account,该account的地址为 seeds确定的PDA地址,空间大小为128字节,由user来支付lamports费用。

执行逻辑

Copy#[program]
pub mod note {
    use super::*;

    pub fn create(ctx: Context<Create>, msg: String) -> Result<()> {
        let note = &mut ctx.accounts.note;

        note.message = msg;
        Ok(())
    }
}

这里整个逻辑就非常简单。直接获取相应的Account对象,然后将该state对象的message赋值即可。

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

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.