The One Question bounty closes Thursday 10 December.

Join in
We Them Media logo We Them Media Support us

Mental Model for Building Smart Contracts on Bitcoin - Abimbola Adebayo

The picture loads from YouTube. The player loads only when you press Play. Turn on captions in the player if you need them.

Abimbola Adebayo talks to Ethereum developers about building smart contracts on Bitcoin through the Stacks layer, which settles on Bitcoin. They compare the Clarity language with Solidity, show the Clarinet development tools, and mention a monthly reward pool for new developers. Questions cover porting contracts and testing.

Not by We Them Media Channel: Web3Bridge. Published Friday 20 September 2024.

Titles are the owner’s. It plays from their channel.

Transcript

This transcript may contain errors. This video belongs to Web3Bridge. Letters mark each change of speaker.

These are the speakers’ own words. We have not checked their claims.

0:00 A: And please, a round of applause as we welcome Mr. Abrahams. A round of applause, come on! Better, better, better, better, come on! Okay, great, over to you.

0:10 B: Good afternoon, everyone. Um, so glad to be here. So my talk is basically going to be covering about mental models for building on Bitcoin. So This talk is going to be more beneficial for people who have experience building with Solidity, existing EVM developers. So 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 EVM. 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 core Bitcoin and stuff. So I'm not going to bore you 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 Bitcoin, 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 6 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 fees- 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 contracts integrated. Unlike Ethereum where you have the Ethereum Virtual Machine which compiles- you can use to compile your smart contracts, bytecode, and stuff. Yeah, there are some solutions that have been built on Bitcoin. You have like the Lightning Network, you have Core, and some others. So for example, Lightning is more focused on payments, using Bitcoin as a payment infrastructure, using different approach. But what I'll be talking about will be Stacks. So Stacks 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 thrown around about blockchain scaling? which is the blockchain trilemma. If, if you are familiar with like blockchain trilemma, can I just see your hand? Okay. All right, so basically, uh, blockchain trilemma is just the fact that it's somehow impossible at this point in time to have 3 components in a chain which is decentralization, security, and scaling. So how does Stacks enable you to be able to like build smart contracts on Bitcoin? It's 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 one thing best, which is secure payment infrastructure, and that's made available on Bitcoin. So Stacks removed this issue of security by leveraging on the security of Bitcoin. So it uses Bitcoin as a base layer on which it operates on. And by doing that on Stacks, you have faster block finality, faster transaction times, and more transactions can be batched into a block on, on Stacks. Even Okay, so now Stacks basically has this faster Bitcoin finality and stuff leveraging on Bitcoin to build. So Stacks is not really a new thing. Stacks has been in ideation 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 stuff like DeFi infrastructure, DAO infrastructure, and, uh, the one that got like more popularity recently, which is the Ordinals 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 contracts? 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 Stacks designed for Bitcoin leverages 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 bulk of transactions you have on your L2 have been settled on Ethereum. So something similar to that you have there. And you have the SBTC, which is basically synthetic BTC. Synthetic BTC is just a 1-to-1 backed asset on L2. 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 chains. So one of them is like Ethereum is good, Solidity is good, but it also has its own pitfalls. Some of the pitfalls, you know, reentrancy, underflow, overflow, and stuff 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

8:27 C: Hello everyone, sorry for the interruption, uh, 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 hall very Quiet. Thank you very much. So please move forward. Thank you so much.

8:52 B: So why- what necessitated this design decision for Clarity to be interpreted is the fact that most smart contract languages that you have are basically compiled languages, and that alone introduced new range of threat factors to your smart contracts. Which is one of them is compiler bugs. So Solidity, for example, still doesn't have a stable version 1 yet. Everything is still on 0 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 stuff like that. And this is actually based on some of the trade-offs that come from using a compiled language. And the next thing is that clarity doesn't allow reentrancy. So firstly, how What's the concept of reentrancy? Basically a code calling into another contract recursively. So how- what enables that on Solidity is because EVM is Turing complete. And when you say a computer system is Turing complete, it means that it can run any computation given a fixed time and 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 Turing-complete system. So the issue of reentrancy is already eliminated with that. So this not being Turing-complete is why you now say Clarity is a decidable language. This factor is because when you say a programming language is decidable, it means that just by having the contracts, your code, the functions, or any, any unit of your logic, you can actually tell if it is going to run successfully or not because it has that factor in it. So it is not like in Solidity 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 on EVM. For example, you can run out of gas, you can have a stack too deep error and all those things. All those like limitations and challenges that you face on Solidity has been eliminated here. So, and what that means, and also another thing in being a decidable language, is that from your code you can easily see the static analysis of any units of your code. So one of the things that we do on Solidity to assert guarantees that this smart contract is safe formal verification. You don't need that with Clarity. So that means like your development time- timeline can be reduced by using this. And on Stacks, transactions are secured by post, uh, conditions. So, uh, So as opposed to Solidity, where you have a statically typed language, Clarity leverages on LISP. 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 2 first plus the, um, number you want to use, but here your operands has to come first. and the input comes later. So like looking at this one, the operand here is the plus sign. Sorry, is it clear enough for us? I should zoom in. It's clear enough? Okay. So this one, you have your operand first, then you have the U2 and U3. So this U2, U3 is the equivalent of unsigned integer. So the difference between this is that, um, for Solidity you have uint8, uint16, sorry, like that, to 256. But here it has fixed size for the uint. So what's happening here is that you are having addition of 3 and 2. The U before it is the data type. So basically, this is just a simple addition, and if I run it, it should give 5, u5. So you have the data type before it. So, um, I'm just covering like the primitive type, just like you have in Solidity. You have integers, unsigned integers, Boolean, and addresses too. So addresses on, um, addresses on Stacks, they are called principals. 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 subtraction. This one too is for multiplication and division. So I think, um, so one of the tools that you have access to is ClearingNet. So the- just like you have ADAT and Ganache stuff on, um, on Ethereum, you have this. So just give me a moment, please. Uh, 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 Devnet running where you have access to, um, accounts funded with Theta and stuff like that. So also on Clarity, on Stacks as a blockchain, the development environment is called Clarinet, built by Hero Systems. The hero is hiro.so. You can just check them out. I'm going to cover it soon. So ideally, remember when you write your smart contracts on Solidity, how you compile them is use maybe Forge build, you compile them and stuff like that. Here leveraging on ClarityNet, what you use to do that is ClarityNet check. So the, for JavaScript, TypeScript developers, you also have some advantages here in the sense that you write your smart contracts in ClarityNet. 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 into like maybe process of like deploying, this is just like a simple Clarity contract that I think I used before. So this is just like a token streaming contract. 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, Bano. So the way you have error codes on EVM, you also have them here. This is how you write your error codes. This is 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 are writing Clarity. So just, uh, it's some of the new things you have to like maybe add to your stack 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 principals. Your uints, the idea you have there still applies here. So this basically here is like on this side, it's similar to what you call structs 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 a Superfluid, Superfluid on Ethereum. So similar to that where you are. So this one, I need some parameters and this way I just define the structure of what I'm going to expect from people and stuff like that. So this one is to create new stream and so So I'm not going to really go into the- or should I go into the code base to explain like the different variables, or 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 hear you. High-level overview. High-level overview. Okay. All right. If you have- okay, let me quickly do that. So if you want to get started with, um, just things. So to get started with this clearingnet, just, uh, come here. There are a lot of tools. We have the CLI package, the Clarinet SDK that you can leverage on for different things. So, but majorly, like development environment that you are looking for that is similar to Foundry that works for, um, this part is the Clarinet. So just get started here to deploy it. So it is quite easy once you have Clarinet installed on your PC. it's quite easy to run everything around deploying, testing, and stuff. So the first thing after installing, this command that you have here, clarinet contract new my contract. How many of us are familiar with Foundry? I want to use maybe Foundry terms or to explain to us. What about other- hmm, 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 files, right? That's the same thing you are going to do here. So you do clarinet contracts 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. Um, So what you have here It's how you initialize a new contract. Oh, sorry. What you have here is how you initialize your project initially. Clarity new 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 Clearing it, each contract file needs to have a unique setting attached to them. You know, you can have just like in a single folder, you can have like multiple Solidity 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, clearing it, 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 contracts. So the first one will just give you a dummy stuff here This is the thing. Okay. So when you have this, I have one of these, then in the test I have stream test. So let's just 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 clearingnet contract. So I want you to take notes of what happens in the contract folder and the test folder also. What should we call the name of this contract? Let's say Web3 Lagos. 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 Devnet and the Simnet parts. So this part is where you had configurations for your deployment environments. So you have the option of having it both on your local and this thing. on your local and on chain too. So generally, the development workflow creates your contracts, then you do Clairnet check for the compile, then let's deploy. So let me just quickly show you one. My time is far going fast. So here now, currently I'm trying to initialize a node on Bitcoin. So what's currently happening is booting. I'm going to try to deploy the contract that I have here later. And the good thing about this is unlike Solidity and stuff, um, Foundry- Foundry gives you some access to see like stack trace, but here I can see how as everything is going on into the mempool. 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 initiate. Okay, almost ready. So what the command I just wrote now is what's going on now. It goes, it goes on fast. I 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 some of the resources you can use to get started. And currently, 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 Stacks ecosystem. 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 should be randomly selected 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 will find a way to get share some of the materials across to you and how you can get started, participate in the pool that I'm talking about. So, okay. Have you all make use of this? So, I can take question, right? Do you have questions?

30:42 D: Thank you for your talk. So my question is, um, is there a better way to like just migrate my Solidity contract to- is there a plugin or you're saying something about the SDK? I don't know. Move your contract from Solidity to Ethereum.

31:01 B: So, 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 Solidity contract is meant to like be compiled into bytecode, 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 not changing to bytecode, no ABI, no need for that. So you just have to make use of that. I think someone was raising their hand there for a question. Okay.

31:45 E: Pardon me, I'm not sure if you covered it, but does Clarity offer test suites like the same way you carry it out with Haddad and Foundry?

31:54 B: Yes. ClarityNet 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 from the console? That's also factoring in how you test. Okay, so that's why you test in TypeScript. You write your code in Clarity.

32:15 E: Okay, so testing, writing interaction scripts, and deployment, everything is covered?

32:20 B: Yes.

32:20 E: Thank you.

32:25 B: You say- sorry, do you have more question?

32:43 A: No?

32:43 B: It's working. Is the phone working for us?

32:48 A: Yes, it is.

32:51 B: All right, so, um, that's the end of my talk.

32:56 A: Please, a round of applause for Mr. Abid. Okay, great, um, great, um.

More from this hub

Something wrong, or a video you want off this page? Write to wethem.xyz@gmail.com and it is fixed or taken down within 48 hours.