1

I am trying to map the parking space in a location. In the flowing is my account and method to return the available position in a location.

#[account]
pub struct Location {
    pub id : String,            // 40 bytes
    pub state_id: u8,           // 1
    pub city_id: u16,           // 2
    // position
    pub top_left : Point,       // 8
    pub bottom_right : Point,   // 8

    pub avaliable_spots: Vec<bool>,

}


impl Location {
   pub fn get_avaliable_spots(&mut self) -> Vec<u8> {
        

        let mut avaliable_spots = Vec::<u8>::new();


        //TODO: CHECK REMOVE PROTECTED CHANELS
        for i in 0..50 {
            if self.spots[i] == false {
                let ui = i as u8;
                avaliable_spots.push(ui + 1);
            }
        }
        avaliable_spots 
        
}

I've create this method to return the avaliable spots of one location.

pub fn get_avaliable_spots_in_location(ctx: Context<GetSpotsInLocation> ) -> Vec<u8> {

    let location = &mut ctx.accounts.location;
    // Ok(())
    location.get_avaliable_spots()
    
}

This is my client code (javascript)

async function setAvaliableSpots(program,key_pair,bd_loc_id,spots) {
    let [bc_loc_id, location_bump] = await bcUtil.genLocationPDA(program,bd_loc_id);
    let loc = await program.account.location.fetch(bc_loc_id);
    let spot_list = Uint8Array.from(spots.map(Number))

    console.log(loc) // this is printed

    //rever esse metodo
    let tx = await program.rpc.setSpotsInLocation( spot_list, {
        accounts: {
            creator: key_pair.publicKey,
            location: bc_loc_id,
            systemProgram: anchor.web3.SystemProgram.programId,
        }, 
        signers: [key_pair]
    })
    console.log('tx: ' + tx);
    console.log('---------------');
}

when executing the code I get the following error:

sendTransactionError: failed to send transaction: Transaction simulation failed: Attempt to debit an account but found no record of a prior credit.
    at Connection.sendEncodedTransaction (/workspaces/xxx/node_modules/@solana/web3.js/lib/index.cjs.js:7580:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Connection.sendRawTransaction (/workspaces/xxx/node_modules/@solana/web3.js/lib/index.cjs.js:7537:20)
    at async sendAndConfirmRawTransaction (/workspaces/xxx/node_modules/@project-serum/anchor/dist/cjs/provider.js:173:23)
    at async AnchorProvider.sendAndConfirm (/workspaces/xxx/node_modules/@project-serum/anchor/dist/cjs/provider.js:89:20)
    at async Object.rpc [as getAvaliableChannelInLocation] (/workspaces/xxx/node_modules/@project-serum/anchor/dist/cjs/program/namespace/rpc.js:15:24)
    at async getAvaliableChannels (/workspaces/xxx/board/app/examples/bcChannelLocationQuery.js:56:14) {
  logs: [],
  programErrorStack: ProgramErrorStack { stack: [] }
}

It is important to mention that my key is valid in the devnet.

PublicKey {
  _bn: <BN: b2e38753222e28b686bfddc8927ddb7d73496fd48b2f1bfc4e63194349239195>
}

Does any one have an idea how can i fix this error?

PS: My wallet has lampors

root@d7a686c37024:/workspaces/xxx# solana balance 12.65069892 SOL

7
  • What is the cluster you are using in the client code?
    – vicyyn
    Commented Jan 25, 2023 at 15:42
  • Hello vicyyn, the program are hosted in the devnet.
    – Thiago
    Commented Jan 27, 2023 at 11:31
  • I'm talking about the client code. How are you creating your program object in your client code?
    – vicyyn
    Commented Jan 27, 2023 at 11:43
  • Oh sorry, I get the account ( program.fetch(id) ) and convert the spots to Uint8Array let spot_list = Uint8Array.from(spots.map(Number)) let tx = await program.rpc.setSpotsInLocation(spot_list, { accounts: { creator: key_pair.publicKey, location: bc_loc_id, systemProgram: anchor.web3.SystemProgram.programId, }, signers: [key_pair] })
    – Thiago
    Commented Jan 28, 2023 at 13:01
  • 1
    @vicyyn, thank you for the asking that. I was able to find my mistake,
    – Thiago
    Commented Jan 28, 2023 at 21:38

1 Answer 1

1

@vicyyn , Thanks for the help.

It was a mistake in my wallet,I have commented the wrong line of code.

let myWallet = new anchor.Wallet(web3js.Keypair.generate());
//let myWallet = new anchor.Wallet(myKeypair);

Actually, I was creating a new wallet instead of opening my real wallet(with SOL). When you ask for the initialization of the program I was able to find my mistake.

//let myWallet = new anchor.Wallet(web3js.Keypair.generate());
let myWallet = new anchor.Wallet(myKeypair);

Once again thank you.

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.