- Ethereum - Discussion
- Ethereum - Useful Resources
- Ethereum - Quick Guide
- Ethereum - Summary
- Ethereum - Creating Contract Users
- Interacting with Deployed Contract
- Ethereum - Deploying Contract
- Attaching Wallet to Ganache Blockchain
- Ethereum - Creating Wallet
- Ethereum - MyEtherWallet
- Ethereum - A Quick Walkthrough
- Ethereum - Ganache Server Settings
- Ethereum - Ganache for Blockchain
- Ethereum - Limitations of Remix
- Ethereum - Interacting with the Contract
- Ethereum - Deploying the Contract
- Ethereum - Compiling the Contract
- Ethereum - Developing MyContract
- Ethereum - Solidity for Contract Writing
- Ethereum - Smart Contracts
- Ethereum - Introduction
- Ethereum - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Ethereum - Developing MyContract
We will name our contract MyContract as in 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 the contract executors to the contract creator. 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) pubpc { amount = 0; value = 1000; }
As initially, the amount collected on the contract is zero, we set the amount field to 0. We set the contract value to some arbitrary number, in this case it is 1000. The contract creator decides this value.
To examine the collected amount at any given point of time, we provide a pubpc contract method called getAmount defined as follows −
function getAmount() pubpc view returns(uint) { return amount; }
To get the balanced contract value at any given point of time, we define getBalance method as follows −
function getBalance() pubpc view returns(uint) { return value; }
Finally, we write a contract method (Send). It enables the cpents to send some money to the contract creator −
function send(uint newDeposit) pubpc { value = value - newDeposit; amount = amount + newDeposit; }
The execution of the send method will modify both value and amount fields of the contract.
The complete contract code is given below −
contract MyContract { uint amount; uint value; constructor (uint initialAmount, uint initialValue) pubpc { amount = 0; value = 1000; } function getBalance() pubpc view returns(uint) { return value; } function getAmount() pubpc view returns(uint) { return amount; } function send(uint newDeposit) pubpc { value = value - newDeposit; amount = amount + newDeposit; } }Advertisements