Ethereum is all the rage right now, promising one world computer that anyone can deploy arbitrary code (known as smart contracts) to. But if you actually try to get started writing your own smart contracts, you might find the entire ecosystem a little tricky to wrap your head around.
Fortunately, Will Warren stops by to help me set up an Ethereum dev environment and teach me the basics of writing and deploying smart contracts to the Ethereum blockchain.
First we write a hello world contract, and compile it using both the browser based solidity compiler and an ethereum dev framework called truffle. Once we get the hang of the workflow deploying and querying contracts against the local dev environment, we create our own metacoin from scratch and figure out how to deploy it to the live Ethereum testnet. We also talk about some basic smart contract design patterns, and some nuts and bolts of how Ethereum works under the hood. If you’re interested in Cryptocurrency, you don’t want to miss it.
TRANSCRIPTION
okay so to do a hello world smart
contract like what’s the first thing you
need to do then when I say hello world I
mean like actually making a contract
deploying it and like testing that it’s
valid like that’s what I consider like
lol we’re like you could call one
attribute on it and like verify that it
gives you the result that you wanted
like what’s the first step well okay so
I’m sure we’re going to go through it
here and now but I would say the first
step is go on to the etherium foundation
website and go through the hello world
tutorials or odd and go through the
series of steps it recommends and
exactly that like this right mysterium
yeah gotcha so what is either learn the
solidity language documentation so just
start here basically yeah absolutely
oh cool yeah there’s actually a series
of pretty good tutorials on there but
you know I think they’re a good starting
point it’s always easier to go through
something with another person guiding
you it can be kind of intimidating to
just go to a web page and try and figure
everything out on your own yeah after
something that I experienced yeah I’m
sure you’re banging your head against
your computer for about six months
before you finally got your head around
that right but fortunately the
documentation it’s done pretty good and
there’s a lot of people working on
developing this so full-time making it
easier for people to pick up so awesome
okay cool so given that I’ve already
gone through the helloworld tutorial but
assuming that I didn’t what’s the first
thing you recommend someone did to like
actually get started like what
configuration would they need or just
should they just open up their text
editor and start coding like immediately
like how do you how can you just get and
deploy a simple thing just so you know
you have the workflow down and then you
could get into like actually the
patterns that you use to develop the
contracts go to uses a solidity online
compiler so those elyda T online
compiler right I actually think I have
the compiler installed locally but I’m
assuming you mean this thingy right yeah
so click the top link and it’ll take you
to the most recent version this time
yeah okay
and so what this is is it it runs in
your browser it compiles your solidity
code in real time and you can simulate
the polling contracts and then you know
you can set values in the contract or
query values of a contract and it’s like
okay if you’re trying to do like a full
scale like you know create like an
entire platform with smart contracts
this is not the way to go but if you’re
trying to basically become familiar with
like very simple contracts then this is
the place that I think is really easy to
start okay
so can we make a simple contract in this
and see what all the different thingies
do yeah all right really
so what’s like the simplest contract you
can write is just contract so this also
like Winsett for you like it’ll tell you
when there’s an error right
yeah sort it has a just-in-time compiler
that’s pretty cool yeah it’s really
convenient and this is compiling
solidity into the ether script that like
assembly kind of language correct so you
you can so as you write your solidity
code you’ll you’ll see on the right side
of the screen there is the bytecode so
the compiler compiles solidity into
bytecode and that bytecode is what is
eventually deployed onto the network so
this yes this is the thing that you
would be deploying correct Wow yeah
that’s crazy and it can take a huge
contract and put it down into like a few
lines in s basically on the bike code
can be pretty big so I feel a big
contract if I code might you know sort
of be proportional to the size of the
contract okay so yeah I mean there’s so
there’s basically just like a few things
that you need to to know about a smart
contract you know if you if you have
your smart contract written and you want
to know what to do with it well you’re
going to compile it into byte code
that’s what you deploy onto the ethereum
blockchain and then you have an
interface then the interface just
basically shows
no other smart contracts or let’s say
you have an application how to interact
with that contract that you’ve employed
so like all the different functions you
could call on it right in some interface
that’s also part of the contract that’s
deployed right so if you just pull if
you write a smart contract you can ploy
the byte code onto the blockchain but
you don’t have the interface right you
you can’t use interact with your smart
contract or you know catch up you have
to know what goes into your functions
what comes out of your functions and
gotcha okay so what’s what’s the simple
like hello world content can we put a
value just a static constant value and
then a function to just query for that
value yeah okay so so I mean what do we
could make to make a value called
balance which is a number and then let’s
just make a function that’s like get
balance okay so you it balance you’re
declaring a variable right entice you
int right what is a you int versus an
interval well why is it you int so an
integer is is just a number that can be
positive or negative it’s an integer
value of an unsigned integer you int is
an integer that can only be positive and
so I forget you know the maximum value
that a normal integer can take on but it
has a maximum value and a corresponding
max the minimum value that’s a negative
and here value essentially would an
unsigned in is it’s just shifting those
that range of values of the integer can
take so it can be higher numbers if it’s
a UN versus in it because we’re I know
that it’s not negative so the same okay
but in fact there’s actually no such
thing it isn’t in validity it has to be
positive or negative
uh well as far as the datatype goes you
can have negative values like this
afternoon
all right so we have au int balance so
it’s just a clearing the variable and
then I’ve seen these things that are
like constructors I guess that look kind
of like that right so I guess you do
like this so so this is like a thing
from like Java right where it’s like
you write the same name as the contract
and no parentheses and that’s a
constructor function yeah then just so
you know you’re on that keyboard I’m
pretty sure the Alt key is mapped to
them command
I like the Mac so okay yeah yeah so the
constructor is just a function that is
run one time and it runs automatically
once you deploy your contract to the
network okay gotcha yeah so it runs on
deploy and then never again correct
you can’t call on your hello roll
function after deploying it and this is
where we would do some sort of
initiation so can I just do balance
equals 1000 yeah and then that’s setting
the value does okay so I guess we’ll get
into the concept of gas at some point
but do these constructors cost gas
Terron yeah so every so every operation
costs gas to run because essentially we
have thousands of computers all over
nodes all over the world that are you
know running the same code and you know
doing anything on a thousand nodes is
pretty costly
so generally doing computing like say
for example if you want to do an
addition or multiplication that’s not as
expensive as say storing a piece of data
on the blockchain right let storing data
such as setting you know setting the
value of your state variable that’s much
more so how can you understand is this
online compiler going to give us
something that like shows us how
computationally expensive this would be
and how much vaguely you would need to
pay to execute these kind of things yeah
but sure enough he does the press create
so if I created this contract but
there’s no way to get the value right
not yet but I think press I just created
cause oh wow cause so that’s a cost to
transact it to the blockchain right and
this is a cost for the constructor to
run like what execution cost is so so it
generally costs around 20,000 gasps to
set a like to sell
Oh gotcha so if you have yeah so for if
you know like multiple stager if we
didn’t do that assignment yeah now our
execution cost is like nothing or mm but
if I do set that and then hit create
now’s 20,000 it cost 20,000 is to do
assignment right so generally like I
said generally scoring data on the
blockchain is more costly than running
sort of computations because yeah
thousands of computers all over the
world have to store this piece of data
so that make sense yeah so it’s like a
huge minimization problem of trying to
minimize your computational cost right
do you want your console be efficient so
how can we write something in this to
get the balance where we could call out
to this contract and view the current
value of the state and then I think
that’s the simple hello world that I
would so it’s actually really nice how
solidity set up you can define different
um gossiped forget what the term is but
you can have like public or private
right well state variables and functions
so in that will so a public variable
would mean that I could just call out to
that contract and get the value without
having to spend by right so so they’re
called yeah I mean an accessor and
accessor is automatically created for
every public variable okay and generally
functions are hard set is public by
default okay okay so this is then hello
world I should be able to deploy this to
a blockchain and see the balance as a
thousand really like a simple Hallowell
so that you can deploy it and if you
have the interface to this contract then
you can call you know I don’t know that
there’s a balance so what’s good what’s
the simplest way to set up like a test
network to deploy this against so what
do you recommend does more PC those I
have that downloaded yeah so what
exactly is test RPC
I know that you can run it by literally
doing this right test arc you say yeah
so test our PC value is essentially the
munching simulated block stream that
runs in memory on your computer so
there’s there’s no mining uh so you know
in a live network even like the test
network the more than test network which
is law
in every transaction it has to be
included in a blog and that block has to
then be mine and so let’s say if you’re
getting a series of different
transactions so you want to deploy a
contract and then once it support you
want to interact with that contract it
can take a long time to go through that
series of steps run 18 seconds for a
simple – so deploying in RAM is just so
much faster than on a line that way
correct so they so just to get this
straight a theorem has a live test
network right more than good Morgan
customer yeah don’t worry about that um
so there’s a live test network that says
ok there’s real people around the world
all sharing that test network with live
this is just the local host essentially
where it’s only locally on your computer
and a disaster ok so when I do tests are
if you say what wallet does it think I’m
live like logged into because like it
gives you these private keys and public
keys but if I like send a transaction to
it which account does it think I’m
sending it from so I think it by default
it sets the it sets your default account
to account 0 oh yeah and these are all
just randomly generated accounts just
for testing purposes okay so this is a
private key to sip uh Blakey and if I
deployed a contract against this is just
going to assume this is the thing that
I’m doing it from but I could set it to
be a different one of these if I wanted
to test like a transaction sending data
to each other yeah okay cool all right
so how do I got this thing it compiled
it in the browser and how do i how do i
deploy it well so you could there’s a
few different options first the first
option is that you could deploy it
directly from the solidity real-time
compiler to my tests are to say here in
the browser or it has its own test RP
say that you could mmm well it has yeah
I’m not sure if it’s test RPC I would
assume that it is actually though yeah
where you can you can basically run you
know transactions against your contract
so like if you click create right now
okay so now yeah I mean essentially this
solidity real-time compiler you know has
this this contraire
that’s deployed in memory and you know
it has this balance variable and you can
you can interact with this so if we were
to create a function in this contract
you would be able to you know run that
function over yeah I say that it’s shows
what the state variable is here
so this hello world at X this address
that it created is the public key or
it’s the address rather of the contract
true so I would need this if I wanted to
call this from another like when you
deploy a contract you need that address
basically yeah that’s kind of the
workflow that I’m having that I want to
just under strap my head around better
is like when I deploy a contract how do
I then like later call on like what’s
the actual steps for that I guess okay
so I see this here if I wanted to play
it locally could I thought what would I
copy and what would I need this thing or
the bytecode if you want you would need
to have the bytecode and you need to
have the interface do you know how you
go about doing that
like you said you like I know you
mentioned truffle as like a tool would
that be what you would use to you would
want to use truffle also so with truffle
you don’t have to worry about compiling
your contracts and then getting your
bytecode and your interface and storing
those and you know energy like it
basically takes care of all that
annoying stuff for you so you can just
have your solidity contracts and you
know that you can work my truffle
compile and it will compile all your
contracts if there’s dependencies
between them it will automatically
remove those contracts batcher so when
you’re doing like more advanced contract
development you’re going to want to use
a tool like chalk box it D like your
dependents absolutely so I only
recommend the online compiler like the
in-browser compiler just it’s a great
way to quickly get started you can like
just pull out stuff in it you can see in
real time if you’re writing you know
something that like throws an exception
in solidity Shera but if you want to
create any sort of like large-scale
project that has multiple contracts
you’re going to use truffle I’m gonna
use crop right so how would I do so say
we want to do that same hello world cut
contract and truffle what I
that you do make a truffle project like
by doing truffle trouble in it and then
some name of it right so we’ll call this
just oh you don’t have I mean you just
jump on it so you go you create a new
folder while you do shuffling it inside
the folder correct yeah I do that on my
desktop find out the smartest all right
let me roll that one back real quick all
right so you’re gonna want to create a
new folder holders named after whatever
you want to pump it in you okay so I
will see I will make a folder called
etherium and then CD into it and then
I’ll do truffle in it so now this is a
truffle folder basic way right now you
have smeared their generators should
make the folder for you just in case any
consensus people are watching this but
now I could open this in a text editor
and look at what it generated so truffle
and MIT what do we got here all right so
we’ve got app contract mediation chest
chocolate us this is like the config
thing right yeah this this basically you
can configure it to run on you know you
can have different test block chains you
can and I believe this so yeah this is
where you have like your your app files
so I get like an application like a
front-end JavaScript Oh gotcha
yeah and then this RPC is telling it
when we run truffle use the localhost
test test RPC as your back-end so to
speak correct so that you know that’s
you’re bluffing okay got it
so that’s configured there then it has
some default contracts it looks like but
this is where we would put the actual
contract for our hello world right yep
yeah all right so let’s try it let’s
just copy what we did here and then do
contracts hello world Sol yeah I have a
it has like the color highlighting for
solidity which is cool uh and okay so I
made this so how do I deploy this to the
test net well like a real DAP developer
okay so I guess so
first year I want to go back your
command line and you’re going to want it
for neuron truffle compile and that’s
the first step and what that will do is
it will compile all your contracts all
the contracts in your contracts folder
it will compile them and it will create
basically yeah they’re called artifacts
but essentially they’re like JSON files
I believe it might contain the byte code
and they contain you know the interface
that that lives in this building
so it makes oh wow yeah so it makes this
basically this complicated scary thing
this looks like what yes this is the
binary I’m assuming is this thing here
right all right cool so it makes big
scary file that I don’t need to look at
yeah but essentially it so you don’t
need to look at it but so what that file
is going to do is it’s going to allow
you to very easily just interact with
your hello world smart contract without
having to do anything
hairy of atom so in the future it’s
going to make your life way so we
compiled it and then if we want to
deploy it is it just truffle migrate
truffle migrate so migration is sort
yeah so what are so what exactly are
migration I mean I know what migrations
are in a database sense but in a kind of
contract blockchain sense what what are
they referring to when I say migrations
so migrations are so I guess I would say
they’re sort of like scripts that go
through a series of steps that you want
to go through so let’s say so it’s kind
it’s not the best example if you have
one one smart contract because it’s very
easy to just write write I want to
deploy my contract and then I want to
query heat balance variable but it’s
let’s say you have a larger scale
project and you have a bunch of
different contracts and say they have
different dependencies or you want to
set them up in a way where they’re they
have sort of like dummy data so that you
can kind of get a feeling for what your
application might look like once people
start using a data then migrations allow
you to kind of just automate the series
of steps that are needed to deploy your
contracts set them up in the way you
need them to be
so they’re like build scripts that way
like chef or something yeah those
scripts for your like DevOps or
something okay gotcha
that makes sense and then do you need to
configure anything or by default if I do
truffle migrate will it know about that
HelloWorld contract that I just added
what I was like added to some array it
already knows about it but what you do
need yeah what is this thing deploy that
have so so okay the likes migrations are
relatively new they crumpled got updated
recently but so essentially you have
just a few different JavaScript files
and each one’s a migration and you kind
of want to set them up in form like a
sequential order to carry out different
steps gotcha
so if you want is like a dependency of
the other you want to migrate that one
first basically correct yeah and so in
it so we don’t need to get into it too
much but essentially the truffle also
creates a migration smart contract and
what that will do is that will keep
track of which migrations you’ve run up
until now so like one that you have
already deployed all these contracts and
stuff to the real like to a live network
and you want to run further migrations
it will know that it already ran so
it’ll actually have states stored on the
etherion blockchain then knows about
your dev environment essentially like
what you get as you are which is kind of
cool it’s a new it’s a new concept it’s
crazy all right
so if I just hit truffle migrate this
hello world thing will get transacted so
what you need to do is you need to go
into the second migration miss Lloyd
contract and add this and what you have
what you need to write is the toy dog
reported article I can remove it so if I
comment what is the auto link and I get
what this means I want this but what is
this thing
so in the example the example project
that I don’t you know it’s job you did
in a default truffle project there’s two
there’s basically two contracts one of
them is called a library contact
contract and the other one is just a
conventional mortgage mark on this guy
right
so what’s what is this what a yeah so a
library contract it’s basically
like let’s say that you are going to
want to do discus a certain operation or
you know execute a certain function very
frequently and you know there might be a
hundred different contracts that want to
execute this function instead of having
to include a hundred of these functions
in each individual contract which is
costly remember right because everything
you put on the blockchain is costly
instead you can deploy one library
contract and all of your smart contracts
and you that function can just refer and
conceivably somebody else’s smart
contract also could use that library
contract right it would need to know the
address of the contract and it would
need to have like an ATAR so the idea is
that if there is a common computational
tasks that you do frequently it’s
actually better to abstract that into a
library and then reference it from your
smart contract yeah yeah that’s crazy
it’s exactly I think I’m gonna say that
about a hundred times learning about a
theory album but that’s crazy yeah
that’s what I think it’s really cool and
it’s kind of neat is like you know it’s
still early days but like eventually you
know I think that what you’ll be able to
do is you’ll be able to go online and
you’ll be able to look up a bunch of
these public libraries that other people
have put onto the blockchain and you’ll
be able to use those in your smart
contracts yeah that’s awesome so it’s
great yeah okay so we added that line
finally good to do truffle migrate it’s
nerve-racking all right so something
happened deploying hello world so the
first thing that migrations that’s the
thing that you are saying that’s the
state where it now deployed this so it
knows about which contracts we’ve
deployed in which ones we have it it
deployed the migration and then it
deploys our hello world and it gives us
a hex and so this is the address of the
contract right all right but so I’m
going to copy this but apparently we
don’t need this right it’s the way that
enough oh okay so if I do truffle consul
that’s what you would do to start
inspecting this oh that’s one way to do
it yeah Oh what would would be the other
way
I don’t think probably fall to dude all
right so this is just some JavaScript
console right we’re in JavaScript
here yeah maybe I’m not I haven’t used
the castle too many times yet but yeah I
mean so I think so now you basically
have this nice hello world object oh
hello robot deployed and I am esta yeah
so this will hello wha da deployed will
always refer to like assuming you have a
single hello world smart contract that’s
deployed onto your block chain of test
watching live blockchain doesn’t matter
you’ll have this hello world dot
deployed that it’s an object and it
refers to everything you could possibly
need to interact with or refer to that
smart contract gotcha
which makes it really easy to do things
like you know create like front-end
applications that interact with your
smart contracts and stuff like that so
right okay so this is just part of the
top of that environment knows about this
contract makes this hello world dot
deployed so we could set this to a
variable called hello well hello world
right yeah so now we have this hello
world object so how would we get the
balance that was the that was the so you
would good so I’m assuming yeah we’re
cool again we have that it depends so
like yeah yeah so you get a prom so that
we could know just Java so uh yeah
I’m pretty confident this point that
this is JavaScript could that put that
just or but so you’ll see so what a
return is a promise it’s basically
waiting short call back and and so this
this is a very important point that you
know every transaction that you you know
if you send a transaction out to the
blockchain
you can’t just instantly get you know a
response you you have to wait for it to
be included in a block and for that
block of you mind
so the the promised object returns after
the block is mind when you when you make
a transaction so like basically if I
submit something of the theory on
blockchain it’s going to return a
promise and the promise will return
after the block is mind if you’re
getting a contract right so if you go so
I think it actually it’s a pretty
straightforward example of how you’re
going to interact with these promises
okay
if you go to your truffle project okay
and then you go to tests our test like a
medic point so this is the medical one
Oh God hahaha so what you just the font
is big maybe it’s not actually that bad
mouse can you go back to the UH I could
get you a mass not funny well what is it
which one you want
there you go yeah so what they’re doing
here it’s sort of what we would want to
do with our with our hello world
contract is it’s actually exactly what
we we would want to do with our hello
world contract is there they’re just you
know querying the state of the balance
and the what what you see for this like
called a call and then in parentheses
account yeah part so there’s two
different types of space of basically
ways of interacting with the blockchain
there’s you can execute a transaction
and so a transaction is something that
changes the state of a smart contract or
changes changes a state on the
blockchain gotcha okay and that requires
you know the network basically mind that
in a block and update it essentially
changes the state of the block there and
then there’s also something called a
call and you’re not changing anything
the call you’re just querying data
gotcha
okay sure so so I guess that you know
that something I don’t even realize so a
call is a query it doesn’t change state
returns a constant value and then so can
a call okay so what if you’re not
changing state but you still need to do
computation so what if you have a
function that returns like X times y
yeah and then you call that so it needs
to like that’s free you don’t have to
because they can statically figure out
the value that would be right so like
assuming so like let’s assume that
you’re running a node so you have the
entire blockchain and yeah and you’re
doing a call that needs to run so series
of the steps of computations your
computer’s just going
to run those computational steps it not
yeah the entire network doesn’t need to
know about yeah that makes perfect sense
okay so a call is a call and a
transaction modify state right so we’re
not modifying state here we’re just
query in for the static value which is
why we can do this call correct and then
log it okay so what’s this object we got
back because with this like this is
weird
format right for like a number I haven’t
gone gone that deep down there every
cool but I’m assuming I could just do
this no it’s just so weird okay but
whatever so I’m assuming just whatever’s
in that see thing is the value and it
looks like a string comes back so all
right but it looks like it knows that we
set it to the thousand so so there was
some way to call out to the chain get
our value back alright make sense yeah
so then the the level to I guess is how
could then can we put a function that
can change that number and then query
for it
that would be that would be level two of
this contract so it balances a thousand
you have some number like deposit that
just increases the balance and then we
call out to it and then this would be
the other example what you’re saying a
stateful transaction as opposed to just
a static query yeah oh we can put
returns you lint and we can put it as a
new value okay what what is the I see
this sometimes where it says like
constant returns or returns constant is
that something you need to specify or is
that like optional so that is actually
what will differentiate between a
transaction and a call so a constant
function does basically what you said it
yeah it will query something it will
query value from your contract and it
can run computations on it etc but it
won’t change state so yeah if you have a
function that doesn’t change the state
of the blockchain it’s a composition so
you’re not returning constant because
this will vary every time your only
return constant when it’s going to be
okay make sense so we’re saying that
this takes one argument of type UN which
is value and it returns when argument of
you Antony Val you’ve got it so this
will just be balance plus equals value
yeah right yeah weight home so I would
do balance plus equals value and then I
need to return balance you know is there
implicit returns and these functions
like will it return the last value the
way javascript or something would or you
always have to specify you know your
functions don’t have to return anything
but since we specify this is returning
that and we have to return yet
correct okay yeah so returns return
return guidance so that’s safe that’s
going to cost money because we did a
computation right so that should cost
around 20,000 gasps to 20,000 get nah I
have no idea how much this so update the
balance did you go through oh man
there’s a little bit of extra gas just
for no housekeeping so okay so balance
equals Ali return balance and that we
should be able to just query the value
balance the same way we did you don’t
need function for it great okay cool so
all right so we already compiled this
contract and deployed it can I just do
that again
little no so if I do um truffle compile
and truffle migrate it’ll know because
it will compile it it’ll be like oh this
is different binary I should redeploy
that right let’s try to compile Oh
grants in the stream died and it’ll come
back it’ll come back grant ah all right
so it didn’t look like it migrated it
and I mean I think if you just did it as
you know truffle papaya what was it
well like I should have given us or
maybe it just doesn’t huh I wonder or I
wonder if you have to I wonder if you
have to reset it I think I remember
having to do that once when you do – –
to reset and it’ll really reconfigure
but okay so if I do truffle console and
then I do the same thing for hello world
equals hello world dot deployed not that
dot balanced call that’s been console
dot log 1000 all right so now I want to
update our I want to deposit something
so how do i how do I call that so you
you take similar approach where you you
know instead of seems good so you pause
it so you just call a function the same
way you would think to so let’s put 500
whatever is into our balance right these
are just arbitrary numbers right and
what so typically you know our function
our deposit function only takes one
input and that’s at Union but what you
can do with with purple when you’re when
you’re interacting with your contracts
is you can specify which account is
sending the transaction oh okay so if I
go to my test RP say scroll back up so
what would you would you use the public
cake like if we wanted to have number
one this guy know so okay so I believe
by default so I should do this now okay
I webster e dot F dot I counsel Colin I
am probably looking for that right yeah
yeah yeah yeah I keyboard kind of stuff
I have my mechanical keyboard which is
awesome but it’s so loud like it’s
impossible to record it yeah so you can
also do a good news like one three dotty
default account or something like that
that’s just that I will perfect now uh
what is that coinbase I don’t know get
accounts yeah whatever
I don’t know a lot of fun with this oh
yo you’re right it was default account
you had it
default account oh I guess it doesn’t
have a default account probably Fassett
it yeah maybe so but yeah so essentially
what you can do is if you’re interacting
with your contract it’s been deployed
with couple you can basically set which
contract is sending that transaction so
that could be important so for example
if you have let’s just say you have two
accounts and one of them has some ether
in the account and the other one does
not
well then if you’re doing sort of
transaction that requires ether they
would need to send that transaction from
gotcha okay so we need either to do this
transactions what you’re saying but I
want to do any sort of transaction
luckily for us where you can test our PC
which it just gives you a bunch of
accounts so can you check the balance of
the account yeah I believe so um let’s
see what three
Oh gotcha so we gave it just put a lot
of this ether into yeah it’s all fake
yeah gotcha
here’s your blood okay so this is our
probably our default count this one so
if we wanted to call into it we would do
hello world dot deposit deposit and then
let’s say you okay so 554 so just
calling this will actually call it oh
yeah but never return a promise so then
you have to you have to intend to
promise right and you can so this would
in theory Oh
so yes my programs action ID yeah and if
I check this balance this should in
theory be down right yeah
yeah look it’s less so it actually cost
you that
to do that yeah call it call so we call
deposit and now if we call which was it
hello world that bounced that call
fifteen hundred there you got stateful
transaction well done yeah so I mean you
can see there’s kind of there’s more
going on here than you know we’re trying
to do a very simple favor try and
increase available you’d like to have
one number that then changes to a
different number yeah but there’s more
going on here underneath the surface and
it’s you know those are these are the
things that make like easy the truffle
is gonna go through for us
gotcha yeah all right cool okay so
that’s how we we made this awesome
transaction that could do some sort of
staple thing so now now it starts
getting out of like hello world and into
more like real stuff if we said for
example I want to have multiple accounts
in this contract that each have their
own balance and then I want to transact
to a specific account so this is
basically building what you call a medic
Owen right which is like a virtualized
currency on top of etherium and this is
what I got to this point where I was
like okay now I don’t really understand
it anymore
so I don’t know like I I think it’s
straightforward I guess from from yeah
so like if you could talk through how
you might go setting something like that
up where you need to have kind of a
little bit more complicated data
structures right yeah absolutely
so actually I think this is this is
probably one of like the most important
smart contracts but you know is
available right now it so basically the
etherium foundation and the developers
and the community came together to form
something called the like a standard
token contract
it’s the VRC 20 standard cocaine el grec
and essentially yeah I mean essentially
what it is it’s just like a very generic
token that you know stores user balances
so you can have a bunch of different
accounts and each account can have a
unique balance and then the the smart
contract includes a few different
functions that allow users to transfer
their tokens to other people and these
tokens can represent anything they can
represent shares in a stock like
ownership over equity in a company they
can represent game token users you know
some sort of scorekeeping they can
essentially represent any sort of value
transfer system and this is what the Dow
was they had a dow standard token that
represented a share in a organization
correct and then this is what people are
doing with crowdfunding now where it’s I
want to have tokens represent equity
interests in my business and I want to
be able to distribute them right so that
so the step I guess you call us a
standard token contract standard token
contract it’s like it’s it’s one of the
most fundamental building blocks of like
I I really think it’s going to be you
know included in most large-scale
applications that are built on etherion
so it’s really important to understand
how the standard token contract works
yeah okay cool so in order to in order
to store the state of of the standard
token contracts you need something
resembling a hash map which is like the
most integral data structure for doing
any kind of program correct so
what’swhat’s the solidity way to do that
so in solidity this is called a mapping
and yeah mapping essentially like a hash
table and you can map from any sort of
sort of any data type so it could be
like an address which is probably what
so in address represent is just that
string hexadecimal but that’s just a
native data structure and solidity yeah
yeah okay so it’s called an addressee I
gotcha so yeah we can make a mapping the
maps from you know addresses so
different wallets are different uh
accounts rather to do a UN so to a
balanced value balanced zoos okay so now
we have I guess we could get rid of this
just to clean it up for a hot minute all
right so we ever helped I guess they
were not even callewaert lame or
whatever we’ll leave like this so we
have mapping of address you went to
balances right now in theory we’re going
to need to create a contract from an
account that is like the the admin
essentially like your because you’d have
the ability to figure out how many of
these virtual coins exist and I assume
they all go to the admin to start and
then he distributes them yeah that’s
correct so I mean we could we can code
it up here or we can look at that scho
testing template i think it’s easier
then yeah and taking a template so okay
so so you’re saying you basically when
when your contract is first deployed you
want the person that deployed the
contract to be essentially the admin
yeah exactly exactly so this is a really
this is a really common pattern in
smartphone and smart contracts
so essentially you you want to have an
own contract and so there’s a few
different ways we can do this I think
probably the best way to start is to
just do it this way so what we’re going
to do we’re going to create an owner
state variable okay
a public address this before hearing
issues yeah owner so this is just a
public address and and what we’re going
to do when our contract is first
deployed is we’re going to set owner
equal to message sender okay so what is
that message object so there are a few
there are a few things that are just
built into solidity that are kind of
nice they make things very convenient
for you one of those things is message
sender so what message sender is it it
just takes on the value of the address
that is that’s just you know sending a
transaction so when when this is
compiled solidity will replace that
things as master dot sender with the
actual etherium address of whoever sent
a contract whoever so yeah yeah upon or
sent a transaction on deployment yeah or
the transaction just message that’s
hundred means whoever’s calling that
contract correct gotcha
so we’re just gonna on the anit take
whoever deployed the contract and store
that as owner that makes sense right and
then I guess we would give that person
in the hash table a starting balance
yeah so what we can yam is we can say
balance is owner or we can say message
sender but we’ll just do over here now
is equal to 1000 dyskinetic points yeah
okay so he’s got the medic wines this is
that and then how can we check the
balances because it can you can you
access that mapping the same way we
access that you went beforehand you
cannot okay so you can’t just call out
any hash map value
well yeah you so what you can I mean by
default like from the console or do you
need a function that does it oh no you
need a function gotcha so I mean like
it’s not that same thing of like that
public variable that right yeah it
doesn’t work that way you have to have
an epoch in excess or that essentially
will you know query the balance of a
specific input address
okay right so yeah so so how do so we
make a custom function for this yeah
great a function called get balance and
it will put dress for this for constant
returns and when you put the word
constant this means that you’re able to
period if you didn’t put constant you
wouldn’t be able to call this without
using gas um yeah so essentially the
constant just just tells you no it just
said it just says that this is a
function that does not change the state
of the watching okay gotcha
so and then we’ll put balance and then
we’ll put return balance is user and
that’s it okay all right that makes
sense
so it’s essentially doing the public
accessor only because it’s a mapping we
need to write this function that does it
for us right so yeah so a hash table
essentially you know so specifically for
a mapping in solidity every single
possible like address so we’re doing a
mapping from an address to you int so
every every possible address is
initialized to 0 0 0 0 and then every
int is also initialized to 0 so instead
of so where you might be used to having
a hash table that only has entries that
have been specifically you know yeah
clicked into the hash table and mapping
is different in that it automatically
has initialized 0 values for everything
and then you just modify those 0 values
first so what that’s essentially saying
is like there’s no way to really loop
through this there’s that there’s no
concept kind of hey let me just loop
through the keys and values and see
what’s in there
you cannot move through this yeah what
so there’s some nice block explores so
let’s say you’re just interested in
looking at yes what addresses exists
yeah there’s a bunch of accounts at a
contract that you’re interested in this
contracts already below
lay on the live Network and stuff you
can go to like a a blockchain Explorer
and like ether camp user camp and what
that does is it it will actually list
out all of the you know key value pairs
for mapping for you oh so if I if this
transaction existed I searched for an
Academy I could then look at what
accounts exist just in this block shanks
for right because it just it contains
the whole history of watch and sort of
just accumulates all cool things that
make sense
all right and then and then how would I
send a value from one address to another
so so then III assume we could deploy
this and it would work I would rather
take it one step mat in the future which
is I want to code the function that lets
someone send money to each other yeah so
we would create a function called
transfers and we’ll put address you know
– right and we’ll put you in value this
Emily Newton dependence we would have
message sender as a we have message
sender as a variable right so we ought
to know you know by default who’s
sending this transaction okay so so
would we do something like balances
message sender – equals value balances –
plus equals value that’s the idea and
then use some sort of check here just
make sure that to where the message that
sender has that value yeah so how do you
do conditional logic and that’s like can
you just do if statements the way you
would yeah it’s it sort of would be very
familiar so you could just do you know
balances message
or so if it’s less than if it’s less
than Oh
fell you yeah huh I sang I missed a
thunder bar there
Thanks return skulls and you know this
this isn’t true and then we need to go
through the transaction you return
gotcha so once it returns your return so
you just have a little check there
that’s saying if they don’t have the
value return false and if they do is I’m
sure okay cool yeah so that’s the that’s
the transfer function alright and that
we can get anyone’s balance oh yeah so
we should be able to play this and test
it let’s let’s see how we did
exit shuffle compile and truffle my
grade – – reset
I think it could be worth going through
the standard token yeah after this
however we’re kind of going through
already I’m down to it I’m assuming is
that what this meta coin thing is is
this I’m assuming this is the standard
token or it’s actually not it’s it’s
different it’s similar it’s just an
example the standard token it’s like
really well thought out a ton of people
okay yeah I’ll take a take a look
interesting I always think it’s to point
there’s our PC story yeah right it
worked just tuck it just took its sweet
time I’m assuming we’re going to need oh
no we had that accounts variable in this
anyways okay so a truffled console
starting to get the get the flow bar
hello world equals hello world dot
deployed got my contract there so we
have that web 3 dot dot addresses that
address what was it the coast town side
right so this is account one this is
zero studies but it also will do account
one equals and then we’ll do a count to
week both accounts one okay so we have
hello world so the way I would think so
we can do hello world dot transfer well
this is great actually
this is really good because what you’re
trying to do yeah exactly what truffle
makes really convenient really easy
because it can sign the sense of what
you’re trying to do is you’re trying to
write a test you’re trying to say let’s
see if I send you know if I send coin
gap control one account to be another
you know sure it’s manual test learning
opposed to automated testing right yeah
but you’re saying you can write
automated test that can that can do this
in fact here’s the default test it comes
with the medic own project but people
will test the transfer function right so
they have a similar function assigned
coin and okay so just to see so we have
hello world and in theory I should be
able to do get balance of the default
address which is a count one yeah like
this right so this is the owner this is
the there should be a fazzy account that
deployed oh yeah so that’s a facet
contract right and then if I did a count
two it would be zero because you’re
saying this already exists in the
hashmap it’s just initialize to zero
which it is okay cool so things are
logical so far so hello world dot
transfer to account – let’s do 25 and
it’s important to note that right now
this you’re sending this transaction ads
account 1 yeah but this is by default
right but if you were to try and send
this transaction from a different
account that did not have a balance they
would error is it Phil oh yeah because
we can take a look at that – like we can
make an account 3 also but this should
work and now if I get balance of account
225 account one is 975 so this seems to
work and then if I didn’t account 3 and
then tried to transfer from well ok so
let’s just try to transfer something
that we don’t have which kids would say
like 10000 right so this would error
then actually I don’t think it would
because we didn’t check if you’re
sending wait yeah we checked that the
balance is less than valuated that would
go so then if I try to get the balance
of account 1 it’s still 975 and account
3 to zero so just never liked it we do
have some checking that it just cost gas
to even try basically so I just
reinstate our time right ok cool so yeah
so what were you saying about the
standard Toby it’s kinda just Google for
this yeah yeah you can gr see 2300 token
that’s how er
c20 standard token to discuss so
essentially this is just a you know it’s
a smart contract it’s very simple one
that a bunch of people you know thought
about how to best structure a token
contract in a way that you know it
allows tokens to be easily integrated
into applications you know all tokens
behave the same way you don’t have all
these skipper Cokin and this is all open
source this is yeah this is a community
front oh that’s great
stock Wow yeah that I’m gonna need to
look into I’m very fascinated by that
right so a standard token essentially it
does what we just did and it goes more
and I’d probably better more robust
error checking and kind of things like
that yeah
okay so so I guess my next question and
that’s probably the extent of it is just
then how you would how you kind of deal
with iterating through data structures
and grouping and things like that
because I’d imagine if you did want to
store all the known values of you are so
you want to make a web UI for this kind
of thing it would be really helpful
potentially to have just a really static
endpoint you could hit that just showed
you all the different accounts and their
balances so I’m assuming you can you
just like essentially cache that
whenever you create an account and then
you have some sort of array data
structure but that you can’t really do
from what I understand like these kind
of you can’t like map arrays and loop
through arrays like it’s kind of like
hard to do that right yeah I mean well
you can but so so one of the issues with
like loops for loops while loops and
some you know every computation cost gas
and so each cycle of a loop would cost
additional cats correct so if you you
know really poorly design smart
contracts you know might do something
like pay out a series of users in a for
loop and you know it should be like a
gambling application or whatever it is
and you know the problem with that is
that if your application gets popular
you know a lot of people are using it
eventually you’re going to run out of
you know you’re going to run out of gas
when you try and pay out all of your
users so you would need to try to pay
them all out at the same time kind of
thing uh well actually so so I think
generally the approach that you want to
take is instead of having be you know
instead of having like the owner of the
contract or the admin pay out all of the
accounts you know iteratively what you
want to do is you want to set up your
contracts in a way where each account
must go in on their own and redeem their
you know you know manually get their
payout individually kind of thing so
when they get their payout and redeem it
they get docked for the computational
price of that transaction they have to
pay the gas but fortunately you know gas
is it’s not that much you know a gas is
not all that expensive to be something
simple like just redeem their routine
here which is just essentially just
transferring you care or something yeah
whatever it may be okay very cool so um
what so just from your experience
developing smart contracts I guess just
like some high-level thoughts like what
kind of data structures you find
yourself using frequently and what kind
of maybe design patterns that we didn’t
get to touch on here do you find
yourself using a lot like what are the
most useful things well so I think one
thing that’s really useful it is the
doubly linked list so like you asked
let’s say you have a mapping and you
have a bunch of different accounts that
are stored in this mapping there’s no
easy way to loop through all of these
entries and so how can you how can you
do that well like a doubly linked list
is one of the ways you can do it you
just create this kind of high level data
structure that operates on mapping and
you know what would that look like I
don’t even know it W on Wednesdays okay
what makes it doubly what makes it
different than a linked list
it’s a limitless yeah yeah I mean doubly
linked list yeah it’s a linked list
where you can even move from front to
back or back from scotch a through the
elements of the list and so Aris
industries has a piece of example of how
to implement a doubly linked list
what are these guys eris industries
they’re they’re creating basically
private and Syrian block chains forever
look at the CSS that’s pretty cool yeah
all right sorry I get distracted he’s
like yeah it’s me
yeah they’re essentially creating tools
that allow you know institutions to
create private block teams usually get
three over to listen again blockchain
you guys should just be UI developers
nicely so what’s awesome so like this
you know Aris is not creating tools for
the public etherion blockchain they’re
creating tools for private etherion
block chains but what’s great is that
they have solidity tutorials that work
you know they work no matter what a
theory you know what what a syrian
virtual machine and documentation
tutorials right I know the marmot stuffs
kind of weird so these so these solidity
tutorials are just these are just a good
overview of gotcha
modular solidity advanced 11 you can
phase right so wow it’s really so like
creating a single contract that does
something you wanted to do is awesome
and but eventually we want to start
deploying like a platform or a large
system where you have multiple contracts
and interacting with each other
you have contracts before and other
contracts you you’re going to have to
start thinking about how you what is
your design pattern going to look like
how are you going to you know once you
deploy a smart contract on the
blockchain it’s there forever the
bytecode is there you know you can’t
change the smart contract once it’s been
deployed so how how are you going to
update your system
like if you if you find an error in one
of your smart contracts how are you
going to update that without affecting
the rest of your system
so I find the errors industries
tutorials stickers they provide some
really great examples if they provide a
gadget with W its list which is four so
the idea is that a contract could in
theory call another contract so you can
kind of abstract
things to basically say here’s a state
variable of the contractor gonna call
and then if I need to make a hot swap I
can then just update that state variable
and then call different contract with
updated code those kind of design
patterns are all coming here right
gotcha
that’s a stuff I want to start getting
into is like as you get like more
in-depth into this yeah how many time
things work that’s sort of its necessary
that’s necessary if you want to build
anything like any sort of scale but you
really have to have you know a bunch of
contracts yeah separated automatically
freeze okay so one last thought yeah I
want to just curious how you would go
about sending like doing this for real
like either deploying a contract or
something either for real like what what
is the different step that goes from
what we just did in that test RPC to
like the real word because this is
localhost but like what is the
deployment like just I guess what steps
are in between us did what we did and
what actually is on the global
blockchain you know like it sounds kind
of crazy but it’s not all that different
so if you want to deploy smart contracts
on to the board and test network or on
to the wide block also wide network on
all that different if you’re using
trouble so truffle you can configure it
so that it will you know do all you can
set set up your contract here yeah you
would just change out that for the test
net so test net is like you run a client
with the test that on the back end I
would just tell trough all that that’s
what I’m that’s what I’m doing
you would run a guess node probably
again mode on the testament words so I
have a question about it so if I kill
test RTC there I have death installed
and I forgot how you do it how do you
run this on the test now act yeah I
think it’s like more dinners over can
you bring up the options yeah uh I
believe this would be just death and
then probably – – that’s not more than
Network this guy
Olympic is this time anything to do with
the Olympics or is that okay yeah so I
do this and then it’s just um like it
just sinks blocks forever and I have no
idea what’s going on
yeah I mean the entire things were
somewhat place though like I don’t know
it says I got 1 million one hundred one
thousand okay so this is an actual note
so that means my local Mac computer is
actually a node on the test net yeah
yeah and in the first step that you’re
really node has to do would have to
download being like iGoogle like a
theorem nodes which like there’s I saw
this in like I don’t even remember the
source of this but there’s I saw this
website ether nodes and apparently this
lists like every node on the network and
there’s only seven thousand seven
hundred thirty-eight okay like if I just
run get on my local Mac that that makes
me a node and now there’s seven thousand
seven hundred thirty nine you know
really yeah really
so like I would be like one eight
thousand of the entire theory of network
just by running that command that seems
like not right look at it yeah yeah
eight thousand those roughly those so is
it are we just like crazy and like I
fear them isn’t actually that big and
just live in a bubble where everyone
talks about it oh no I don’t think so
cuz I mean a lot of people use for
example like the etherium wallet from
this and they oh we’re not actually note
it’s just well it runs a full note but
you know they’re just logging on the
mist to send a quick transaction and
make shut it down oh so it’s not
full-time notes they’re just like just
in time it’s kind of right so you know
but I would say that seven thousand
seven trying what are they doing over
there seven seventy seven hundred nodes
is actually not a small number I mean I
don’t know the number of Bitcoin managed
off the top of my head but that network
is you know the value of that network is
ten hypothetically ten times now you
would be theory network they certainly
don’t have ten times
so now I’m on the Tesla right like
alleged life well you’re downloading the
test net blockchain and well yeah you
are once once you’re noticing then it
will be relaying transactions and stuff
too so okay and then um I would
basically go through the entire same
thing we did only just changing that
configuration chop all right so then I
got audience idea Trump was trying to
make it mean for my understanding
crumpled trying to make it really easy
for you to set up your your your
contracts all the script you need to
deploy them and then you know deploy
them on a test on PC or the test no yeah
so then I just do the exact same thank
yeoreum I just run gasps normally now
I’m etherium mode on the real network
and then I just deploy my contract and
I’ve done it I deployed in theory I just
need to have a theory I’m in the address
that I’m deploying phone in order to
deploy exact cost money yeah like if
there was no ear you know
yeah yes all right Wow
okay so that that’s really it like
that’s I think I think that’s the flow
well what else what else what else is
there a lot what up so then what’s the
next point of complexity that I’m going
to run into as you keep going is it just
like the data structures you can use in
the design patterns of smart contracts
that’s really serves it in um well I
think it really depends or is it in the
you eyes around these gaps like what
what have you been working on what have
you been spending most of your time like
and I so I’ve been trying to figure out
what what is like the most efficient way
of you know having a bunch of smart
contracts but interact with each other
and but you want to separate them as
much as possible to your injury so it’s
like distributed system I’d been
thinking yeah the way you do a friendly
distributed system yeah yeah gotcha
so it’s yeah it’s just the so it’s
really just that like it’s not any
different than like building just a
normal data system only you just doing
it on a blockchain yeah stupid same
thing is just like data flow and all
that kind of stuff it’s regular
programming like it’s not so different
than regular program it’s just on the
blockchain yeah cool
all right yeah anything else you can
think of that you think is I can it’s a
pretty good 101 oh I think it would be
really good you know to go through
people can do it offline go through the
toget the standard token okay just the
code base alright yeah it’s a great
example it’s highly audited if it’s you
know it’s probably going to be a
component of really different systems
they’re gonna be awesome Commack well I
appreciate you coming here and giving me
all that information and I hope when we
upload this to YouTube either people
find a finer point man all right sweet
thanks peace