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