1. 教程1:Getting Started With Anchor In A React Project (Quick Solana Tutorial)
https://www.youtube.com/watch?v=7p_8549sinc
需要做的第一件事: build this program locally
- anchor test
会deploy程序,然后提示让你solana test validator
anchor test产生的文件folder
https://github.com/project-serum/anchor/blob/master/tests/ido-pool/tests/ido-pool.js
// Read the generated IDL.
const idl=JSON.parse(require('fs').readFileSync('./target/idl/basic_0.json','utf8'));
// Address of the deployed program.
const programId=new anchor.web3.PublicKey('<YOUR-PROGRAM-ID>');
// Generate the program client from IDL.
const program=new anchor.Program(idl,programId);
// Execute the RPC.
await program.rpc.initialize();
Now, make sure to plugin your program's address into <YOUR-PROGRAM-ID> (a mild annoyance that we'll address next), and run Notice how we dynamically created the initialize method under the rpc namespace.
现在,确保将您的程序地址插入到 <YOUR-PROGRAM-ID> 中(我们将在接下来解决这个问题),然后运行 注意我们是如何在 rpc 命名空间下动态创建 initialize 方法的。
Now, make sure to plugin your program's address into <YOUR-PROGRAM-ID> (a mild annoyance that we'll address next), and run
现在,确保将您的程序地址插入到 <YOUR-PROGRAM-ID>(我们将在接下来解决的轻微烦恼)中,然后运行
node client.js
You just successfully created a client and executed a transaction on your localnet.
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
mod basic_1{
use super::*;
pub fn initialize(ctx:Context<Initialize>,data:u64)->
ProgramResult{
let my_account=&mut ctx.accounts.my_account;
my_account.data=data;
Ok(())
}
//首先,让我们从初始化指令(initialize instruction)开始。注意传递给程序的数据参数。这个参数和任何其他有效的 Rust 类型可以传递给指令来定义程序的输入。(First, let's start with the initialize instruction. Notice the data argument passed into the program. This argument and any other valid Rust types can be passed to the instruction to define inputs to the program.)
//Additionally, notice how we take a mutable reference to my_account and assign the data to it. This leads us to the Initialize struct, deriving Accounts.
#[derive(Accounts)]
pub struct Initialize<'info>{
#[account(init, payer = user, space = 8 +8)]
//my_account 字段用 init 属性标记。这将创建一个由当前程序拥有的新帐户,初始化为零。使用 init 时,还必须提供付款人,它将为帐户创建提供资金,定义帐户应该有多大的空间,以及运行时创建帐户所需的 system_program。
pub my_account:Account<'info,MyAccount>,
//my_account 字段的类型为 ProgramAccount<'info, MyAccount>,告诉程序它必须为当前正在执行的程序所有(相当于check program ID),反序列化的数据结构为 MyAccount。
//The my_account field is of type ProgramAccount<'info, MyAccount>, telling the program it must be owned by the currently executing program, and the deserialized data structure is MyAccount.
#[account(mut)]
pub user:Signer<'info>,
pub system_program:Program<'info,System>,
}
pub fn update(ctx:Context,data:u64)->
ProgramResult{
let my_account=&mut ctx.accounts.my_account;
my_account.data=data;Ok(())}}
#[derive(Accounts)]
pub struct Update<'info>{
#[account(mut)]
pub my_account:Account<'info,MyAccount>,
}
//同样,更新帐户结构用 #[account(mut)] 属性标记。将帐户标记为 mut 会保留退出程序时所做的任何更改。
#[account]
pub struct MyAccount{
pub data:u64,}
在这里,我们已经介绍了如何与帐户交互的基础知识。在后面的教程中,我们将更深入地研究派生帐户,但现在,只知道您必须在第一次使用它时标记帐户 init 并标记 mut 以保持更改。
使用 Anchor 创建的所有帐户布局如下: 8-byte-discriminator || borsh 序列化数据。 8 字节鉴别器是从帐户类型的 Sha256 散列的前 8 个字节创建的——使用上面的示例,sha256("account:MyAccount")[..8]。 account: 是一个固定前缀。
重要的是,这允许程序确定某个帐户确实属于给定类型。没有它,程序很容易受到帐户注入攻击,恶意用户指定了一个意外类型的帐户,导致程序做出意外的事情。
在创建帐户时,这个 8 字节的鉴别器不存在,因为帐户存储被清零。 Anchor 程序第一次改变一个账户时,这个鉴别器会被添加到账户存储阵列中,所有随后对该账户的访问(没有用 #[account(init)] 修饰)都将检查这个鉴别器。
Creating and Initializing Accounts
reference:
1. https://project-serum.github.io/anchor/tutorials/tutorial-1.html#creating-and-initializing-accounts
传递给该方法的最后一个元素在 rpc 命名空间上的所有动态生成的方法中很常见,其中包含事务的几个选项。在这里,我们指定accounts 字段,一个包含交易需要接触的所有地址的对象,以及签署交易所需的所有Signer 对象的signers 数组。因为正在创建 myAccount,所以 Solana 运行时需要它来签署交易。
The last element passed into the method is common amongst all dynamically generated methods on the RPC namespace, containing several options for a transaction. Here, we specify the accounts field, an object of all the addresses the transaction needs to touch, and the signer's array of all Signer objects needed to sign the transaction. Because myAccount is being created, the Solana runtime requires it to sign the transaction.
referene:
1. https://project-serum.github.io/anchor/tutorials/tutorial-1.html#initialize-instruction
2.https://www.youtube.com/watch?v=oD1umX_DnUw&t=758s