Smart contracts globalize the industries in which all transactions and exchanges take place. They simplify the process of transferring, delivering, and exchanging. Regardless of which ICO you use, a smart contract must be well-constructed: it has to be integrated with other contracts inside the blockchain, it has to be secured, and it has to be transparent. Let’s take a look at some tricks and possibilities for creating and improving it.
The ICO and the Importance of Tokens
ICO stands for initial coin offering. During an ICO, there are two kinds of tokens you can buy. The first is the utility token. It’s meant to be used on goods and services offered by the company that has developed the cryptocurrency. The second type of token is the security token. The expectation is that the token is going to be worth something once the company develops a way for people to spend it. To secure and improve ICOs, we have to write a fast, light, secure smart contract in order to benefit from it in the future.
Different platforms use different tokens to perform computations in the network. These tokens can be obtained by buying them in exchange for fiat currencies like USD (you can read an article about accepting fiat currencies during the ICO here); or they can be earned by performing specific services on the network (like mining). If the token is highly-circulated in the network and is in highly demand for different types of services, its value increases.
Before getting into the process of creating a smart contract, you need to define your project’s goal. Is there the potential for this goal be “tokenized?”
What Are Smart Contracts, and How Do They Work?
Smart Contract (crypto-contract): a computer program that directly controls the transfer of digital currencies or assets between bodies under certain conditions. When these conditions are met, a contract executes itself.
The 4 basic attributes of smart contracts are:
The Most Frequently-Used Platforms for Writing Smart Contracts
Ethereum is a superior platform for creating and writing smart contracts, but it’s not the only one available on the web. There are also:
Most of these provide a basic set of templates for creating your smart contract. Their scripting language is quite limited, however, and only basic crypto-functions are available.
Deploy Your Smart Contract Quickly and Easily
There are multiple tools available now for deploying a smart contract. They all get positive feedback from the community. They simplify and reduce the amount of work done by an Ethereum developer.
Here is a list of them:
- Metamask: brings Ethereum to your browser. You can get a Chrome extension as well as a Firefox extension. It allows transactions to take place on regular websites.
- Remix: allows you to get rid of bugs and compile your smart contract.
- Infura: helps you to specify which clients/contracts are eligible to join your contract. Plays a so-called “filter” role.
- Truffle Framework: manages your smart contract on its own. Has a built-in system that compiles, links, and launches your contract.
- Mist browser: useful for Ethereum wallet users. Browse and use Dapps on Ethereum.
IDE for Creating Smart Contracts
Solidity is a superior application for creating, testing, and running you own smart contracts. It was specifically designed for Ethereum Virtual Machine (EVM), which improves the connection and integration of contracts inside the Ethereum blockchain.
Solidity IDE comes with a compiler, an editor, and a debugger. The User Interface is divided into 4 main sections.
1. file explorer
4. extra tools box
How to Create Your Own Smart Contract
Here’s an example of how to create a contract that is going to do just one thing. It’s going to send Ether to the person who sent Ether to the smart contract.
- Open up any file, and start with writing “contract” in line 1. Name it “Example”.
- Then you have to create a “constructor”. Write “Function Example” in line 7. This function can only be called once and that’s when the contract is created for the mine. It means that this piece of code is going to go to the blockchain and miners are going to put it into the network, so it can be run and this smart contract can never be changed. That’s the great thing about Ethereum-based smart contracts.
- Then you put “owner = msg.sender” in line 7. This means that the person that creates the contract is actually going to be the owner. It’s better to set the owner as a variable, so output “address public owner”. This creates a public variable called “owner”. “Msg” is an object for the message, so when anyone sends anything or interacts with their contract (even when you create it), MSG contains all of the information examples. “.sender” is the person who sent it.
- Line 10. “Function SendEtherBack”
- Line 12. “Msg.sender.send(msg.value)”. This will send a certain amount of Ether. “Msg.value” is the amount of Ether that was sent in, so it’ll just send it right back.
- Let’s say someone just sends money and doesn’t call a function or doesn’t load the contract. Just say “function” on line 15, and leave a comment below (line 16): “just sending some cash?” For example, if you send money to a contract, it’ll just collect it.
- You can also input another function that sends all the cash back to the owner.
Here, we can see the actual source code. The first portion is basically telling Ethereum which version of the solidity compiler you want to use, and that’s the latest right now: 4.19.
All you have to do is click on ethereum.github.io/browser-solidity.
Paste your code in, Use the MetaMask browser extension to unlock your account, and access your wallet. Enter your password, and unlock the account. Once the account is unlocked, you have access to your wallet.
To update the page, click “Create”. MetaMask will get a notification; press “Accept”. It will cost a bit of money to actually deploy the it.
Once the procedure of waiting is over,, the contract will be in the Ethereum blockchain. Since it has been created, people can now interact with the contract on the blockchain all the time.
The next portion here is a library called safe math, which helps to avoid any sort of bugs or funky things that might be happening with the mathematical calculations we do in the contract. The library shelf is actually copied and pasted from a project called Open Zeppelin, which is just a GitHub repository full of peer-reviewed smart contracts that are freely available for anyone to use. Note! The less code you use, the lighter and the better your contract will be. Here, you’ll find the multiplication and addition functions we will use.
Let’s consider some of the methods inside the contract:
- You have to set up the contract using “Terminal” Type in “geth attach”. “Geth” is a CLI that you use to interface with the Ethereum blockchain. Have your Ethereum wallet opened.
- Unlock your account, and set your default accounts. “Eth.defaultaccount = eth.accounts ”. Make sure you have a council set up.
- “personal.unlockAccount(eth.account”, and enter a password for the account “enterprise”.
- Once it’s unlocked, you’ll get a notification that it’s true, so now we can create an object.
Now you can create an object:
- “desk = eth.contract (enter the code from Interface).at(‘put the contract address here’).
- Copy this code.
3. Paste it into the terminal.
4. Write “desk.sendEtherBack”, and press “enter”. Then it creates a transaction hash that you can use to actually see what happens on the Ethereum blockchain.
Sales Contract Example on Solidity
1)Start off with “pragma solidity ^0.4.0”
3)Set up public variables
“address public owner”
“Bool Public is sold = false”
“string public selesdesription = ‘volvo v40’”
“Vint public price = 2 ether”
5) Move on to the Constructor part. Set up the first function so the sale is possible. “function SalesContract”. “owner=msg.sender”
6) Set up the 2nd function – “function buy”. It will be payable, because it will need to accept Ether, and it will have an “if” condition that the “msg.value>=price”.
7) Make sure that the previous owner gets the money by putting “owner.transfer(this.balance)”, and assign the new owner to “owner=msg.sender”.
8) The last change “is sold = true”, and the transaction is completed.
9) In “else” type “revert”. No gas is spent when nothing is sent.
Open your Ethereum wallet to test the contract. You will be able to see all the information about the contract you have created. You should also notice “no” in the “is sold” field.
Select the function “buy”. Make “person 2” buy this. Enter “send” 2 Ether. Execute the contract.
As soon as it is done, you will notice that the owner has changed, and the sales status has changed from “no” to “yes”, as well.
That is how it works. Ethereum provides great opportunities for fine-tuning and setting your own contracts in order to use it for safe transactions. Now it’s time to look at possible ways of improving your existing smart contract.
Making Smart Contracts Smarter: Helpful Tools
After your smart contract has been written, there are some ways to improve it, thus making it more secure and stable. These tools detect popular bugs:
Securify: a useful and free service. You can verify your smart contract here. It will give you an accurate estimation of how well the code was written. You can get a rough picture of the quality of the code. All lines containing errors are marked in red, and you get a brief report about what’s wrong. Securify is useful for non-technical users.
Oyente: an analyzer of smart contracts. It is a Python script from which you can run from your command line. Write “python.oyente.py” to evaluate your Ethereum contract. This tool will take your contract, compile it to the machine code to bytecode, and build a flow graph of your program (a representation of your program designed to allow the script to determine which states your program can be in).
Oyente helps to:
- make sure no commit is pushed
- make sure no pull request is merged
- identify bugs such as Transaction Ordering Dependence and Timestamp Dependence
Any questions – feel free to contact us!