Workings of smart contracts

Now, let's look briefly at the following factors and conditions that trigger the working of a smart contract:

  • Agreement: An option contract between two or more parties is written, coded, and deployed on the blockchain platform. The individual parties involved can remain anonymous, but the contract is written in the public ledger and remains unchanged.
  • The triggering or execution point: Any triggering event, be it within the blockchain or outside of the blockchain, gets the contract into execution according to the coded terms. A triggering event, for example, could be an expiration date or a renewal date of a contract, a particular price being hit on an exchange, or some cut-off rate being reached on our system. 
  • Outcome: The execution of a smart contract can produce an outcome that will change the state of a transaction, or the state of the blockchain for that matter, for example, a cash ledger. It may also impact on other systems as well.
  • TransparencyThis is another important aspect of contracts. These contracts are immutably available on the blockchain. Regulators can use these on blockchain to understand the activity of the market while maintaining the privacy of the individual parties involved.

Now, let's look at an example of writing a contract. The following are the steps to follow to start:

  1. Create a new file and name it sample.sol.
  2. Next, open the file to begin writing the code, as shown in the following screenshot:

B11516_3_03

Let's see the steps covered in the preceding code:

  1. First, let's declare the version of Solidity we'll use, which is pragma solidity^0.4.17. This allows the Ethereum platform to compile the contract in a specific version.
  2. Then, let's declare a contract and name it Welcome.
  1. Next, let's declare two instance table variables in this contract. One is the contractOwner address. This will capture the address or the account of the owner of this contract, who is going to deploy the contract on the Solidity platform.
  2. Next, we declare a greetMsg string, which we capture from the contract's owner.
  3. Now, we declare a Welcome function. Whenever the contract gets deployed on the Ethereum platform, this instance or function gets called publicly.
  4. Next, we pass the _greetMsg string in this function. We will also make this a public contract.
  5. Next, we assign the contractOwner variable its value: contractOwner = msg.sender. So, whoever has called this function will actually be the message owner, who has a right to create this contract.
  6. Likewise, we also assign the value of greetMsg as greetMsg = _greetMsg. So, what we have done here is we have created a  constructor function called Welcome, which gets called when the contract gets deployed on the Ethereum platform.
  7. Then, we declare one more function called sendGreeting(), which will return a string-type variable, greetMsg
  8.  To see one more function, let's convert public returns(string) to a void view function by adding the public view returns(string) view.
  9. We can slightly tweak this function to become a condition: if message.sender == contractOwner. This will return Hello Mr. Owner why you need to be greeted! to the owner, or else it will return greetMsg.
  10. Again, we will declare one more public function called killContract()
  11. Then, we give a condition, if message.sender == contract Owner, and assign a task, selfdestruct(this). So, unless the owner of the contract calls this kill contract function, it will disturb the contract from the Ethereum platform.
This is very important so you can decide the conditions in which you want to resolve the contract from the platform. So, there you can have a time-bound contract being created there.

So, that was an example of how you write detailed smart contracts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.186.79