In this tutorial, you will learn about Ethereum Developing MyContract step by step. So without much to do, let’s get started.
We will name our contract MyContract as within the following declaration –
contract MyContract {
We will declare two variables as follows –
uint amount; uint value;
The variable amount will hold the accumulated money sent by using the contract executors to the contract author. The value field will hold the contract value. As the executors execute the contract, the value field will be modified to reflect the balanced contract value.
In the contract constructor, we set the values of these two variables.
constructor (uint initialAmount, uint initialValue) public { amount = 0; value = 1000; }
As initially, the amount collected on the agreement is 0, we set the amount field to zero. We set the contract value to some arbitrary range, in this case it is 1000. The contract writer makes a decision this value.
To take a look at the amassed amount at any given factor of time, we provide a public agreement method called getAmount defined as follows −
function getAmount() public view returns(uint) { return amount; }
To get the balanced contract value at any given point of time, we define getBalance technique as follows –
function getBalance() public view returns(uint) { return value; }
Finally, we write a settlement approach (Send). It enables the clients to send a few money to the settlement author –
function send(uint newDeposit) public { value = value - newDeposit; amount = amount + newDeposit; }
The execution of the send approach will alter both value and amount fields of the contract.
The complete contract code is given under −
contract MyContract { uint amount; uint value; constructor (uint initialAmount, uint initialValue) public { amount = 0; value = 1000; } function getBalance() public view returns(uint) { return value; } function getAmount() public view returns(uint) { return amount; } function send(uint newDeposit) public { value = value - newDeposit; amount = amount + newDeposit; } }
READ NEXT
Ethereum – Solidity for Contract Writing
This is about Ethereum – Developing MyContract, and we hope you have learned something from this tutorial and share your opinion about this tutorial. What do you think about it, and if you think this tutorial will help some of your friends, do share it with them.