New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

High Performance Smart Contracts in Rust with Camila Ramos

Ethereum DenverMon, Oct 9, 2023, 12:00 AM

Sway is a rust-based DSL that enables developers to write safe, high performance smart contracts with ease. Sway was inspired by rust and offers stronger security guarantees at the compiler level. This enables a new wave of developers to write production-ready smart contracts by leveraging Rust's safety and compile-time checks, paired with blockchain-specific needs. Learn about Sway's reentrancy guard which makes reentrancy attacks a thing of the past, namespaced storage access, exhaustive pattern matching, and more. By Camila Ramos https://twitter.com/camiinthisthang #BUIDL Palace

Transcript

lot of cool projects that have come out of it but it can kind of feel like a Minefield sometimes for vulnerabilities and sway is a rust-based DSL for the philvian and it's built with Lessons Learned From solidity in mind specifically for a developer experience so really quick what does fuel fuel is a modular execution layer you can execute on fuel and still settle on ethereum and it uses a fuel VM which uses a utxo model that allows for parallel execution so you can have super fast execution for your contracts so when you're developing with solidity you're probably going to use a lot of independent tools you'll probably for developing you'll use hard hat or Foundry or maybe scaffold eat if you're here for that presentation for the front end you might use ethers JS and one three JS for your indexer you might build your own custom indexer or use something like the graph and with sway uh so sway is developed by a few labs and fuel Labs also develops like a whole Suite that's totally integrated with sway so you have a fuel up which is a tool chain manager and you can install fuel core which is the VM core software client and Fork which is sort of like the equivalent of npm you can use it to create a new project with fork new build your project with fork build and deploy with work deploy there's also rest and typescript sdks that are specific specifically meant for sway and a fuel index for service a little bit more about the difference between these languages so solidity you can really just write contracts and in sway there's four types of programs there's contracts libraries scripts and predicates so here's your like really basic solidity contract uh has a public string called greeting and the value is hello world and real quick if you notice that there's just this greeting in here but if you're familiar with solidity you'll know that there's a getter function that is generated for this so you can read the value of greeting automatically with sway instead um it's really like more what you see is what you get you can see everything that's included in the contract so there's no getter functions automatically generated so we have this get greeting function and uh you'll notice that the greeting here is just sort of inside the contract but for sway all persistent State variables are stored into storage block so it's super organized you can see everything in that storage block that's going to be persistently stored and it's not going to be sprinkled in between the functions so you have to like remember where all of your persistent variables are and another thing about this is if you were going to use this greeting variable in solidity you could just use the word greeting but in sway you use this storage dot reading so that helps with namespacing all of these variables so you're not going to going to you know accidentally call something else greeting and forget like oh there's a persistent variable somewhere else that already has that and another big difference you'll notice is there's this like hashtag storage read annotation so all functions and sway are here by default versus in solidity they are they can access storage they can read and write by default and you have to say when they are pure or read-only so that's sort of all these differences here syntactically solidity is really influenced by C plus plus python JavaScript and sway is really close to rust um and yeah and sway the function what you see is really what tells the whole story there are no hidden functions everything in the storage block is what is going to be stored persistently and all the storage variables are namespaced another really cool thing about uh developing in sway is that we have a standard Library so like I said there's different program types for sway you can have contracts and you can also have libraries and they're sort of like libraries how you would think of them in other languages and the sort of generally replaces a lot of global variables and solidity so there's a lot of stuff from the standard library that comes out of the box but you can also specifically import this stuff um sort of like if you know react you can import use State use effect from react um but it's sort of there out of the box right so here's an example of a contract that in solidities that uses Global variables and sway we can use the standard Library so this contract has a an event uses this event keyword and the event is called log and it just logs a number and a string called message and there's a function called logger and it emits this event and and sway you can import a log function from our standard Library and it's just this really simple like log function where you can just log a value you don't have to set up any events or anything one really cool thing about sway is that is just how you can work with tokens in your contract so in solidity you'd probably use erc20 contract from open Zeppelin and you have to use these approve and transfer functions but in sway you can actually mint native tokens that you can just transfer in a call just like how you would transfer eth or Matic on polygon or something so here's the erc20 contract for how would you set this up in solidity and in sway you can import this mint function from our standard Library and just call this function to Mid tokens and the tokens will have an asset ID that is the contract ID and what that looks like when you're receiving tokens so in solidity you can only receive the base asset for payable functions and you can use this message.value Global variable to access the value sent in the call and in solidity because you can send or sorry in sway because you can send any token we have two functions here a message amount and message ID from the standard library that you can find the amount sent in the call but also what asset is that going to call because it could be any asset and we also have this payable annotation here it's like solidity so another really cool feature and difference between these languages is pattern match pattern matching so in solidity if you are let's say you have an enum and you want to check all the values you could use an if else statement or a ternary statement but if you miss one of those options the compiler will warn you but it will still build and in sway if you miss one of those options it won't build it will make you address what all those options so here is what that might look like in solidity you have this enum called weather it could be sunny rainy or snowy and this contract builds like there's a warning saying that you know whether dot snowy is not addressed but it will still let you deploy versus sway will not let you deploy and it has this exhaustive pattern matching system at compile time where you know all of the options are addressed all right and last I'm going to talk about re-entrancy and ranch and sea guards so this is a tweet that says yes your entrances hacks are still a thing and this link to this GitHub repo that has this history of all reentrancy hacks um you know since 2016 and it's quite a long list it's quite a large amount of money that's been stolen because of ranch and Seahawks so this is a huge problem that fuel Labs really cares about um and solidity uh if you're compiling a contract that's vulnerable to re-entrancy you it will compile without any warning and in sway the compiler will detect the serangency vulnerabilities and let you know like hey uh you should probably move this because this is vulnerable and when it comes to re-entrancy guards um in the evm you would use something like Abyss so you can use storage to have this locked variable here and like essentially execute your function within this no Ranch modifier but it's a little bit expensive right you have to use storage so in the fuel VM you can actually directly access the entire execution context of a call which you can't do in the evm so you can just directly look has this transaction called my contract before um so this is sort of what that looks like so this is using a library called Ranchi and it has this Rancho C guard function and inside that Library here's what that looks like it's just this require statement requiring that some it's not re-entrant and it's a little bit complicated here but essentially what this is doing is looking at the call stack and looping through it and seeing if this transaction has already called our contract so we can directly see here if there's re-entrancy without using any storage and yeah if you want to check out more one to one sway versus solidity examples you can check that out with this QR code we also have a really cool developer quick start you can build a counter contract a full stack and with our typescript SDK and we also have this Marketplace tutorial you can build sort of the Amazon like Marketplace this way so yeah thanks um let me know if you have any questions [Music]

Automatic transcript — names and jargon may be misspelled.