'Unable to fetch an already created account
I have the following api method, which I am calling when a button is clicked in the front end:-
export const sendMessage = async (content, roomPublicKey) => {
const { wallet, program, provider } = useWorkspace()
const message = web3.Keypair.generate()
const tx = await program.value.rpc.sendMessage(content, roomPublicKey, {
accounts: {
message: message.publicKey,
author: wallet.value.publicKey,
systemProgram: web3.SystemProgram.programId,
},
signers: [message]
})
console.log(tx);
const messageAccount = await program.value.account.message.fetch(message.publicKey)
}
sendMessage
rpc call is creating a new account, and I am then trying to fetch the just created account. But I am getting an error that no such account exists.
I logged the transaction hash and checked on solana explorer and it seems that the account is definitely there, but I am not sure why I am not able to fetch that account
Solution 1:[1]
I would recommend always confirming the transactions you run on your code, because the problem may be that you are creating the account, but you check it too fast and the RCP has not been updated yet or something.
That is considering you did everything correctly in your contract code, but i can't know that since you didn't provide it.
Add this line of code after your transaction request:
await program.provider.connection.confirmTransaction(tx);
so it will look like this:
export const sendMessage = async (content, roomPublicKey) => {
const { wallet, program, provider } = useWorkspace()
const message = web3.Keypair.generate()
const tx = await program.value.rpc.sendMessage(content, roomPublicKey, {
accounts: {
message: message.publicKey,
author: wallet.value.publicKey,
systemProgram: web3.SystemProgram.programId,
},
signers: [message]
})
console.log(tx);
await program.provider.connection.confirmTransaction(tx);
const messageAccount = await program.value.account.message.fetch(message.publicKey)
}
Also another check you can do is getting the account info to see if it was created correctly, since fetch uses the discriminator from anchor to determine if the account is the right type.
like this :
const collectionPDAAccount = await program.provider.connection.getAccountInfo(message.publicKey);
Hope this helps!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Fernando Binkowski de Andrade |