0

I'm new to SOL.

I've been reading this and this but there's no vanilla JS code. And this actually mixes a lot of conditions - from knowing the wallet private key to transferring from a wallet connected to your site, etc.

Could someone show me how to transfer USDC from one wallet to another with a wallet already connected to my site (I've achieved this and my site is supporting Phantom)?

UPDATE Here's what I have and it doesn't work as I get Transaction is not defined error

const transferTransaction = new Transaction()
.add(
    createTransferInstruction( // imported from '@solana/spl-token'
        "zGmof8SeyvHK3zZqsTBjsKXSV",
        "ErfpX6PMGUKdBWzUXqgZfbhrC4h4CWE",
        "zGmof8SeyvHKnSe85D3zZqsTBjsKXSV",
        parseInt(amount * Math.pow(10, 6)), // tokens have 6 decimals of precision so your amount needs to have the same
        [],
        "TokenkegQfeZBvf9Ss623VQ5DA" // imported from '@solana/spl-token'
    )
);

const network = "https://api.devnet.solana.com";
const connection = new Connection(network);
transferTransaction.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
transferTransaction.feePayer = alice.publicKey;
const { signature } = await window.solana.signAndSendTransaction(transferTransaction);
await connection.confirmTransaction(signature);

console.log(signature);
1
  • what framework you using?
    – C.OG
    Aug 8, 2022 at 11:55

1 Answer 1

2

There's a guide in the Solana Cookbook on how to send SPL-Tokens.

You need to the mint address of the SPL token you want to transfer to find the associated token addresses.


import { createTransferInstruction, getAccount, getAssociatedTokenAddress, getMint } from '@solana/spl-token';

// Add token transfer instructions to transaction

// Get the sender's ATA
const senderATA = await getAssociatedTokenAddress(splToken, sender);

// Get the recipient's ATA
const recipientATA = await getAssociatedTokenAddress(splToken, recipient);

// Create an instruction to transfer SPL tokens
const ix = createTransferInstruction(
  senderATA,
  recipientATA,
  sender,
  amount,
  [],
);
11
  • I don't think the above is vanilla JS. But anyway, what is ATA? AssociatedTokenAddress? What is that?
    – ratib90486
    Aug 8, 2022 at 12:35
  • And where you got your splToken, sender values from
    – ratib90486
    Aug 8, 2022 at 12:36
  • What about this is not vanilla? This is vanilla JS. Link for AssociatedTokenAddress: solanacookbook.com/references/…
    – C.OG
    Aug 8, 2022 at 12:38
  • And how you calculated your amount? Do you do this - parseInt(amount * Math.pow(10, 6)),
    – ratib90486
    Aug 8, 2022 at 12:38
  • Could you share the code you have already, that will be easier. And to clarify, the code here is vanilla JS also: spl.solana.com/…
    – C.OG
    Aug 8, 2022 at 12:40

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.