Rust & Solidity: Interacting with Solidity on EVM chains from Rust
Creating Solidity Dapps using Rust · 22 Jan 2025 · 34:38
Serge Radinovich and Peter Robinson explain how to create applications in Rust that interact with Solidity smart contracts on EVM chains. The session comes from the Ethereum Engineering Group's online talks and is aimed at developers who want to bridge the two languages.
Speakers
Topics
From Creating Solidity Dapps using Rust, hosted by Ethereum Engineering Group.
Transcript
hello and welcome everyone my name is Peter Robinson and this is the ethereum Engineering Group Meetup so today Serge and I are going to um chat about rust and solidity um and so I don't know bit of background on myself worked at a mutable head of blockchain research for about almost two years before that consultant at Unis swap before that worked at consensus helping to um get the prodical Engineering Group happening um so that's me Serge can you introduce yourself please yeah definitely um Serge renovich staff software engineer at mutable I work closely with Peter um yeah been at amable for just over
three years um most of the work we do is is in goang but Russ is picking up a lot and um over the past year have been um spending more and more time with Russ so quite Keen to um share some of the stuffff and get some conversations going around these capabilities cool well can you share the slide Serge and let's let's take off sounds good how does that look that looks brilliant and the first slide says it all um we're now up to seven years of the Meetup talks um we've had 179 talks talks there have been about a dozen
short talks as well and there were about half a dozen conferences supply chain and blockchain cross train Workshop um that we've run as well so um it's been huge but let's keep on going for another couple of years at least next slide please and um yep so today we're going to talk about creating solidity daps using rust and so next slide all right so you know we're going to talk about how rust and solidity work together and we're going to dive into the code because we figured that that's the way to do it um but um we're we're leveraging we're using something that
was created by other people so George James and Danny uh from Paradigm and probably a whole stack of other people have worked on alloy which is the set of libraries that make it all happen so you know we've got to thank them uh next slide please so I started thinking oh look I'm going to you know come up with this big review and then I thought Google's got this AI result thing which is sort of good on many things so I just put it in you know banged in you know rust what is alloy because if if you just go rust you suddenly end up looking at metal that's all um ready and if you a ditto for Alloy so if you put in that search term
you'll get this sort of result and so um alloy is a rust package so what it says is pretty much correct um that allows you to connect your rust application to a blockchain to ethereum and um so it's it's based on ether RS so ether RS was something that the guys at Paradigm were developing and then they said uh we've got a whole lot of rough from this early development that we're really not happy with and so they did a bit of a rewrite rework and released it as alloy so there's um you know like a whole lot of new new documentation and a few new features as
well so alloy allows you to connect up you know you your um application to ethereum and it's got all the types it's got rlp encoding it's got RPC it's got you know it's got everything you need uh next slide um and and um you know so you can use it just by integrating it into um cargo which is you know no surprise for anyone who's developed stuff in Rust uh next slide and it lists out some of the crates that are there which is a whole stack of crates but an interesting one at least for this talk so next slide is that there's actually a soul
macro crate which um gives you the ability to interact with solidity that the AI didn't return which I found curious so um I guess that's something to um note for AI results is most of it will be true but it might not you know might not all be true and it might emiss some important result that was actually the thing you're interested in um next slide so um we've got our our example code is here and I'm going to hand it over to Serge to talk through um the next part for the talk thanks Peter um yeah so we have this rust workspace um in the repository
here um that's kind of like a small playground that we're going to use to demonstrate some of the capabilities um of alloy and specifically around that Soul macro um that Peter mentioned um so if you do want to have a play around yourself um you can go ahead clone the repo and install some prere because it's technically you can actually avoid these like you do need rust um that's implied but you don't necessarily need Foundry adjust if you if you don't want to and I'll show you why but certainly get those going um highly recommended um and we'll show you how we use them in the project um just briefly yeah you don't need to use Foundry or Forge because we've kind
of just baked in a contract for us to work with um if you did want to generate your own contracts um you would have to build them yourself with um which you can see how we do um inside the solidity repo and it has some instructions there as well but if you if you just want to jump in and have a play around you don't need to do that um so we're going to be demonstrating how to deploy a contract and interact with it um uh against some ether RPC backend and we're going to use a mutable ckvm testnet um as the RPC back end today the contract is super simple just a typical counter contract um I would just highlight um things we'll see later on is like this event um
that a number has changed when we have called the set number function uh for example and you know the increment function um we'll see that come up as well but super simple stuff um okay so let me get into the code or like let's do a demonstration you guys should be able to see my um IDE now so I mentioned just as one of the dependencies if you're not familiar with just just files they're just like a alternative to make file make file has been around for a while and some of the like scripting is pretty obscure the way it works anyway um just file is just a way for us to run these commands and
like we're not doing much here we're running really simple commands we're just using cargo run so if you don't want to use just file you don't have to um just noting yeah like the environment variable we have that loading into the just file is just for rust log so um nothing too crazy so um uh the main the main things we're going to do today is deploy and interact with the contract if you don't have a key store file you can also use this other binary in the binary crate um that we have in the repo um if you have a private key and you want to pick a password you can use it to generate a key store that you can use with the rest of the setup um and the rest of the setup just
um it has a confli file um let me know if you want me to zoom in any further by the way um but yeah it just has a simple confli file for your key store file and your password file that you want to point to um so this is kind of like an overview of how this stuff works and we can just go ahead and run it now um so the first thing we're going to do is deploy a contract um and we'll just see what the output looks like when we do this I'll just try to highlight it as it goes um so obviously reading the configuration and setting up a RPC provider like I said we're using um immutable test net um and we deploy this counter contract to it um we get the
address that's the newly deploy contract went to and then we make a few calls um so number to get the number and increment I kind of talked about those previously in the um solidity code so yeah so now that we have the um contract code I'll just steal that and um the other kind of binary that we have um will take that address and instead of you know deploying it it'll just hook into an existing contract and it'll do more or less the same stuff um again I'll try to highlight some um it'll connect to the contract and then the what it's doing differently here just for the sake of demonstration is
also listening for that event that we looked at um and making a few other contract calls um so the reason we have these two separate binaries is just to show you the two main and two different ways you can use the so macro um to achieve all this um so we can start looking at the code if if you're not familiar with rust um this kind of thing can be super overwhelming um so look just I would yeah I'd say let's just focus on the alloy specific stuff um there's a lot going on here around like the tomel paing and like uh async and Tokyo um we
can try to ignore all of that and just hone in on you know what we're doing with alloy um and you know the first thing we are doing is constructing a wallet um that contains our key store um or that is created out of our key store um and we're feeding that into um sorry just a bit jumped a bit too far um feeding that into this provider um that's talking to the RPC back end um and so here like I said just uh we're talking to mutable testnet um yeah and fitting in the wallet um we're using recommended fillers that that's kind of like this this um provider Builder um provides you know default ways of constructing uh the
provider and if you wanted to do like uh custom nuns management or you wanted to manage the priority for yourself then you would either use some different specific pillars or um yeah just do it more manually but for our purposes um we get away just fine doing it this way um yeah for example for like the tip cap um this client will use the eth fee history end point to figure out an appro priety fee and you know that that should work pretty fine for most use cases um so now we have this provider um yeah okay um again yeah like uh Russ can be pretty unforgiving with
the amount of generics and the uh the types involved so I'll try to avoid um as much complexity as possible um but the rest of this file specifically so this is the file that we use to deploy the contract in the first place the rest of it um sort of uh it leverages this struct that Peter and I just put together to like wrap up up the the interesting stuff that we're about to get into and um it's just theoy in the contract in ping these functions but we can drill into the interesting alloy stuff through this structure here um yeah and so this is this is the structure I was talking about counter deploy um it contains this token
contract which is this is one of the main things coming out of the Soul macro that we're doing here so I mentioned we're going to use we're going to show you two different ways of using the soul macro um the first one we're doing here is that we're feeding in a well actually I'll take a step back and talk about macros more generally um but just to finish this thought um we're feeding in a Json ABI right so we've built this ABI out of the slly source code using Forge and now we're feeding in the file um to this macro so um yeah rust macros rust macros are really powerful feature of rust um and a really complex one um they are
about code Generation Um metaprogramming in the sense that it's code that creates more code and um yeah the macros are going to be basically expanding some code into um another set of uh code that the compiler is going to use um so these macros they're essentially functions um that expand code but the difference between them and typical functions is that they run at Capal time um so they are really powerful but they you probably have a LoveHate relationship there's like some different cons about them in terms of usability and debugging and trying to understand them but they give you um a lot a lot for free um and one of
the main things that they give you is avoiding having to write um boilerplate and we're going to see the amount of boiler plate that this macro um generates for us um in this case and the other aspect to macro is that it is kind of really powerful for us in the context of this also and we'll see this in the other file bit of a spoiler is that we can we can supply macros with like domain specific languages to generate rust code from and in this situation it's going to be solidity so um in the other file where we interact with the contract where we don't deploy it we'll see how that works um yeah so in this macro we're specifying a
module um that's going to contain all the generated types um and the main type that's coming out of it for us to like interact through is this counter instance um and if you just read the like this is the this is the some of the code that's generated and I I'll show you what else is generated um but you know contains a type safe type safe methods for interacting with onchain inance of counter um located at a given address okay and given a provider P so this this struct depends on the RPC provider we were implementing um in the other part of the stack um so yeah I did mention this
thing this macro does a lot of Border plate for us and we can look at exactly what that is um if we expand it um so usually you won't have to do this yourself um but if you're interested or deug or something's going wrong you can um and like this can be really garly code but um let me just make this so like this is straight away we can see it's made you know 1,200 lines of code for us so that's the kind of bll I'm talking about that it's going to build for you and the so macro if you look at the alloy crate docs um on crates IO um it you know the main thing it does is it generates types rust types that you can use um with alloy
capabilities these rust types um Implement traits that the alloy crates rely on to for example encode and decode uh types that the um uh that the smart contract will expect you know so the ABI encoding of those um obviously not going to spend too much time in here because it's you know kind of scary um but some things to look at you know for example what is generated for us is this some type this en that contains um every function call that the contract um actually has and so for example we talked about set number and
getting the number incrementing it those are all there and these enums they're all um composed of these um other strs that are again generated by the macro and these strs are the ones that Implement that those traits that I P about so I think for example over here one of those structs has an implementation of Soul interface um and Sol interface is one of those alloy types that it relies on yeah so that's just kind of a little bit of a deep dive into what's going on under the hood um still kind of scratching the surface you in the happy path you really won't have to worry about this and this will all um just magically happen for you when you're using the um the macro um so before we actually have this
counter instance we've got to create it uh the way we create is through a kind of static function coming out of that counter module um called deploy you know and we can guess what this does it takes a provider RPC provider and actually deploys the contract to do this um you know we have to have provided the ABI of the contract naturally and we did that through the Json ABI above um another way to do it you can um you can specify B code here so there's like these attributes to the soul macro um bite code is one of them RPC is the other one you definitely need RPC if you want to do um anything with the RPC back in so um that's something to keep in
mind so at this point here we've actually deployed a contract to the chain and we have um a handle to it in the form of stoken contract here and for type um you know uh in line we can see that's a counter instance as we expect um and then the rest of the functional is just the functions that we see in the contract so set number um this is a rust implementation of the Sol function that we're expecting and you know it takes in a u26 that's one of those alloy types you know Peter talked about alloy providing a bunch of stuff um and alloy Primitives has this U 256
right and so you can use these functions to create these po Builders and just really you know um or our we're just going to call send on those and um they also implement this um watch capability that is a tidy way of you know waiting for um the transaction to have been confirmed on the Chain so it's a pretty tidy way to do things um it's been a while since I did this and go but I definitely recall more writing more code to implement the the watch aspect of thisness yeah so that's most of that one um the deployment so so that's that's
deploying the contract the binary that interacts with the existing contract is pretty similar um you know the one thing it does differently is that it waits for or it looks for events um so yeah without getting into async rust um we've got a task here an asynchronous task that basically watches um for events coming out of the contract while we're also interacting with the contract um and we do that through this um function another helper function that we've created over here um and again you know this is all alloy types that that it generates the soul
macro generates for us so number changed that's our event and then this function is generated for us in the in the um token contract type and um you know okay yeah forgive me that gets uh pretty busy pretty quick but you can turn that event filter into a stream a stream is just like an asynchronous iterator so what we do with that stream is you know at in this function we're just waiting for the next one because we know like this is a simple invation we know there's going to be one coming up and we await it um but the the difference with this um so this is the second approach with soul the macro um is this
time one we're not providing any bite code um because we're not deploying the contract and two we're kind of inlining the solidity um and the the macro is turning that again into those similar types that we saw before when we expanded the macro um in the original in the original file um so the thing that comes out of this is very similar but um this module it won't have the deploy function you can't deploy this thing so when you go to construct your instance this time it's got a Constructor called you know typical new and it takes in a an address and that's how we get our handle to the contract this time and the rest of the stuff is really um the same apart
from you know we added that event um event watching Loop um so that's like uh more or less everything we did here the alloy GitHub um repo what is it examples it's called examples it's like alloy RS on GitHub and um they have this repo with really exhaustive examples of how to use alloy so um if you do go to start doing this stuff definitely work from the happy path provided from these um especially when you're dealing with macros um as the advice I'd give um and you know in here you'll see
them using the soul macro uh and I think you know it's more or less the same as as we've shown already so there's an example of that bite code attribute um done alongside the inline solidity um and yeah again same thing and I think somewhere here they have like a struct definition as well function yeah but more or less everything we've already okay some more events and stuff yeah so definitely take a look at that um repository and work from
it um yeah so kind of wrapping up um to summarize you know we've shown you the two ways you can use the soul macro um one being the Json Avi and the other one being the inline solidity um and in the examples rep you could kind of see them being used in tandem especially if you want like um if you want named variables in your rust types that's something I didn't really show but um if you do want named variables in your rust types um you'll kind of have to provide the inline solidity otherwise you'll be referencing um variables by like index anyway something you can see
in the code um yeah talked about the bite code definitely don't forget the RPC attribute um in your soul macro um and yeah just generally you know use the right tool for the job rust you know I love rust or anything that's going to you know any system that's going to wake me up um at night if it goes wrong in production you know what I mean um but it is it's um it is a complex language it's very strict um you know a good way if it's if it's if it's the right use case right but um yeah you can see like you know you can compare this to other languages and even like go with go generate and decide what you like really um yeah so just list of links
here um definitely would recommend starting from the examples Reaper just clone it down get it compiled um run some of it you can run any of it really really quickly um the the way it like leverages anval like your local chain setup is you know really handsfree easy to use so you can get started really quickly experimenting with the stuff with that repo High highly recommend it um yeah and that's all from me now I just need to go off mute yeah thank you very much Serge um all right why don't we do this part and then we'll answer the questions that are in chat CH because this will
answer one of the questions in chat and it's not about the merch though though please you know want a t-shirt good location all right next slide um uh one before this one oh sorry yep all right so um next fortnite we've got Jen yangang presenting on his ethereum client that he's created then we've got amus telling us all about data availability and then we've got Arana who's talking about her U methodology for analyzing and visualizing blockchain transaction anomalies and essentially doing anomaly detection uh next
slide so if um yeah if you're watching today this is on um going to be on YouTube and if you're watching this on YouTube you can join um and then ask the tricky questions live so if you want to contact people so there was someone in chat who um is doing some research into cross metaverse um avatars and wants to chat to people about their thoughts on that I'd say the best idea is join the slack workspace and then put in the general thread in the general um Channel hey I'm doing this research is anyone interested
and there are a whole stack of people who are have been involved in the blockchain space for years there and so you might be able to tap into some of them the example code that Serge has gone through is in that um repo down the bottom SL rust okay so that's the end of the slides so let's quickly scour through the questions again um y so we had Cobra talking about the cross chain Avatar so uh or cross domain cross system Avatar so I'd say please reach out on the slack I think that's probably the best way to engage with this group so so Rob you asked about what is the advantage of doing
this I wasn't quite sure what you were talking about um yeah so I was curious uh like uh why even we would like to do this or is there any specific uh advantage of using rust uh for interacting with uh you know the evm chains like already yeah yeah yeah yeah I understand all right I understand what you're saying um so what what so I'm going to give my take and then I'm sure sech will probably do a much better take but M if you're developing your application in goang or in jav typescript or something
you're not going to write your interaction code in right R but a good language to write say some backend code would be rust and so if you're developing a rust application and you're thinking how on Earth do we interact with the blockchain then this is it um but I don't know sech what what why would someone use rust yeah it's almost a question of Faith no um the main for me the main thing about rust is the safety um so um I do you know I do mainly backend stuff so it tends to be goang um both in web 3 and kind of like General web to
distributed um systems but with goang goang is a great language easy to use but you can get runtime um errors that are completely impossible in Rust so rust's compiler is very strict there's like these ownership models um for every variable and the compiler um keeps track of that so you can't have things like dangling pointers or free um that kind of stuff that you might see in like C++ c um I don't know even Java it's been a long time look at Java um so I don't know how JS behave but um the main thing for me with rust is the safety and I mentioned like if I have a system critical component that needs to talk to um some EBC like I'll sleep better if I know
it's implemented in Rust because I know that these runtime erors are not possible um yeah that that's the main thing to me but there's you know there's a lot um that you could debate on that topic yeah yeah yeah yeah yeah it's backend languages is a is a big one I mean I remember someone saying can't remember who um but they they said look the good thing about rust is that if you write your code um and you know it looks right and you and it compiles because getting the code to compile can someone times be the challenge but if it compiles chances are it's going to work and it's going to be
pretty bulletproof um so obviously you know it's got to do the right functionality but it's sort of um yeah doesn't have a lot of the issues of other languages all right so George had a question and he said um was there a solidity compiler version in there somewhere um you know especially when you've got say an interface or something and you don't you know like don't have everything from the jonon file so the answer to that is as I said in chat is no there's no solidity version and so I think the idea is that they're using a recent version of um the compiler but really all they're doing is they're looking at the solidity code and
creating ABI they're not actually creating bite code and so hence they don't have to worry about the compiler version they're just trying to use I mean as long as ABI 2 is supported they're all good so that that that's where that that's up to um yeah and I think that's the last question in chat um so does anyone have a question that they want to ask live I have one question U not sure it is covered so now we have two approaches right one is through Json and another one we are specifying the Prototype
function prototypes so now in which scenario which one to use uh yeah oh yeah go ahead Serge yeah I mean the main the main thing um that I'm aware of at least is just providing the bite code to be able to deploy the contract um and you can on top of that do the inl solidity to help the um the macro generate um variable names for um like variables in your solidity interface so like something returned from a function um if you don't provide lat in line solidity then the the
variable and the rust type won't actually have the have a name it'll be like an underscore zero or you know one based on the index that is okay and I think another thing is so which is highlighted so um we didn't show it here but previously we played around with Unis swap um contracts and erc20 contracts and the you can only have all that big gyon file if you've got the code the source code and you can compile it but so if you just are going to interact with one or two functions in a in code that's got like a hundred functions and all sorts of stuff happening then it's much much easier just put you know like that little bit
of code in that little interface in rather than um you know and and you might so you might not have access to the Json file itself but as well you might just be interacting with a little tiny part of it so maybe it it's just simpler to just have that does that make sense yes yes yes yeah thank you and does anyone have any other questions all right well in that case um see you all in 2 weeks time and look thank you Serge for partnering with me on this it's been awesome and we're
going to have to partner on the next talk that we've talked about but is currently secret um but anyway coming coming to you all in about I don't know what three months six months time do you reckon Serge the promises we'll see how we go see how we go yeah there's a lot of like all good talks there's a lot of work to be done before we can talk about the great work we've done all right talk to you all later bye-bye
Related talks
- "Web3 with Web3j" - Peter Robinson, Conor Svensson & Dan Shaw
- EVM: From Solidity to byte code, memory and storage
- Accelerating DeFi Innovation: A Single Platform for Smart Contract Development
- Solidity Development with Foundry: Cast, Anvil, Chisel, and Forge
- Build Cross-Chain Ethereum Applications With ICP Chain Fusion | Kristofer Lund - DFINITY
- Foundry, a portable, fast and modular toolkit for Ethereum application
- NoirHack 2025: Official Kickoff & Intro to Noir
- Thomas Hodges ~ Build Smart Contracts that Connect to Real World Data, Events & APIs
- 'Radicle: Towards decentralized code collaboration' - Abbey Titcomb
