5

I'm currently trying to run an escrow program and when i turn on the solana-test-validator in a different terminal it gives me this error:

failed to send transaction: Transaction simulation failed: Attempt to load a program that does not exist

When I turn that off i get the following error:

failed to get recent blockhash: FetchError: request to http://localhost:8899/ failed, reason: connect ECONNREFUSED 127.0.0.1:8899

I'm on the localhost btw

Any help is much appreciated!

EDIT: I have an instruction in my src folder as such:

#[derive(Accounts)]
#[instruction(application_idx: u64)]
pub struct InitNewExchange<'info> {
    #[account(
        init, 
        payer = user_sending,
        space = Exchange::LEN,
        seeds = [b"exchange".as_ref(), 
                user_sending.key().as_ref(), 
                user_receiving.key.as_ref(),
                mint_of_token_being_sent.key().as_ref(),
                application_idx.to_le_bytes().as_ref()],
        bump,
    )]
    application_exchange: Account<'info, Exchange>,

    #[account(
        init,
        payer = user_sending,
        seeds = [b"exchange".as_ref(), 
                user_sending.key().as_ref(), 
                user_receiving.key.as_ref(),
                mint_of_token_being_sent.key().as_ref(),
                application_idx.to_le_bytes().as_ref()],
        bump,
        token::mint =  mint_of_token_being_sent,
        token::authority = application_exchange,
    )]
    //Should not be signer or writable
    escrow_wallet_exchange: Account<'info, TokenAccount>,

    #[account(mut)]
    user_sending: Signer<'info>,
    user_receiving: AccountInfo<'info>,
    mint_of_token_being_sent: Account<'info, Mint>,

    #[account(
        mut,
        constraint = wallet_to_withdraw_from.owner == user_sending.key(),
        constraint = wallet_to_withdraw_from.mint == mint_of_token_being_sent.key(),
    )]
    wallet_to_withdraw_from: Account<'info, TokenAccount>,
    system_program: Program<'info, System>,
    token_program: Program<'info, Token>,
    rent: Sysvar<'info, Rent>,}

and the following handler function for it:

pub fn handler(
    ctx: Context<InitNewExchange>,
    application_idx: i64,
    amount: i64
) -> Result<()> { 
    //Assign attributes to Exchange
    let exchange = &mut ctx.accounts.application_exchange;
    exchange.idx = application_idx;
    exchange.user_sending = ctx.accounts.user_sending.key().clone();
    exchange.user_receiving = ctx.accounts.user_receiving.key().clone();
    exchange.mint_of_token_being_sent = ctx.accounts.mint_of_token_being_sent.key().clone();
    exchange.escrow_wallet = ctx.accounts.escrow_wallet_exchange.key().clone();
    exchange.amount_tokens = amount;
    exchange.bump = *ctx.bumps.get("application_exchange").unwrap();
    msg!("Initializing exchange instance for {} tokens", amount);

    let bump_vector = exchange.bump.to_le_bytes();
    let mint_of_token_being_sent_pk = ctx.accounts.mint_of_token_being_sent.key().clone();
    let application_idx_bytes = application_idx.to_le_bytes();

    let sender = ctx.accounts.user_sending.key();
    let receiver = ctx.accounts.user_receiving.key();

    let inner = vec![
        b"exchange".as_ref(),
        sender.as_ref(),
        receiver.as_ref(),
        mint_of_token_being_sent_pk.as_ref(),
        application_idx_bytes.as_ref(),
        bump_vector.as_ref(),
    ];

    let outer = vec![inner.as_slice()];

    let transfer_instruction = Transfer{
        from: ctx.accounts.wallet_to_withdraw_from.to_account_info(),
        to: ctx.accounts.escrow_wallet_exchange.to_account_info(),
        //Authority will be the one sending
        authority: ctx.accounts.user_sending.to_account_info(),
    };

    let cpi_ctx = CpiContext::new_with_signer(
        ctx.accounts.token_program.to_account_info(),
        transfer_instruction,
        outer.as_slice(), 
    );

    anchor_spl::token::transfer(cpi_ctx, exchange.amount_tokens.try_into().unwrap())?;
    Ok(())
}

and I ran anchor build, anchor deploy (both working), and anchor test (which did not work). The erroneous code comes from this chunk in my test file (but i think all my programs.method calls incur this error):

const tx1 = await program.methods
        .initNewExchange(pda.idx, amount)
        .accounts({
            applicationExchange: pda.exchangeKey,
            escrowWalletExchange: pda.escrowWalletKey,
            userSending: sender.publicKey,
            userReceiving: receiver.publicKey,
            //Account data type of mint
            mintOfTokenBeingSent: mintAddress,
            //Wallet of sender for that particular token
            walletToWithdrawFrom: senderWallet,
            systemProgram: anchor.web3.SystemProgram.programId,
            tokenProgram: spl.TOKEN_PROGRAM_ID,
            rent: anchor.web3.SYSVAR_RENT_PUBKEY,
        })
        .signers([sender])
        .rpc();

THANKS A LOT once again!!

2
  • 1
    Can you please update the question with the commands/code that you ran?
    – sohrab
    Jul 28, 2022 at 3:44
  • also the program you're targeting
    – trent.sol
    Jul 28, 2022 at 4:13

5 Answers 5

2

If you are running NodeJS version 17, try downgrading to version 16 and see if that helps.

Apparently, node v17 resolves localhost to IPv6 instead of IPv4 (127.0.0.1), which as been causing issues with solana-test-validator for some time.

1
  • I'm currently on version 16.15.0 though, checked by using node -v
    – Aengus
    Jul 29, 2022 at 15:50
2

I found that my error was because I did not change the program id to the one shown after i ran anchor deploy, I had to change it under [programs.localnet] in anchor.toml and the declare.id! field in lib.rs. Thanks everyone for the help!

1
  • 1
    please add code blocks referencing the config and code changes that were required
    – trent.sol
    Aug 2, 2022 at 6:22
0

@Aengus it seems like you haven't deployed your program on the net but trying to access PDA's via test cases. Try by first deploying your program on the net.

2
  • Ah i tried this but unfortunately it did not work. I ran anchor build, anchor deploy then anchor test but i ran into the same error. I am unsure if this would be relevant but I have a beforeEach hook in my test file that actually works, its just the program call that runs into issues. Thank you!
    – Aengus
    Jul 28, 2022 at 6:13
  • This is not an answer, but would be fine as a comment.
    – mikemaccana
    Aug 10, 2022 at 18:22
0

I tried to add this to Jupo's existing answer but the edit was rejected. 🤷🏻‍♂️

Node 17+ did indeed change the resolution order - node now uses the order from your OS, which may be IPV6 first.

However you don't need to downgrade node to use solana-test-validator, you can set the resolution to use IPV4 by default:

await dnsPromises.setDefaultResultOrder('ipv4first')
0

I remember facing this issue and then realising that I had deleted the target folder and on building the program later, I forgot to change the program_id in Anchor.toml and lib.rs.

Might wanna check this haha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.