0

I have Googled about this and only found how to send tokens via command line:

https://spl.solana.com/token

But I need to stake/withdraw Native SOL, not tokens, into/from Rust programs.

Also I found this about Rust native token:

https://docs.rs/solana-program/1.7.10/solana_program/native_token/index.html

https://docs.rs/solana-program/1.7.10/solana_program/native_token/struct.Sol.html

It seems I have to declare this Sol struct

pub struct Sol(pub u64);

Then use its "send" trait to send it... right? Could somebody explain how to do this via Rust programs?

Russo
  • 2,186
  • 2
  • 26
  • 42

1 Answers1

1

You can wrap Sol into a wrapped sol Token via the SPL Token program. From there you can use the Wrapped SOL like any SPL Token. Here is a pretty good example

https://github.com/project-serum/serum-dex-ui/blob/5b4487634e3738c57ace6da1377704e95f53c588/src/pages/pools/PoolPage/PoolAdminPanel.tsx#L250-L272

const wrappedSolAccount = new Account();

transaction.add(
  SystemProgram.createAccount({
    fromPubkey: wallet.publicKey,
    lamports: parsedQuantity + 2.04e6,
    newAccountPubkey: wrappedSolAccount.publicKey,
    programId: TokenInstructions.TOKEN_PROGRAM_ID,
    space: 165,
  }),
  TokenInstructions.initializeAccount({
    account: wrappedSolAccount.publicKey,
    mint: TokenInstructions.WRAPPED_SOL_MINT,
    owner: wallet.publicKey,
  }),
  TokenInstructions.transfer({
    source: wrappedSolAccount.publicKey,
    destination: vaultAddress,
    amount: parsedQuantity,
    owner: wallet.publicKey,
  }),
  TokenInstructions.closeAccount({
    source: wrappedSolAccount.publicKey,
    destination: walletTokenAccount.pubkey,
    owner: wallet.publicKey,
  }),
);

So essentially what it's doing here is

  1. Create a random account where you can deposit some SOL into and deposit the amount required plus the rent amount (hence parsedQuantity + 2.04e6)
  2. Use the token program to initialise the account as a WRAPPED_SOL_ACCOUNT
  3. Transfer the wrapped SOL (or in your case, you may just want to call the program and put the wrapped SOL account as a parameter)
  4. Close the Wrapped SOL account so that the user gets back their rent
yangli-io
  • 16,760
  • 8
  • 37
  • 47
  • Yes, I saw that but not sure if that is the correct way... quite complicated but I guess that is the only way to go for this. Thank you! – Russo Aug 17 '21 at 16:18
  • Hi yangli-io, would you be interested in a job for modifying Solana's SPL token program, so every transaction comes with a fee deducted from the sender? – Russo Sep 04 '21 at 04:14
  • @Russo, I'm honoured :D but sorry super busy at the moment – yangli-io Sep 04 '21 at 18:33
  • Hi yangli-io, do all wrapped SOL tokens have the same token address of TokenInstructions.WRAPPED_SOL_MINT, right? Therefore I can check if a token address is the same as TokenInstructions.WRAPPED_SOL_MINT, that token must be a wrapped SOL, is that correct? – Russo Dec 03 '21 at 07:42
  • After wrapping a new token, I have So11111111111111111111111111111111111111112 as the token mint acount. So that must be the same as TokenInstructions.WRAPPED_SOL_MINT, right? – Russo Dec 03 '21 at 08:11
  • Quote "Accounts containing wrapped SOL are associated with a specific Mint called the "Native Mint" using the public key So11111111111111111111111111111111111111112" from https://github.com/solana-labs/solana-program-library/blob/master/docs/src/token.md#wrapping-sol – Russo Dec 03 '21 at 08:25