Smart Contract Development with Vyper, the Pythonic Language - Mladen Milankovic | HTEC Group
ETH Belgrade Community·Sat, Oct 7, 2023, 12:00 AM
Smart Contract Development with Vyper: A Deep Dive into the Pythonic Language - Mladen Milankovic | HTEC Group
Transcript
foreign [Music] but you will see that so uh what is the big thing that we always say in the blockchain space is do your own research you should check the community the founders everything around them see if it if they are okay but yeah scammers are doing a good job to do everything about it to uh so that you believe that they are real and the only thing that is valid in this space is that code is low whatever is in the code will be executed we cannot say people the to the masses to go and check the code why because we are Engineers we have been trained to do things and think differently we have design patterns that we learn and when we see something we can say this is this design pattern and I know how everything is done in the files and I can read that what would you say if you go to a lawyer he goes to your contract because you don't understand it of course and say this is my opinion but you should do your own research and do things based on upon that it would say that this wasn't worth your money this doesn't know every doesn't know anything and what is this legal documents contracts are written so that many people can read them turn to a lawyer when they have ambiguity ambiguity in it and to see how that is fit in the big part of the law so for this a group have come together vitalik has started it and started a new programming language that is working on evm and that would be Viper what are the goals of Viper is to enhance security and not just security as in the blockchain that it's immutable that yeah only one key can open a token and things like that but security for us Engineers because we are a clever bunch and we can write really good code and really make it complex in the code and things get hidden in it so we wiper is made for Simplicity there are no complex things things in it and you will see that later and this is all made because of readability because I'm a python developer I come from there and we have a mantra and part of it is readability counts so Viper contracts are made for audibility for anyone to come in to read it and to grow a trade what it is not and it's not its goal is not is to be a replacement for solidity as we say the right tool for the right job there are things that vipre cannot really do and the solidity might be a better option and for some things now it is a better option and solidity has been made at least what I think is for the simple reason that you will have a developer who can do everything he can do front end in JavaScript he can do backend in node.js because it's JavaScript and so it is like JavaScript so that it's familiar to get in as much developers as as they can but with that they invited Developers who had experience with systems where they don't really have to think about memory allocation what the user will do if it goes into Infinite Loops things like that and they put it into put them in an environment where it's evm it's a virtual machine running where every little thing counts before we move forward let's just go briefly through what smart contracts are for those people who are not really into everything here so they just execute functions on the blockchain and change its state they have to have a caller must have a collar so they cannot do things just by themselves on some conditions why we like them is that the infinite possibilities that interaction between smart contracts give us and of course they're immutable but whatever you write I put it on the blockchain will stay there forever so if you write code that has a has an issue it stays there you can there are techniques where you can reject the user to another smart contracts but the old smart contract is still there what features does Viper give us it is evm uh it's running evm so it is compiling coding to evm code it is pythonic as a python developer for me python is the most the language most close to the English language and the most easier to read it's a strong typing I'm a python developer as I said many times I I like that typing and I was like why strong typing but at the end of presentation you will see why they went for that route but let's move forward so strong strong typing is there it has to be decidable so whenever you want to call a smart contract a function and a smart contract you need to be able not to simulate it you need to be able to tell how much it will cost how much gas you need to send in to the system for the function to be executed and for this reason it has to be decidable we have bounds and overflow checks on arrays and the compiler code is small and understandable you can even go into the compiler code and see it for sale for yourself how it is working how it is compiling the code into bytecode let's look let's take a closer look at a simple smart contract written in Viper what was strange for me at first because I'm a beginner so I didn't write wiper smart contracts for years and years and that's why I think I'm a good fit to try to explain to you what it is doing and what it cannot do so what's strange for me is that there is no class definitions when you write a smart Contracting solidity what you do is create a file and you say that it's a contract called this and this and then you have the whole code intended from there and you have an intimidation at each line for me that was like redundant because the file is already telling you what the name of the contract is so they removed that there is no definition like a class definition but you still have a self if someone is a python developer do we have python developers here yeah back there whoa so they know that that self is usually the keyword that you are using to refer to an object to itself and they kept that here so but the self is referring to anything that is in this file uh defined so the functions the variables as you can see we've defined some variables outside of these functions and those are referred with self there are also ways to add interfaces vipre has some standard ERC interfaces and it's predefined so we can just Define and import it and tell that this will be this will be implementing interface and in that case whenever we want to compile it it will tell us this function you are missing this function is not defined doesn't have the correct parameters and things like that so you can be sure that you are implementing a smart contract that is uh according to the standard as you can see in the function Clause there are some variables defined and what uh what is different from the variables outside of the functions is that you have to initialize them in the memory by the storage the variables outside of the functions are storage variables that they cannot be initialized and you still have the global variable message from solidity where you can refer back to the message that was sent into the smart contract when calling a function a bit comparison selective with Viper yeah wiper has fewer resources tutorials so wherever you search for it on Google you will get less returned links than for solidity of course you have limited tooling and Integrations the Viper has some of its own Frameworks and toolings that they that you can use but they have also some integration into Foundry hard hat which is in progress there are a lot of many contracts written in Viper it's like a self-fulfilling prophecy I will write it insulated because everyone is writing it in solidity and then everyone knows it and that means fewer developers also and uh it's a bit uncertain because of this reason about this future future and where it will go and about its adoption there are also some limitations so these are the big things that you can see when you were writing smart contract uh from different from soil so in solidity there is in Viper there is no inheritance it as you saw there are no classes no con no uh contracts defined yeah but you will maybe have modules so there that's a new thing that they are working on because this is good from the readability perspective because everything is in here as you can see this is this is a basic erc721 smart contract it's like it took me 16 lives it could be maybe less but in Viper it's 371 lines because what do you do without inheritance you are going to a Smart contract or a default implementation and copying that into your file so you're just copy pasting it over this has the issue what happens if the smart contract that you copied over headed back had an issue that you have to follow all of these that you want to use and then make the fixes copy over into your code and especially if you change that change the code that you copied you have to in incorporate that inheritances has issues with readability because you can extend a class that extends a class that extends the class and you have a hierarchy and you can have external functions that are hidden somewhere down in the fifth level sixth level so it's for the reader it gets confusing you don't have modifiers from solidity in Viper why because those are effectively functions they are called modifiers that's the keyword but they are effectively functions that you can call and uh what will happen in this case when you call change price it will go to the only only owner before calling the body of the change price function it will check whatever is there that the message sender has to be the owner of the contract and it then it will execute the change price body that is defined in the function this has issues as that function because of inheritance can be defined anywhere and it usually is there is a contract on open sapling for uh vulnerable so it can be down there but you can change it a bit and make the function execute something that the caller doesn't want to do for this reason Viper told this because it's it's usually used for some kind of checks before you run the body of the function that it should be done in line as you can see on the right side when the line and 12. it's just this you're asserting that the message sender is the owner of the uh of the contract that has put up there so for this reason there are no modifiers this is done in Viper to not get into a situation like this where you are going in down the rabbit hole following through all the inheritances and modifiers and things like that you should be able to read line by line the code that is executed there's no function overloading this is just one piece of code retaining solidity I don't have within wiper because Viper doesn't have overloading and what I wanted to show here is that yeah we have a mean function defined two times one is receiving an integer and the other one is receiving a string so depending on whatever you the caller wants to do they might just think I will call whatever function because it's it's mint it is receiving a token ID and if they don't if they are not careful they can probably lose their board API Club monkeys according to this smart contract here well the other function will really mean something I left out all the code for minting it's just the transfer but this shouldn't happen that someone needs to think about it which function to call according to that python little Zen poem in face of ambiguity refuse the temptation to guess so there should be one function that is defined and if another function is doing something a bit differently give it another name there is no inline assembly why because it makes reading the code more complex you need to know a second language you need to know assembly how it is working what are these commands here it's not enough just to know how to reach solidity but another language and it is going down into the details of how something is executed on the byte level so that's not good for readability in any case and for that reason there is no inline assembly and don't give it into temptation I know it's the templation is high to optimize everything out and to spare a few gases for whoever whoever is calling the contract but contract readability is more important for whoever is calling than to spare a few gas there is no recursion right because the recursion can go infinitely if someone wrote it like that and one of the things that Viper wants to give us is decidability we need to know in advance how much gas we need to pay in case of the left side of the solidity code this will go infinitely if this was a uh and another in any other programming language that is running on our computer it would just hang there and yeah when we get bored we can just stop it in case of uh ethereum it will just stop after it's spent all the gas for this reason uh it is forbidden as you can see on the right side Viper even gives you when you try to compile the code it will go into it try to see it and it will give you an error you cannot deploy that you cannot compile this code because it has recursion in it also there are no infinite Loops why for the same reason because yeah we need to know how much gas we can spend there is even there is no vile keyword even in Viper you only have four loops that can go over there over an array and if you want to go a certain number of times you will call the range keyword which will generate for you an array so and it is always working like that so you will always know how many times the loop will go through there is no infinite loops and yeah I know no one no one is doing this but yeah then why should it be there in solidity or in any language that you are using so we are trying to tell people this is a trustless system and yet they have to trust us that we will do a good job that we will with that we don't want right code like that but with that there are malicious entities who can write code like that in Viper we are preventing that and getting back to why it's strongly typed it's because of the memory handling how memory is handled uh sorry so what we also have is that we have statisticized data we cannot have arrays or strings that we don't know the length at compile time we have to Define how long of things we want to have and we must for example in functions Define them by values why we are doing this because allocation happens at compile time and not at run time for this reason the compiler needs to know how big of a memory it need to assign and this is even more gas efficient and let me explain why this is how memory Works in Viper you define a function and in it you define some variables in the memory you see that there is an integer and the Boolean variable first the integer is defined it will assign it to the first slot in the memory I know that the memory doesn't really work like this but this is just an extraction to get the point over and the next slot will be assigned to the Boolean this will be done at compile time so in the byte code the address directly in HEX code will be written there it doesn't have to think about it it will always put it at the same place while in solidity what are they doing we have a free memory pointer because we wanted uncountable arrays we wanted to have raised that we just putting something in and we we don't know how many times we will do that so for this reason this is for example the memory but you have a free memory pointer and the free memory pointer is just the location the memory where it is written in the next location in the memory that is available to write something in so when you want when we want to add something into the memory we want to go to the free memory pointer look what is written in there we have to go to that address that is written in the free memory pointer with write the data in there in that location and then we have to get back to the free memory pointer and update it to point to the next available memory location that we can write into for this reason as you can see just assigning the memory is more expensive to be done and with this you can get a bit of gas efficiency in your wiper code what can I say for the end this wasn't Financial legal advice do your own research we are heading west this is the frontier so I'm just here to ramble a bit about Viper and to make it this easier for you I will give some directions for you you can go to the doc documentations that are really good they're not that long you can just go over see it's really good structured nicely and try to learn from it you can try Live code so you just go this to this address try wiperlink.org and there's a Jupiter there that you that you can start you can write your code and test it live in your web browser it doesn't don't have to install anything there is a learn page also for Viper link where you can go here in you get some assignments and you can write them in your browser it is not really compiling them and stuff like that but it will check your code that you written and you can learn from there you can more learn the most when you are learning from those that have already gone before you curify has their contracts in written in Viper so that's a big big dap that you can go go over there code and see how they are doing things one of the tools that wiper is using is aprox it's a framework uh like hard hat it is made especially for this and you can use that you even have a starter kit that you can just get clone and start working immediately and of course they have everyone has now in the space Discord Channel where you can go on they're really friendly and ask your questions learn from them and yeah that would be it thank you [Applause] any questions yeah there foreign that you think there are so few people using Viper there are many benefits to Viper why are people still using solidity all the time are they lazy no uh I think that's yeah writing game Viper is harder because as you see there is no inheritance you cannot just use open zap play you will need to copy paste that makes it harder they're working on modules which will effectively be something like that you in in you can Define in another file how functions and things like that is still in in development but the main reason they are doing it like that in in modules nothing inheritance because you will in even in that case in your main contract have to have a function that is calling the module so you will have all the functions defined in the uh in the main contract and with that it will still be readable there is no hidden functions through inheritance but yeah so that might be one of the reasons that it's easy to read maybe harder to write so and we are yeah we are the developers lazy and we want just to import something yeah attach something to it and that's it but also it's small so yeah it's the size so so it had a head start it it got big and now it's there and keeping its position but yeah you see graphite even I think that unisfa brought their first version in in Viper and then the event facility I'm not sure why any more questions once twice twice nothing yeah then that's it thank you [Applause]
Automatic transcript — names and jargon may be misspelled.