Search code examples
rustblockchainsmartcontractssolana

How to send spl-tokens with Solana smart contract?


I'm a beginner Rust/Solana Developer and wanted to ask if someone knows how to send a spl-token from an account when the account receives a SOL?

I read the https://docs.solana.com/ but i could not find anything about SPL transfers.

Example of Smart Contract: https://github.com/solana-labs/example-helloworld

Example:

Acc1 sends 0.1 SOL to a address and for that Acc1 receives 1 SPL-TOKEN from that address.

Simple explanation: I need a minting website for a token.

I would really appreciate some help, thanks!


Solution

  • You can send SPL-Tokens by using CPI on the token program's transfer instruction.

    For example:

    spl_token::instruction::transfer(
      token_program.key,
      source.key,
      destination.key,
      authority.key,
      &[],
      amount,
    )?;
    invoke_signed(
      &ix,
      &[source, destination, authority, token_program],
      signers,
    )
    

    You can find a more in depth example by looking at how the token-swap program uses CPI to transfer spl-tokens