Open1
Anchor
Anchor
リソース
- ⚓ Anchor
- The Complete Guide to Full Stack Solana Development with React, Anchor, Rust, and Phantom - DEV Community
やること
- Anchorでの開発の習得
メモ
- AnchorはSolanaのProgramとそのクライアントの開発のためのフレームワーク
-
A Minimal Example | ⚓ Anchor
-
basic-0: Anchorの動かし方
- Anchorの使い方解説が主
-
anchor build- IDLを生成する。SolidityでいうところのABIと同じ(多分)。
-
anchor deploy- 文字通りデプロイ
-
ANCHOR_WALLET=<YOUR-KEYPAIR-PATH> node client.js- IDLを使ってclientからRPCでProgramとやりとりを行う
- Tutorial0では
NODE_PATHを元に実行するのでexport NODE_PATH="$(npm config get prefix)/lib/node_modules"を忘れない。忘れるとCannot find module '@project-serum/anchor'というエラーが出る。
-
anchor test- networkの起動~deploy~testまでやってくれる
-
basic-1: Accountの作成と基礎
-
#[program]-
#[program]のmodule(program)にはRPC request handler(いわゆるinstruction)を書く。clientから呼び出されるendpointみたいなやつになる。
-
-
#[account]- structにserializeとdeserializeを実装する
-
#[account]のstructは#[program]の引数にContextに加えて渡される。
-
#[derive(Accounts)]-
&[AccountInfo]をstructにデコードできるようにする -
#[derive(Accounts)]のstructではprogramのinstructionで使われる全てのAccountを定義する。これはprogramのContext<Initialize>やContext<Update>のような形でinstructionの引数のcontextに渡される。 -
#[account(init, payer = user, space = 8 + 8)]- initは新しく作成されるAccountには必要。payerが誰でどのくらいのサイズを必要とするかを指定する(これによってガス代が変わるので必要)。
-
#[account(mut)]- ProgramによるAccountの変更を保持することを意味する
-
-
Account- deserializeされたAccountのラッパー。
-
AccountsをderiveしているstructのAccountはdeclared_id!で定義されたProgramに所有されてることが保証される。
-
-
basic-2: Access Controlについて
-
#[account(mut, has_one = authority)]-
#[derive(Accounts)]のstructで利用すると、そのAccountへのアクセス制限が可能になる。例えばこの例でいうとIncrement.counter.authority == Increment.authority.keyに等しい制限をかけられる。
#[account] pub struct Counter { pub authority: Pubkey, // <- このauthorityと pub count: u64, } #[derive(Accounts)] pub struct Increment<'info> { #[account(mut, has_one = authority)] pub counter: Account<'info, Counter>, pub authority: Signer<'info>, // <- このauthorityが一致しないとAccountにアクセスできないようになる } -
-
-
basic-3: 異なるProgram間での呼び出し:
-
CpiContext- programIdと関連するaccountをまとめてcontextを作る。このcontextを
puppet::cpi::set_data(cpi_ctx, data)みたいな形でCPIで呼び出すinstructionに渡す
- programIdと関連するaccountをまとめてcontextを作る。このcontextを
-
-
basic-4: Errors
-
#[error]-
#[error]で定義されたenumは各fieldに#[msg("This is a custom error.")]みたいな感じでアノテーションをつけられる。あとはErr(ErrorCode::Hello.into())感じで使えば良い。
-
-
-
basic-0: Anchorの動かし方
-
The Complete Guide to Full Stack Solana Development with React, Anchor, Rust, and Phantom - DEV Community
- AnchorでCRUD(Dはないけど)アプリを作ってみるTutorial
-
anchor testでいきなりFailed to run test: mocha -t 1000000 tests/: No such file or directoryが出るが...- issueが報告されてる: Issues w/ anchor test · Issue #945 · project-serum/anchor
-
Anchor.tomlを下記のように修正test = "node_modules/mocha/bin/mocha -t 1000000 tests/"
-
- issueが報告されてる: Issues w/ anchor test · Issue #945 · project-serum/anchor
- localhostのairdropが出来ない
- URLを指定したらできた:
solana airdrop 1 <WALLET ADDRESS> --url http://127.0.0.1:8899
- URLを指定したらできた:
- Javascriptだとparameterの間違いに気づけなかったりでハマりやすいのでTypescriptでUIは書いた方がいいと思った(
signersをsignerと書いておりしばらくハマったので...)
-
- AnchorでCRUD(Dはないけど)アプリを作ってみるTutorial
-
Anchor Example: Escrow Program - HackMD
- わからなすぎなのでとりあえずツッコミを入れながら写経することにした
- ツッコミ入れたバージョン。一応完全に理解できた感はある。
- わからなすぎなのでとりあえずツッコミを入れながら写経することにした