Smart-contracts
Today we are going to continue to more practical part of our tutorial and we will deploy all the smart contracts we will need to run our own ICO using space ICO dashboard.
I hope that you already know what is Ethereum, smart-contract and you have at least general understanding of Solidity programming language. I highly recommend you to spend a couple of hours reading Ethereum and Solidity docs and trying it out.
If you are not developer and not familiar with programming, hire us or any other professional development team to handle everything for you. Remember that smart-contracts are working with real money of real people and in most of the cases — not your own money.
I also recommend you to take a look at OpenZeppelin, read their docs and source code of the contracts. They have high quality and secure implementation of all the basic functionality you may require during smart-contract development starting from token contracts to access management and vesting time-locks. So if you want to implement something using Solidity, first check out if ready to use implementations already exist in OpenZeppelin and/or other projects.
In this tutorial I will use smart contracts we used in one of ICOs we were helping with, you can find this contracts here. I used modifications of this contracts multiple times and they show pretty nice result. Another reason I choose this contracts is that before ICO we order independent security audit of this contracts and we made all the changes offered by auditors to fit security requirements. This contracts have a good test coverage, every function is covered by multiple tests making you sure that they work correctly. Also this contracts had some attention from the community which help us to improve the quality of this contracts. For this tutorial I updated master branch with updated versions of dependencies and Solidity compiler version but you’d better check for updates yourself.
First of all create directory for your ICO where you will clone all the repositories which belongs to your ICO. I will create folder `~/Work/demo-ico` for this tutorial. Than clone this repo to the contracts subdirectory by running `git clone https://github.com/secret-tech/demo-ico-contracts.git contracts` from the folder you just created for your ICO.
There is ERC20 burnable token with limited supply, EthPriceProvider which is used to update eth/usd rate(used to set the token price in USD instead of ETH and keep USD price the same despite of ETH volatility) in pre sale smart contract using oraclize, InvestorWhitelist contract which is used to protect our pre sale smart contract from the contributors which didn’t passed KYC/AML as well as to track referrals for partner program. And of course you can find PreSale smart-contract there. All contracts are covered with tests which you can find in the ./test folder. Tests are the good starting point if you want to understand how to interact with this contracts, how contracts are supposed to be deployed and what are their use-cases.
I highly recommend to spend a couple of hours going through source code and understanding this repo. It will help you to create almost any ICO structure you want.
Go to the demo-ico/contracts folder and build docker images required for this repo by running `docker-compose build –no-cache`
It will take some time and when finished successfully you will see something like this:
Now, let’s try to run the tests of our contracts. To do this we need to run our docker containers and execute `truffle` test in our workspace container. You need to do this steps:
- Run docker-compose up -d
- You should wait a bit until Oraclize ethereum-bridge initialize. Wait for Please add this line to your contract constructor: OAR = OraclizeAddrResolverI(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475); message to appear. To check for it run docker logs ico_oracle_1
- Install dependencies: docker-compose exec workspace yarn
- To run tests: docker-compose exec workspace truffle test
If all tests passed correctly — all functionality works as expected. It means that we are ready to deploy our contracts. You can see what have been tested in nice messages in console output.
Usually to deploy your contracts you will use truffle migrations which allow you to automate deployment process what means less human factor impact. But in this tutorial I will use remix IDE + MetaMask to do the trick. If you don’t know what are Remix and MetaMask, please spend some time in google reading basic info what is it all about.
Before deploying our contracts we need to merge them. Merging contracts means replacing `import X` statement with the imported file contents. So as the result we will get a few large source files which doesn’t contain any imports. We need to do it this way if we want to verify our contracts on Etherscan later because Etherscan verification doesn’t support imports.
To merge your contracts via sol-merger run: docker-compose exec workspace yarn merge. Merged contracts will appear in `./merge` directory.
Before we start to deploy the contracts, let’s take a look how deployment works in tests. We can find it in test/DemoPreSale.js lines 31–47
According to this file we need to do the following steps:
- Deploy DemoToken
- Deploy InvestorWhitelist contract
- Deploy DemoPreSale
- Deploy EthPriceProvider(if needed)
- Set EthPriceReceiver in EthPriceProvider contract
- Set token transfer agents: your address, PreSale address
- Set EthPriceProvider in DemoPreSale contract
- Transfer tokens we are going to sell to DemoPreSale
Open your favourite text editor, copy the contents of ./merge/DemoToken.sol file. Go to https://remix.ethereum.org , login to your MetaMask account and select Ropsten test network in network menu.
Remix will ask if you want to reload the page. Press “Yes”.
Create new file in Remix, call it DemoToken.sol and paste the contents of ./merge/DemoToken.sol in this file.
On the right pane find “Run” section and open it. Ensure that Environment is Ropsten testnet, selected account belongs to you and you have enough ropsten ETH to deploy contracts and test functionality(Something like 0.5 ETH will enough). If you don’t have ropsten ETH you can use one of free ropsten faucets to get some free ether for your tests.
Select DemoToken in the dropdown menu in “Run” section as shown on the picture and press “Deploy” button.
If everything is OK, you will see MetaMask window asking you to confirm contract creation transaction. You can increase gas price if you want your transactions to get mined faster or decrease it if you can wait but want to save some money on deployment.
Press submit and wait for transaction confirmation. When transaction confirmed you can check it out at Etherscan and find your new smart contract address.
In my case it was this transaction: https://ropsten.etherscan.io/tx/0xb32c83f8a2337574f06a7c020591a59f398525f88cef6c0d443b67d919e5313c
and this contract: https://ropsten.etherscan.io/address/0x5c50387b85c8e48315ad403d408ea9c67f036088
I’ve got my contract address in the tx info on Etherscan. There are also some more interesting info like the fact of token transfer and actual tx cost.
OK, we just deployed our first contract and issued tokens to our own address. Now, let’s validate our contract source code on Etherscan so everyone can trust that the source code of the contract we publish belongs to the deployed bytecode. To do this, open contract address on Etherscan, and go to the “Code” tab. You will see “Verify and publish” link. Open it.
Open remix IDE, copy the source code of DemoToken.sol file and paste it in the text area on Etherscan smart contract validator. Enter the name of the contract “DemoToken” in the corresponding field. Go back to Remix IDE open “Compile” tab on the right pane and find current compiler version. In my case it is 0.4.25+commit.59dbf8f1. Find the same compiler version in Etherscan contract validator dropdown menu and select it.
Scroll down, fill captcha and hit “Verify and publish” button. If everything is OK you will see the window saying that the contract source code and ABI verified and published.
If you update smart contract page on Etherscan you will notice some changes like green label on the Code tab and 2 new tabs called “Read contract” and “Write contract”. Only verified contracts on etherscan have this 2 tabs. Verification doesn’t make any sense in terms of functionality, it’s just an additional point for the trust to your contracts. So if you don’t care about the trust you can just skip this step. I will do verifications of all the contracts I deploy in this tutorial to make it easier for you to review and read the source code of the contracts as well as to check their current state.
Create the new file in Remix IDE and call it InvestorWhitelist.sol. Copy the content from the corresponding file in the ./merge folder on your local machine and deploy InvestorWhitelist contract using Remix IDE and MetaMask.
When contract deployed, verify the source code on Etherscan like we did with DemoToken contract.
My deployment transaction: https://ropsten.etherscan.io/tx/0xfaaba7f611323050a05621139fc3335ef9530483b796433f95492c6764e1cbb4
My contract address: https://ropsten.etherscan.io/address/0x3a7cc340d7c2c6a1f0b92886edfd935b8559be7f
Now, let’s take a look at our DemoPreSale contract. It would be more tricky to deploy because now we will need to use constructor arguments in order to configure this contract to our needs.
To see what constructor arguments we have there and how we need to pass them we can take a look at DemoPreSale contract constructor and test/DemoPreSale.js test, especially it’s “beforeEach” hook.
We will need to pass the following arguments:
hard cap in tokens(ether, not wei), soft cap in tokens(ether, not wei), token address, beneficiary(who will receive the money when presale is finished), whitelist contract address, eth/usd rate, start timestamp, end timestamp. I would like to sell maximum 1M SPACE tokens during this presale. With no soft cap. I would like to be beneficiary of this contract and use whitelist we deployed before. Current eth/usd rate is 225.62$ for 1 ETH and I want my presale start at 10th October 2018 20:00 GMT +3 and end at 10th October 2019 20:00 GMT +3.
Open Remix IDE create file called DemoPreSale.sol and copy contents of the merge/DemoPreSale.sol file. Select DemoPreSale contract in the dropdown menu at Run tab on the right pane.
Now, on the right side near “Deploy” button you can see text input field. We will pass our constructor arguments using this input field. In my case I will input the following:
1000000, 0, “0x5c50387b85c8e48315ad403d408ea9c67f036088”, “0x11EbB8665FcB1Ad9743566fDd2Dc76D234E9b7CA”, “0x3a7cc340d7c2c6a1f0b92886edfd935b8559be7f”, 22562, 1539190800, 1570726800
Pay attention at numbers. We pass eth/usd rate in cents so if rate is 225.62$ . we need to pass 22562. The last two numbers are unix timestamps. 1539190800 stay for 10th October 2018 20:00 GMT +3 and 1570726800 is for 10th October 2019 20:00 GMT +3. You can convert human readable time to unix timestamps easily using timestamp convertor.
Press “Deploy”.
My deployment transaction: https://ropsten.etherscan.io/tx/0x7e3d875e2d8c4d30c83d854001fac5458d5c7021880973ded5215c30a0fad165
My deployed contract: https://ropsten.etherscan.io/address/0xc470597fa47c589997f9d1c24d1f5ece9d18bd6a
As usual I will verify contract source code on Etherscan. Please note that this time we will need to pass ABI-encoded constructor arguments in Etherscan contract verifier. In my case it will be
00000000000000000000000000000000000000000000000000000000000f424000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c50387b85c8e48315ad403d408ea9c67f03608800000000000000000000000011ebb8665fcb1ad9743566fdd2dc76d234e9b7ca0000000000000000000000003a7cc340d7c2c6a1f0b92886edfd935b8559be7f0000000000000000000000000000000000000000000000000000000000005822000000000000000000000000000000000000000000000000000000005bbe3010000000000000000000000000000000000000000000000000000000005d9f6390
After that deploy EthPriceProvider and do all like we did it before. When done you need to set watcher on this contract — our previously deployed DemoPreSale.
After that let’s tell this price provider to update the price by calling startUpdate function and pass starting price parameter in cents. In my case it’s 25563.
It’s time to set EthPriceProvider address in our DemoPreSale contract so it can receive updates from provider contract. You can do this in Remix IDE on the right pane in Run section.
We are almost done with our contracts. All we need to do now is to allow us and presale contract to transfer tokens by setting this addresses as transfer agents in DemoToken contract.
To be able to sell tokens from presale smart contract we need to send the tokens we want to sell to the contract address. To do this find `transfer` field in DemoToken and pass “0x34240b44ff61c4c3542578636a12ff13e439ff2f” as the first argument and 1000000000000000000000000 as the second. It will send 1 million tokens(hard cap).
When transaction is mined you can see tokens on smart contract at Etherscan
Yay! We have successfully deployed our contracts. It’s time to test and if everything is fine we will continue to the backend configuration stage. I will show you how to test the basic functionality – contract must send . corresponding amount of tokens for received ETH according to current ETH/USD rate and token price in USD. Note that our contract checks if investor was added to the whitelist and don’t allow to invest if address is not there. So if you want to buy tokens from your wallet you need to add this address to the whitelist. To do this just find InvestorWhitelist contract in the “Run” section at the right pane. Pass your address in double quotes as an argument and send transaction.
Now, let’s go to MetaMask and send 4 ETH to our contract.
My transaction: https://ropsten.etherscan.io/tx/0xc0b3a03d979989fd43240ac5f1c35c22a3f4beffb463101fcbbd86842145b755
As you can see it’s completed successfully and I received back 12,810 SPACE tokens from the contract.
In the next parts we will configure backend and frontend applications to run locally and work with our ropsten smart contracts.
Need your own custom ICO dashboard or other software?
Contact us by email: [email protected] or Telegram https://t.me/secret_tech1
Have any troubles making it work?
Join our Telegram group and discuss with developers! https://t.me/spaceicodashboard
Source: Crypto New Media