Why solana?
Course: Programs, Accounts and the Token Program - chapter 1
Until now, we’ve gone through the following -
- What are blockchains, how do they work under the hood
- Public and Private keys, how you can use them to
signtransactions that miners use toverifyand credit/debit balances
In today’s class, we’ll understand about one of the biggest use-case that blockchains like Solana/ETH solve for - Programs/Smart contracts.
Programs/Smart contracts
Section titled “Programs/Smart contracts”ETH was one of the first blockchains to introduce the concept of decentralized state / programs. These are popularly known as smart contracts on the ETH blockchain.
Here is a simple ETH smart contract
// SPDX-License-Identifier: MITpragma solidity ^0.8.0;
contract Counter { uint public count;
// Constructor to initialize count constructor() { count = 0; }
// Function to increment the count function increment() public { count += 1; }
// Function to decrement the count function decrement() public { require(count > 0, "Count cannot be negative"); count -= 1; }
// Function to get the current count function getCount() public view returns (uint) { return count; }}Here is a simple Node.js HTTP server that does something similar
const express = require('express');const app = express();const port = 3000;
// Middleware to parse JSON bodiesapp.use(express.json());
// Initialize countlet count = 0;
// Route to increment the countapp.post('/increment', (req, res) => { count += 1; res.json({ count });});
// Route to decrement the countapp.post('/decrement', (req, res) => { if (count > 0) { count -= 1; res.json({ count }); } else { res.status(400).json({ error: 'Count cannot be negative' }); }});
// Route to get the current countapp.get('/count', (req, res) => { res.json({ count });});
// Start the serverapp.listen(port, () => { console.log(`Server running at http://localhost:${port}`);});HTTP Servers are deployed on cloud providers like GCP, Azure
Smart contracts/programs are deployed on the blockchain
The way solana programs work is significantly different from other blockchains. Lets understand how.