Writing to Network
In the previous section, we learned how to read data from the Solana network. Now let's explore how to write data to it. Writing to the Solana network involves sending transactions that contain one or more instructions.
These instructions are processed by programs (smart contracts) that contain the business logic for each respective instruction. When you submit a transaction, the Solana runtime executes each instruction in sequence and atomically (meaning either all instructions succeed or the entire transaction fails).
In this section, we'll walk through two basic examples:
- Transferring SOL between accounts
- Creating a new token
These examples demonstrate how to build and send transactions to invoke Solana programs. For more details, refer to the Transactions and Instructions and Fees on Solana pages.
Transfer SOL
Let's start with a simple example of transferring SOL between accounts. To transfer SOL from our Playground wallet, we need to invoke the System Program's transfer instruction.
A key concept of Solana's account model is that only the program that owns an account can deduct an account's lamport (SOL) balance. This means if you want to transfer SOL from a wallet account, you must invoke the System Program since it is the program specified in the owner field of the account.
Open Example
Click this link to open the example in Solana Playground. You'll see the following code:
Run Example
Run the code using the run
command.
Ensure your Playground wallet has devnet SOL. Get devnet SOL from the Solana Faucet.
Click on the output link to view the transaction details on the SolanaFM explorer.
Transfer SOL
Congratulations! You've just sent your first transaction on Solana! Let's break down what happened:
- First, we created an instruction specifying what we wanted to do
- Then, we added that instruction to a transaction
- Finally, we sent the transaction to be processed by the Solana network
This is the basic pattern for building transactions to interact with the programs on the Solana.
Create a Token
Next, let's create a new token using the Token Extensions Program. This requires two invoking instructions:
- Create a new account using the System Program
- Initialize the account's data as a Mint using the Token Extensions Program
Open Example
Click this link to open the example in Solana Playground. You'll see the following code:
Run Example
Run the code using the run
command.
Ensure your Playground wallet has devnet SOL. Get devnet SOL from the Solana Faucet.
You'll see two links printed to the Playground terminal:
- One for the transaction details
- One for the newly created mint account
Click the links to inspect the transaction details and the newly created mint account on SolanaFM.
Create Token
Mint Account
In this example, we demonstrated how to combine multiple instructions into a single transaction. The transaction contains two key instructions:
- Creating a new account using the System Program
- Initializing that account as a token mint using the Token Extensions Program
This pattern of combining multiple instructions from different programs into one transaction is common when building more complex Solana transactions. It allows you to sequentially and atomically execute multiple instructions, ensuring they either all succeed or all fail together.
Last updated on