Mental Model for Building Smart Contracts on Bitcoin - Abimbola Adebayo
Web3Bridge·Wed, Oct 9, 2024, 12:00 AM
Speaker
Transcript
and please a round of applause as we welcome Mr abim a round of applause come on better better better better come on okay great over to you good afternoon everyone um so glad to be here so my talk is basically going to be covering about mental model for building on bitcoin so this talk is going to be more beneficial for people who have experienced building with solity existing evm developers so uh prior to my exploration around Bitcoin the kind of notion that has always been is the fact that is it really possible to build smart contracts on bitcoin I would say yes the only thing is that it depends on how you think about it because there's a different approach to how you are going to go about building smart contracts on bitcoin as compared to ebm so what are the current challenges that we have on bitcoin first thing is that um I know most of us here this is not really our first time hearing about Bitcoin and some of the speakers have come along they've spoken about call Bitcoin and stuff so I'm not going to buy with all the reasons why you should build on bitcoin the tvl and stuff like that so looking at all the components and the features benefits available to you on bcoin what makes it difficult for people to like be able to like innovate and build on bitcoin just like it is currently available on ethereum one of them is that it is slow one transaction on bitcoin has six confirmation Cycles so ideally it should be between 10 minutes but nowadays a single transaction can take as high as 1 hour to confirm on bitcoin so this is not really really feasible for more expansion and use case and the high gas Fe there are sometimes you use Bitcoin and your gas fees will be going close to $300 quite higher compared to ethereum so this makes it difficult for people to use both as developers and also Traders and Bitcoin layer does not have native smart contract integrated unlike ethereum where you have the eum virtual manchine which compile you can use to compile your smart contract B code and stuff yeah there are some solutions that have been built on bitcoin you have like the lightning Network you have or and some others so for example lightning is more focused on payment using Bitcoin as as a payment infrastructure using different approach but what I'll be talking about will be Stacks so sta is a leading Bitcoin L2 which is one of the leading Solutions there working on the limitations of Bitcoin expanding it to give you a very robust solution so how many of us are familiar with the popular term being TR around about blockchain scaling which is the blockchain trma if if you are familiar with like blockchain trma can I just see your hand okay all right so basically uh blockchain Tria is just the fact that it's somehow inoss at this point in time to have three components in a chain which is decentralization security and scaling so how does STX enable you to be able to like build smart contract on bitcoin is the fact that the security issue about the security layer for stacks leverages on bitcoin because we can go on and on about different blockchains different solutions around and we still come to the conclusion that Bitcoin does want best which is secure payment infrastructure and that's made available on bitcoin so Stacks remove this issue of security by leveraging on the security of Bitcoin so it uses Bitcoin as the base layer on which it operates on and by doing that on Stacks you have faster block finality faster transaction times and more transaction can be bashed into a block on on Stacks even okay so now sta basically has this faster Bitcoin finality and stuffs leveraging on bitcoin to build so Stacks is not really a new thing St has been in idation stage since 2017 2021 finally launched on mainnet and since then there have been different improvements so for example um in the Bitcoin ecosystem we have like stuffs likei infrastructure Dow infrastructure and uh the one that got like more popularity recently which is the ordinance stuff is also available there which you can have access to so I know I'm speaking to developer audience all this might not really make sense to you if the point of oh where how do I build smart contract how do I do this how do I extend my existing applications how do I bring them there so we are going to cover that so designed for Bitcoin leverage us on this thing so on most of us for consensus mechanism we are more aware with proof of work proof of stake and the likes so Stacks use a different approach which is proof of transfer this proof of transfer basically means that transactions on Stacks are settled on bitcoin just like the way you have your rollup on ethereum where the bu of transactions you have on your L2 are been settled on ethereum so something similar to that you have there and you have the sbtc which is basically synthetic BTC CTI BTC is just a one to1 backed asset on l22 so the Nakamoto release is still ongoing which is part of the things that's going to make stacks more faster than it is currently so the one that concerns most of us here how do you build on bitcoin and that's Clarity so Clarity is quite different from solidity but your previous knowledge on solidity and everything can actually make it easier for you to build here and stacks was actually born out of research on limitations of different chain so one of them is like ethereum is good solidity is good but it also has its own pit Force some of the pit you know re-entrancy underflow overflow and stuffs like that so Clarity works on this intentionally to eliminate all this stuff so I'm going to go into it as we go on so Clarity is not a compiled language it is an interpreted language so why is it hello everyone sorry for the interruption um but just so that our speaker isn't being distracted by us I'd like to ask a lot of us that are behind to move forward please fill up the space you know in front of you and if you know that you're not Technical and you're supposed to be in the non-technical hall this is your time to move please let's just keep this allall very quiet thank you very much so please move forward thank you so much so why what what's necessity ated this design decision for cl to be interpreted is the fact that most smart contract languages that you have are basically compiled languages and that alone introduce new range of threat factors to your smart contract which is one of them is compiler books So Sol for example C doesn't have a stable version one yet everything is still on zero point and these are due to some of the technicalities around building compilers a bug in compiler can affect everything you've deployed on a chain so for example some of the improvements on solidity that you've seen in recent times on older versions you have support for self-destruct but now self-destruct is no longer available and stuffs like that and this is actually based on some of the tradeoffs that come from using a compiled language and the next thing is that Clarity doesn't allow re-entrancy so firstly how what's the concept of re-entrancy basically a code calling into another contract recursively so how what enables that on SOL is because evm is storing complete and when you say a a computer system is storing complete it means that it can run any computation given a face time and uh resources so it can keep on working on it as long as there's resources and time available but Clarity doesn't give you a full toring complete system so the issue of re-entrancy is already eliminated with that so this um not being Tor in complete is why you now say Clarity is a decid language this factor is because when you say a programming language is decidable it means that just by having the contract your code the functions or any any unit of your logic you can actually tell if it is going run going to run successfully or not because it has that factor in it so it is not like in solut where you write a function that you yourself just looking at your code you are not sure if it is going to run successfully based on some factors in on evm for example you can run run out of gas you can have a stack uh stack to deep error and all those things all those like limitations and challenges that you face on solity has been eliminated here so and what that means uh and also another thing in being a decidable language is that from your code you can easily see the static analysis of any unit of your code so one of the things that we do on um on solidity to Asser guarantees that this smart contract is safe is foral verification you don't need that with Clarity so that means like your development time timeline can be reduced by using this and on stats transactions are secured by post uh conditions so so as opposed to solid C where you have a statically typed language Clarity leverages on lips this means you are going to be seeing a lot of parentheses in your code so it's not as it's not as fine looking as solidity to be honest but it works so looking at this this is how you do a simple addition on Clarity so let's say you want to add 2 + 3 ideally mathematically you do two first plus the um number you want to use but here your operant has to come first and the input comes later so like looking at this one the operant here is the plus sign sorry is it clear enough or I should zoom in it's clear enough okay so this one you have your upand first then you have the you and u3 so this U2 u3 is the equivalent of unsigned integer so the difference between this is that um for solidity you have unit8 unit Unit 16 sorry 32 like that to 256 but here it has F size for the unit so what's happening here is that you are having addition of three and two the U before it is the data type so basically this just a simple addition and if I run it it should give five U five so you have the data type before it so um I'm just covering like the Primitive type just like you have in Sol you have integers unsigned integers Boolean and addresses too so addresses on um addresses on Stacks they are called principales so it works the same way you just like I said earlier if you have experience with solidity you are going to have like less less time less trouble navigating through Clarity so this one is for addition and this for substraction this one two is for multiplication and division so I think um so one of the TOs that you have access to is clearing it so the just like you have Adat and G stuff on um on ethereum you have this so just give me a moment please okay so if you remember for most of us I think right now we have access to Foundry so Foundry makes things easier for you and when you are doing your development stuff you can easily have a local devet running where you have access to um accounts funded with s and stuffs like that so also on Clarity on Stacks as a blockchain the development environment is called clearing it by Hero systems the hero is H do so you can just check them out I'm going to cover it soon so ideally remember when you write your smart contract on solidity how you compile them is use maybe Forge build you compile them and stuff like that here leveraging on clarinet what you use to do that is clarinet check so the for JavaScript typescript developers you also have some advantages here in the sense that you write your smart contract in Clarity and you can also test with typescript or JavaScript anyone that works with you so your knowledge around JavaScript ecosystems can also come into play here so before I go go into like maybe process of like deploying this just like a simple Clarity contract that I think I used before so this is just like a token streaming contracts what you have here is it clear enough for people at the back or I should zoom in the code I should zoom in hi B so the way you have error codes or on evm you also have them here this how you write your er codes this for variables you also have mapping here so the just like I said earlier you are going to see a lot of parentheses around your code base when you writing Clarity so just uh it's some of the new things you have to like maybe out to your stock while learning this and okay so on the data types remember I said addresses just like you have contract address externally owned addresses on ethereum you have them here too but they are called principles your un the idea you have there still applies here so this basically here is like on this side it's similar to what you call struct in solidity so this one is just a struct for streaming so ideally this contract is just like for a token streaming contract just like um super fluid super fluid on ethereum so it's similar to that where you are so this one I need some parameters and this where I just Define the structure of what I'm going to expect from uh people and Stu like that so this one is to create new stream and St so I'm not going to really go into the or should I go into the code base to explain like the different varable so I you want me to give high level overview about like the folder structures and what each part represents which one should we go for first or we can cover both we should go into the code okay are you all okay with that sorry I can't say high level overview okay all right if we have in okay let me quickly do that so if you want to get started with um just a moment so to get started with this clearing it just uh come here there are a lot of tools we have the CLI package the Claret SDK that you can leverage on for different things so but majorly like development environment that you are looking for that similar to Foundry that works for um this part is the clearing it so just get started here to deploy it so it is quite easy Once you have clearin net installed on your PC it's quite easy to run everything around um deploying testing and stuffs so the first thing after installing this command that you have here clear next contract new my contract how many of us are familiar with found here I want to use maybe Foundry terms or to explain to us what about ad H interesting okay so normally when you have when you are using Foundry you need to initialize a project you do Forge New and the name of your project and get the contract FES right that's the same thing you are going to do here so you do clarinet contract sorry you do clarinet new and the name of your project this one this clarinet contract new is to is for the specific contract file not for the project in itself so let me come to my terminal for that so what you have here is how you initialize a new contract oh sorry what you have here is how you initialize your project initially Clarity knew this thing so the difference what makes this approach different with Foundry is that with Foundry you need just one command to initialize everything including your test file and and others but in clarinet each contract file needs to have a unique setting attached to them you know you can have just like in a single folder you you can have like multiple solity files and maybe if you want you can test them in the same test file you can't do that here you have to like have separate files for testing and separate configuration for each contract so the first one that I showed you was supposed to be for a specific contract so let me show you the file structure so it gets more clear so on this one in the contract section I have just one contract which is stream. clear if I want to create this what the command I used to create this will be the um clarinet contract new then the name of your file so the difference is that if you are creating a specific contract file after clearing it you add contract then new but if you are initializing the folder structure itself you don't need to add the contract so the first one will just give you a dummy stuff here okay so when you have this I have one of these then in the test I have stream test so let's just [Music] do a new stuff with this so I'm going to remember in my contract I just have one contract file there so let's add one more there so let's say clearin cont contract so I want you to take note of what happens in the contract folder and the test folder also what should we call the name of this contract let's say webos so now I have another one here and in the test file I have another one picking the same name that I use here so generally it just gives you like a a template to write your contract on so you can just edit it based on your configuration so the most important part you need to look at is the devet and the simet part so this part is where you had configurations for your deployment environment so you have the option of having it on your local and uh this thing on your local and on chain to so generally the development workflow create your contract then you do clarinet check for the compile then clarinet deploy so let me just quickly show you one my time is far going first so here now currently I'm trying to initialize a node on bitcoin so what's currently happening is butting I'm going to try to deploy the contract that I have here later and the good thing about this is unlike solut and stuff on Foundry Foundry gives you some asset to see like stack trees but here I can see how as everything is going on into the me pool so I'm just going to wait a bit for this to load and everything to change to Green so that I am able to like just initi okay almost ready so what the command I just wrote now is what's going on in now goes It goes on Fast and remove it there okay so but um if you need more information on this I can the time is my time is almost over but I have some few more things to share so these are the some of the resources you can use to get started and currently the there are some new incentives for developers trying to come on here so currently something started last month which is for new developers coming into the stack ecosystem you there's a 20K pool every month and all you have to do is just start your contract update it show proof of work that you've done and it will be randomly C for you I mean it's a good way to get started just it doesn't have to be something complex learn and deploy so this one this QR code gives you access to a form so just like drop your email there I we find a way to get um share some of the materials across to you and how you can get started participate in the pool that I'm talking about so you can okay have you all make use of this so U um I can take question right do you have question thank you for your talk so my question is um is there a better way to like just migrate my solid contract so is there a Plugin or you saying something about the SDK I don't know clear okay so you can't actually Port it directly with a plugin the difference is because your solidity contract Works based on a different Paradigm of restrictions your Sol contract is meant to like be compiled into bite code but Clarity the way you write it is the same way it's being deployed on chain no no modification to the code at all it is is not changing to bite code no Ai No no need for that so you just have to make use of that I think someone was Raising hand there for question okay um pardon me I'm not sure if you covered it but um does clarity offer um test suits like the same way you carry it out with hard Foundry yes clearing it is a full development environment for you so you write your contract and you test so if you look at remember when I was showing I showed you how to initialize a contract use from the console that's also factoring in how you test into it that's why you you test in typescript you write your code in Clarity okay so testing um writing interaction scripts and deployment everything is covered yes thank you you sir sorry do you have more question no it's working is the form working for us yes it is all right so um that's the end of my talk please a round of applause for Mr abib okay great um great um
Automatic transcript — names and jargon may be misspelled.