2

I'm getting this error while trying to transfer SPL tokens. I've seen people getting similar errors while trying to transfer SPL tokens but I can't figure out how to fix this:

if (!allowOwnerOffCurve && !web3_js_1.PublicKey.isOnCurve(owner.toBuffer()))
                                                                        ^

TypeError: Cannot read properties of undefined (reading 'toBuffer')

This is my JS script:

const { Connection, Keypair, PublicKey } = require('@solana/web3.js');
const { getOrCreateAssociatedTokenAccount, transfer } = require('@solana/spl-token');

const mint = new PublicKey(process.argv[2]);
const toWallet = new PublicKey(process.argv[3]);
const amount= (process.argv[4]);

async function transferSPLToken() {
    // Connect to cluster
    const connection = new Connection('MY-CUSTOM-RPC-URL');

    // Generate a new wallet keypair and airdrop SOL
    const fromWalletKeypair = [...];
    const fromWallet = Keypair.fromSecretKey(Buffer.from(fromWalletKeypair, 'hex'))

    // Get the token account of the fromWallet address, and if it does not exist, create it
    const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
        connection,
        fromWallet,
        mint,
        fromWallet.publicKey
    );

    // Get the token account of the toWallet address, and if it does not exist, create it
    const toTokenAccount = await getOrCreateAssociatedTokenAccount(connection, fromWallet, mint, toWallet.publicKey);

    // Transfer the new token to the "toTokenAccount" we just created
    signature = await transfer(
        connection,
        fromWallet,
        fromTokenAccount.address,
        toTokenAccount.address,
        fromWallet.publicKey,
        amount
    );
    console.log('transfer tx:', signature);
}

transferSPLToken()

1 Answer 1

1

It looks like toWallet is set here as a PublicKey:

const toWallet = new PublicKey(process.argv[3]);

And the issue may due to using toWallet.publicKey here:

const toTokenAccount = await getOrCreateAssociatedTokenAccount(connection, fromWallet, mint, toWallet.publicKey);

Try just toWallet since you have it already set as a PublicKey

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.