Skip to main content
The program transfers lamports of SOL from the owner to the Pot’s WSOL account and has SPL Token count them as WSOL. The cook reverts when the owner holds less than lamports or the Pot’s WSOL balance does not rise by exactly lamports. Target chains: Solana. Use --chain solana; the recorded simulation is described below.

Prerequisites

You need the sauce command (npm install -g @eco-incorp/sauce), a Solana key holding the SOL to wrap (a keypair file, or sauce wallet new <name> --solana). See Installation.

Program

solana-wrap-sol.sauce.ts

How it works

  • No token method wraps SOL, so the program makes the two calls itself with svm.call: the System Program’s transfer (instruction 2 as 4 bytes, then the lamports as 8 little-endian bytes), signed by the owner, then SPL Token’s SyncNative (instruction 17, hex`11`).
  • Each account is written where it is used: WSOL.accountOf(pot).writable(), pot.owner.writable().signer(), Account.systemProgram, Account.tokenProgram. Compiling adds five accounts to main, each derived, so the command is given none.
  • accountOf names an account that exists: its check reads the Pot’s WSOL account when the program starts, and sauce run creates a missing one before the cook.
  • The program reads pot.WSOL once after the calls and returns both values from that read. That keeps the cook at 1,173 of 1,232 bytes, small enough that one transaction also creates a new Pot ahead of the cook.

Run the example

1

Compile

The --json output gives the program’s size, the payload with the argument, and the cook against Solana’s transaction limit:
2

Simulate

--assume-balance WSOL:0 runs the cook in a local LiteSVM copy of the cook’s accounts, read from the cluster, with the Pot’s WSOL account made empty where it does not exist, as sauce run creates it before the cook. The owner must hold the lamports.
3

Send

--wallet <name> signs with a Solana wallet instead. sauce tx with --owner <your-pubkey> in place of --keypair <keypair> planned two steps for a new Pot: a setup transaction that creates the Pot’s WSOL account, then one transaction that creates the Pot and runs the cook.

Simulation result

Simulated on Solana at slot 462066340 with the command above, in a local LiteSVM copy. Nothing was signed or sent; values change with the slot. Lines that name the owner are left out.
The Pot’s WSOL account rose by exactly 10000000 lamports, 0.01 SOL. The full output’s lamport changes show the owner paying that plus the Kitchen’s 85672-lamport execution fee and the 5000-lamport transaction fee. The local copy ran the program from a code buffer; sauce tx plans the sent cook without one, as above. Asking to wrap 100,000 SOL (100000000000000), more than the owner holds, at slot 462066347:
The System Program refuses the transfer, and the whole cook reverts with it.

Next steps