0
0
Blockchain / Solidityprogramming~5 mins

Accounts (EOA vs contract accounts) in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Accounts are how we identify who owns or controls assets on a blockchain. There are two main types: Externally Owned Accounts (EOA) controlled by people, and Contract Accounts controlled by code.

When sending cryptocurrency from your wallet to a friend (EOA).
When deploying a smart contract to automate tasks (Contract Account).
When interacting with a decentralized app that runs code on the blockchain (Contract Account).
When checking who owns certain tokens or assets (EOA).
When creating a new feature that needs automatic rules, like a voting system (Contract Account).
Syntax
Blockchain / Solidity
EOA: Controlled by a private key, can send transactions.
Contract Account: Has code, executes functions when called.

EOAs require a private key to sign transactions.

Contract accounts cannot initiate transactions themselves; they respond to calls.

Examples
This shows a user (EOA) sending cryptocurrency to another address.
Blockchain / Solidity
// Example of an EOA sending Ether
web3.eth.sendTransaction({from: eoaAddress, to: recipientAddress, value: amount});
This deploys a smart contract to the blockchain, creating a contract account.
Blockchain / Solidity
// Deploying a contract account
const contract = new web3.eth.Contract(abi);
contract.deploy({data: bytecode}).send({from: eoaAddress});
An EOA calls a function on a contract account to execute code.
Blockchain / Solidity
// Calling a function on a contract account
contract.methods.myFunction().send({from: eoaAddress});
Sample Program

This program connects to the Ethereum blockchain, checks the balance of an EOA, and detects if a contract account exists at another address by checking for code.

Blockchain / Solidity
// Simple example in JavaScript using web3.js
async function example() {
  const Web3 = require('web3');
  const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_PROJECT_ID');

  // EOA address example
  const eoaAddress = '0xYourEOAAddress';

  // Contract account address example
  const contractAddress = '0xYourContractAddress';

  // Check balance of EOA
  const eoaBalance = await web3.eth.getBalance(eoaAddress);
  console.log(`EOA balance: ${web3.utils.fromWei(eoaBalance, 'ether')} ETH`);

  // Check if contract code exists at contract address
  const code = await web3.eth.getCode(contractAddress);
  if (code === '0x' || code === '0x0') {
    console.log('No contract found at this address.');
  } else {
    console.log('Contract account detected at this address.');
  }
}

example();
OutputSuccess
Important Notes

EOAs are controlled by private keys and can send transactions directly.

Contract accounts contain code and can only act when triggered by transactions from EOAs or other contracts.

Checking if an address is a contract can be done by checking if code exists at that address.

Summary

There are two main account types: EOAs (people) and contract accounts (code).

EOAs send transactions; contract accounts run code when called.

Understanding the difference helps you interact correctly with blockchain applications.