Posted By : Jagveer
Developers code smart contracts in solidity language. It gets stored on the blockchain and executes automatically in terms of the agreement between a seller and a buyer. In a smart contract solution, certain conditions have to be fulfilled to execute the order successfully.
Solidity smart contract is the collection of following
Pragma is used to enable checks and certain compiler futures. The below statement defines that the smart contract will not compile earlier than 0.5.0 and compiler version after 0.7.0
pragma solidity >=0.5.0 <=0.7.0
To declare a contract we use the "contract" keyword. This declares an empty contract which is identified by the name “BuyOrder”.
contract BuyOrder {
}
In Solidity language, variables are of two types
Add variables:- Let us add a variable quantity which will be of the data type of integer. Unsigned integer variables are represented by uint256, here 256 is signifying the 256 bits storage.
contract BuyOrder {
uint256 quantity;
}
Defining the constructor:- Constructor is always called when the contract is deployed on any node. the constructor initializes the contract with some values. In this scenario, we are initializing the quantity value to 50 when the contract is deployed.
constructor() public {
quantity = 50;
}
Adding getter functions:- Getter methods are used to read the stored value and the function declaration looks like function <function name> <access modifier> <state> <return value>.
function getQuantity() public view returns(uint256) {
return quantity;
}
Adding setter functions:- Setter methods are used to write/update the stored value.
function setQuantity(uint256 _newQuanity) public {
quantity = _newQuanity;
}
After Plugging all this together, Overall contract should look like
pragma solidity >=0.5.0 <=0.7.0;
contract BuyOrder {
uint256 quantity;
constructor() public {
quantity = 50;
}
function getQuantity() public view returns(uint256) {
return quantity;
}
function setQuantity(uint256 _newQuanity) public {
quantity = _newQuanity;
}
}
We can use Remix Online IDE to test and deploy the smart contract. Remix Ide completely browser-based where you can write and deploy your smart contract. it also provides you an online solidity compiler to compile your smart contract.
1:- Create a new file and name the file BuyOrder.sol.
2:- Copy and paste the whole contract in the buy order.sol file.
3:- Click on the second icon on the left side of IDE and select the compile to compile the code.
4:- Click on compile BuyOrder.sol.
5:- Once the smart contract compiles successfully, click on the third icon Deploy and Run Transaction to deploy the contract.
6:- Click on the deploy button to deploy the contract.
This is an example to build your first smart contract on Remix Online IDE and this can only be checked on the local environment.
October 8, 2024 at 03:30 pm
Your comment is awaiting moderation.