Search code examples
solanasolana-web3js

Give another account ability to transfer portion of SOL - Solana


In SPL Token there is a function called Approve which gives the delegate account the ability to transfer an amount of tokens. So far I can only see this working for a custom token.

Is it possible to give the delegate account the ability to transfer SOL?

The flow I am after is:

  1. Client grants permission to transfer X SOL
  2. API creates a transaction to perform multiple actions including transferring SOL from the client's account to a system account

Resources:


Solution

  • Your best bet is to wrap your SOL into an SPL token account (mint So11111111111111111111111111111111111111112) and use the SOL as an SPL token, e.g.:

    $ spl-token wrap 1
    Wrapping 1 SOL into GJTxcnA5Sydy8YRhqvHxbQ5QNsPyRKvzguodQEaShJje
    Signature: 4f4s5QVMKisLS6ihZcXXPbiBAzjnvkBcp2A7KKER7k9DwJ4qjbVsQBKv2rAyBumXC1gLn8EJQhwWkybE4yJGnw2Y
    $ spl-token approve So11111111111111111111111111111111111111112 1 pubkey_to_approve_to
    

    More info about wrapping at https://spl.solana.com/token#wrapping-sol and https://spl.solana.com/token#example-wrapping-sol-in-a-token