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

Viewed 552

In creating a simple program, I can't get Solana to use the devnet 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.devnet.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 devnet. What can I do to force the RPC calls to do so too?

1 Answers

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());

Related