Smart Contract 101: What is the Structure of a Contract?
Below is an example of a simple smart contract. Let's observe the structure of the contract and go through each line of its code.
pragma solidity >=0.4.16 <0.8.0; contract FirstContract{ string _name; function setName(string name) public{ _name = name; } function getName() view public returns(string){ return _name; } }
The First Line pragma solidity >=0.4.22 <0.6.0; is telling the smart contract compiler which version of solidity compiler code you are using.
At line 2, FirstContract could be replaced by any text, this is the place for you to name or title your contract.
Inside the curly brackets, there is a string variable called _name, this line of code will record a value to be stored on the blockchain.
Also, there are two functions: setName() and getName().
setName takes a string input name. Its type of visibility is public, which means it can be called both internally and externally. Inside the function, the parameter name will be replacing the existing value stored by _name. As this function will change data on the blockchain, it will cost ether.
Another function getName, view means the function is read-only. This function returns a string value _name which is the same as the one we stored on the blockchain.
The combination of the above components creates a simple smart contract.
In the next lesson, we will describe the life-cycle of developing smart contracts.
More Tool: BTC to USD Price Calculator
Read More
Bitcoin Can’t Drive Financial Inclusion for Unbanked But CBDC Can says Mastercard CEO
Oct 28, 2020 2 Min Read
China to be First Cashless Society with Digital Yuan Launch in 2022, SCMP Report
Oct 28, 2020 2 Min Read
MicroStrategy's Bitcoin Investment Earns $103 Million with ROI of 24.3% in the Last 79 Days
Oct 28, 2020 2 Min Read
Ethereum Killer Polkadot Rallies as ETH Lags Behind, What’s Next for DOT?
Oct 28, 2020 2 Min Read
Bitcoin Needs to Overcome One Key Factor Before Its Price Run to $20K
Oct 28, 2020 2 Min Read