Skip to main content

hello & problem

example

https://github.com/hpgo6688/anchor-hello.git

新项目部署的总是本地

alt text

修改

alt text

program id 不一致

alt text

在 Solana 上,每次部署程序时,都会根据程序的内容和部署参数生成一个新的程序 ID。如果你在本地构建后进行多次部署,ID 可能会不一致。

要确保程序 ID 一致,你可以在 Anchor.toml 中指定一个固定的程序 ID。例如:

[programs.devnet]
hello = "你的固定程序ID"

然后,确保在部署时使用这个配置:

anchor deploy --provider.cluster devnet

这样,Anchor 会使用你指定的程序 ID 进行部署。

alt text

anchor idl init -f 不成功问题

alt text

alt text

anchor idl fetch

alt text

anchor keys list

$  ~/Desktop/temp/solana/hello anchor keys list
hello: AfaZgexvDNmt8K2km9MPEtabNkw3GNq7fsiF1K1bSxj9

delpoy balance 消耗太多sol

$  ~ solana balance
500000000 SOL

$ ~/Desktop/temp/solana/hello anchor deploy
Deploying cluster: http://localhost:8899
Upgrade authority: /Users/haotian.chen/.config/solana/id.json
Deploying program "hello"...
Program path: /Users/haotian.chen/Desktop/temp/solana/hello/target/deploy/hello.so...
Program Id: AfaZgexvDNmt8K2km9MPEtabNkw3GNq7fsiF1K1bSxj9

Deploy success
$ ~/Desktop/temp/solana/hello solana balance
499999998.741322517 SOL


$ ~/Desktop/temp/solana/hello anchor deploy
Deploying cluster: http://localhost:8899
Upgrade authority: /Users/haotian.chen/.config/solana/id.json
Deploying program "hello"...
Program path: /Users/haotian.chen/Desktop/temp/solana/hello/target/deploy/hello.so...
Program Id: AfaZgexvDNmt8K2km9MPEtabNkw3GNq7fsiF1K1bSxj9

Deploy success
$ ~/Desktop/temp/solana/hello solana balance
499999998.740412533 SOL


solana 程序部署,交易次数太多179笔交易

alt text

space to small 改了一点就不行了

  1. 增加了一行日志,无法二次部署
  2. 第一次部署花费2.6 sol
  3. anchor deploy 一次,为什么有几十个交易?

alt text

alt text

$  ~/Desktop/temp/solana/hello anchor deploy
Deploying cluster: http://localhost:8899
Upgrade authority: /Users/haotian.chen/.config/solana/id.json
Deploying program "hello"...
Program path: /Users/haotian.chen/Desktop/temp/solana/hello/target/deploy/hello.so...
Program Id: F9cCiXLwxPd5MmbMLHLTSenZGaKRKUNkftobtGphTSrj

Deploy success
$ ~/Desktop/temp/solana/hello solana balance
499999993.706014693 SOL

alt text

devnet

alt text

cargo clean && anchor build && anchor deploy id change

地址改变是因为 target/deploy/hello-keypair.json 被清理掉了?

是的,target/deploy/hello-keypair.json 文件包含了部署时的程序密钥对。如果这个文件被删除或清理,Solana 会在下次部署时生成一个新的程序 ID。这会导致你的程序 ID 与 Anchor.toml 中声明的不匹配,从而导致 DeclaredProgramIdMismatch 错误。

要解决这个问题,你可以:

  1. 备份程序密钥对

    • 确保 hello-keypair.json 文件被安全保存,以便在重新部署时使用相同的程序 ID。
  2. 更新配置

    • 如果你确实需要重新生成密钥对,确保在 Anchor.toml 中更新新的程序 ID。
  3. 避免清理关键文件

    • 在使用 cargo clean 或其他清理命令时,注意不要删除 hello-keypair.json,或者在清理后手动恢复该文件。

这样可以确保你的程序 ID 在部署和测试过程中保持一致。

问题解决solved

备份 keypair.json , 下次 anchor build 完,将 备份的 keypair.json 恢复

问题解决操作记录

  1. solana program show --programs
  2. cargo clean
  3. anchor build
  4. anchor keys sync
  5. recovery keypair.json # 恢复
  6. anchor keys sync
  7. solana program show --programs
  8. solana balance # 通过账户余额前后变化,看看是升级还是新部署,新部署,要2.5sol
  9. anchor deploy
  10. solana balance

alt text

升级program

alt text

关闭后不能重启

solana program close BBSapo4qsKqN77YNzjHBmUbmcQWdYrUjoMz5oJdRkpZ6 --bypass-warning

alt text

https://www.rareskills.io/post/solana-close-account

alt text