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)
}
}