Stacks 101 - Building Dapps on Bitcoin - Kenny Rogers
web3con 2022 · 6 Mar 2022 · 20:52
Kenny Rogers, developer advocate at the Stacks Foundation, introduces building decentralized apps on Bitcoin with Stacks. The talk explains how Stacks adds smart contracts to Bitcoin without changing it, how the Proof of Transfer consensus mechanism connects the two chains, and what that does and does not mean for security. It then looks at Clarity, a Lisp-like smart contract language designed to avoid common exploits, with a short NFT example and resources for getting started.
Speakers
Topics
From web3con 2022, hosted by Developer DAO.
Transcript
this talk is sponsored by desantology decentology is the innovative company behind the hyperverse the open composable smart contracts marketplace that makes it easy for developers to build web3 apps without writing a single line of smart contract code learn more at dsantology.com hey there welcome to stacks 101 building depths on bitcoin on this talk i'm going to go over what stacks actually is give you a quick rundown of how it works uh why you might want to build on it as a web3 developer and then we'll go look at a little bit of sample code we'll look at clarity the native smart contract language that the stacks runs on and then finally since we're a little bit
limited on time and i won't be able to do an actual full-on code demo um i have a link to a resource page that i put together in case you are interested in looking a little bit more into stacks maybe start building some stuff on it so real quick file we'll do this really fast i want to spend too much time on this my name is kenny i'm the developer advocate at the stacks foundation i've been a full stack dev for about eight years about a year year and a half ago started getting really into blockchain web3 started building things on ethereum and solidity and then i found stacks and fell in love with it and i'm going to go over some of the reasons why i did and why you might also be interested in building on it in this talk so let's go
ahead and get started by going over what stacks actually is so at a high level what stacks does is it allows us to build smart contracts for bitcoin and what that means is that a lot of the functionality that was previously unique to just ethereum or maybe networks like solana we can now utilize uh with bitcoin without actually changing bitcoin itself and that part is key so stacks is connected to bitcoin with a consensus mechanism called proof of transfer we'll go over this a little bit in more detail in a minute and what it's kind of being branded as is a layer 1.5 blockchain so stacks is technically its own blockchain
but with this proof of transfer consensus mechanism it cannot exist without bitcoin so that's kind of where the layer 1.5 comes in in order for something to be a true l2 um the function it can only perform the same functionalities that the l1 can so like with ethereum smart contract functionality the fully expressive smart contracts and the programmability they're built right into the layer 1 into ethereum itself and so l2s like polygon they can then take that same functionality and make it more scalable stacks is a little bit different than that so the goal with stacks is not to just take the same functionality bitcoin already has and duplicate it or excuse me not duplicate it but make it more scalable that's more similar to what something like lightning might do
with something like lightning you're not you don't actually have any extra capability outside of what bitcoin can already do it just takes that same capabilities and makes a little bit faster and more scalable the goal of stacks is to actually extend bitcoin's capabilities um and add capabilities to it and so that's kind of where this 1.5 comes in something else i want to point out really quick because this is often a point of confusion is that stacks is an open network so anybody can run a stacks node and verify the history of the chain just like they can with bitcoin it's not a federated chain it's completely open and decentralized um a federated chain would be something like liquid and what um a federated chain the compromise there is it's not an open network but that allows you to run a much faster
network because the transactions are validated and managed by this closed network of people but the downside there obviously is that it's not open and completely decentralized so stacks is open it is decentral it is a decentralized network um one thing i also want to mention really quick because this is something that people often get stuck on is you'll hear this language if you're in the stacks world for any amount of time of built on bitcoin um that can be a little bit misleading because it's not technically true um sax is i would say built for bitcoin or built with bitcoin and it does leverage some of the security of the bitcoin network but it does not inherit all of its security so this is a common
misconception is that stacks is as secure as bitcoin and it is as hard to hack or fork or compromise stacks as it is bitcoin that's not quite true because stacks is its own separate blockchain you could compromise stacks without necessarily compromising bitcoin however what it does mean is that because stax is connected to bitcoin via this proof of transfer mechanism and because it cannot exist without bitcoin that means that the entire history of stacks including any forks have to be recorded onto the bitcoin blockchain so what we're really doing with stacks is we are using bitcoin as this uh broadcast medium so every transaction that happens on stacks has to be
broadcast via bitcoin so while technically you could compromise the stacks network without necessarily compromising the bitcoin network what you would have to do in this case scenario is you would have to broadcast that attack publicly on the bitcoin network which means that it only requires one honest stax miner to cut that attack off and so that's what people mean when they say that stax inherits bitcoin security or that it's built on bitcoin um just be careful that you don't take that too literally and think that stacks is literally built on bitcoin or in the same network as bitcoin it is technically its own separate blockchain but it cannot exist without this proof of transfer consensus mechanism and that's what we're going to go over a little bit more right now or excuse me after we go over this
why build on stacks so we got into this stuff um a little bit on the previous slide but stacks is built with bitcoin which means it extends bitcoin's functionality and it uses bitcoin as the settlement layer so one really cool aspect of this is that stacks actually has visibility into the bitcoin state and so we what we can do here and we're still in the early stages of this but we're seeing more and more applications built utilizing this methodology and technology but what we can do is we can actually enable dapps that use bitcoin as currency so because stacks has visibility into the bitcoin state we can read that bitcoin state and initiate transactions on stacks based on what is happening on the bitcoin chain and so eventually what we'll be able to do
is we'll be able to use stacks as sort of an invisible programmable smart contract layer but users may only ever have to interact with it using bitcoin so what they may do is they may initiate a bitcoin transaction which will then trigger a stax transaction which will do some sort of logic on the smart contract um using clarity the language we're about to go over on the stacks network behind the scenes it will convert to the bitcoin two stacks in order to use the gas token to actually run and fund the transactions just like we would on any other blockchain and then on the other side of that behind the scenes it would complete the transaction convert whatever action happened back to bitcoin and uh transfer to the other person's wallet so the goal here is that this really unlocks a ton of functionality for bitcoin um
previously bitcoin was often seen as just a store of value and so you couldn't do things that you could do on ethereum with it but now with stacks you are able to do those things so one of the other things i want to talk about real quick is why why would we want to do that why would we want to separate those two things so if you're trying to make a blockchain or a decentralized network sound money or a programmable system to build sensor resistant software on top of those actually end up being two relatively different use cases and so you want to the blockchain to have certain qualities based on one of those two use cases ethereum can run into problems when it's trying to be both and we hear debates
about this all the time bitcoin sound money is ethereum sound money um but the bottom line is that they're both successful they both exist previously it was only possible to build programmable things on ethereum so if you did prefer bitcoin's consensus mechanism of proof of work and if you did think that bitcoin was a more secure immutable blockchain um you still could not use that to then build sensor resist censorship resistant software on top of or smart contracts on top of so you had to do it on ethereum regardless now if you are of the mind like i am that proof of work is a more secure and immutable consensus mechanism you still have the ability to now build programmable smart contracts and depths utilizing bitcoin as that final
settlement settlement layer and so that's the sort of high level of why you might want to build on stacks and then again that is made possible with this proof of transfer consensus mechanism we'll go over that in just a second but i do encourage you if you're interested in it some of the resources i have at the end really go deeper into that it can be a little bit difficult to wrap your head around it first but once it clicks then you'll kind of realize how powerful it is because it does allow us to connect into a more secure blockchain without actually having to change it at all and inherit quite a bit of its security capabilities and then finally one of my favorite parts about stacks is that they have the clarity smart contract language so clarity is a language that was created
specifically for stacks it took a lot of the common exploit and flaws exploits and flaws that ethereum or excuse me solidity has and it sort of built a smart contract language that was purpose built to avoid some of those common flaws and exploits so clarity is designed to make it much easier to write safer code by default and after we go cover proof of transfer a little bit then we'll switch modes and go and spend the rest of the time talking about this clarity smart contract language will give you the kind of the high level level overview of what makes it unique why you might want to build on it and then we'll look at a really brief small code sample and then you'll have some resources to go dig into it further if you would like so
this diagram kind of explains at a high level what is happening in proof of transfer and how stacks actually um functions and how the consensus mechanism works so it starts with the minor and that top left is the first step so the way i like to think about stacks is recycling the energy of bitcoin which is also something that's really cool because a big concern with bitcoin is that it has too much of an impact on the environment whereas a blockchain that might use proof of stake uses less electricity i'm not going to get into the proof of work versus proof of stake debate that's a whole separate can of worms on its own um but the point that i want to make here is that bitcoin does use a lot of energy um there's not really any way around that
people can debate the pros and cons of that and whether it's worth it but what stax does is it allows us to actually recycle that energy so at the bottom layer you have bitcoin where you are exchanging a resource in bitcoin's case energy you're exchanging energy for bitcoin that's the proof of work the proof of work that you're doing is you spent this you did this um computation of this cryptography task you spent the energy to complete the problem you were awarded with a bitcoin then next level up we have stacks in the proof of transfer consensus mechanism so a minor mines bitcoin they have the bitcoin and then what they will do is they will then spend bitcoin in order to mine stacks so it's sort of one level up from bitcoin
so this first step is the miner will spend bitcoin and then there's a verifiable random function that is weighted based on how much bitcoin you actually spend and that is what determines who wins the block and gets to mine it and then what will happen is that miner excuse me stacks will take the bitcoin that was put up as um that the miner put up and they'll transfer it over to stackers and what stackers will do is they will lock their stacks into the network in order to boost its security and they're rewarded with a large portion of the bitcoin that the miner has put into the network and so there's another consensus mechanism
called proof of burn which is similar um except that in that case the cryptocurrency that you're using as the base layer is just burned and it's gone in this case the bitcoin is transferred to other members of the ecosystem in this particular case stackers so then that miner after the bitcoin transfer that they put up is done they will then write that transaction to the stacks blockchain and then receive the um a portion of the newly minted tax reward and then the transaction fees and the process starts all over again so again i have some resources at the end to dive more into how this actually works because it can be a bit confusing to wrap your head around but that's how it works at the high level let's quickly dive into clarity with the last few minutes that we have so i can
go over some of the reasons why clarity is unique in my why you might want to write with it so first big difference between clarity and something like solidity is that clarity is interpreted and not compiled so when you write a clarity contract and then you submit it to the chain it's getting submitted as submitted exactly as it was written so it's not compiled to bytecode so this has a couple of advantages one it just eliminates one more process that can go wrong it's unlikely but it's possible there's a bug in the compiler it's just another intermediary step that in stacks is not necessary and then the other major advantage of this is that by default any contract that gets written to stacks is human readable with solidity and ethereum that doesn't happen by default the developer or the
user has to go through an extra step to decompile that and that's an external tool not built into solidity and not built into ethereum itself it has to be created so it's another third party thing um that is just an extra step that you have to do and so with clarity by default all contracts committed to the chain exactly as they were written which just helps a ton with making everything more transparent and making it more readable and then the second thing is that clarity does not allow re-entrancy so clarity considers re-entrancing an anti-pattern and it is disallowed at the language level if you're not familiar um go ahead and look it up dive deeper into it but at a high level reentrancy is where one contract can call into another contract before it's
finished executing and it can start this back and forth pattern to where somebody can perform a second action before the first action has been completed and this has been the cause of a few hacks and exploits in the past again it's something that has to be in solidity on ethereum it's up to the developer but clarity does not allow it at the language level another feature of clarity another um thing that it doesn't allow at the language level is that overflows and underflows are automatically aborted and so what this means is that if some function that you have in your smart contract generates a number that is either too big or too small for the language to handle um it will automatically abort so that's not going to cause any problems for you it's just going to cut it and you're not going to it's gonna the function execution will
end there and you won't have to worry about that one of the other cool things that we'll go over in just a minute when we look at the code sample are that custom tokens are built in and so instead of having to use something like open zeppelin where you're you have to create the actual tokens functionality in the token interface itself um from scratch um these tokens are first class citizens in clarity so we'll look at that in just a second but there are built-in functions and clarity to define like non-fungible tokens fungible tokens and so you can create your own fungible cryptocurrency tokens on stacks you can create your own nfts on stacks and those functions are built right into clarity one of the other things is that clarity is decidable and so from the code itself you can know with certainty
exactly what the program will do this has a couple advantages one it allows us to do something called post conditions which we're going to go over in just a second but the other thing you can do is it's guaranteed the program execution will end and you can get an accurate picture of the exact cost that it's going to cost to process that transaction before execution actually happens so this is pretty theoretical so i'm not going to get into it now but another thing to look up is decidable languages and turing non-complete languages which is what clarity is versus touring complete languages solidity for example is a touring complete language so go ahead and look at the differences in that because it's really interesting and it's worth a look but from a high
level the main advantage is that you can know exactly what the programs are going to do just by looking at the code and it's guaranteed the execution will end which on something like a blockchain which is a globalized distributed network is a really important feature to have and again that also allows us to do something called post conjunct post conditions so on the stacks network what we can do is if we're building a full stacked app using stacks on the client side when a user is initiating a transaction we can put into the javascript okay once this transaction runs these things should have happened and if anything else happened the transaction should be aborted so what happens when a user will initiate the transaction let's say they're going to purchase an nft and the nft cost 10 stacks there will be a window that pops
up on their wallet that says um this transaction will trans transfer no more than 10 stacks from this contract to this contract otherwise the transaction will abort and if you were to try to initiate the intent initiate the transaction and somebody was trying to do something fishy and say double the stacks you were going to transfer the function the smart contract function would just automatically abort and the user would only be required to pay the gas fees to process that transaction one more thing the return responses cannot be left unchecked so in solidity it's up to the developer to make sure they are handling any return responses and clarity you cannot write a function without handling the return response which just helps to make more functional clean code that is less
error prone another thing that makes you unique is its composition over inheritance so rather than contracts inheriting from one another um developers instead define traits which are then implemented by different smart contracts so instead of having like a class-based system where a contract will inherit things from one class down to another you can implement piecemeal traits and then your contract can interface with those and the main purpose of that is to define standards so like there's a good a good example is one for nfts there's an nft trait and by implementing that trait you then go into your contract itself and you have to define these certain functions that do certain things and that is to make sure that there are
cohesive standards across public interfaces for different smart contracts and then finally went over this a little bit before but it has access to the bitcoin chain which means that we can read the state of the bitcoin chain and take action based on that so real quick i want to go over just a few lines of code to create an nft and clarity this is just going to be super high level super whirlwind and then i'll give you some resources at the end but this is the basic um first initial amount of code that you would need to implement an nft so clarity is a lisp like language and so you'll see a lot of parentheses which is weird at first especially if like me you were used to languages like solidity in javascript it's definitely structured a lot differently but once you start using it and it clicks you realize how kind of
clean and beautiful it is so what we're doing here is we're calling the built-in clarity function called define non-fungible token fabulous frogs and the identify there is a u-n so just that one line of code created an nft for us and then down below we're implementing another couple of built-in clarity functions to mint that and give it to the transaction sender to transfer it and send it from the transaction center to another principal which is another word for a contract in stacks and then we can also get the owner and we can burn the nft so these functions have built-in security properties so that um you can't transfer an nft that doesn't belong to you you can't burn an nft that doesn't exist you can't mint an nft that's not unique so if it are if
the id is already taken and so these are some built-in security features of clarity that just make it a little bit easier to write contracts like this and then what you would do is something like this is you would then implement a trait which would allow you to create the public interface for this contract so that other people could interact with it in a standardized way so that is the super whirlwind high level overview of stacks and clarity if any of this jumped out to you if this was interesting to you i have this page that i created of resources stx.is slash web 3 con dash resources so you can go there you can check out i've got some getting started tutorials on there i've got some more information on stacks and proof of transfer
so go ahead and dive in there see which and see what's interesting to you i also have my twitter link on there so if at any point at all you have any questions want to get involved in stacks need help whatever feel free to reach out to me on twitter my dms are open so thank you so much for taking the time to watch this i know this was super quick um and probably a little bit confusing but if you dive into these resources and reach out if you need any help i might be happy to help you with that again thank you so much for your time i hope you enjoyed this and hopefully i'll see you soon thanks
