简体   繁体   中英

How do I force Solana/Anchor methods to use the devnet?

In creating a simple program, I can't get Solana to use the de.net for its RPC connection. I keep getting the following error:

{
  blockhash: '7TTVjRKApwAqP1SA7vZ2tQHuh6QbnToSmVUA9kc7amEY',
  lastValidBlockHeight: 129662699
}
Error: failed to get recent blockhash: FetchError: request to http://localhost:8899/ failed, reason: connect ECONNREFUSED 127.0.0.1:8899
    at Connection.getRecentBlockhash (/home/simeon/dev/freelance/niels_vacancies/node_modules/@solana/web3.js/lib/index.cjs.js:6584:13)

even though I have set all of my settable constants like ANCHOR_PROVIDER_URL=https://api.de.net.solana.com , or the relevant entries in my Anchor.toml file. I also explicitly specify the following:

const connection = new anchor.web3.Connection("https://api.devnet.solana.com/", {commitment: "max"});
const wallet = anchor.Wallet.local();

const provider = new anchor.Provider(
    connection,
    wallet,
    {
        commitment: "max",
        preflightCommitment: "max",
        skipPreflight: false
    }
)

I even test console.log(await anchor.getProvider().connection.getLatestBlockhash()); to ensure that I can, in fact, get a blockhash from the de.net. What can I do to force the RPC calls to do so too?

You just have to set the Anchor.toml cluster to devnet and programs.devnet and then deploy the program using a wallet with devnet-sol. I will drop an Anchor.toml for devnet.

[features]
seeds = false
[programs.devnet]
first_program = "FPT...bd3"

[registry]
url = "https://anchor.projectserum.com"

[provider]
cluster = "devnet"
wallet = "PATH/TO/WALLET/WHO/WILL/PAY/FOR/DEPLOY.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

in this case the first_program is the program_id declared on the declare_id macro.

Then you can use ur test file totally normal with anchor.setProvider(anchor.Provider.env());

If you have already updated the anchor.toml to use de.net, and are having this issue with program.provider.connection.whatever or program.account.whatever.fetch.whatever , make sure that you have set the anchor provider BEFORE creating the program, eg:

  const provider = AnchorProvider.env();
  anchor.setProvider(provider);

must come before the line const program: Program<Whatever> = workspace.Whatever;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM