# 'Synthetix Analysis' - David Goldberg

- Channel: [ETHGlobal](https://streameth.org/ethglobal)
- Date: 2021-10-09
- Duration: 30:51
- Topics: ethereum, scaling, david goldberg, synthetix, ethglobal, derivatives
- Watch: https://streameth.org/watch/yt-rZHo75x_Q7Y
- YouTube: https://www.youtube.com/watch?v=rZHo75x_Q7Y

## Description

David Goldberg, Tech lead of Synthetix, as he explains how Synthetix approaches scaling derivatives.

## Transcript

next up we have david from synthetics and i guess just a little tidbit about synthetics you know they're probably one of the most innovative teams and dapps in the space so not only in being you know one of the first uh d5 derivatives platforms and now also being the first implementation on optimism optimistic roll-ups uh synthetics has also pioneered a really unique community dow system uh so i'm not sure how many people are aware of the spartan council and how a lot of the really excellent work coming out of uh you know the synthetics community like the logos are actually part of the dow's purview uh so really excited to hear more from david about how you guys are thinking about layer two scalability great thanks so much for having me so i guess i can go ahead and share my screen so yeah today i'm going to talk a little bit about what we're doing at synthetics around layer 2 and ethereum and then uh we've actually been one of the first protocols to integrate with optimism um and so pretty excited to share with you uh some different aspects of where we've been so one of the things that seem we get asked a lot is like why did we choose optimism right so as far as ethereum scaling goes there's many solutions it seems like we're hearing about new scaling solutions all the time so one of the things that was really important to us was to have one solidity code base we wanted to avoid rewriting our contracts and then maintaining a set of smart contracts for mainnet ethereum layer one and then having you know contracts for layer two we wanted them to be the same and so optimism uh really gave us that that ability which was which is huge um the second thing that we really liked was the security and the simplicity of what how they're designing their system right in decentralized uh blockchains the more complexity that you bring into the system the more risk there is both from a security perspective and from just you know um business logic bugs security bugs etc and so um optimism is trying to fork as much as they can from layer one and for example they have a solidity compiler that's just a 200 lines of code diff between what runs on layer one and then their geth client is like a thousand line diff so it's a pretty lightweight implementation and they're trying to stay as lightweight as possible and then i think the last reason is because uh you know as we it's not just about the tech right part of what makes ethereum special is the community right where are people going to be moving is kind of what we've tried to get ahead of and figure out and so we think that most of the community is going to be going the optimistic direction and so that's why we kind of bet on this as our path forward right and then you know if you think about where we anticipate liquidity flowing that's something we're trying to get ahead of and you know we want to be where the liquidity is when it moves to l2 so there's also a social aspect to it and if you want to learn more about it kane warwick the founder of synthetics has a blog post called why optimism that you could check out so if i keep moving here some of the features that we're rolling out as part of synthetics if you're familiar you know what is synthetic synthetics is a derivatives trading platform built on ethereum so we have you know anyone can come on create synthetic assets that we offer or sorry not create but you know purchase synthetic assets that we offer and then uh do things like derivative strategies with them and so um the way that people uh get value from or the way that we've uh created liquidity in the system is through staking snx so people come on they stake their snx tokens which creates liquidity so then as people trade in and out of synths synthetic assets right like we have a synthetic version of ethereum synthetic version of bitcoin synthetic version of tesla stock for example we're going to be releasing pretty soon a synthetic version of coinbase stock so things like that you know the way that the liquidity comes into the system is from staking and so we're going to look at an example of how we've implemented staking on optimism mainnet to provide liquidity for synths on l2 um and then on our road map coming up we're gonna have exchanging of since on l2 and then uh you know the ultimate goal is to offer leverage for since right things like shorting sense futures um you know uh margin stuff like that so um yeah so we'll walk through um let's see like an actual uh kind of walkthrough of the different contracts that we have so one of the fundamental concepts of if you tuned into the optimism talk i think it was yesterday around an introduction to optimism they talked about the bridge right the bridge contracts to optimism and so this idea of a bridge you know you can think of it as layer 1 and layer 2 need to know about each other and need some way of communicating right and so what i'm going to do is i'm going to show you our code around the bridge contracts that we have and again the bridge is just basically how do you go from you know erc20 tokens on l1 and move them over to l2 and then when you want to withdraw like how do you withdraw them from l2 back to l1 and uh i guess you're not really moving them over it's more of like like a mirror kind of which we'll look at you know in a second and then if you're interested in learning about these contracts um you can find them on our github so i've included the link here but if you just go to synthetic io github and then you look at synthetics which is our our repo you'll see that the contracts you know you can find our contracts and then the two contracts that we actually care about are going to be synthetics bridge to optimism and synthetics bridge to base and let's go ahead and i'm going to switch over to my code editor so i can show what the actual code looks like i think the presentation yesterday gave a great overview of how they work at a high level i'll try to recap as i go along here but i just want to show you like an actual example that's been deployed and is running on maintenance so um i just want to make sure you can see my my code editor is there a way um someone can just maybe control it so yeah so the first kind of thing to think about is as i mentioned you could just imagine like there's two sides to a bridge right on one side we have layer one ethereum and then on the other side we have layer two ethereum and they need some mechanism to communicate so what i'm going to show you is how we're doing that so the first thing to kind of pay attention to is let's say someone wanted to initiate a deposit right to stake on l2 it actually might even be easier to just show you our like the ui first to kind of see the dap and then look at the code because i feel like it might give you a sense of like what we're what we're trying to do and i'm actually going to show you an example of how we're going to stake stake on l2 so you can see right here i'm on our staking.synthetics.io which is our dap it's our decentralized application right and the front end's built in react and then we've got the backend built in solidity so that the back end is powered by ethereum so as you can see here right um we have a tab called layer two right so when you come to staking dot synthetics dot io you can go to layer two and then what you can do is you can deposit snx and transfer it from layer one to layer two right so what i'm gonna do here is i'm going to you see how i have ten snx in my wallet right so i'm just using metamask um we just we're just hooked up with metamask for those of you who are familiar it's just you know i assume most people are familiar if you're not this is just a wallet that lets you interact with the ethereum blockchain and so i'm going to go ahead and i'm going to deposit 10 snx into layer 2 right so when i click on deposit you can see i've already done the approval so whenever you're sending erc20 token there's the function that's called transfer from that's the function and before i'm able to do this i had to approve uh that i can this contract can interact with this erc20 token called snx that's in my wallet so i'm going to go ahead and deposit 10 snx now you can see the gas fee that i have to pay is quite substantial right this really solidifies the need for scaling ethereum and for moving towards layer 2 solutions because once we see me move to layer two and actually do a transaction you'll see there'll be no gas now there will eventually be gas right now the optimism team is just covering the gas but eventually you'll have gas costs on l2 that will probably be in the 10 to 100 times you know cheaper order of magnitude than what you see on l1 and you know one thing to keep in mind is as the gas costs change on l1 they'll also change on l2 so if you have higher gas costs in l1 like we do right now like the gas costs right now are pretty high you'd have you know relatively higher gas costs on l2 then if the gas costs were lower on l1 they'd be a little bit lower on l2 but just remember like right now it looks like i'm going to pay you know somewhere around 132 for the gas cost to submit this transaction you can think of like in on layer 2 you'd probably be paying a dollar 30 to 13 like as a rough estimate somewhere in that range just depends on a number of factors but you know the difference between a dollar thirty and thirteen dollars and then a hundred thirty dollars is quite substantial so i'm going to go ahead and confirm my transaction so now basically what's happening is we are just waiting for the transaction to go through and um you know just out of the interest of the sake of the demo i'm gonna go ahead and speed up my transaction so i'm gonna actually go ahead and speed it up so that we let's see what is the current why don't we just go to advanced and i'll just put in like 250 as my guess price oh it's excessive okay let's go with 200 just to make sure that it goes through so i'm going to save it now it should go through pretty quickly and what we're going to see is the deposit's going to go through and then we're going to be able to do something on l2 with our funds okay so while it's pending here i'm going to go back and now maybe we could go back to the code so we can just look at just while we're waiting here we'll just go ahead and look at the example of what's actually happening in the code as we did that so let me just share my code editor again okay so the first thing you can see is we're in the synthetics bridge to optimism contract and again you can find this on our github by just going to the synthetics repo and then looking at the contracts folder okay so let's go ahead and um so we're in synthetic bridge to optimism we've got um a couple things to take note of the first thing is we're going to have a function called initiate deposit right so this is a function that can be called externally right so somewhere where i where i clicked deposit snx remember where i deposited the 10 snx that what it was doing it was under the hood it was calling this initiate deposit function and that takes in an amount in this case it was 10 s and x right and so um we have a few things that we're requiring here to make sure that i'm actually able to make this deposit in the interest of time i'm just going to keep moving and so we go into the depos initiate deposit function this is a private function but again this just gets called by our public initiate deposit function here we have our 10s and x we've sent in remember how i mentioned you first have to approve a transaction which i had already done before the demo started but we're using the transfer from function to transfer in ears to 20. so you can see we we're transferring an erc20 from this uh this call here gives us an erc20 implementation and so the sender right is my wallet address that i was showing you in metamask and what we're doing here is we're sending the 10 snx from me from my wallet into this smart contract right so the snx is going from message.sender which is my wallet into synthetics bridge to optimism contract and then you know we have the deposit amount which is 10 right so that's the first step so now we've taken the 10 snx from my wallet and put it in the smart contract so now it's it's locked in our l1 contract which is synthetic bridge to optimism so it's locked there now what happens so here you can see we have this um this uh synthetics bridge to base interface that we get right and remember synthetics bridge to base is the contract that lives on l2 so what we're saying is we have this interface right and now we're going to get the abi which is the encoding of what the um what the signature how does that contract work the one that's on l2 and so we're going to say um you know take the interface and then the function we're going to care about that we're going to want to call on l2 is called complete deposit so we're initiating a deposit from l1 and we're just saying the function we want to call on l2 imagine we started here and we walked across the bridge and we went to the other side which is l2 we're saying call complete deposit right and again we're saying the sender which was the metamask wallet address that's my address we're marking that as the sender so l2 knows who the sender was and then we're sending in the deposit amount um so this builds the api that doesn't actually send the message right that's just saying it's just telling our contract you need to know what to call on l2 right so then what we do is we have this messenger instance so if we look at what this is doing um so if i look at messenger right so this is um basically the the base cross domain messenger contract that was provided to us by optimism right so we didn't actually write this base cross domain message this is part of optimism and um they've also given us you know like a basically an identifier here for what the contract name is and so here when we're in remember initiate deposit um we are basically here calling the the function send message on the messenger contract that optimism has given us so optimism supplied the messenger and we're saying hey messenger send this message across the bridge to layer two with the message data and remember the message data is saying call complete deposit with our address and the deposit amount right and so then what's going to happen is we also have to deal with the gas limit the gas limit on layer 2 optimism currently is 9 million there's a number of reasons for that we can probably skip that just because it'll take us down the rabbit hole um but now we're going to go back over so basically this message you know we send the message right and so the messenger says okay i have to go find the the synthetics bridge to base contract i have to tell that layer you know layer two contract to call complete deposit right with the sender and the deposit amount right so now if we go back to synthetics bridge to base remember that now we're on layer two right synthetic bridge to base is running on layer two um and we remember we um we said the function name was called complete deposit right you see how this is invoked by the messenger on l2 so now we call complete deposit um we have you know member of the address was my address message.sender which was the meta mask address i was using and then the deposit amount was 10 10 snx now um here we have a modifier so if you're familiar with solidity this modifier is basically a check that needs to happen and um so this runs right and i can explain modifiers i'm just going to assume some basic level of familiarity with solidity here so um what really happens here is this little underscore it represents the code block of the function that's using the modifier so um that so first before we run the function that was being used by only optimism bridge we're gonna first call only allow for optimism so this is gonna do two things right this is gonna say um only the uh the sender has to be the relayer right and then the original caller needs to be the bridge to optimism contract so we're saying if either of those conditions aren't true that the relayer didn't make the call which or which is again the like the messenger contract right or the synthetics bridged optimism the l1 contract wasn't the first point of entry then revert and and fail the transaction because we don't want it to go through right don't call complete deposit so now um assuming that check has gone through will then proceed to execute the body of the function that's using the modifier and remember the modifier is being used by complete deposit right so again what this was saying was first run those checks and then proceed with this code right this code gets plugged in where the underscore was so let's just go back down to where that uh where we had um only optimism bridge so you can see we reuse this in a lot of places right that's why we have it as a modifier otherwise we could just have those checks directly in the function right but anyways so what happens is this gets replaced for that little underscore that we saw and then so we did the checks and now this executes and so what we're going to do is we're going to mint 10 snx we deposited 10 snx from l1 right that's now locked in the synthetics bridge to optimism contract which runs on l1 and then over here we've got our we've got we're going to mint 10 snx so now we have a mirror we have 10 snx locked in our l1 contract and then we have 10 smx we've created on l2 in the synthetics bridge to base uh and we're in the synthetic contract on l2 so now there's like a mirror between the two right um and then we'll emit an event that this happened right so now we've got 10 on l1 that's locked and we have another 10 on l2 and they kind of represent each other right so now let's go ahead and think about okay so we're on l2 we've got our 10. what if i wanted to withdraw my funds from l2 right i'm going to i'll show you what i'm going to do with them in a second but what if i wanted to withdraw them right what's going to have to happen is on the layer two synthetic uh synthetics bridge to base right remember synthetic bridge to base runs on l2 right i would kick off and initiate withdrawal uh let's see if we can spell that right um so um so initiate withdrawal so basically what would happen would be on our l2 dap right i would click on a button that's like withdraw it would make me sign the transaction just like you saw before and it would call this function initiate withdraw and so i'd specify like if i wanted to try everything i would specify 10 s and x right because remember we had 10 on l1 10 and l2 so the first thing it's going to do is going to check that i have 10 s and x to withdraw right and if i do okay so i've got my 10s in x it's going to burn the s and x on l2 so now we went from 10 to zero right so we had to burn that snx before it could be withdrawn because when it goes back to l1 you can't have you know both ex like existing otherwise we will have created money out of thin air they have to be kind of like in sync so we burnt the uh the 10 s and x and l2 and remember burning is just like destroying right minting is like issuing um so in the previous example we issued now we're destroying and so um then we do the same thing that we did when we were coming from l1 to l2 but just vice versa right so we say okay give me the abi for the function that should run on l1 right and that's going to be complete withdrawal right and remember complete withdrawal is going to live on l1 so that's going to be in the synthetics bridge to optimism contract which runs on l1 we're going to send in the sender right so we're sending in again my address as the sender and the amount i'm withdrawing which is 10. and so then what happens is so we do the same thing we take the optimism supplied messenger contract we relay the messenger or we relay the message by calling send message but this time we're calling to synthetics bridge to optimism which runs on l1 we have our abi that says call complete withdrawal with these arguments message.sender and amount and then uh you know we we have to specify the gas limit we have to abide by that 9 million guest limit and so now what's going to happen is this gets sent we omit the event but what happens is we go back to synthetic bridge to optimism and now we need to call complete withdrawal all right so we're here we've got we've we've been called we've called this from layer two right on my address that was in metamask and then the amount which was 10 s and x we get we have another modifier here which was require active i think we've already looked at what modifiers do um so just know that there's a check there that happens right and then again we make sure that the relayer is calling this right and we make sure that the synthetic bridge to base was the original caller so to speak um which means that it came from our l2 contract and then what do we do we transfer the 10s and x from the smart contract remember how we locked the snx in the smart contract it's basically like step one when it went from my wallet into l1's contract locked now we're basically unlocking it we're saying take it from the smart contract send it back to the original user or the original sender with the amount and that's it that we just completed the withdrawal so this is the bridge right they have to kind of be in sync and i think just looking at a real world example hopefully would be pretty helpful so now that we've covered that i'm just going to jump back into chrome here you can see how the deposit went through like if we open up oh let's see might have ended my screen share there so let me go back in here so we can see that there was success we deposited 10 s and x right from my wallet on l1 on to l2 we paid 210 in gas which is just insane right and that's part of the reason why we're so excited about moving over to optimism because again in this type of transaction you know if it occurred on l2 again withdrawals i don't know but like if you were just sending money from l2 to l2 like an l2 address to an l2 address you might be anywhere from two dollars to uh two dollars ten cents to like 21 again that's just a broad kind of range but just to show some expectations there of like what to expect over time and so now if we go back to the dap right let me just go ahead and refresh here just see if it's updated so now um what i'm going to do is you see how i have now zero s and x right that's because i sent it to l2 so if you look at my account here and we look at my assets i now have zero snx right and now i'm gonna you see how we have this switch to l2 button here right so this is like again this might even give you a good sense of how to as you're building daps that integrate with l2 from a user experience standpoint like how do you manage going from layer one to layer two right we put a lot of time into thinking through how should the experience go so now i'm going to go ahead and switch to layer 2 right this is going to bring up my meta mask hopefully you can see that and you see how we have optimism mainnet as the selector right as the network that's being selected so i'm going to switch to that network now that i'm on optimism l2 you can see i'm on l2 staking right our layer 2 dapp right if i open my metamask you can see i'm on the optimism mainnet account and then you know you can see there's also an optimism covan uh test net that we have but right now we're actually on mainnet and so then if i come down here i can mint susd with my staked snx you see how i have 10 snx i've staked right so we're going to go ahead and mint 35 dollars and 12 cents in susd and again because of the collateral ratio right that's all the susd i can mint again this is a this is a thing that's specific to synthetic so when you go on synthetics when you stake uh your s and x you have to have 500 percent collateral ratio um compared to or it looks like 600 percent compared to what you can actually mint so i can only mint like i have to have 600 more than what i commit so here i can mint 34.95 so i'm going ahead and click mint usd so that's going to bring up here and you can see how the gas is zero right that's currently because the optimism team is covering the gas costs on mainnet um you know again this will change like normally maybe this would cost me like a couple dollars like very cheap right or relatively inexpensive so i'm going to confirm the transaction look how fast that was it was like basically instant right we now have 209 dollars staked right because that was my 10 snx 209 and then i've got active debt of 34.95 and my collateral ratio is 600 percent right so i'm i'm right where i want to be and you saw how instant it was it's like so exciting to see ethereum transactions that fast um and so yeah that's pretty much it i mean that's the demo we talked about the bridge contracts i'm kind of coming back to the presentation for a second and then i'll try to wrap up because i know i'm not sure exactly how long i'm supposed to be here for i could be here all day if uh if you wanted me to so um but yeah so we talked about the bridge um we looked at um the demo um you know if you're interested in some good resources these were really helpful for me as i started to get ramped up on optimism and layer two and roll ups and so um georgios constanopolis has a couple great uh resources so almost everything you need to know about optimistic roll-ups and how does optimism roll up really work the first one is more high level the second one is much more detailed around like the the technical implementation and i can try to make these you know either slides available or you know if you wanted to dm me or find me on discord you can find me on the synthetics discord if you if you want these actual links um so i'll put the link for our discord on as well and then vitalik has some great resources as well that helped me kind of gain an understanding of roll-ups so those would be two good ones an incomplete guide to roll-ups and then a roll-up-centric ethereum road map the first one is on his blog and then i think the second one was on either reddit or theory magicians read it i think so um yeah and then the optimism team has like a scenario walkthrough where they kind of go through like a complex scenario of depositing transferring and then like a scenario where fraud proofs would come into play so if you want to learn about that that would be a great resource i'd also suggest in the eth global discord for the hackathon optimism has a channel so if you have specific questions you can go in there and post about it um yeah and then you know here's my twitter here's uh synthetics twitter if you want to join our discord this is the link in there is davidji and uh yeah just really excited to have been able to give this presentation and share with you just some learnings we've had as we've gone gone along here with our l2 implementation and we're just really excited to be working with optimism and the ethereum community to make this happen so thank you so much amazing david thank you uh so much for that awesome presentation um it's it's just incredible to see over the past few years from you being part of eight global events and going from hacker to mentor to to developer to giving a talk and just making these events even more useful for everybody else so thanks again for that and uh also for a really comprehensive walkthrough of how synthetics does everything on their one and layer two so thanks again and uh i to just share some quick closing thoughts uh for everybody and uh with that said i also want to make sure that i give our uh our guest mc eva balen a really big thank you from our side she really did an amazing job just introducing all of her speakers and sharing so many cool and fun stories it was awesome to have her help us out and that will uh willing to get her involved again in the future so all that said this was our last hack of of the day we had so many introductory talks on how everything from optimistic roll-ups to to zk roll ups to to side chains to have specific chains to what's happening in layer two all together and cross layer two it's just been a massive overflow of information on how all these things work and uh if you were not able to catch all of these talks uh they are recorded and they will be made available live for the next couple of days on our youtube and after this live stream ends you can also refresh uh eatglobal.tv and just catch the whole summit um all together as a single uh video so i don't have a lot more to say i just want to remind everybody that this was our one the first of three summits uh we did the scaling introductions and just talked about high level what's happening in the world of scalability next friday the 23rd will be hosting our merge summit where we'll be talking about what's happening on the layer one side of each two and what the merge is going to look like and everything that's happening from the ethereum development side and then the week after which is friday the 30th of april we are hosting our summit on zero knowledge so you'll see a lot of applications about the possibilities of zero knowledge proofs and how that technology and those primitives can be applied to scalability programmability and everything else so with that i want to thank all of you for being here today and thanks to everybody who was on chat asking all these amazing questions and uh we look forward to seeing all of you for the next summit next week for all the information about the talks the upcoming speakers and the schedule you can head over to scaling.educable.com i hope all of you have
