Notes on Blockchain, Solidity, and Full Stack Web3 Development with JavaScript -part 1

SideNote:

Core fundamentals

Blockchain Oracle: Any device that interacts with the off-chain(not blockchain) world to provide external data or computation to smart contracts. The oracle is a layer that queries, verifies, and authenticates external data sources, usually via trusted APIs, proprietary corporate data feeds and internet of things feeds and then relays that information. If we want our application to stay truly decentralized, we need to have multiple Oracles. Smart Contracts that use Oracles are called hybrid smart contracts. eg, Chainlink: Decentralized Oracle Network

A Dapp / Decentralized Application is usually a combination of many smart contracts.

  • Web 1: permissionless open sourced web with static content.

  • Web 2: permissioned web, with dynamic content. where centralized server handles pretty much everything.

  • Web3: permissionless web, with dynamic content. where decentralized, censorship resistant networks run your agreement and code.

What is the value of smart contracts? what problem does it solve? Nearly everything you do in life is a result of a promise/ or a contract,

  • You agree to buy a wooden chair > The manufactures agree to buy the lumber to make that chair.
  • You agree to buy an insurance > they agree to cover your medical bills.

We live in a world where we have to trust a companies logo(brand based), and hope they keep their promise, in a blockchain based world, we see the contract and we know for a fact that it will hold true (Math Based) as everything is transparent and again, in blockchain "a contract cannot be altered"

everything works well as long as the promise is kept. The moment the promise is broken, all hell breaks loose :)

A smart contract is an agreement, contract or set of instructions that is deployed on a decentralized blockchain, and once deployed, it cannot be altered and everyone can see the agreement. no one party can say they will no longer follow the contract.

Typically a smart contract is on a decentralized blockchain and used in combination of a decentralized oracle network. to get the real world assets and information.

What have Smart Contracts done so far?

DeFi- Decentralized Finance, DAOs- Decentralized Autonomous Organizations NFTs- Non fungible token

Your first transaction

  • Install MetaMask
  • Set it up.
  • Write down your secret phase.
  • etherscan.io (paste wallet address) get all info related to that address.
  • METAMASK > Show/hide (click) > Show test networks >Rinkeby network
  • rinkeby testnet explorer > ~etherscan.io (but for testing)
  • faucets.chain.link (Connect your wallet address here to get test token to work with) (Network-Etherium Rinkeby)

Looking into the transaction table

image.png

  • Transaction Hash: Unique identifier that Identifies this particular transaction.
  • Status: status on the transaction
  • Block: block number the transaction was included in
  • Transaction Fee: Amount paid to the miners for processing the transaction.
  • Gas Price: >Gas: A unit of computational measurement. The more complex your transaction is the more you have to pay. eg. A simple transaction to send ether will have lower gas, compared to a tokens send via a smart contract.

Sidenote:
transaction Fee = how much gas we used for this given transaction * gas price per unit

How do blockchains work!

image.png

Before we move forward we first need to understand what SHA256 or hashing is.

  • Hash: A unique fixed length string, meant to identify a piece of data. They are created by placing said data into a Hash Function .

Basically what happens is the data is run through the hashing function and outputs a fixed length unique string of characters, Irrespective of the length of the input string. Even a slight bit change in the input, the values of this fixed length unique string of characters changes.

Ethereum uses Keccak256, which is a hashing function similar to SHA256.

Now what happens in a given block in a blockchain?

image.png

The final hash value of a given transaction is the combination of Block, Nonce, Date. A miner has to solve a given problem. In the case of Ethereum, the problem that needs to be solved is finding a suitable Nonce where the final hash starts with 4 0's.

As we can see in the above Image, for no data present we have a nonce that when hashed with the data and block number gives a hash starting with 0000. With the change of data the nonce changes.

This is the problem that the miners try to solve. Below is another example of the above mentioned information.

image.png

As we can see, changing data changes the nonce by the miners when mined.

A given block is a collection of multiple transactions that have occurred. for ex.

image.png

changing one transaction field, changes the hash value. ultimately making the bock invalid, even if you recompute and find the new Nonce for a given block the other blocks belonging to different peers will be like "Hey!!, it does not match ours, GET OUT!!!". In a blockchain democracy rules. If the majority of the peers have a different value compared to you. You end up being kicked out.

Now lets just revise what we've learnt so far

  • Hash: is a Unique fixed length string to Identify a piece of data.
  • Hash Algorithm: A function that computes data into a unique hash
  • Mining: The process of finding the solution to the blockchain problem. In our example, the "problem" was to find a hash that starts with four zeros (0000). The node gets paid for mining the block.
  • Block: A list of transactions mined together.
  • Decentralized: Having no single point of authority.
  • Nonce: is the answer found by the miner, i.e. what number if inserted to the hashing algorithm, makes the hash start with 0000. It can also be used to define the transaction number for an account/address

Signing a Transactions

So you have a public key and a private key. A private key can be used to generate a public key using the Elliptic Curve Digital Signature Algorithm. The public key we want everyone to see, while the private key is kept a secret.

we use the private key to digitally sign transactions, while people can verify them with the public key. This means for a given message we sign it using a private key, and verify it using a public key

  • (Private Key + Message => Message Signature)
  • (Public Key + Message => Verified Message Signature)
  • Amount 10$; From: Aaron; To: Laron; Signed by Aarons private key.
  • Aarons Public key is used to verify that Aaron itself is sending that amount.

So if someone gets your private key. they can sign transactions for us. So always keep your private key a secret. Your address is derived from your Public Key

  • The more people use a chain, the more expensive it is to send transactions

High-Level Blockchain Fundamentals

Blockchain can be considered as a decentralized database. while Ethereum has an extra feature which also allows computation in a decentralized network.

Consensus

Consensus is an umbrella term for proof of work and proof of stake

  • Proof of work uses a lot of energy as everyone is competing to solve a problem.
  • Proof of stack puts up a collateral as a sybil resistance mechanism, as proof that they will behave in the network. if you try to misbehave you loose your stake. Here only one node solves the problem. and the rest just validate this transaction