[Music]
patterns by the gang of war is uh is is
very famous and also outside of the
field of developing you have patterns so
if you want to if you want to knit a
Celtic pillow I wouldn't know where to
start so first I Google and then I get
this kind of stuff I I'm not sure what
it is it looks like brain F codes but
probably it tells me how to uh how to
knit a
pillow so what what is a pattern a
pattern is a is a reusable solution for
common occurring problems um that you
can that you can apply it's not some
boilerplate piece of code that you just
copy paste and you're done with it you
apply it to your own codex but you you
try to reuse it you try to to see when
does it make sense when does it make
sense and in the the the the pattern
description the pattern language we
mention when is the good problem to
apply this pattern when you shouldn't do
it and if you're applying it what are
the considerations and of course some
some code
examples um yeah Microsoft guys with
their their Azure Cloud they also
released all kinds of patterns so in all
kinds of software engineering Fields you
have this pattern system so probably the
next version of this slides we'll have
the blockchain pattern in here at as
well so when when looking at the
patterns we discovered three areas we
have data patterns transaction and value
patterns and we have action and control
patterns probably there are much more
different categories but these are the
three distinctions that we came up with
um and I'm going to mention six examples
from this uh this set actually this
morning we also we we've seen the fan in
and fan out pattern from the Block apps
demo we have seen the the escrow example
earlier so already during this day you
have me you have witnessed a couple
patterns without even realizing it so
for each of these patterns we'll mention
well of course the name of it a
definition of the problem so when when
can you apply this pattern and then if
you're going to apply it what kind of
context what kind of considerations what
kind of constraints are there to uh to
be assure
of followed by an actual solution of uh
of this pattern and a couple examples
out of the all the code that is publicly
available on getp already and you see
these patterns are quite often related
to each other so if you use one pattern
you might as well consider another
pattern or even use them in combination
of each
other
so first of all the the data patterns
two two
examples the first one is the the name
registry it's um so you you need this if
you're looking for for a human readable
name for a ethereum address
um and you might need this human real
name because you want to display it in
your user interface maybe you have a
wallet application and you want to show
the person that you're transferring
something to or you have a game and you
want to show the nickname of the person
that was uh just just winning your your
contest or because your dep consists of
a a cluster of contracts that are
related with each other you want to make
a reference from one contract to the
other but this relationship isn't known
uh up front so you want to refer to the
other contract by name instead of
hardcoding the address in your your
contract um and of course that has the
advantage that if you update the name
registry you can also update the code of
your of your code
construct um and you can e even apply
hierarchic patterns here where you get
some kind of subdomain registry so you
can register uh you can appoint a
registry for a for a domain and then for
every I don't know for every household
within within in an area there would be
another reference to another name
registry for the individual people
living
there of course there's some issues with
this uh you have the traditional kind of
problems that you're also seeing in the
the DNS uh name registry so you have
this name squatting potential um what
happens if you lose your key is this
name going to be lost and we're going to
expire it maybe you can out bid somebody
else so there's all these complex things
to to think of as well
so a solution here is to create uh a
contract a registry and this could
either be a global one that you use in
all your ethereum applications or it
could be one specific for your context
or for your application and it has a a
reference from a from a name to an
address it's just a big hashmap that
just looks in the contract storage for
the given name what is the address that
is associated with it and then it
Returns the address and you can extend
this by giving a reverse lookup so you
can query it for address address and
then you can see what is the name that
belongs to this uh to this
address and a good example here is the
is the name the name rare contract that
is uh actually implemented by by gin I
think it's in the the de bin GitHub
project in the ethereum um repository uh
and here you have the the name register
implementation so you can take a look at
this and see how how G uh how G does it
so a second data pattern is this data
feed smart contracts they cannot reach
out to the real world you cannot make a
HTTP call and and see what a website
returns as a value you cannot directly
connect the camera to it so can make a
picture so you need you need to actually
feed the data to your contracts to get
some kind of value out of it so what if
if you're making application that needs
to know the weather for a certain area
or or it needs to know an exchange rate
so you can exchange your ether for
Bitcoin Vis Versa
um and yeah there's this limitation of
the ethereum virtual machine where
because it's deterministic you cannot
look outside and fetch data uh into the
into the blockchain by itself so what
you need to do you actually need to feed
this data into ethereum onto the
blockchain and then once it is
there every other application on the
ethereum blockchain can actually use it
and reuse it and use it in fashions that
you really didn't think of yourself uh
before
so typically you'll need an automated
process some kind of demon that
frequently publishes updates to this
data feed so maybe once every day what
the weather was or what the exchange
rate was or more frequent or less
frequent uh depending on your on your
usage um and of course you can you can
publish this as a free service but you
can always you can also uh ask for a fee
if people are getting the data out of
this data feed so of course there's some
cost associating with with setting up
such a process that keeps updating keeps
pushing data to the to the ethereum
contract at least you need to pay the
the gas and also some kind of Hosting uh
fee so you could actually charge every
contract that is using this and this
could be a a viable Revenue model for a
for data feed
um yeah I've given you these uh examples
already and a much more advanced example
is the the BTC relay uh contracts where
it's actually storing blocks and
transactions from the Bitcoin onto
ethereum so then you can use this on the
ethereum uh blockchain as
well and and as an example here we have
the the price fre the the price feed
example from Nick Dodson I think he's
also here or might be presenting later
this week so here they're pushing uh
fiat currency uh prices on the uh on the
on the on the blockchain and then you
can use that for some kind of set up
some kind of trade some set set up some
kind of escrow without having to rely on
the exchange rates of The Ether
itself so looking at these transaction
and value related patterns two two
examples uh the asset token we you've
seen that in Peter's presentation so you
need to have some kind of representation
from a either virtual or physical asset
uh on the blockchain so you can for
crowdfunding purposes so you can you can
sell it or maybe so you can you can
lease your car or you can you can trade
it with with somebody else or just you
can track it for auditing
purposes and typically these these
assets um are are are equal are equal in
kind so you can enumerate that them and
you can have multiple of them and if you
for this if you use a standard interface
and there's the the The Meta Meta
currency uh API description on the
ethereum wiki if you use the same kind
of interface for this then you can use
all kinds of different wallets and
exchanges to to to track your uh sub
currency to track your assets without
the the wallet itself having to
specifically support
you um
so what you do you create a contract and
basically you track per account how much
of these assets they have in uh uh in in
the the contract storage and then you
set up maybe at least two two functions
one to transfer this assets from one
account to the other account and one to
return the the balance of one of these
account holders in there and if you have
this you can use this to to create a
crowdfunding tokens set up IOU
constructions uh track Fiat currencies
so US US Dollars Euros whatever or even
as uh digix is doing uh keep track of
gold certificates on the on the
blockchain and here as an example is uh
from slockit they crowdfunding contracts
where they also using this this pattern
so they're doing some kind of
crowdfunding probably will hear more
about this this later on but you can
transfer these tokens to somebody else
and you can also see uh what what your
balance is
um another another one of these uh uh
value and transaction related patterns
is the exchange or or market so you have
one of these asset tokens so that's
actually from the previous example and
you want to sell it buy it exchange it
for something else so you can set up an
exchange on the blockchain itself um you
you don't need any kind of central
websites that will do this exchange for
you and then lose your money and and and
run away with it but you can do all of
this on the
blockchain and typically both the assets
for a one-on-one exchange will be one of
these assets on the on the uh on the
blockchain or one of the two could be
just the The Ether currency itself but
that I guess that's less uh less common
and then of course the trick is how are
you going to do this settlement if you
have a huge Ledger um maybe it's very
good to have the user indicates which is
the the winning pair and and have have
it matched that way or you need some
kind of external trigger that will be
called uh to to do the to the matching
and the and the settlement payouts but
that's potentially
expensive so what you need to do you
need to have a contract you need to keep
track of the bits or the Buy orderers
keep track of all the asks or the sell
orders and have some kind of matching
mechanism and once a bit and ask is
matched you swap the assets from both
account holders uh to the other one an
example of this is uh vitalic Cyber diey
Markets it's written in serpent instead
of solidity so it's a good homework
exercise to try to convert that from
serpent to uh to solidity and another
example is the The Ether x uh
exchange so an example from a
cyberdine you see there's a buy function
there's a sell function but you can use
this this Market this exchange to
exchange all kinds of assets that have
the same interface without having to
upfront Implement support for this in
your in your
contracts so the the third and final
group that we have seen are these action
related
patterns um event lock so it's a feature
of the evm itself you can emit events
and they will be persisted on the
blockchain and you can use them to to
track state or to track when some kind
of action happens and have the outside
respond to it there's a limitation
though with the evm from one contract
you cannot inspect the event lock of
itself or of another contract so you can
only use this data from the outside but
this makes it ideal for user interface
purposes or if you have some kind of
watch doog process that is looking at
these event logs and act uh upon
them so yeah it's just a single line of
solidity code to emit an event if an
action happens and then you have a a
listening process or a watching process
in your user interface that will update
its state uh accordingly so examples of
this if a buy order took place May on to
update your crowdfunding website if you
just least your car key to somebody else
you need to change the the the the codes
the public key on the on the lock system
if an account is registered maybe some
other event takes uh takes
place so a good example here is the is
the W fund again from uh Nick doson the
wund crowdfunding uh uh contracts so yes
these events
for that are emitted when a new c
campaign is created when a contribution
is made when a payout happens and even
when a refund happens maybe he doesn't
even use this these kind of events
himself but by just sharing them you
allow others to consume them and create
all kinds of user interface and
interactions with them that you didn't
think of didn't think of
yourself so
um yeah there's a bit of issue with um
ethereum there's no alarm up code you
cannot trigger some kind of future event
to happen automatically um so it every
every action in a contract needs to be
triggered by a transaction that is
eventually being sent from a from a user
so what what if something needs to
happen in the future how are you going
to to make sure that is that this
happens an example for this are if you
need some kind of I don't know
expiration of your of your um of your
lottery tickets doing frequent dividend
payouts other kinds of cleanups how are
you going to make sure that this will
that this will happen so the solution
here is to to reward the collar for
actually trigging this mechanism so you
create a function anybody can trigger
this but once they do this they get
rewarded for doing so and that way you
hope of course you cannot guarantee that
somebody will do it but they have a
strong economic incentive for triggering
this method and making sure that it will
be uh that it will be
called so an example here from uh gin uh
casino contract is this recycle method
which anybody can trigger
and if there's some kind of expiration
event the person that was trigging this
get rewarded with half a percent of the
of the of the funds so makes sense
there so combining these things together
you can come up with all kinds of use
cases so say you have a a betting
website where you want to allow your
users to bet on soccer scores you you
would use a name registry to keep track
of the the different usernames so you
can have a a top a top scoring list High
all Fame on your on your on your site of
on your da need some kind of data feed
to know which soer events happened and
what scoring was was there um event lock
so if somebody takes a bet or if you
want you want to update again your
website or your dep accordingly so you
use event lock to get the output uh from
that so already you see with such a
system you're combining ing multiple of
these patterns
together um again for Market if you have
some kind of manufacturing site and you
do some kind of internal research
optimization you can try to centralize
this but you can also think about can we
do this on the on the blockchain using
asset token to indicate resources having
a exchange contract where people can bid
and ask for for resources and then of
course having some kind of incentivized
trigger for making sure that frequent
updates such as Market settlements
happen and another example um is if you
want to have your dep interact with with
real Hardware so in this case it's a
smart meter in my utility closet that I
connect with my ethereum Rasberry Pi I
get data out of it um but how how do I
know that this device belongs to me so I
need some kind of notary data pattern
that that that makes this Association I
have this data feeds so for
every energy consumption update uh a
contract is updated with my uh data
usage and if I want to display this in
my dep I need to emit event locks um
using the event lock
pattern so what's next for you guys take
a look at existing codes in GitHub
existing contracts study them and see
what kind of common patterns do you do
you see do you you are used and and can
be used re use for
yourself and
also we're trying as a community effort
to get this uh stack overflow stack
exchange site setup for E ethereum so if
you go to Area 51 on the stack exchange
please vote for it as of this morning I
think we were at n um almost 70% so a
couple more votes and we have a
community site for all kinds of Q&A all
kinds of discussions around best
practices about patterns sharing code
sharing knowled with each other and this
would be a great place to exchange uh
ethereum or Deb patterns as well thank
you guys
[Music]