Getting Started with Ethereum Development: Smart Contracts

·

Introduction to Solidity

Smart contracts are developed using the Solidity programming language. Below are key features of Solidity:

Key Features

No Floating-Point Numbers

To avoid floating-point operations, Solidity uses wei (the smallest denomination of Ether).

1 \text{ Ether} = 1 \times 10^{18} \text{ wei}  

Exponential Operators

Solidity supports exponentiation for precise arithmetic.

Function Visibility Modifiers

Function Behavior Keywords

Multiple Return Values

Solidity functions can return multiple values.

keccak256 Hashing

Generates a 256-bit hash for cryptographic operations.

Event Logging with emit

Reduces blockchain traversal overhead by emitting logs.

msg.sender

Stores the address of the contract caller.

Error Handling: require vs. assert

Owner and Pause Controls

Use underscores (_) for Ownable and Pausable patterns.

👉 Explore Solidity Docs

Installing the Compiler (solc)

Option 1: NPM Installation

solcjs --help  

Option 2: Docker Installation

docker run ethereum/solc:stable --help  

Compiling a File

docker run -v /local/path:/sources ethereum/solc:stable -o /sources/output --abi --bin /sources/Contract.sol  

Example Contract

// SPDX-License-Identifier: UNLICENSED  
pragma solidity 0.8.10;  

contract Vault {  
    uint vaultData;  

    function set(uint data) public {  
        vaultData = data;  
    }  

    function get() public view returns (uint) {  
        return vaultData;  
    }  
}  

Compilation Command

docker run -it --rm -v `pwd`:/sources ethereum/solc:stable -o /sources/output --optimize --combined-json abi,bin /sources/Vault.sol  

Output (combined.json)

{  
  "contracts": {  
    "sources/Vault.sol:Vault": {  
      "abi": [/* ABI array */],  
      "bin": "6080604052348015600f57600080fd5b5060ac..."  
    }  
  },  
  "version": "0.8.10+commit.fc410830.Linux.g++"  
}  

| Field | Description |
|-------------|-----------------------------------------------------------------------------|
| abi | Defines contract methods and parameters. |
| bin | Bytecode executable on the Ethereum Virtual Machine (EVM). |

Deploying the Contract

Step 1: Load Contract Script

var output = { /* JSON from compiler */ };  
loadScript("/path/to/temp.js");  

Step 2: Prepare Deployment Variables

var vaultAbi = output.contracts['sources/Vault.sol:Vault'].abi;  
var vaultContract = eth.contract(vaultAbi);  
var vaultBin = '0x' + output.contracts['sources/Vault.sol:Vault'].bin;  

Step 3: Unlock Account

personal.unlockAccount('0x72fe0d...', "password", 3000);  

Step 4: Deploy Contract

var vaultDeploy = { from: '0x72fe0d...', data: vaultBin, gas: 1000000 };  
var vaultContractInstance = vaultContract.new(vaultDeploy);  

Contract Address Example

0x3828d7e57f70522c3a6e01941a821ff2378146f5  

Step 5: Interact with Contract

Read Data (Call)

vaultContractInstance.get.call();  // Returns: 0  

Write Data (Transaction)

vaultContractInstance.set.sendTransaction(100, { from: "0x72fe0d...", gas: 1000000 });  

Verify Update

vaultContractInstance.get.call();  // Returns: 100  

👉 Master Smart Contracts

FAQs

1. What is Solidity?

Solidity is a statically-typed language for writing Ethereum smart contracts.

2. How do I avoid floating-point errors?

Use wei for precise arithmetic (1 Ether = 10¹⁸ wei).

3. What’s the difference between require and assert?

4. How do I deploy a contract?

  1. Compile to bytecode (solc).
  2. Send a transaction with the bytecode in the data field.

5. Can functions return multiple values?

Yes! Solidity supports multiple return values.