× SPECIAL FORCES      |     LOCKSPORT      |     SCI FI      |     INSECTS      |     RECIPE      |     RED DEAD REDEMPTION      |     WOODWORKING      |     FISHING      |     SCI FY      |     CRIME STORIES      |     BIRDS      |     STYLE      |     TV      |     INTERESTING      |     ENVIRONMENT      |     STAR WARS      |     OLYMPICS      |     EASTER      |     GOD OF WAR      |     FOOD & DRINK      |     PHOTOGRAPHY      |     WEDDING      |     COLD WAR      |     WAR      |     CHRISTMAS      |     ARCHITECTURE      |     FITNESS      |     ART      |     TOYS      |     SKATEBOARDING      |     YOUR HOME      |     PSYCHOLOGY      |     RELIGION      |     HEALTH & WELLNESS      |     PETS      |     GUNS      |     MATH      |     BIOLOGY      |     WW1      |     BUSINESS      |     WORLD WAR 2      |     AUTOMOTIVE      |     STEM      |     GUITAR      |     AQUARIUM      |     HOMEBREW      |     KITCHEN      |     SPEECHES      |     KIDS      |     ECONOMICS      |     PROGRAMMING      |     REMOTE CONTROL      |     MINECRAFT      |     MOTORBIKES      |     VALENTINE'S DAY      |     MULTIPLICATION      |     LAWNCARE      |     GARDENING      |     PI DAY      |     LEGO      |     BLOCKCHAIN      |     COMPUTER SECURITY      |     SPORTS      |     CATS      |     MUSIC      |     FORTNITE BATTLE ROYALE      |     FARMING      |     ARTIFICIAL INTELLIGENCE      |     ROCKCLIMBING      |     RELAXING      |     CSGO      |     BABY      |     AVIATION      |     MENS STYLE      |     ANIMALS      |     SCIENCE      |     DOGS      |     SHOOTING SPORTS      |     MOVIES      |     OUTDOORS      |     BIKING      |     HISTORY      |     FASHION      |     FAR CRY 5      |     HALLOWEEN      |     STPATRICKS      |     CRYPTOCURRENCY      |     ENGINEERING      |     GEOPOLITICS      |     POLITICS      |     TECHNOLOGY      |     FUNNY      |     TRAVEL      |     MARTIAL ARTS      |     GAMBLING      |     GAMING      |     KNITTING      |     BEAUTY      |     PHILOSOPHY      |     ANCIENT WORLD      |     POTTERY      |     ARCHERY      |     THANKSGIVING      |     HOME REPAIR      |     CRAFTS      |     ORGANIZATION      |     BIG THOUGHTS

  • 00:05
    - [Instructor] The most interesting part of ethereum are smart contracts.
  • 00:09
    You can think of smart contracts like autonomous agents deployed onto the Ethereum block chain.
  • 00:14
    Smart contracts are made up of some state, or data as well as some code that manipulates that data.
  • 00:20
    Just like a user account, smart contract accounts also have their own balance, as well as a public address which looks the same as a user account address.
  • 00:29
    To interact with smart contracts you send transactions to it with some extra data to specify which function you want to invoke, as well as any input parameters for the function.
  • 00:39
    The transaction will invoke the function and return any possible output once it has been mined.
  • 00:45
    We're going to be using Remix to write smart contracts, which is an online IDE available for free at
  • 00:53
    It comes with a compiler, editor, and debugger for solidity.
  • 00:57
    The great part is that you don't need to download or install or set up anything.
  • 01:01
    You just go to this webpage and start writing smart contracts, which is awesome.
  • 01:05
    The UI is divided into four main sections.
  • 01:08
    The left part is our file explorer, where we can open Solidity files from our hard drive.
  • 01:13
    The middle section is our editor, where we can view and write our source code.
  • 01:17
    At the bottom we have a terminal which prints out important events and transactions.
  • 01:22
    And the right most section contains some handy tools for compiling, running, and debugging our code.
  • 01:28
    The first example we'll look at is a smart contract called Hodor.
  • 01:32
    Let's load up the source code.
  • 01:34
    You'll notice that Solidity source files end with the .sol extension.
  • 01:38
    The syntax of Solidity resembles JavaScript, so it should be fairly easy to pick up.
  • 01:43
    The first line we see at the top uses the pragma directive, this tell the compiler that the contract is written for a solidity compiler version of at least .4.0 or anything newer that doesn't break functionality.
  • 01:57
    We then have a multi-line comment block briefly describing what this class does.
  • 02:01
    The next line is where we declare the name of our smart contract using the contract keyword.
  • 02:07
    We've simply called our class Hodor, because as you'll see, all it does is return a simple greeting.
  • 02:12
    We also declared two state variables called creator and greeting.
  • 02:16
    Creator has an address data type, which is used for storing addresses of accounts.
  • 02:21
    Greeting is a string data type which just stores some text greeting.
  • 02:25
    We initialize both of these variables in our constructor.
  • 02:28
    The constructor is declared using the function keyword followed by the name of the class.
  • 02:33
    The constructor is a special function that is invoked only once when a contract is first deployed to the ethereum block chain.
  • 02:40
    You can only declare a single constructor for a contract.
  • 02:44
    We also inject the initial string greeting as a parameter into the constructor and set the greeting variable to that value.
  • 02:51
    In the second line of the constructor we initialize the creator variable to a value called message.sender, but if you look closely you might wonder where this value came from since it's not being injected into the constructor.
  • 03:03
    This is because message is a global variable that provides certain information about the message such as the address of the account sending it.
  • 03:12
    We can get the address of the account creating the contract using message.sender in the constructor.
  • 03:18
    You may notice that we don't actually use this creator variable anywhere in our contract because this is just a simple example.
  • 03:25
    We could potentially use this information to implement access control to certain functions.
  • 03:30
    We'll see an example of this later.
  • 03:32
    Below the constructor we define two functions for this contract, greet and set greeting.
  • 03:38
    Invoking greet will simply return the currently saved greeting.
  • 03:41
    You can see that we declared a return type of string for the function and we used the return keyword to give back the value of greeting.
  • 03:49
    We also used the constant keyword, which is a way of saying that this function does not modify the contract state, and it doesn't make any rights to the block chain.
  • 03:57
    Anyone can also change the saved greeting using the set greeting function.
  • 04:01
    This method doesn't have a return type, but does take a string input parameter as the new greeting.
  • 04:07
    Now, let's try to actually deploy and interact with this contract.
  • 04:11
    We can go back to our right most section and take a look at the tools available there.
  • 04:15
    The first tab is the compile tab, which allows us to compile the code or enable disable auto-compile.
  • 04:22
    It also prints out any static analysis errors and warnings.
  • 04:25
    The run tab is where we can actually deploy and execute our code.
  • 04:30
    We can select from three environments to run the code.
  • 04:33
    The first is JavaScript VM, meaning that is uses and ethereum node running inside the browser to deploy the contract.
  • 04:40
    It's convenient but it also means that if you reload the browser it will restart a new node from scratch and you lose the old one.
  • 04:48
    The other two options rely on nodes running external to the browser.
  • 04:52
    Injected provider connects to a web three provider like MetaMask, that can inject web three.
  • 04:57
    And web three provider allows you to connect to a node by specifying the url.
  • 05:02
    This could also be a locally running node, like TestRPC or Geth.
  • 05:06
    You can also choose from a list of accounts associated with your current node to be the author of the contract, and specify the amount of gas you want to pay for the deployment transaction as well as any initial ether to provide.
  • 05:19
    Below we also see an at address and create button with a list of the compiled contracts available.
  • 05:26
    You can use at address to connect and interact with an already deployed instance of a contract if you know its address, but since we're deploying for the first time, we'll use the create button.
  • 05:36
    It has a field for the input greeting parameter which we need to specify as a string in quotations.
  • 05:42
    Otherwise, you'll get an encoding error.
  • 05:44
    Clicking the create button will deploy the contract to the block chain using a transaction.
  • 05:50
    We can create as many instances as we like and each deployed instance will be visible at the bottom of the run tab.
  • 05:56
    We can also see the corresponding transaction logs in the terminal.
  • 06:00
    Clicking on the details button will give us more info such as the resulting contract address, and the amount of gas used to process the transaction.
  • 06:08
    We can then interact with the instance, and invoke any of its functions.
  • 06:12
    If we click the greet button to invoke the greet function on this contract we can see it returns the greeting set from the constructor.
  • 06:19
    Let's set a different greeting and click the set greeting button.
  • 06:22
    We see the corresponding transaction in the terminal as well.
  • 06:26
    And if we now invoke the greet function we should get back the updated greeting.
  • 06:31
    Let's look at another example, say we wanted to create our own cryptocurrency called DragonStone.
  • 06:37
    How would we do this?
  • 06:38
    Let's look at the code.
  • 06:40
    We declare our DragonStone smart contract using the contract keyword.
  • 06:44
    We have two state variables, creator and balances.
  • 06:47
    The address of the account creating the contract is stored in the creator field, like the last example.
  • 06:53
    We also added the public keyword to the creator field.
  • 06:56
    The public keyword automatically generates a function that allows contracts to access the current value of the field like this.
  • 07:05
    Without this keyword other contracts cannot access this field.
  • 07:09
    The balances field, also publicly accessible, keeps track of who owns how many DragonStones.
  • 07:15
    The automatically generated function in this case is a bit different, and would have a signature like this, allowing anyone to query the balance of any account.
  • 07:25
    This is because we have a data type of mapping, which can be thought of as hash table of key value pairs.
  • 07:31
    The key value data types are defined inside the brackets as address keys mapping to unsigned integer values, representing the amount owned by an address.
  • 07:41
    We also see a new keyword, the event keyword, events are used to indicate that something of interest has happened.
  • 07:49
    They're fired from within functions and can be listened to by other applications, either Server Side or Front End.
  • 07:56
    As soon as the delivered event is fired the listener will receive the from, to, and amount arguments.
  • 08:02
    This feature of solidity makes it easy to track transactions.
  • 08:06
    In the constructor we only record the address of the creator.
  • 08:10
    This information is then used to ensure that only the creator can invoke the create function to issue currency.
  • 08:17
    The if condition in create verifies that the sender of the message is the same as the creator.
  • 08:23
    Otherwise, it throws an exception and does not create any currency.
  • 08:27
    If the sender is the creator then the balance is updated for the receiving account by the specified amount.
  • 08:33
    Transferring DragonStone can be done by anybody, not just the creator.
  • 08:37
    The only requirement to transfer an amount is to actually have that amount in your account.
  • 08:43
    Otherwise, the transfer can't be completed.
  • 08:45
    We verify the account balance as the first step in the transfer function, returning early if there is an insufficient balance.
  • 08:53
    The balances for the sender and receiver are then updated by the specified amount to complete the transfer.
  • 09:00
    A delivered event is then fired to notify listeners of the successful transfer.
  • 09:05
    And there we have a very basic crypto-currency that the creator can issue out of thin air, and owners of this currency can transfer it to whomever they please.
  • 09:14
    Note that this class is only four lines longer than the Hodor class, which is pretty cool.
  • 09:19
    Let's deploy this contract and test it out a bit.
  • 09:23
    We can try to create some DragonStone as the creator of the contract, and issue ourselves five DragonStone.
  • 09:51
    If we try to create DragonStone as someone who is not the creator, we'll get back an exception since it won't get past our if condition.
  • 10:03
    Let's try transferring some DragonStone from an account that doesn't have any.
  • 10:07
    We should also see an exception since we checked for the sender's balance before transferring.
  • 10:18
    Now, let's try that again but as the creator, and we can see that the transfer was made successfully.
  • 10:35
    And there we have a simple introduction to smart contracts.
  • 10:39
    To learn more about ethereum and solidity check out our online guides and courses at
  • 10:45
    Thanks for watching and we'll catch you next time.

How To Create An Ethereum Smart Contract

How To Create An Ethereum Smart Contract The most interesting part of Ethereum are smart contracts. You can think of smart contracts like autonomous agents deployed onto the Ethereum blockchain.

Community tags: blockchain     HOME     SIGN UP     CONTACT US