TypeError: anchor.BN is not a constructor

Solana Prgram Examples - Transfer Hooks をテストしていたときに発生した現象。

環境

  • ローカル環境は、 @coral-xyz/anchor@0.31.1 を利用
  • package.jsonは、 "@coral-xyz/anchor": "^0.30.1" を指定

現象

pnpm install してから anchor test を実行すると以下のエラーが発生。

anchor test

WARNING: `@coral-xyz/anchor` version(^0.30.0) and the current CLI version(0.31.1) don't match.

    This can lead to unwanted behavior. To fix, upgrade the package by running:

    yarn upgrade @coral-xyz/anchor@0.31.1

~~~ omit ~~~

Found a 'test' script in the Anchor.toml. Running it as a test suite!

Running test suite: "/Users/256hax/Desktop/dev_space/program-examples/tokens/token-2022/transfer-hook/hello-world/anchor/Anchor.toml"

(node:69272) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:69272) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///Users/256hax/Desktop/dev_space/program-examples/tokens/token-2022/transfer-hook/hello-world/anchor/tests/transfer-hook.ts is not specified and it doesn't parse as CommonJS.
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
To eliminate this warning, add "type": "module" to /Users/256hax/Desktop/dev_space/program-examples/tokens/token-2022/transfer-hook/hello-world/anchor/package.json.
bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)


  transfer-hook
Your transaction signature XAEB53MFEqDvYYFkv6dbkJ6cthmEq2hyX69Kae7Eidsk59WCF1Av8Jhn2vzs85kwmP5A2BNSD3vCyi6zBAHJ8SV
    ✔ Create Mint with Transfer Hook Extension (557ms)
Transaction Signature: 3Tp5DatYGB1FouXivPGFpVcfuJy4JZVtQiAcv5Siy3Yq5EARGcaXMBgpEYH4zDweGsnZSX9tAncorGWv6pdcuhUw
    ✔ Create Token Accounts and Mint Tokens (460ms)
Transaction Signature: 2BjfAn1WxGowqShSDMtwDoaA1tjiFrAsNjktdpZxKqEUd886YfCvus7YSYxpm8yY8dizSxY2RCnpo74iftXFBgRq
    ✔ Create ExtraAccountMetaList Account (509ms)
Transfer Signature: zxpWokpXbR37ZD4KHcdDacphEchh3QUDQtE76ffzDEHxftsf1CcYUbwLemqE5NKP77GdBfR5rToiDjz1xYin4uX
    ✔ Transfer Hook with Extra Account Meta (469ms)
    1) Try call transfer hook without transfer


  4 passing (2s)
  1 failing

  1) transfer-hook
       Try call transfer hook without transfer:
     TypeError: anchor.BN is not a constructor
      at Context.<anonymous> (file:///Users/256hax/Desktop/dev_space/program-examples/tokens/token-2022/transfer-hook/hello-world/anchor/tests/transfer-hook.ts:133:21)
      at processImmediate (node:internal/timers:505:21)

原因

Warp Terminal で、AIを使って自動的に修正してもらった。

I can see the issue is still present. The test is failing because anchor.BN is not available in the newer version of Anchor. Let me check the current state of the test file and fix the BN import issue.

Anchor version 31から、anchor.BNがなくなったとのこと。
(package.jsonでは30以下を指定)

対策

package.jsonに記載のAnchorのversionを使うか、もしくはBNをインポートして使う。
最新バージョンはanchor.BNが使えなくなっていることもあり、 Warp Terminal の提案はソース修正だったので、今回はそれに従った。

補足

Anchorのバージョンを下げたい場合は、Anchorのavmコマンドを使う。

avm list

0.29.0
0.30.0  (installed)
0.30.1
0.31.0
0.31.1  (latest, installed, current)
avm install 0.30.1
avm use 0.30.1
avm list

0.29.0
0.30.0  (installed)
0.30.1  (installed, current)
0.31.0  (installed)
0.31.1  (latest, installed)