I am trying to figure out why createAssociatedAccount
does not work. This is the code:
const SendTransaction = ({ children }) => {
const { connection } = useConnection();
const { publicKey, signTransaction } = useWallet();
const onSendSPLTransaction = useCallback(async () => {
try {
if (!publicKey || !signTransaction) throw new WalletNotConnectedError();
const toPublicKey = new PublicKey("string pubkey is here");
const tokenPublicKey = new PublicKey("another is here");
const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
publicKey,
tokenPublicKey,
publicKey,
signTransaction
);
// this console.log confirms that getAccount went well
console.log(fromTokenAccount.address.toBase58());
// from here the code fails
const toTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
publicKey,
tokenPublicKey,
toPublicKey,
signTransaction
);
...
})
When I run this function, I can observe that the fromTokenAccount
part has no problems, and I can confirm that it gets the correct ATA Pubkey (confirmed in explorer). However, for some reason, I receive TokenAccountNotFoundError
when the toTokenAccount
runs...
Please, help me to understand what is going on --- I believe that I am doing something dumb here.
EDIT
: When I change the payer from the one I got from useWallet()
hook to the hardcoded Keypair, the whole function works. So, what am I missing here? Have any ideas?