[Archive] How to send USDC token or other tokens on Solana Blockchain? Also, how to get the user's Solana keypair?

:classical_building: Archived Content from Web3Auth Community

This topic was originally posted by leandrogavidia1234 on 4/2/2023.
This content has been migrated from our previous community forum to preserve valuable discussions.


Hi everyone! I’m developing a dApp on Solana Blockchain. I want to send USDC token from the user’s address (or some other token).

I’m using the signAndSendTransfer method, bug I can only send SOL. When I try to send USDC I got the next error: internal rpc-error

Also, how to get the user’s keypair? I got the private key, bug I can’t convert it to Uint8Array to get the keypair.

Thanks so much.

I attach my code.


import { useAsyncFn, useEffectOnce } from ā€˜react-use’
import { Connection, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction } from ā€˜@solana/web3.js’
import { chainConfig } from ā€˜~/config/chain’
import { useAuth } from ā€˜~/hooks/use-auth’
import { getAssociatedTokenAddress, getAccount, createTransferInstruction } from ā€œ@solana/spl-tokenā€;
import { useGlobalContext } from ā€˜~/context/global’
import { SolanaWallet } from ā€œ@web3auth/solana-providerā€

const { session } = useAuth()
const { web3auth } = useGlobalContext()
const userAddress = session?.user.address
const usdcMintAccount = "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU"
const devnetRpc = "https://api.devnet.solana.com"
const solanaWallet = web3auth?.provider && new SolanaWallet(web3auth?.provider)

const transferUsdc = async () => {
try {
if (!userAddress || !solanaWallet) return;
const connection = new Connection(ā€œhttps://api.devnet.solana.comā€)
const block = await connection.getLatestBlockhash(ā€˜finalized’)
const senderAta = await getAssociatedTokenAddress(new PublicKey(usdcMintAccount), new PublicKey(userAddress))
const receiverAta = await getAssociatedTokenAddress(new PublicKey(usdcMintAccount), new PublicKey(ā€œFk5wfsBUakaRhwJvP2nsUdDhB55kwVoRYDadmbwWncs6ā€))
const txInstruction = createTransferInstruction(
senderAta,
receiverAta,
new PublicKey(userAddress),
1
)
const tx = new Transaction({
blockhash: block.blockhash,
lastValidBlockHeight: block.lastValidBlockHeight,
feePayer: new PublicKey(userAddress)
}).add(txInstruction)

    const privateKey: string = await solanaWallet.request({
        method: "solanaPrivateKey"
    })

    const { signature } = await solanaWallet.signAndSendTransaction(tx)
    console.log("USDC TRANSACTION ID(HASH)", signature)
    return signature
} catch (err) {
    console.error(err)
}

}