Writing a Simple Smart Contract

·

Creating a smart contract is a foundational skill for anyone diving into blockchain development. This guide walks you through the essentials of writing, deploying, and interacting with a simple smart contract on the Ethereum network. We’ll cover core tools like Geth, Solidity, Web3.js, and Remix IDE, while guiding you step by step through setting up your environment and executing your first contract.

Whether you're building decentralized applications (dApps) or exploring blockchain logic, understanding how smart contracts work is essential. Let’s begin by exploring the key technologies involved.

Understanding Core Tools

Geth: Ethereum’s Official Client

Geth (Go Ethereum) is the official Ethereum client developed by the Ethereum Foundation and written in Go. It enables you to run a full Ethereum node, interact with the blockchain, mine ether, and deploy smart contracts. By running Geth, you connect directly to the Ethereum network or create a private test environment for development.

👉 Get started with Ethereum development tools today.

Solidity: The Language of Smart Contracts

Solidity is a statically-typed, contract-oriented programming language designed for writing smart contracts on blockchain platforms—primarily Ethereum. Developed initially by Gavin Wood and later led by Christian Reitwiessner and other core contributors, Solidity compiles to bytecode that runs on the Ethereum Virtual Machine (EVM).

Its syntax draws inspiration from JavaScript, C++, and Python, making it accessible to developers familiar with these languages. Solidity supports features like inheritance, libraries, and complex user-defined types, enabling robust contract logic.

Example:

pragma solidity ^0.4.24;

contract Hello {
    function multiply(uint a) public pure returns (uint result) {
        return a * 3;
    }
}
}

This basic contract defines a function that takes an unsigned integer and returns its triple.

Web3.js: Interacting with the Blockchain

Web3.js is a JavaScript library that allows frontend applications to communicate with Ethereum nodes via HTTP or WebSocket connections. It abstracts low-level JSON-RPC calls, letting developers easily read data from the blockchain, send transactions, and interact with deployed contracts.

Common uses include:

Remix: Browser-Based IDE for Beginners

Remix is an open-source, browser-based IDE officially recommended by the Ethereum community. It's ideal for beginners due to its intuitive interface, real-time error checking, and built-in debugger. You can write, compile, test, and deploy Solidity contracts without installing any software.

For local use, Remix can also be installed via GitHub: github.com/ethereum/remix-ide

Setting Up Your Development Environment

Installing Geth on macOS

To install Geth using Homebrew:

brew tap ethereum/ethereum
brew install ethereum

Once installed, you can start a local private Ethereum network for testing.

Launching a Local Test Node

Use this command to launch a developer instance of Geth with a custom data directory:

geth --datadir testNet --dev console 2>> test.log

This starts a temporary private chain useful for development and testing. All data resets when the session ends.

Managing Accounts in Geth

After launching Geth, access the JavaScript console to manage accounts.

View Existing Accounts

> eth.accounts

This returns an array of available accounts. A default account is usually created automatically.

Example output:

["0xcc4164b8535eb7a9291486a1a5c982fd15c8b75d"]

Check Account Balance

> eth.getBalance(eth.accounts[0])

The default account often has a large balance in test mode.

Create a New Account

> personal.newAccount("password123456")

Replace "password123456" with a secure passphrase. After creation, check eth.accounts again—you should now see two addresses.

New accounts start with zero balance. To perform transactions or deploy contracts, you need Ether.

Transfer Ether to the New Account

Send funds from the default account:

eth.sendTransaction({
  from: '0xcc4164b8535eb7a9291486a1a5c982fd15c8b75d',
  to: '0x0a9961a0cde47c7e806a2874dc065cb33f6201c5',
  value: web3.toWei(1, "ether")
})

You’ll see mining logs in test.log. Verify the balance:

> eth.getBalance(eth.accounts[1]) // Should return 1000000000000000000 (1 Ether)

Unlock the Account for Deployment

Before deploying a contract, unlock the sender account:

personal.unlockAccount(eth.accounts[1], "password123456");

This grants temporary signing permission for transactions.

Writing and Deploying Your First Smart Contract

The Contract Code

Here’s a minimal Solidity contract:

pragma solidity ^0.4.24;

contract Hello {
    function multiply(uint a) public pure returns (uint result) {
        return a * 3;
    }
}
}

This function accepts an integer input and returns triple its value—ideal for testing deployment and interaction flows.

👉 Explore more smart contract templates and examples.

Deploying via Remix and Web3.js

  1. Paste the code into Remix IDE.
  2. Compile it using the Solidity compiler.
  3. Copy the generated ABI and bytecode.

Back in the Geth console, define the contract object:

var helloContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"result","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"}]);

Deploy it:

var hello = helloContract.new({
  from: web3.eth.accounts[1],
  data: '0x608060405234801561001057600080fd5b5060bb8061001f6000396000f300608060405260043610603f576000357c010000000000000000000000000000000000000000000000000000009...',
  gas: '4700000'
}, function(e, contract){
  if (typeof contract.address !== 'undefined') {
    console.log("Contract mined! Address: " + contract.address + ", Hash: " + contract.transactionHash);
  }
});

On successful deployment:

Contract mined! address: 0xa29b84d8d3... transactionHash: 0x15dd29aa...

Interacting With the Deployed Contract

Now that your contract is live on the local blockchain, interact with it using its address and ABI.

var Demo = eth.contract(hello.abi).at("0xa29b84d8d3...");
Demo.multiply(3); // Returns 9

You’ve successfully called a function on your deployed smart contract!

Frequently Asked Questions (FAQ)

Q: What is a smart contract?
A: A smart contract is self-executing code deployed on a blockchain that automatically enforces rules and performs actions when conditions are met.

Q: Do I need real Ether to test contracts?
A: No—tools like Geth’s --dev mode or testnets provide free Ether for development and testing.

Q: Why use Remix IDE?
A: Remix offers instant setup, real-time debugging, and integration with Ethereum environments—perfect for learning and prototyping.

Q: Is Solidity hard to learn?
A: If you know JavaScript or C-style syntax, Solidity will feel familiar. Start small and build up complexity gradually.

Q: Can I edit a deployed smart contract?
A: No—once deployed, contracts are immutable. You must deploy a new version if changes are needed.

Q: How do I secure my private keys?
A: Never expose them in code or logs. Use encrypted keystores and avoid hardcoding credentials.


Core Keywords: smart contract, Solidity, Ethereum, Geth, Web3.js, Remix IDE, blockchain development, decentralized applications (dApps)

With these fundamentals mastered, you’re ready to explore more advanced topics like event logging, payable functions, and integrating contracts with frontends. Continue experimenting in safe environments before moving to production networks.