Defining State Variables and Constructor

Andrei Dumitrescu
A free video tutorial from Andrei Dumitrescu
DevOps Engineer and Professional Trainer
4.6 instructor rating • 7 courses • 20,814 students

Learn more from the full course

Master Ethereum & Solidity Programming:Build Real-World Apps

In-depth Ethereum & Solidity Programming. A step-by-step approach to ERC20 Token, ICO, Auction, FundRaising,Lottery,IPFS

09:28:53 of on-demand video • Updated August 2020

  • Understand the Ethereum Blockchain Platform (accounts, addresses, ether, gas, opcodes, transactions, blocks, mining, etc)
  • Have a very good understanding of Solidity Contract Programming Language (syntax and concepts like state variables, storage, functions, visibility specifiers, mappings, structs, enums, etc)
  • Understand concepts like Events, Function Modifiers, Contract Inheritance, Abstract Contracts & Interfaces, etc
  • Design, create and deploy a fully-compliant ERC20 Token and launch an Initial Coin Offering (ICO)
  • Design, develop and deploy a smart contract for a decentralized Lottery
  • Design, develop and deploy a smart contract for a decentralized Auction Platform
  • Understand good practices for developing Ethereum Smart Contracts and see real-world examples to comprehend what is the advantage of Blockchain Technologies.
  • Design, develop and deploy a smart contract for a decentralized FundRaising Platform
  • Use geth and Set Up a Private Ethereum Blockchain
  • Understand what is InterPlanetary File System (IPFS) and how it works. Installing and Running an IPFS Node on Linux or Windows
English Let's plan our state variables. Every auction has an owner, the owner is to the account that deploys the contract and the person to whom the winning bid will go if auction completes successfully. We can separate the admin, that's the account that deploys the contract and has more permissions, from the beneficiary being the person or the contract to whom the funds will go if the auction completes successfully. At this moment we don't separate the Amin from the beneficiary. And we only have an owner: address public owner I'll declare all state variables public because this way it is easier to access them when testing the contract. Auctions also require a start and an end time. Time in solidity is very tricky. BLOCK time stamps are set by minors and can be easily spoofed. So it's not safe to use block timestep. An emerging best practice is to calculate time based on block number. We know that Etherium blocks are generated roughly every 15 seconds. So having this value we can calculate the Start and the End Time. Let's have one example; let's suppose we want our auction to last one hour. This is just an example. We know that every hour 240 blocks are mined. So my auction will end after 240 blocks. So I declare anuint variable startBLOCK Another uint public variable named endBLOCK These two variables will be used for calculating start and end time. It's not cost effective or scalable to save information about the product or thr service that is sold in this auction on the Etherium block chain. Saving a single byte of data on the block chain costs 5gase; recommended solution is to use the IP FS or the inter-planetary file system for saving a large amount of data. So our product information photos and so on will be saved on the inter-planetary file system and on the block chain using a state variable will save only the hash the hash that uniquely identifies my information on IP FS string public IP FS Hesh. We'll talk about the inter-planetary file systemin a bonus to lecture. I also declare an enormous gamble for auction to state my auction can be in writing state or can be canceled or could have already been ended in some state. Both are my auction states. Auction can be started can be drowning in the running state and then let's say canceled. The owner can cancel the auction if something bad happens or if he simply wants and I declare another state variable of type state. My new variable This is public and the name of the Vargo. But it will be auction state. Will save the highest binding bid in an you ain't viable. So you in public highest binding beat the highest binding bid is the current auction price and we have also an our dress a public address where we save the highest bidder. We'll save all bids you know mapping valuable so mapping the keys are of type address and the values are the amount of way the beads are sent to the auction and the name of the variable is beats. So if somebody sent Lexi want ether for this auction I'll register his address is a key of the mapping and the value associated with that key will be one either way. So this variable holds every value sent to my auction with the address from which it came. These are my contract state variables. Maybe we'll add other variables but still at this point this is not. Now let's define the constructor. The constructor will initialize the state variables with eaks arguments. So constructor and now constructor arguments start to block you're in it and the bloke strung on borderline p f s hash here we have underlined to you in underline beat in Parliament and I think these arguments are enough for Blake and other constructor body. I'm initialising the owner with the account that deploys the contract so owner equals MSJ don't sender. The auction will be in the running state. So auction state equals state might be non-viable daughter running. This is how we nationalize the non-viable and then we have the other state viable start block equals line start a block and block equals underline and block IPF face Hesh equals sándor by an IP address Hesh and beats environment equals underline increment OK. I forgot to declare a state variable named bit increment. So here on SCIENCE in GURREY bid increment. I'll explain more about the increment variable in the next lecture. When we implement the place bit function when deploying the contract we must specify here the constructor arguments. So the stock block I don't know. It was just an example of this. Block and block this value from here. Then we have the IP address hash we write it between double codes. So it's a hash here. And so on and the beat increment for example 10 Okay. And the contract has been deployed. We can see here are state viable. This is how this contract will be deployed. But in our example at this moment I want to modify the contract because it's complicated. Every time when I want to test something to write all of these arguments here so only for our example I'll delete all arguments from here and I'll hard code the values. So it's easier for example the stack block is this block if I want the auction to start immediately. So block dot number. This is the current block IP address Hesh like say an empty string and bid increment like say 10. OK let's suppose we want our auction to be valid for one week. What does 1 week mean. Let's see how many seconds are in one week. So 60 seconds in a minute 60 Minutes in an hour. Twenty four hours in a day. This is the number of seconds in a day multiplied by seven. The number of days in a week. So this is the number of seconds in a week and I divide this value by 15 15 being the Ethiopian blook time. So we can say that in a week. Forty thousand three hundred twenty game blocks are mine so my end block will be the start block plus this. This means that the auction will be in the running state for a week. Now it's much easier to test different small things and to try different features. So for example if I want to deploy my contract I simply press here on deploy and the contract is deployed in my state to variables have these values from here. Maybe you think that it's strange for an end user to enter the block number instead of for one week. There is no problem. The end user doesn't have to know about the area blocks and block time and so on the end user will enter the auction end time and the front end interface will calculate all the math behind.