Ethereum in Depth: Smart Contracts – Part 1: What is a Smart Contract?

In this video I give an introduction to smart contracts in ethereum.

First I explain what accounts are in a simpler currency only blockchain.

Then I show how ethereum introduces a second type of account: a code controlled account (aka smart contract).

This type of account does not have a private key but instead is controlled by code.

It can store ether, just like user accounts, or it can keep a balance of 0 and be used to deliver some other function within the context of an application.

I then give examples of how you can use smart contracts.

TRANSCRIPTION

all right so in this video I’m going to

explain what aetherium smart contracts

are now it’s going to be actually a two

part series in this first video I’m

going to use the whiteboard to explain

and then in the second part I’m going to

use a screen cam capture on my computer

while I write a smart contract I then

publish it to the blockchain so let’s

get started here with the first part so

the first part is what is a smart

contract

well basically smart contracts are the

building blocks that you use in order to

create decentralized applications so in

order to understand how they’re

structured we first have to understand

how accounts work so if you have any

blockchain like let’s say Bitcoin or

light : or any of the other currency on

my block chains the way that it works is

you have these accounts okay and they

each have a balance so let’s say ten

Bitcoin and they each have an address so

if this guy wants to send some Bitcoin

over here then he’ll make a transaction

he’ll say you know five Bitcoin and then

of course this will go down by five and

this will go up by five right and so we

have these accounts and these accounts

are controlled by a private key okay so

that’s important to note is the way that

the user has access to the account it’s

because he owns the prat he or she owns

the private key and then can dictate

where the Bitcoin is going to go well in

aetherium

it’s actually very similar in the sense

that we do have these accounts we call

them user accounts and they each have a

private key they each have an address

and the e type of balance the difference

of course is that instead of them being

called Bitcoin they’re called ether so

the units are ether

okay so you could actually use aetherium

in the same way you use a currency only

blockchain where you just exchange you

between user accounts these units that

have value now in aetherium though there

is a second type of account so instead

of just having user accounts are

controlled by private keys we also have

what are called smart contracts and

smart contracts have a balance and they

have an address

just like these other accounts except

instead of having a private key they are

completely controlled by the code that

is programmed into them when they are

created and this code can never be

altered okay so nobody in the system

there’s no administrator that has the

power to come in there and change the

code on a smart contract it is immutable

it is 100 percent per minute so if you

made a typo then you’d have to create a

brand-new smart contract that would

exist at a brand-new address and start

with a balance of zero so smart

contracts have very similar privileges

in fact I think all the privileges to

the user account has in the sense that

they can send ether they could receive

ether okay so let’s say that this person

wants to send a smart contract five

ether so he’s going to use the address

of the smart contract send it there and

then now the balance goes up to five ok

then of course this smart contract can

send ether so long as the permissions or

the rules dictating how that’s going to

happen are in the code and this address

is in there so then it can send one

ether over here and then this guy will

get six ether

okay so basically a smart contract is an

account that is controlled by code

rather than an account that is

controlled by a user now here’s where it

gets interesting and work is pretty cool

is that these smart contracts actually

do not need to keep a balance okay they

can actually just always have a balance

of zero and since they can message and

interact with other smart contracts you

can have these smart contracts that are

interacting with each other like objects

in an object-oriented program that are

working with each other to deliver some

sort of task so for instance like a

voting maybe you want to put how

elections happen on the blockchain and

so people would vote it would message a

specific smart contract in order to

update its variable so let’s say there’s

two people there’s one candidate here

and one candidate here and so then you

would message the smart contract and the

variable for this candidate would go up

and another person and the variable for

this candidate would go up right and so

you can use smart contracts in order to

store information and update information

in the same way that if you have an

object-oriented program you have all

these different little objects and

classes instances of crosses that are

working together

in order to deliver some sort of

function and so smart contracts can

store balance but they do not have to

store balance in some instances it makes

sense for them to store balance for

example if you’re a smart contract was

what’s called a decentralized autonomous

organization which is a basically a

company and you know the CEOs specific

key and the CFO and the chief design

officer and operating officer all these

people to have a specific key that then

gives them you know privileges within

the smart contract to hire people

and add them as employees know that so

maybe you have an autonomous

organization it’s within a smart

contract well in that case then yeah

maybe you’d want to have some funds that

are stored in there and so you can

actually store ether within that and you

can use that to pay employees and stuff

like that so a smart contract can store

a balance of ether but it does not have

to and decentralized applications or

what are called apps are in many cases a

collection of many smart contracts that

are working together it’s a collection

of many of them instead of just one but

in some cases if it’s a simple app it

might just be one so let’s go ahead and

give some examples of how this works

just to you know cement this here so

let’s say that we own a company and we

have five employees and I’m going to

represent these employees with these

circles on the bottom and we want to

just for simplicity of this example

we’re just going to pay them all equally

so we have ten ether and we’d like to

divide that between these employees so

we’re going to go to the payroll manager

here which input to get into this

example is going to be a human and we’re

going to ask them can you please spit

this nut spit but split this ether up

between these employees and they say

sure no problem

and so we trust it to them to send to

ether to all of these people well it

turns out that we look back after work

and because this person has free will

you know they can do what we say or not

turns out today exercise their free will

to do something that we didn’t ask

they actually favored this person over

this person so they only gave this

person one ether and this person three

ether and they gave these people the two

like we asked well we didn’t we didn’t

ask them to do this this was either a

mistake on their part or deliberate

corruption you know like why are they

doing this for them asking to do this ok

it might just be human air

who knows okay well when we’re dealing

with people that have freewill and

they’re doing just you know basic menial

tasks sometimes they can screw those up

and so wouldn’t it be great if we could

actually automate this person’s job and

replace it with a smart contract and so

in the next video I’m actually going to

show you how to write this smart

contract and it’s basically a payroll

the very simple payroll thing where it

just stores the addresses of five people

and then all either that is sent to this

contract is then divided equally between

these five people and they can collect

it okay so instead of it being uneven

it’s going to be two for each person

okay so in this case we have a permanent

smart contract on the blockchain that

stores these five addresses and nobody

has the power to change this in other

words the code dictates how this is

going to happen now this is a really

important point that you can’t change

this in the last video I gave an example

of how you know maybe have a social

network or something and you’re like

let’s say Twitter type thing where

you’re sending status updates and then

they’re publishing the status updates to

all of your friends okay well since they

have control over that server they have

like the master key and administrator

account

to that server that means that they can

delete things and edit things and censor

things and stuff like that whereas and

of course an example like Twitter of

course for the most part they’re going

to do it the right thing but let’s say

you have a government that doesn’t want

people to have free speech or whatever

and so they go knock Computers door and

they say you know you got to remove this

tweet or else

okay so then Twitter’s like we’ll shoot

I guess you have to remove the tweet so

with the blockchain though nobody

not even the person that created the

smart contract has the power to change

the smart contract so once it’s there

it’s there and it’s immutable it’s not

changeable and so in the case of a

dissent or in the case of a centralized

server then you have to protect that

administrator account you have to guard

the master key you have to worry about

hackers that might get in there and

change things around well in this

example nobody has the power to change

this and so once it’s there and you’re

sure it’s bug free then you don’t have

to worry about people changing it there

is no master key to protect and so in

this sense you will always be able to

know that when you send ether to this

spark contract it will be divided

equally among these these people and so

if I added another ten ether then these

people each collect four instead of two

if I added another ten and they would

collect six instead of two right so it’s

always going to divide it equally

between these people and so that’s a

good example of a simple smart contract

and again in the next video I’m actually

going to write the spark contract I’ve

already tested it and wrote it out so

then I’ll just do it again for you guys

and you’ll be able to see how this works

so let’s go ahead and give another

example of what you could use a smart

contract for so there’s this company

that’s really smart

I think and what they’re doing is called

augur and what they’re doing is they’re

collecting a bunch of and whether or not

this company will be the one that makes

this work or not I don’t know okay so

I’m not this isn’t like an investment

advice or anything I got to be careful

about that because this industry is so

you know filled with stuff so I don’t

know if orders can be the one but

there’s the certain technology where

basically they’re they’re collecting

real world information like sports

scores or like election results or even

were important things I guess in my

opinion which would be like a stock

price okay or natural disaster

information like earthquakes and floods

okay and so you’re collecting all of

this information and then this is now

available and it’s doing it in a

decentralized way you could read about

how that company happens but it’s

collecting all this information that

didn’t is available to smart contracts

and so you could imagine that if you

know what the stock price is then you

can do like options like we do in the

stock market where you can you know

write up a certain set of conditions and

if they’re match then you get a payout

and it’s not you don’t write so you can

do all sorts of things like that as far

as natural disasters imagine that you

want to automate an insurance company so

you have all these people that are

paying in a certain fee every month and

then the smart contract is collecting it

okay so it has a ton of ether and then

it also has access to this natural

disaster information and so let’s say

that you know this guy lives in a

certain area that was just flooded well

then this guy can now get his payout

because the smart contract knows about

that and so you can automate insurance

companies where instead of relying on a

person that says yeah if your house is

flood we’ll pay don’t worry about it

well now you can rely on something

that’s immutable and unchangeable so if

you have like a government program where

everybody’s paying in and had

expectation there’s a certain age or

whatever they’re going to get their

money back but then the government

spends the money on going to war or

something and it turns out they don’t

have the money okay well in that case

you’re trusting somebody who may or may

not pay you back well in this case all

the rules on how this money will ever be

handled are in the contract and so you

know and you also know that nobody has

the power

change this so you know that if those

rules say what it is that you want to

happen that if there’s natural disaster

and then you get your payout then you

know that so long as those rules say

what you want they will always be there

and it will be reliable all right so

that is another example of course

there’s so many different things imagine

any sort of you know information that

you need in order to trigger some event

there’s so many different things you

could you could do with this and then

let’s go ahead and give another example

we’ll give one more example then all in

the video so another example that I’ve

found kind of interesting is this whole

idea of the Internet of Things devices

so the Internet of Things is like the

smart light bulbs and the door locks and

all those things we have in our house so

imagine that you have this door lock

kind of like the electric ones at the

hotel where you flash the card but

instead of flashing the card this is

connected to the Internet okay and

because it’s connected to the Internet

it can be tied to a specific smart

contract and so the smart contract may

also control like the lights and in the

hotel room that this opens up into might

even control the water in the this is a

water drop the water like the water

meter inside a hotel room or whatever so

that if you want to use this hotel room

then what you can do is pay into the

smart contracts

maybe there’s a fee for 24 hours you pay

in that fee and then all of these things

unlock the door unlocks for your account

the water turns on and the light bulbs

start working for 24 hour period okay

and so you can imagine all the cool

things we could then automate within our

society as these things have connection

with real-world things and items like

door locks and light bulbs and stuff

like that

so yeah this is pretty cool and one more

thing I want to say is that how did

these smart contracts work together in

order to create like a bigger thing like

a social network or something like that

well in programming you have this idea

of object-oriented programming where you

divide the tasks up between a bunch of

things well each smart contract can be

treated as specific objects right and so

then you have all these contracts so

working together and smart contracts can

inherit from other smart contracts and

so you can organize things in a similar

way that you do with your existing

programs now the difference between this

and an object that may exist on a

centralized server is that the people

have to actually pay to change the state

of an object okay so let me write this

out so let’s take the centralized model

and just for drama I’m going to make it

I’m going to call a mean mad server okay

it’s a centralized server and it has

this evil phase because it steals your

data on that kind of thing of course I’m

exaggerating because servers are

obviously a wonderful thing that have

helped us a lot but just for drama sake

so you have this server and let’s say

it’s a social social network social

network where you’re uploading you know

images or whatever or status updates to

the server well whoever owns this server

is paying for the electricity costs

they’re paying for the warehouse that

it’s in and they’re paying for all that

stuff and so they have total control

over it and you don’t really have to pay

okay I mean in some cases you pay a

monthly fee but you don’t have to pay

because they’re paying for you and maybe

a little make their money off to

advertise themselves well in this

decentralized network we have a copy of

all the data that would be on a

centralized server that is actually

spread among those

two different nodes and all of these

people are then hosting basically what

would be a server and they’re keeping it

in harmony with one another so once this

person updates it all the other people

if it’s a valid change didn’t it make a

copy of that change and so basically

have all these people that are running

what would be a server and they’re

keeping it in sync with one another and

they actually don’t have the power to

change it because the way the protocol

is written they’re just adding changes

and it’s the people that are uploading

that are making the changes but they

don’t have the power to change it there

is hosting it well these people need to

get paid okay they need to get paid and

so who’s going to pay for their

electricity costs and who’s going to pay

for the warehouse that they have all

their supercomputers in and that kind of

thing well the way that it’s going to

work is that the person that makes the

change which would be you is going to

pay to make the change and so if you

want to you know interact with the spark

contract then you will have to pay in

order to update all of this of course

they’ll just be as a little fee but what

will happen is that it’ll pay for these

people to then have you know their

electricity costs and things like that

and these of course are the miners or

does the people that are storing a note

but they don’t get paid if they’re just

sort of note the miners are the ones

that are making changes to the

decentralized server and therefore

there’s a one stat get paid to do that

all right so what was the point of all

this well remember we started with this

idea that you have all these objects

just like you do an object-oriented

programming but the thing that’s

interesting and these are all working

together but the thing that’s

interesting is that the user is going to

be paying in order to change the state

of these objects okay and so you have to

design your network in such a way where

you’re cognizant of that fact

because people aren’t going to want to

pay just to update their profile picture

unless they’re really worried about it

being censored or something but if you

have a social network where people then

get reimbursed if they make a post that

has thousands of likes or something

well then maybe in that case people

would be willing to actually pay a fee

to use your network so there’s this

whole set of design challenges that we

get into as far as how are you going to

make a decentralized social network

viable okay and there’s some people that

are trying to do it one of them is

called Akasha

which is making the first decentralized

social network but we’ll see if it is

actually a viable thing whereas certain

apps already totally viable 100% you

know working as of today and those apps

are the primary killer app so far is

crowdfunding apps where you know you

have a collection so let’s take

decentralized version as the first

example which would be Kickstarter or

something like that

well Kickstarter has to pay in order to

have the server they have to figure out

how all the different currencies are

going to come in and what they’re going

to which currency they’re going to pay

it out in get to deal with all the laws

in all those countries and stuff like

that

now with aetherium since you already

have this globally accepted currency

then people are just going to pay into

the crowdfunding thing with ether okay

and the contract can store the ether so

you don’t even need to have a bank

account so you have all these people

that are paying into a contract for a

crowdfunded thing and if it reaches a

certain fundraising level then you know

it pays out to the person who’s going to

create the products and if it doesn’t

reach that level then all the money goes

back to the people that paid into the

contract okay well in this case this is

like a totally awesome thing to do

because you don’t have to pay that 10%

to the centralised version like what

Kickstarter would be and so in this

example it actually makes sense people

don’t have to care if they pay a five

five

see to donate in five dollars or $10 or

maybe a whole lot more dollars in order

to a project because they’re already

sending money and in the fact that the

person that is receiving the payout it

makes a lot of sense for them because

they don’t have to pay the ten percent

to the middleman and so if you are

raising like ten million dollars well

you can save ten percent of that which

would be like what like a million

dollars and so it makes sense for you

just to write a decentralized contract

to handle your crowdfunding campaign and

to go through a centralized source so

hopefully that makes sense people who

are watching here and I’m going to go

ahead in the video here and then in the

next part what we’re going to do is of

course write a smart contract and then

publish it to the blockchain so that you

can see how that process works alright

thanks for watching