Posted By : Abhishek
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.
1) OS:- Ubuntu 16.04 or later
2) A running geth node
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.
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.
$ 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
>greeterHex = "0x<copy and paste the contents of contract bin file here>"
> greeterAbi = <Contract ABI here>
>greeterInterface = eth.contract(greeterAbi)
>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
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 ,
>eth.mining
> miner.start()
> eth.getTransactionReceipt(greeterTxHash)
> publishedGreeterAddr = eth.getTransactionReceipt(greeterTxHash).contractAddress
> 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.
You learnt what smart contracts are and how you can publish a smart contract on the geth console.
September 8, 2024 at 02:35 am
Your comment is awaiting moderation.