Beginner’s guide to blockchain/Ethereum (1) — Build a smart contract on a private network (2024)

Beginner’s guide to blockchain/Ethereum (1) — Build a smart contract on a private network (3)

Since many tutorials, including the official tutorial on ethereum.org, have some errs(ex. the updated version of geth removed the solidity compiling function) which make it harder for beginners like me to get started, I decided to write this tutorial in the hope of expanding the Ethereum community.

I’m using mac. If you encounter any problem, please kindly let me know in the comments so I can update it accordingly:)

Open source tools & language used:

  1. Solidity: smart contract programming language
  2. geth (version 1.5.9): CLI for running ethereum node
  3. brew: package manager for MacOS.

Install it by typing this command in terminal:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

As starting from geth version 1.6.0, compiling solidity is no longer available, we cannot install it directly using brew(only the newest version package is provided there). Here we use geth version 1.5.9 and build it from source code.

  1. Download Source code (tar.gz)

In terminal:

cd “path of the file you just downloaded”
// in Mac, you can simply drag the folder to console to get the path
make geth

This will generate an executable file in the “your current directory”/build/bin folder which can be moved to wherever you want to run it from. The binary is stand alone and doesn’t require any additional files.(Reference: https://github.com/ethereum/go-ethereum/wiki/Installing-Geth#build-it-from-source-code )

2. Then, create alias:

echo "alias geth159='your_folder_path/build/bin/geth'" >> ~/.bashrc

In my case, it is /Users/tina/Desktop/go-ethereum-1.5.9/build/bin/geth

To make the changes affected immediately:

source ~/.bashrc

To make what you editted in ~/.bashrc available upon every login

echo "source ~/.bashrc" >> ~/.profile

Now you can type geth159 whenever you want to launch geth :)

3. Install Solidity compiler

brew install solidity

4. launch your private net

First create a folder to store your blockchain data

mkdir privchain

Use geth to launch it

geth159 --rpc --rpcaddr 127.0.0.1 --rpcport 8545 --dev --datadir privchain

(Reference: https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options, https://stackoverflow.com/questions/33826018/in-ethereum-what-is-a-private-testnet-why-would-i-want-it-and-how-do-i-set-it )

Should see something like

INFO…. IPC endpoint opened: /Users/tina/privchain/geth.ipc

INFO….HTTP endpoint opened: http://127.0.0.1:8545

And if you open a new console and cd privchain then ls , you can see the originally empty dir is now having geth, geth.ipc, and keystore

5. Keep that Ethereum node running and open a new terminal. Use “geth attach” to connect to the node and open geth console

geth159 attach ipc://Users/tina/privchain/geth.ipc 
// use your own ipc url here!

6. Now use the api commands as specified here https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console#personallistaccounts

(In geth console)

personal.listAccounts

output: []

personal.newAccount('') 
// you can enter your password like newAccount(‘password_ya’)

output: [0x…….] // your newly created account address.

  • Note that every time a new account is created, you should see .”..New wallet appeared..” In the console where you opened your private net
personal.listAccounts 
// just to make sure you hv made the account successfully

output: [0x………]

7. Use the ethereum js api as specified here https://ethereumbuilders.gitbooks.io/guide/content/en/ethereum_javascript_api.html

(in geth console)

web3.eth.coinbase 
// This property is read only and returns the coinbase address where the mining rewards go to.

output: [0x………] // the output should be the same as your newly created account address, cause it takes the first account you created as default value

8. Create your smart contract!

source = "contract test { function multiply(uint a) returns(uint d){return a * 7;}}"

9. Make sure your solidity compiler is installed

web3.eth.getCompilers() // the output should be ["Solidity"]

10. Compile your code

code = web3.eth.compile.solidity(source)

The output(the “code” you just created) is like a json object. The highlighted part is your bytecode, which you will need to create a smart contract, and the abi array, respectively.

{<stdin>:test: { code: "0x60606040523415600b57fe5b5b60918061001a6000396000f300606060405263ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663c6888fa181146039575bfe5b3415604057fe5b6049600435605b565b60408051918252519081900360200190f35b600781025b9190505600a165627a7a723058206a8cbef13138690601a279c73e208f9dcf42e4678d860038475fe555599593d60029", info: { abiDefinition: [{...}],
compilerOptions: "--combined-json bin,abi,userdoc,devdoc -- add-std --optimize",
compilerVersion: "0.4.11", developerDoc: { methods: {} }, language: "Solidity", languageVersion: "0.4.11", source: "contract test { function multiply(uint a) returns(uint d){return a * 7;}}", userDoc: { methods: {} } }}}

*What is abi? You can see this as an api for machines as explained here.

When you write source code, you access the library though an API. Once the code is compiled, your application accesses the binary data in the library through the ABI.

11. Create a contract object by its abi

MyContract = web3.eth.contract(code["<stdin>:test"].info.abiDefinition)

(Official document on how to create a contract: https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract )

12. Check if you have a default coinbase account (to which your mining reward goes) with ether

First set your coinbase address as account1

account1 = web3.eth.coinbase

Then Check its balance

web3.eth.getBalance(account1)

If the output is larger than 0, go on to the next step:

Otherwise, start mining to get some ether in it!

miner.start()

You should see the mining in progress in the other terminal where you opened your private net

Stop when you feel like having sufficient funds ;p

miner.stop()

Check your balance again

web3.eth.getBalance(account1)

13. Unlock account to enable it to send transactions (In this case, we have to pay the gas price for contract deploy). Here we use account1.

personal.unlockAccount(account1, '') 
// you can replace '' with the password you set previously

14. Next, init the contract using the compiled code. In this case, it is code[“<stdin>:test”].code

You can also use other ways (ex. brower-solidity) to compile the contract and get this code. Here, replace the contract code with our “source” contract code, and click “contract details” at the bottom-right corner and you would be able to see the same bytecode.

*Note: browser solidity cannot work on safari. You can use chrome instead.

*Note: you can also set the contract to a previous one using the address of the contract you created before (contractInstance = MyContract.at(“address”);)

bytecode = code["<stdin>:test"].code

Estimate how many gas you will need to deploy this contract

web3.eth.estimateGas({data: bytecode})

15. Deploy contract and set callback function for our better understanding :)

contractInstance = MyContract.new({data: bytecode gas: 1000000, from: account1}, function(e, contract){if(!e){if(!contract.address){console.log("Contract transaction send: Transaction Hash: "+contract.transactionHash+" waiting to be mined...");}else{console.log("Contract mined! Address: "+contract.address);console.log(contract);}}else{console.log(e)}})
  • Note: The value of param “gas” can be anything higher than the estimated gas costs
  • Note: you may have to unlock your account1 again here.

Now you should see

In this console:

Contract transaction send: Transaction Hash: “your_transaction_hash” waiting to be mined…

In the other console for starting the private net:

… created “your_transaction_hash”

16. Your contract is now waiting be mined. You should see ‘Contract mined!` soon after the mining starts. After that, you can stop mining.

miner.start()

After you see

Contract mined! Address: 0xf1bc128edf9d7d4a7d567b50c1d8080cf58ef068

Ctrl+c to go back to geth console and type

miner.stop()
  • Note: Ctrl+C is to interrupt any process return to the simple console mode. Ctrl+D is to exit the console

17. Last, check if your contract is created successfully

eth.getCode(contractInstance.address) // the output should be the same as your bytecode

18. Call our contract function

contractInstance.multiply.call(6) // replace 6 with any unsigned int

Output: 42

  • Note: call() is a local invocation of a contract function that does not broadcast or publish anything on the blockchain. The command “contractInstance.multiply(6)” will not work.

(Reference: https://ethereum.stackexchange.com/questions/765/what-is-the-difference-between-a-transaction-and-a-call)

  1. Another great guide on the greeter tutorial:

https://samsclass.info/141/proj/pEth1.htm

Covers how to make a contract available to other machines on the same private network and how to delete a contract

2. Blockchain visualization and charts:

https://etherscan.io/

Now you know how to set up a private network, mine ethers, compile and run smart contracts in Solidity.

This is actually my first article and first tutorial. Share it if you like it. I’m open to any suggestions! Thank you:)

Beginner’s guide to blockchain/Ethereum (1) — Build a smart contract on a private network (2024)

FAQs

How do you create a smart contract in Ethereum blockchain? ›

Create and Deploy your Smart Contract
  1. Step 1: Connect to the Ethereum network. ...
  2. Step 2: Create your app (and API key) ...
  3. Step 3: Create an Ethereum account (address) ...
  4. Step 4: Add ether from a Faucet. ...
  5. Step 5: Check your Balance. ...
  6. Step 6: Initialize our project. ...
  7. Step 7: Download Hardhat. ...
  8. Step 8: Create Hardhat project.

What is the Ethereum answer? ›

Ethereum is a decentralized blockchain platform that establishes a peer-to-peer network that securely executes and verifies application code, called smart contracts. Smart contracts allow participants to transact with each other without a trusted central authority.

How do you deploy smart contract on Ethereum private network? ›

How to Deploy Your First Smart Contract on Ethereum With Hardhat and Tenderly DevNets
  1. Set up a Tenderly account to use Tenderly DevNets.
  2. Get some test ETH on a DevNet.
  3. Compile and deploy a smart contract using Hardhat. ...
  4. Use Tenderly DevNets to get in-depth transaction insights. ...
  5. Edit the contract source.
Jul 12, 2023

What is a smart contract in blockchain for beginners? ›

A smart contract is a self-executing digital agreement coded on a blockchain. It automatically enforces the terms and conditions when predefined criteria are met, which ensures secure and trustless transactions without the need for third parties. Think of a smart contract like a vending machine.

How much does it cost to put a smart contract on Ethereum? ›

Smart contract creation cost can be anywhere from $10 to $2,000 assuming Ether costs between $1,500 to $2,000. The biggest factors are 1) Ethereum price, 2) the size of the compiled contract (in bytes), 3) the current gas price on the Ethereum network.

What is an example of a smart contract? ›

Smart contracts eliminate intermediaries by automatically enforcing terms once conditions are met. Think of a smart contract like a vending machine. When you insert a dollar, you get a co*ke. The machine follows built-in rules, similar to if-then statements in code.

Is Ethereum better than Bitcoin? ›

Bitcoin is primarily designed to be an alternative to traditional currencies and hence a medium of exchange and store of value. Ethereum is a programmable blockchain that finds application in numerous areas, including DeFi, smart contracts, and NFTs. In that respect, they are both the best at what they do.

What is Ethereum for beginners? ›

Ethereum is a decentralized global software platform powered by blockchain technology. It is most commonly known by investors for its native cryptocurrency, ether (ETH), and by developers for its use in blockchain and decentralized finance application development.

What burns Ethereum? ›

Several mechanisms within the Ethereum ecosystem facilitate the burning of ETH. One of the most significant is the base fee burning introduced by EIP-1559. This upgrade fundamentally changed Ethereum's fee structure, mandating that a portion of every transaction fee (the base fee) be permanently burned.

How to create a private Ethereum network? ›

Below is the step-by-step guide to setting up a private Ethereum network.
  1. Step 1: Install Geth on Your System. ...
  2. Step 2: Create a Folder For Private Ethereum. ...
  3. Step 3: Create a Genesis Block. ...
  4. Step 4: Execute genesis file. ...
  5. Step 5: Initialize the private network. ...
  6. Step 6: Create an Externally owned account(EOA)
Apr 24, 2023

Where to deploy a smart contract? ›

To deploy your smart contract, go to the “Deploy & Run Transactions” tab, and select “IncrementDecrement” from the dropdown menu. In the “Environment” dropdown, select the network you want to deploy your contract to (e.g., “Remix VM” for a local testing network or “Injected Web3” for the main Ethereum network).

Where do Ethereum smart contracts run? ›

Smart contracts are self-executing contracts with the terms of the agreement between buyer and seller being directly written into lines of code. They run on the EVM and automatically execute transactions and other specific actions when predetermined conditions are met, without the need for intermediaries.

Is smart contract easy to learn? ›

The difficulty of learning Ethereum smart contract development depends on your prior experience and expertise as a mobile app developer, as well as your familiarity with blockchain and programming languages such as Solidity.

Can blockchain work without smart contract? ›

Smart Contracts cannot function without Blockchain

When preset circ*mstances are satisfied and validated, a network of computers conducts the operations When the transaction is complete, the blockchain is updated.

How can I make money with smart contracts? ›

Opportunities to Earn with Smart Contracts
  1. Lending and borrowing - Supply assets to liquidity pools and earn interest from protocols like Aave and Compound.
  2. Liquidity mining - Provide liquidity to DEXs like Uniswap and Balancer to earn trading fees and rewards.
Nov 13, 2023

Can Ethereum do smart contracts? ›

Ethereum provides a decentralized virtual computer — the Ethereum Virtual Machine (EVM) — on which developers can build applications consisting of multiple smart contracts. Think of the EVM as a distributed global computer where all smart contracts are executed.

What is the programming language for Ethereum smart contract? ›

Solidity is the most popular blockchain programming language of the Ethereum Virtual Machine (EVM), also widely used across a range of EVM-compatible blockchains.

How are smart contracts created? ›

A smart contract is defined as a digital agreement that is signed and stored on a blockchain network, which executes automatically when the contract's terms and conditions (T&C) are met. The T&C is written in blockchain-specific programming languages such as Solidity.

Can you send ETH to a smart contract? ›

A smart contract is not able to pull a contract-specified amount of ETH from an address. The amount needs to always be specified and signed by the sender. How to chose the amount depends on the wallet software you're using.

Top Articles
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 5901

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.