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
program
object in your client code?