Solana Anchorのチュートリアル「A Minimal Example」のハマリポイント

原因究明まではやっていないため、以下すべて仮説。今回は対応策で解決はできたものの、状況によって異なる可能性あり。

found no record of a prior creditエラー

現象

「ANCHOR_WALLET= node client.js」を実行したときに出るエラー。

# ANCHOR_WALLET=/root/.config/solana/id.json node client.js
Running client.
Transaction simulation failed: Attempt to debit an account but found no record of a prior credit.

Translating error SendTransactionError: failed to send transaction: Transaction simulation failed: Attempt to debit an account but found no record of a prior credit.
    at Connection.sendEncodedTransaction (/usr/src/app/anchor-master/examples/tutorial/node_modules/@solana/web3.js/lib/index.cjs.js:6591:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Connection.sendRawTransaction (/usr/src/app/anchor-master/examples/tutorial/node_modules/@solana/web3.js/lib/index.cjs.js:6550:20)
    at async sendAndConfirmRawTransaction (/usr/src/app/anchor-master/examples/tutorial/node_modules/@solana/web3.js/lib/index.cjs.js:8513:21)
    at async Provider.send (/usr/src/app/anchor-master/examples/tutorial/node_modules/@project-serum/anchor/dist/cjs/provider.js:85:22)
    at async Object.rpc [as initialize] (/usr/src/app/anchor-master/examples/tutorial/node_modules/@project-serum/anchor/dist/cjs/program/namespace/rpc.js:11:31)
    at async main (/usr/src/app/anchor-master/examples/tutorial/basic-0/client.js:22:3) {
  logs: []
}
node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

SendTransactionError: failed to send transaction: Transaction simulation failed: Attempt to debit an account but found no record of a prior credit.
    at Connection.sendEncodedTransaction (/usr/src/app/anchor-master/examples/tutorial/node_modules/@solana/web3.js/lib/index.cjs.js:6591:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Connection.sendRawTransaction (/usr/src/app/anchor-master/examples/tutorial/node_modules/@solana/web3.js/lib/index.cjs.js:6550:20)
    at async sendAndConfirmRawTransaction (/usr/src/app/anchor-master/examples/tutorial/node_modules/@solana/web3.js/lib/index.cjs.js:8513:21)
    at async Provider.send (/usr/src/app/anchor-master/examples/tutorial/node_modules/@project-serum/anchor/dist/cjs/provider.js:85:22)
    at async Object.rpc [as initialize] (/usr/src/app/anchor-master/examples/tutorial/node_modules/@project-serum/anchor/dist/cjs/program/namespace/rpc.js:11:31)
    at async main (/usr/src/app/anchor-master/examples/tutorial/basic-0/client.js:22:3) {
  logs: []
}

原因

取引履歴が存在していない。つまり、チュートリアルのプログラムのdeployがちゃんとできていない。

対策

1.Solanaノードを起動しておく。

# solana-test-validator
--faucet-sol argument ignored, ledger already exists
Ledger location: test-ledger
Log: test-ledger/validator.log
Identity: 96aeBVR8ZopwXx3ttLiQWxjaVuWFfBhsmhocP1nj6ttK
Genesis Hash: 3rrq1z2jgqA74vVKJLseFiQvmhG7aF2zdP1iWayYsXiZ
Version: 1.8.0
Shred Version: 48177
Gossip Address: 127.0.0.1:1024
TPU Address: 127.0.0.1:1027
JSON RPC URL: http://127.0.0.1:8899

2.Solanaの設定をlocalhostにする

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

# solana config set --url localhost
Config File: /root/.config/solana/cli/config.yml
RPC URL: http://localhost:8899
WebSocket URL: ws://localhost:8900/ (computed)
Keypair Path: /root/.config/solana/id.json
Commitment: confirmed

3.10 SOL airdropする
localhost, devnet, testnet, mainnetと、いろんなサーバーがあるが、トランザクションがなにかしら走る場合は、SOLの手数料が必要。

# solana airdrop 10
Requesting airdrop of 10 SOL

Signature: 53QEKUxsrjueDUP1XUmGxuK6ULDX9vNdNxRNRteiMw1ZyhqipLHvYN2rsn6BUHQkj1RyBTahp4WXiNJBH6mFLZJF

10 SOL

4.プログラムをデプロイする

# anchor deploy
Deploying workspace: http://localhost:8899
Upgrade authority: /root/.config/solana/id.json
Deploying program "basic-0"...
Program path: /usr/src/app/anchor-master/examples/tutorial/basic-0/target/deploy/basic_0.so...
Program Id: 8Cj6ZYCXk6F15TgJqPr9pSjtMFaY4wmhxVcQPghcGeDs

Deploy success

5.client.jsを起動する
Successになったら成功。

# ANCHOR_WALLET=/root/.config/solana/id.json node client.js
Running client.
Success

insufficient fundsエラー

現象

# anchor deploy
Deploying workspace: http://localhost:8899
Upgrade authority: /root/.config/solana/id.json
Deploying program "basic-0"...
Program path: /usr/src/app/anchor-master/examples/tutorial/basic-0/target/deploy/basic_0.so...
=================================================================================
Recover the intermediate account's ephemeral keypair file with
`solana-keygen recover` and the following 12-word seed phrase:
=================================================================================
scatter music supreme arm someone swap indicate strong stairs good appear because
=================================================================================
To resume a deploy, pass the recovered keypair as
the [PROGRAM_ADDRESS_SIGNER] argument to `solana deploy` or
as the [BUFFER_SIGNER] to `solana program deploy` or `solana write-buffer'.
Or to recover the account's lamports, pass it as the
[BUFFER_ACCOUNT_ADDRESS] argument to `solana program close`.
=================================================================================
Error: Account 3H3yDb6ph9fAT6iowbwizvR2CXCHJPoLBa9xsQtrGk5V has insufficient funds for spend (1.05455832 SOL) + fee (0.00077 SOL)
There was a problem deploying: Output { status: ExitStatus(ExitStatus(256)), stdout: "", stderr: "" }.

原因

手数料が足りない場合に発生するエラー。

対策

ケース1.devnet, testnet, mainnetの場合はSOLの手数料をゲットしておく。「solana testnet faucet」などで検索すると出てくる。
ケース2.localhostの想定だった場合は、「# solana config get」でlocalhostの設定になっているか確認する。もし違っていたら、「# solana config set --url localhost」で設定する。そのあとに「# solana airdrop 10」などのコマンドで手数料分の残高を増やしておく。

FetchError: request to http://localhost:8899/エラー

現象

# ANCHOR_WALLET=/root/.config/solana/id.json node client.js
Running client.
Translating error Error: failed to get recent blockhash: FetchError: request to http://localhost:8899/ failed, reason: connect ECONNREFUSED 127.0.0.1:8899
    at Connection.getRecentBlockhash (/usr/src/app/anchor-master/examples/tutorial/node_modules/@solana/web3.js/lib/index.cjs.js:5965:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Provider.send (/usr/src/app/anchor-master/examples/tutorial/node_modules/@project-serum/anchor/dist/cjs/provider.js:77:31)
    at async Object.rpc [as initialize] (/usr/src/app/anchor-master/examples/tutorial/node_modules/@project-serum/anchor/dist/cjs/program/namespace/rpc.js:11:31)
    at async main (/usr/src/app/anchor-master/examples/tutorial/basic-0/client.js:22:3)
node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

Error: failed to get recent blockhash: FetchError: request to http://localhost:8899/ failed, reason: connect ECONNREFUSED 127.0.0.1:8899
    at Connection.getRecentBlockhash (/usr/src/app/anchor-master/examples/tutorial/node_modules/@solana/web3.js/lib/index.cjs.js:5965:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Provider.send (/usr/src/app/anchor-master/examples/tutorial/node_modules/@project-serum/anchor/dist/cjs/provider.js:77:31)
    at async Object.rpc [as initialize] (/usr/src/app/anchor-master/examples/tutorial/node_modules/@project-serum/anchor/dist/cjs/program/namespace/rpc.js:11:31)
    at async main (/usr/src/app/anchor-master/examples/tutorial/basic-0/client.js:22:3)

Node.js v17.1.0

原因

localhostのSolanaノード(solana-test-validator)が起動していない。

対策

localhostのSolanaノードを起動する。

# solana-test-validator

node: --dns-result-order= is not allowedエラー

現象

【テスト結果が出ない状態(AsIs)】
一見、成功しているように見えるが、テストが走っていない。passingやdescribeが出力されておらず、テスト結果がfailになるように修正してもfailにならない(反映されない)。

$ anchor test
BPF SDK: /Users/user/.local/share/solana/install/releases/1.8.0/solana-release/bin/sdk/bpf
Running: rustup toolchain list -v
Running: cargo +bpf build --target bpfel-unknown-unknown --release
    Finished release [optimized] target(s) in 0.48s
Running: /Users/user/.local/share/solana/install/releases/1.8.0/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /Users/user/Desktop/blockchain/anchor-master/examples/tutorial/basic-0/target/deploy/basic_0.so

To deploy this program:
  $ solana program deploy /Users/user/Desktop/blockchain/anchor-master/examples/tutorial/basic-0/target/deploy/basic_0.so
The program address will default to this keypair (override with --program-id):
  /Users/user/Desktop/blockchain/anchor-master/examples/tutorial/basic-0/target/deploy/basic_0-keypair.json
node: --dns-result-order= is not allowed in NODE_OPTIONS

【テスト結果が出る状態(本来期待する結果ToBe)】

# anchor test
BPF SDK: /root/.local/share/solana/install/releases/1.8.0/solana-release/bin/sdk/bpf
Running: rustup toolchain list -v
Running: cargo +bpf build --target bpfel-unknown-unknown --release
    Finished release [optimized] target(s) in 1.69s
Running: /root/.local/share/solana/install/releases/1.8.0/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /usr/src/app/anchor-master/examples/tutorial/basic-0/target/deploy/basic_0.so

To deploy this program:
  $ solana program deploy /usr/src/app/anchor-master/examples/tutorial/basic-0/target/deploy/basic_0.so
The program address will default to this keypair (override with --program-id):
  /usr/src/app/anchor-master/examples/tutorial/basic-0/target/deploy/basic_0-keypair.json
yarn run v1.22.17
$ /usr/src/app/anchor-master/examples/tutorial/node_modules/.bin/mocha -t 1000000 tests/


  basic-0
    ✔ Uses the workspace to invoke the initialize instruction (2758ms)


  1 passing (3s)

Done in 8.28s.

原因

anchorのバグ。Issues w/ anchor test #945
1.0.18.2で修正済み。

対策

Anchor CLIを最新にする。
インストールされているバージョンを調べる。自分の場合は0.18.0だった。

$ anchor --version
anchor-cli 0.18.0

Anchor公式サイトの Build from source for other operating systems に沿ってインストールする。

$ cargo install --git https://github.com/project-serum/anchor --tag v0.18.2 anchor-cli --locked

・・・

$ anchor --version
anchor-cli 0.18.2

ReferenceError: z is not definedエラー

現象

$ anchor test
BPF SDK: /Users/user/.local/share/solana/install/releases/1.8.0/solana-release/bin/sdk/bpf
Running: rustup toolchain list -v
Running: cargo +bpf build --target bpfel-unknown-unknown --release
    Finished release [optimized] target(s) in 0.47s
Running: /Users/user/.local/share/solana/install/releases/1.8.0/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /Users/user/Desktop/blockchain/anchor-master/examples/tutorial/basic-0/target/deploy/basic_0.so

To deploy this program:
  $ solana program deploy /Users/user/Desktop/blockchain/anchor-master/examples/tutorial/basic-0/target/deploy/basic_0.so
The program address will default to this keypair (override with --program-id):
  /Users/user/Desktop/blockchain/anchor-master/examples/tutorial/basic-0/target/deploy/basic_0-keypair.json
yarn run v1.22.17
$ /Users/user/Desktop/blockchain/anchor-master/examples/tutorial/node_modules/.bin/mocha -t 1000000 tests/

ReferenceError: z is not defined
    at Object.<anonymous> (/Users/user/Desktop/blockchain/anchor-master/examples/tutorial/basic-0/tests/basic-0.js:17:1)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at ModuleWrap.<anonymous> (internal/modules/esm/translators.js:195:29)
    at ModuleJob.run (internal/modules/esm/module_job.js:145:37)
    at async Loader.import (internal/modules/esm/loader.js:182:24)
    at async formattedImport (/Users/user/Desktop/blockchain/anchor-master/examples/tutorial/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async Object.exports.requireOrImport (/Users/user/Desktop/blockchain/anchor-master/examples/tutorial/node_modules/mocha/lib/nodejs/esm-utils.js:48:32)
    at async Object.exports.loadFilesAsync (/Users/user/Desktop/blockchain/anchor-master/examples/tutorial/node_modules/mocha/lib/nodejs/esm-utils.js:88:20)
    at async singleRun (/Users/user/Desktop/blockchain/anchor-master/examples/tutorial/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/Users/user/Desktop/blockchain/anchor-master/examples/tutorial/node_modules/mocha/lib/cli/run.js:374:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

原因

原因はわからなかったが、いじりまくっているうちにおかしくなってしまったと思われる。

対策

きれいな状態で1からやり直す。自分の場合は以下2点で直った。

  1. 以下をもとに環境を最新版にする。Yarnも最新版にする。
    Installing Dependencies
  2. Anchorの最新版をGitHubから改めてDLしてから、再度チュートリアルを実行する。
    GitHub - project-serum/anchor

ログがどこにあるかわからない

deployログは .anchor > program-logs にアドレス毎に出力される。

参考

anchor testがうまくいったときのバージョン。

# anchor --version
anchor-cli 0.18.2

# solana --version
solana-cli 1.8.0 (src:4a8ff62a; feat:1813598585)

# rustup --version
rustup 1.24.3 (ce5817a94 2021-05-31)

# npm -v
8.1.2

# yarn -v
1.22.17