Publishing a Smart Contract on Geth Console

Posted By : Abhishek

Nov 14, 2019

Smart Contracts are an integral part of the Ethereum blockchain. They assit in enablingg credible transaction executing without getting third parties invloved. Using this tutorial, you will learn how to deploy smart contracts on your geth node, be it testnet like ropsten and rinkeby or mainnet. You can also use this method to deploy contracts on a private testnet.

 

Pre Requisites

1) OS:- Ubuntu 16.04 or later

2) A running geth node

 

What exactly are smart contracts?

A smart contract is a self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of code.It can be visualised as an application where the code and the agreements contained therein exist across a distributed, decentralized network like a blockchain network. The code controls the execution, and transactions are trackable and irreversible.

 

Once a smart contract is deployed , its code and functionality cannot  be changed. If you make any error while publishing , a new contract instance has to be published. Thats why we should test our smart contract by testing it on testnets first before publishing it on the mainnet.

 

Advantages of smart contracts

  1. Transparency:- The code for each smart contract is available for anyone to see on the ethernet explorer. This reduces any chance of foul play by the contract publisher.
  2. Immutability:- Once a smart contract is published , its code and functionality cannot be changed ,so users always get what they signed up for. 
  3. Speed :- High speed of execution thanks to the use of mathematical algorithms in blockchain applications instead of bureaucratic mechanisms.

 

Publishing the smart contract

Now that you know what smart contract are and what are its advantages ,you can use the following steps to deploy your own smart contract using geth console on the ethereum network.This network can be a private testnet , ropsten or even mainnet.

 

  1. Enter the geth console
  $ geth attach <ip>:<port on which geth is running>

 

For example , if you want to publish contract on your local machine , say at port 8545 , run

$ geth attach http://127.0.0.1:8545

 

 

  1. Take the bytecode of the contract you want to publish and store it in a variable name greeterHex
>greeterHex = "0x<copy and paste the contents of contract bin file  here>"

 

  1. Take the ABI of the contract and store it in a variable

 

 > greeterAbi = <Contract ABI here>

 

  1. Create Contract by using ABI variable
>greeterInterface = eth.contract(greeterAbi)

 

  1. Publish contract
>greeterTx = greeterInterface.new(

"hello contract",

{

from: eth.accounts[0],

data: greeterHex,

gas: 1000000

}

)

 

Using this command, the coinbase address would publish the contract on the network.

If you want to publish the contract using some other address,

 

(i) first of all unlock that account using 

>personal.unlockAccount(<publisher address here>)

 

This will prompt you for the password to unlock this account 

(ii) After successfully unlocking the account you can use command mentioned in step 5, just replace eth.accounts[0] by account address of your publisher

 

  1. get transaction hash

Since deploying contract is also a type of transaction , you can see the transaction hash for the above contract using 

 

>greeterTxHash = greeterTx.transactionHash

Using the steps mentioned above ,

  1. check if miner is running or not
>eth.mining

 

  1. If there is no mining , use 
> miner.start()

 

  1. Check if the transaction is mined using 
> eth.getTransactionReceipt(greeterTxHash)

 

  1. get the address at which the contract is published at
> publishedGreeterAddr = eth.getTransactionReceipt(greeterTxHash).contractAddress

 

  1. Calling function from Contract
> greeterInterface.at(publishedGreeterAddr).greet()

 

The above statement calls a function greet from the published contract.If you want to call any other function, just use that function name instead of greet with the correct number and data type of arguments.

 

Conclusion

You learnt what smart contracts are and how you can publish a smart contract on the geth console. 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

November 18, 2024 at 03:50 pm

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button

Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text