Solana Smart Contract šŸ“

Saakshi Raut
3 min readApr 5, 2024

--

Hello friends, in this blog, Iā€™ve explained smart contracts and the Solana Program. So if youā€™re unfamiliar with these concepts, make sure to read the blog until the very end.

What are Smart Contractsā“

Letā€™s break down the meaning of ā€œSmart Contractā€. We all understand what a contract is ā€” an agreement between two parties where both agree to accept the rules outlined in the contract. Similarly, a smart contract is a piece of program code that comprises if-then rules for the network. This code is automatically executed when some preconditions are satisfied.

If you are still confused, I recently posted a tweet explaining Smart Contracts using an example.

Understanding Smart Contracts ;)

Why do we use Smart Contracts šŸ‘‡

Smart Contracts are utilized to ensure security and foster trust among participants in decentralized networks.

For instance, consider a scenario where youā€™re renting a house, and suddenly the landlord notifies you of a $100 increase in rent starting next month. This unexpected change can be distressing, leaving you with the choice of either accepting the increase or searching for a new house.

However, if a contract was established beforehand, such unexpected changes would be mitigated. Smart Contracts offer predefined rules and conditions that both parties agree upon.

In this example, the terms of the rental agreement, including the rental amount and any increases, would be encoded in the Smart Contract. Therefore, any change in rent would require mutual agreement and could not be imposed by one party. This fosters transparency, reduces disputes, and ensures that agreements are kept, enhancing overall trust and security within the transaction.

Solana Program šŸ“

Smart contracts on Solana are called Programs.

Solana Smart Contract Diagram

As we can see in the above diagram there are two things we need to know about to understand the Solana Smart Contract:

On-Chain Code: The on-chain code is deployed directly to the Solana Network. This code, namely the smart contract code, is written in either Rust or C. These contracts are deployed onto the Solana blockchain and executed in a decentralized manner, implementing predetermined rules and executing actions based on inputs from transactions. Weā€™ll study more about transactions further.

Off-Chain Code: On the other hand, off-chain code is executed outside of the Solana Network. This code typically runs on the userā€™s end, such as in a web browser or CLI, and is written in typescript. It is used to query information or execute transactions on the Solana network.

Solana Transactions šŸ”

Transactions are one of the most important parts of a Smart Contract Life Cycle. These transactions are created by the user on the client side and consist of two attributes: the first is the Signature, and the second is the Message. The signature is an array of private keys (of accounts), and the Message is the payload information that is sent to the smart contract.

The Message consists of the following information:

  • Header: Metadata about the signatures, and accounts.
  • Account Addresses: Holds addresses of all the accounts that are part of the network.
  • Recent Blockhash: Hash of the latest observed blockchain ledger.
  • Instructions: Array of data used to complete the transaction as per logic.

The Instructions consist of the following:

  • Program ID: The ID of the contract that will interact with the account.
  • Accounts: Keeps track of state on a Solana program.
  • Data: Byte array of data the program will use to handle each transaction.

The information held by Accounts are:

  • Signer: User giving the authorization to execute the transaction.
  • Read-only: Indicates whether the data can be modified or not. (Boolean value)
  • Executable: Account created while deploying the program.
  • Rent: Lamport the User pays to keep the account on the program.
  • Data: Serialized data that allows users to store custom information.

Conclusion šŸ‘‹

Thatā€™s it for today. In summary, smart contracts are incredibly useful for ensuring the security and reliability of our decentralized network. Solana smart contracts are highly scalable and have lower rent fees. Nowadays many of the DApps are built using Solana.

If you think I missed out on something, please let me know in the comment section. Iā€™ll be covering more on Solana development in future blogs.

Stay tuned. ā­ļø

--

--