Antoine Estienne, Kevin Neilson : Enhancing Ethereum Smart Contracts with Moonbeam Precompiles ...

ETHCC 4 · 22 Jul 2021 · 34:10

At ETHCC 4, Antoine Estienne and Kevin Neilson discuss how Moonbeam precompiles can enhance Ethereum smart contracts, extending contract functionality beyond the standard EVM feature set. The talk looks at practical ways developers can put these precompiles to use when building on Moonbeam.

Speakers

Topics

From ETHCC 4, hosted by Ethereum France.

Transcript

hello everyone thanks for joining us today here in beautiful france it's such a pleasure to be back in person uh i think my last big event was in osaka almost two years ago so antoine and i are here today to talk to you about enhancing ethereum smart contracts with moonbeam precompiles and we'll finish today by doing a demo of pooled staking in a smart contract so first for uh for our road map we wanted to give some speaker introductions uh to speak to you a little bit of a background about moonbeam we'll talk about our staking model and then we'll go into detail on the parachain consensus framework

and we'll finish off with the evm compatible staking demo along with an alice user story and if you'd like to follow along we have a bit.ly so that's bit.ly slash ecc4 so you can follow along with the presentation hi my name is antoine esten i'm a full stack blockchain developer in the moonbeam team i've worked previously at consensus and hypernet labs and i studied here in paris i graduated from min paritech with a masters of science and executive engineering and i've been working for moonbeam as a developer since october 2020 and my hobbies are traveling

and music and my name is kevin nielsen i'm a developer relations engineer at moonbeam i used to be a software engineer at jpmorgan and i have a degree in computer science and economics from dartmouth i've been working at moonbeam since april 2021 and cars and skiing are my two main hobbies so you may know but i wanted to give you a brief background of polka dot so polka dot's a layer zero it's a base layer blockchain that has numerous customized blockchains that are built on top of it and in a sense these blockchains that are running on top they're called parachains

they're not competing for scarce resources the entire network benefits and is uplifted you know kind of like a rising tide lifts all boats analogy um all these pair chains can be completely different and they most likely are so pair chain teams who are building these blockchains they compete in auctions to win the right to operate their blockchain on the network for long periods of time they have a dedicated slot so that slot is reserved for them if there's any network that doesn't need full-time access to the blockchain uh they can reserve uh block uh by block finality

uh and kind of a pay for uh each time they need it polkadot also has a relay chain and this is where blocks are finalized so blocks get submitted from each of the para chains and they get finalized at a relay chain level kusama is the experimental network it's more than a test network because there's actual value behind it but many times teams that want to deploy to polka dot will choose to deploy to kusama first validate their ideas in a real network environment and there's two uh important components that i want to go over before we move on to polka dot and that's the shared security model which i already mentioned but second is a framework for cross-chain communication

so you can think of moonbeam as your on-ramp to the polkadot ecosystem so if there's an asset that isn't available that's on a separate parachain you can go through moonbeam to access it and that's enabled by the framework for cross-chain so next up i want to talk about substrate so substrate is the foundational building block for polka dot and substrate is a reusable and flexible and extensible framework a simple analogy is lego blocks you can you don't have to reinvent the wheel you don't have to roll your own crypto there's a lot of functionality that's

already been built so you can plug and play and choose exactly which features you need for your blockchain now that's not to say that you can't customize it yourself or build your own palette that's open source but it might be helpful to start to use one that already exists and these are modular right so you can add you can take away you can substitute all right so our project moonbeam is a parachain as mentioned previously and our main goal is to be ethereum compatible we want ethereum developers to be able to go to our power chain and the movie

is as simple as just switching network uh the rest of the user experience should be exactly the same so we're actually uh forked out of frontier which is an open source project that that was created by parity tech so it's open source so we contribute to it regularly and we maintain it because we want other blockchains to be able to use that base layer of evm replication on substrate the reason why we chose to do a power chain is of course because we think that the polkadot ecosystem brings scalability and interoperability you might be wondering why do we have frank sinatra here it's because he sung the jazz standard

uh polkadots and moonbeam which gave the name to our to our power chain so that's a real reference to the relay chain per cadet next slide so here we see a diagram that shows the different layers that make our product the moon beam power chain so at the bottom you see the evm layer which enables people to deploy 30 smart contracts on our power chain as if it was ethereum so that's something that a lot of power chains are doing but what we wanted to do is basically do the hard work of completing the the exact experience of

developing depth on a power chain so for example and the next layer is the web3 rpc we wanted the rpc api to be exactly the same so when you're using web3.js or ethers.js or any rpc library in javascript to develop your dap you're actually interacting with the rpc and so you can just smoothly use those libraries with our power chain because the rpc interface is exactly the same here you see the unified accounts what does it mean well kevin is going to explain that a little bit later but basically we really worked so that you're only using ethereum addresses and you're only using

ethereum type crypto libraries and finally the cherry on the cake last but not least are the defy integrations because i think what makes the ethereum ecosystem so great is that when you come in as a developer you have access to all these great tools for developing for infrastructure and we have a developer relations team that work really hard to integrate those tools with moonbeam they're already live and deployed so i'm talking about the graph channeling chainbridge sushi swap idex and many more so i think that's like really what makes it really smooth for ethereum developers to migrate to moonbeam

so this slide shows a generic implementation of an evm on substrate so it looks very complicated and that's because it is if you have a generic implementation of kind of plug and playing with the substrate pallets so if we plug in an evm to substrate right first we have an ethereum style address that allows you to control any contracts transactions that are on the ethereum side you have a private key but you also want to interact with functions that are in substrate right and what we can do is we can what you can do is you can take your ethereum address and you can map it to a substrate style address right but the problem there is that you only have access to read only

methods and you don't have access to do any interacting with substrate such as democracy or staking and that's quite an important missing feature so what you then do is you create a substrate address so you have a second private key and so now you have to manage an ethereum key you have to manage a substrate key and it's all quite confusing in terms of which one you use instead with moonbeam we have a unified account structure and what that means is that you have an ethereum address that controls both and so you can access your transactions and your smart contracts on the evm but you can also participate in staking governance and everything on the substrate side all right so we talked about how we

worked hard to just replicate the ethereum user experience and developer experience now let's talk about the real difference that makes the topic of this talk so here we have a diagram that shows a little bit the infrastructure of polkadot so in the center we have the relay chain and around it we have all the parachains are connecting to it so the way it works as kevin explained previously is that the power chains are going to propose blocks and uh so the the actors the notes who propose blocks on the sparrow chains we call them collaters they're basically the equivalent of validators for pirate chains and then on the relay chain we have

validators that will take those blocks and validate them so that's why a lot of the security is centered around the relay chain because the validation is really uh the heaviest parts of the the security of the system and that's why we have a very thorough proof-of-stake consensus algorithm on the relay chain but the thing is of course we also want security on the collators why mostly because we want to be censorship proof because if an actor were able to predict which collateral were to propose an x-block that would open a new attack vector for example for a front-running so we were thinking

how do we create an incentivization mechanism that securizes the power chain well at first we're thinking of just copying the palette so the the module that was developed by parity called pallet staking but we realized that it's actually a little overkill for a power chain because it implicates uh really a resource incentive algorithm of balancing that we don't need on the purchasing so that's why we decided um to uh to create our own uh palette that we call power chains taking and we are asked a grant from the foundation because we believe that this is work that could be used by other power chains

so on the next slide uh we see uh the face of amar who is the great developer who's currently working on the the power chains taking implementation so if you are interested i recommend you check out his blog post where he explains in detail why we need a different palette for power chains and next slide i'm going to explain the big lines of this consensus algorithm we don't want to get too much into the detail but what you need to understand is just like the the relay chain is taking we have shared risk and reward between uh collator and nominators that means that

you can nominate a collater without being a collateral yourself and therefore put your money to work if you trust some of those collaterals the second point is that there's a parameter that we call around and that's currently set to 300 blocks and the idea is that every round the block authors who are basic so collaterals who are selected to produce a block are rewarded and there are four denominators too so that concern reward creates inflation something that is a parameter that we can also change through governance basically all the parameters of the planet are changeable through governance

so moving on uh to the next slide uh for parachain staking uh how do we decide who gets to produce blocks right and the first step is that we have a candidate pool of collaters and these are all the folks who are meeting all of the requirements to be a co-later and the the requirement is a minimum amount of bond and that's a fairly it's adjustable by governance but this is the absolute minimum requirement but just because they've met the minimum requirement uh does not mean that they're actually eligible to produce blocks so how do we go from meeting the requirements to choosing who actually gets to produce the blocks the first thing we do is

we choose the top 32 candidates by total stake so total stake that includes the call later initial bond but it also includes any nominators who have pledged their stake they've said that they believe in this co-later and that they will share in the rewards of that of that co-later so the top 32 candidates are chosen this is a parameter that's adjustable by governance but it could change to be more or less but probably more um so once we have these selected candidates right uh and also just want to mention that uh i i mentioned nominations right but how does that work exactly

so folks who don't have enough steak to be able to be a collater can be a a nominator and that has a much much lower minimum amount so moving on once we have our 32 selected candidates right how do we choose the actual block author uh and this is where nimbus comes in as well uh we go from our 32 candidates to the eligible candidates but let's uh think about it in the abstract right we could just do a round robin we could take the top 32 collaters and just iterate through them one by one but there's a problem right the problem is that if the collater knows exactly when they produce blocks they could participate in some nefarious

behavior but what's more than that is if other people know when a specific co-later will be producing a block they could ddos them or they could do other things that would be uh harmful and so we have to have a source of of randomness of entropy right so right now we have a pseudo-random source and we're choosing uh a specific subset of candidates so it's going to be more than one right it's more than one because in the event that one collateral candidate is offline there's an issue we need to have a more than one producing blocks so suppose we take three collators they all produce blocks and then the first one that produces a

valid block uh will be the one that has successfully published that and receives the reward okay all right so you might be wondering as an ethereum developer like this is all very interesting but i don't want to be learning the new api to interact with porcada the documentation i don't want to be learning rest where here's the good news pre-compile so what are pre-compiles it's a way to access a subtract functionality through the ethereum api so we chose to expose the this our staking interface to the

ethereum api so that developers can just access it through authority call how does it work in the back well actually the way that the palette evm was designed was already designed to handle these cases where the code can recognize that certain types of core will execute some type of substrate rust command so in the next slide we can see a little screenshot of the rest code well i know not everybody knows how to understand rust but basically what you're seeing is a switch or a match depending on your favorite languages that is going to associate a certain byte code with a certain function so on

the top you see the ethereum pre-compiles that are already included in ethereum you might know them the first one ec recover i've used back in the days when i was coding a side channel here you see uh some other um non-boom beam specific nor ethereum specific precompiles so as you can see they have a very weird name shaft 3 fib so you recognize it's a cryptography library we're trying to provide all these cryptographic tools that we enable you to create a powerful smart contract and in the bottom you see the moonbeam specific pre-compiles of course we're planning to add more but right now we have the parachain staking wrapper

it means that if you're calling this by code you're going to activate a power chain staking function all right so here are some screenshots of the game pre-compile interface because what does it look like for an ethereum developer it looks like a contract interface so whenever you're used you're calling another contract inside your theoretical you need the interface of that contract and so you can access that interface on our website the the url is in the and all you have to do is copy and paste it into your code and then you can just call this function inside your soytico

so as you see it's pretty straightforward rest you can ask to join as a candidate leave the candidate group you can nominate a candidate and nominate a candidate challenge change uh how much uh you are staking on a on a candidate and um i we think that's pretty powerful because basically like uh right now we have this huge ecosystem of defy and uh what's missing is the staking and staking offers a lot of opportunities for applications so that's what we're going to see in our demo so i'll introduce a little bit of

background on the demo so our our user story we have someone named alice and she is a defy enthusiast she's skilled with smart contract design she knows about metamasks she's familiar with remix all those types of tools truffle hard hat she's familiar with proof of stake protocols and how they work and she has experience with daos she's familiar with she's participated in hackathons and she's also familiar with yield farming so this is just some background on alice alice discovers moonbeam uh she's been in she's familiar with ethereum and she loves it

but she's looking for a gateway to also be able to access the polka dot echo system uh the evm is familiar to her and uh it works with all of her favorite tools in moonbeam so alice falls down the rabbit hole and yes yes so she's getting acquainted with nimbus with staking with cross message uh uh cross-chain message protocol uh and with governance uh but the important thing is that she's able to work with all of her existing solidity skills uh she doesn't have to pick up a new language um and uh thanks to our pre-compile she can interact seamlessly uh with the russ

code um using the solidity ethereum pre-compile uh and she's interacting with these pallets uh in a native fashion um without learning rust so um alice she wants to share her expertise right she wants to bring more people into the ecosystem uh people who aren't really familiar um with cryptocurrency um and who want to dip their toe in the water right and she realizes uh that one of the potential hurdles is that um new uh folks in the ecosystem might want might not want to put a ton of money in early on so she realizes that she wants to code her own pooled uh staking contract to let her friends

get in on this without having to put up a lot of money so we mentioned before that there is a minimum nomination amount it's a small amount so it's accessible to many people but still we're trying to show that somebody might not want to put in you know very much much money at all and so this is why and this is how alice wants to get her friends involved so there's a couple requirements though she doesn't want to make a sacrifice in security so we need to make sure that anybody who participates in the smart contract staking solution is entitled to the same security as a collator that's very important uh we mentioned the low cost to entry um and we also have no minimum

contribution amount so with this pooled contract any amount is fine any amount is fine to contribute but the contract itself cannot make a nomination until it has reached the minimum nomination amount and the next thing is proportionate reward so there's no sort of interesting reward structures they vary according to the amount it's all commensurate with what you put in so there's no no tears or anything like that it's uh perfectly proportionate now her her friends uh delegate the her friends who are learning about cryptocurrency right they may not want to participate in so they're going to delegate to the pool admins

the ability to select who's being nominated but they're going to share in the reward something important to point out is that there's no slashing in moonbeam so the very worst uh that could happen uh if the co-later is offline and and has issues for the entire staking period uh then they would not receive uh the staking rewards but they're not going to lose any of their original state stakes so that's very important to point out uh that the downside um that they're not going to lose anything all right so now alice gets to work and she starts doing some research which is of course the first step to getting anything done so

she goes to the url provided by moonbeam of course she reads the documentation and she copies that interface into her truffle repo and she also realizes that by reading the documentation that moombin has provided the truffle book specially tailored for moonbeam so for those who are not familiar with truffle it means that you're setting up a javascript repo with testing a folder for your solidity files and migration scripts so she adds the interface to her repo and she starts coding her own smart contract so what is the smart contract going to

do well what she wants is governance so she uses for example open zeppelin governance access control smart contracts and that way she can use role-based role-based access the idea is that in her dao she wants admins and regular members she's going the admin she's going to be the admin obviously and her friends are going to be members because her friends don't necessarily want to participate in changing uh which collateral is going to be nominated changing basically like doing the hard work of learning about staking and choosing the best collaters

so she creates this role of admin and member and she creates this function that we call a updated nomination where that is only accessible to admins and that will transfer the stick that is on the dow to the nomination on the collater that way at the end of the round she she can call another function that will retrieve the money into the dow and if any member wants to cash out their share of the demo anytime they can call the cash out function so next i'm going to show the video but right before that uh because the video might be a real fast right after this slide i just want to tell you what

you're going to see um so this is the update nomination function where you recognize the modifier if you're so easy that guarantees that only an admin can access this function and here you see staking what is taking staking is the instantiation of the sticking interface with the right address and that means that she's interacting with taking as if she was interacting with a smart contract that is already deployed and here is the interesting chords taking nominate with the right amounts so in the demo what you're going to see is the truffle box repo that i built for for the demo uh you're going to see the sticky

interface you're going to see the nomination dial and then you're going to see the test scripts and we're going to run the test script and the goal at the end is that we want to make sure that we successfully nominated a collator with the dell so let's start the video please all right so here is the repo you see the domination down contract with a lot of role-based functionalities the caching out functionality the updated nomination next so yeah here at the top you can see how the power change thinking is

imported and now we see the part the seeking interface and finally the test it's real fast and so under on the left uh i'm starting a local moonbeam instance and on the right i'm going to start the test and with truffle test and as you can see it's going to beautifully succeed so um this is like a pretty actual like pretty simple display of uh what you can do with nomination dao but you could imagine uh further testing where

you could uh see what happens when different members join uh how they they can have different shares of the dow how they're rewarded every 300 rounds so i encourage anybody who was interested by this demo to go to the repo and send me a pr and i'll be glad to review it alright so back to slide 25 please so what we've demoed is that alice created this doubt that empowers her and her friends to pull together their resources to nominate a collater and who do we have on the left it's the very mean queens of hearts who has a lot of money and so far she's

been ruling the proof-of-stake game but now alice has pulled enough money that she can compete with her and establish justice and decentralization which of course is our goal and that is the end of the demo so you can be like alice and so how can you do that we have some ideas for expansion and that is a lottery so in the event that folks who want to participate in the staking they may not be swayed by you know they have a low amount to contribute and so they may not be swayed by correspondingly lower rewards so instead what we're thinking about is a lottery

contract right a lottery where somebody can get outsized rewards secondly i would be sending an xcm directly uh from solidity code that would be delivered to a neighboring power chain and so the options are you know unlimited here and we encourage you to submit your prs uh to this repo yeah so the conclusion is that uh we hope that we're able to show how we're basically bringing the best of both worlds uh we have the very modern infrastructure of polkadot with working proof-of-stake

with interoperability with scalability and we have the rich ecosystem and user experience of ethereum so yeah that i i think that the poolstick ingredient demonstrates on one side the applicability of ethereum and the strong backend infrastructure of substrate and finally i think we're going to end with a little uh shout out to our two colleagues joshi and amar who couldn't be there but our talk was very inspired from a talk that they gave at illuminate 21 and i recommend checking it out

if you like this talk [Applause] yeah so i don't know if you have questions we are open if somebody has a question hello uh you mentioned that there is no slashing mechanism for misbehaving collaters is that is it going to stay like that or is that something that will be added in the future so um first of all uh we do want to say that like slashing

is not super necessary for the level of security that we want for our power chain but also yes we it could be an option that in the future we do add slashing do you have anything to add perfect okay any other question well yeah it was great to have you guys thank you so much always a pleasure to be at fcc you