Skip to content

Blockchain

Posted on:September 23, 2022 at 03:22 PM

What is Bitcoin

What is Ethereum

Interaction with Ethereum Blockchain

Chrome plugin for metamask

Metamask account contents

One metamask account can interact with multiple Ethereum Blockchain/network like main net and test net.

Ethereum test networks

Ethereum transaction object

Contents of Ethereum transaction object

Keyworddescription
nonceHow many times the sender has sent a transaction (in lifetime)
toAddress of the account this money is going to
valueAmount of Wei to be sent to target address (or contract)
gasPriceAmount of Wei sender is willing to pay per unit gas to get this transaction processed
startGas/gasLimitUnits of gas this transaction can consume
vCryptographic piece of data that can be used
rto generate the sender’s account address
sgenerated from the sender’s private key

Sequence of transferring funds to another account on Blockchain:

Hashing fundamentals

Generic Blockchain fundamentals

Contents of generic blockchain

Fielddescription
Block no.Block no. in the blockchain
NonceA number such that the hash starts with 4 zeros
dataSome data (Token/transactions)
PrevHash of previous block
HashThe hash of above four fields

Ethereum specific blockchain

Link to see block time of Ethereum blockchain https://etherscan.io/chart/blocktime Shows Averagre amount of time taken to calculate the blocks in the Ethereum chain.

Smart contracts to build interesting apps (Dapps) on the network

Contract account contents/fields

External Account

Contract account

External account to create contract transaction (revisit)

Solidity language

Smart contract

IDE for Solidity (Remix) https://remix.ethereum.org/

Remix editor:

pragma solidity ^0.4.17;

contract Inbox {
    string public message;

    function Inbox(string initialMessage) public {
        message = initialMessage;
    }

    function setMessage(string newMessage) public {
        message = newMessage;
    }

    function getMessage() public view returns (string) {
        return message;
    }

}

Common function types

Keyworddescription
PublicAnyone with Ethereum account can call this function
PrivateOnly this contract can call this function
ViewThis function returns data & does not modify the contract’s data
ConstantThis function returns data & does not modify the contract’s data
PureThis function will not modify and even read the contracts’s data
PayableWhen someone call this function they might send Ether along

Remix IDE

External account to create contract transaction

Keyworddescription
nonceHow many times the sender has sent a transaction
to- (left blank)
dataCompiled bytecode of the contract
valueAmount of Wei to be sent to target address (or contract)
gasPriceAmount of Wei sender is willing to pay per unit gas to get this transaction processed
startGas/gasLimitUnits of gas this transaction can consume
vCryptographic piece of data that can be used
rto generate the sender’s account address
sgenerated from the sender’s private key

Ways to run a contract function

First WaySecond way
Calling a functionSending a transaction to a function
Cannot modify contract’s dataCan modify contract’s data
Can return datareturns the transaction hash
Runs instantlyTakes time to execute
Free to docosts money

Gas and Wei

Mnemonic and ethereum accounts

To get free Ether from faucet

Truffle

Boilerplate design

IssueSolution
Need to be able to write solidity code in JS projectSetup solidity compiler to build our contracts
Need some way to quickly test contracts without manual testing we were doing in remix editorSetup custom mocha test runner that can somehow test solidity code
Need someway to deploy our contract to public networksSetup compile script to compile and deploy our comtract

Ganache

Web3 versions

Web3 providers

Deploying to local Ganache network

Provider

Mocha

FunctionPurpose
itRun a test and make an assertion
describeGroups together it functions
beforeEachExecutes some general setup code

Creating contract on Ethereum network

-To create a contract an external account makes a transaction with the Ethereum network.

Information needed by web3 to deploy a contract and communicate with an already deployed contract.

Deploying the contract to “real” test network

Infura Provider

truffle-hdwallet-provider

Interacting with deployed contract without a front-end

Basic Solidity types

msg global variable

Property NameDescription
msg.dataData field from the call or transaction thatinvoked the current function.
msg.gasAmount of gas the current function invocation has available .
msg.senderAddress of the account that started the current function invocation.
msg.valueAmount of Wei that was sent along with the function invocation.

Reference types in Solidity

Solidity gotcha

Global function

this.balance: amount of money in the contract.

address type has transfer method on it to transfer eth to the address.

Function modifier

For eg.

modifier restricted (){
require();
\_;
}

Web3 utility methods

Building Dapp

export default new web3.eth.Contract(abi, address)??

Rendering contract data in a component