I wrote a Solana Program with two functions, one for the initialization of an account and a second to upload data to the account. While testing on localhost it was working fine, but when I uploaded it to Devnet I get the following error:
Error: AnchorError: AnchorError caused by account: base_account. Error Code: AccountNotInitialized. Error Number: 3012. Error Message: The program expected this account to be already initialized.
at AnchorError.parse (C:\Development\SmartHealth\smarthealth_js\node_modules\@project-serum\anchor\dist\cjs\error.js:138:20)
at translateError (C:\Development\SmartHealth\smarthealth_js\node_modules\@project-serum\anchor\dist\cjs\error.js:224:37)
at Object.rpc [as uploadRecord] (C:\Development\SmartHealth\smarthealth_js\node_modules\@project-serum\anchor\dist\cjs\program\namespace\rpc.js:18:53)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async uploadToSolana (file:///C:/Development/SmartHealth/smarthealth_js/index.js:69:22)
at async file:///C:/Development/SmartHealth/smarthealth_js/index.js:131:28
The error comes up only when I try to upload the data and sometimes it doesn't come up at all and the data gets uploaded successfully to the account. I am not sure why I am getting this error sometimes. Can someone please tell me what is the problem?
Below is the JS code through which I am calling the Solana program functions:
const tx = await program.rpc.initialize({
accounts:{
baseAccount: baseAccount.publicKey,
user:provider.wallet.publicKey,
systemProgram: SystemProgram.programId,
},
signers: [baseAccount]
}).catch((err)=>{
console.log(err)
return {"Error":err}
});
console.log('New Base Account Generated: ',baseAccount.publicKey.toString());
console.log("Transaction Signature: ",tx);
const timestamp = new Date().toUTCString()
const uploadTx = await program.rpc.uploadRecord(pid,did,title,file_id,timestamp,{
accounts: {
baseAccount: baseAccount.publicKey,
user:provider.wallet.publicKey,
},
}).catch((err)=>{
console.log(err)
return {"Error":err}
});
I have even tried keeping the pid,did, title,file_id values the same but still I get the error sometimes. Even the account gets generated as I get the transaction signature for that on the console but data doesn't get uploaded.