Superfluid 🛠Using Superfluid for DAO Payments - Samuel Flamini
ETHGlobal·Sun, Oct 9, 2022, 12:00 AM
Welcome Samuel Flamini from Superfluid for a workshop titled "Using Superfluid for DAO Payments". ETHOnline is our biggest event of the year! Collaborate with thousands of developers and creatives from around the world and win over $300k+ in prizes! We're bringing in 30+ of the biggest teams in web3 to give you access like never before. Learn from ecosystem leaders and build your biggest ideas as the merge nears reality. The future of Ethereum is being written right now, and it's time to make your mark. --------------------------------------------------- Follow us: https://twitter.com/ETHGlobal​ Check us out: https://ethglobal.co​m Email us at: hello@ethglobal.com
Transcript
[Applause] [Music] sweet okay hi everyone welcome welcome um we have sam here from superfluid and he's going to do his workshop using superfluid for dow payments um so yeah super excited um i am obviously like a super fluid fan girl um and yeah we will have a q a towards the end so maybe like the last five or ten minutes if everyone has questions you can raise your hand or feel free like while sam's talking um just so you don't forget your question if you want to just drop it on the chat and then we'll make sure to answer it um but if you're a little bit shy like me um you can always just wait and ask your question in the superfluid discord so cool yeah i'll let uh sam share his screen and take it away all right hello everybody it's good to be here thank you cree i will just share my screen and we will go into this so let's share my whole desktop and today what we're going to do is we're going to start off with a little slide deck presentation and then get into some code and show you how you can use superfluid both the easy way using our sdks and stuff and also at the smart contract level with some more advanced things but let me start presenting here in keynote notes all right so we're super happy to be sponsoring ethan line 2022 this is always a very fun event and it's it's good to have so many people here so be fun uh so what is super fluid from a high level point of view right some of you might have seen superfluid but i'll give you the 101. so superfluid is a protocol that will allow you to transfer value in really novel interesting ways right and the way that you'll see this reference most often is through something called money streaming so we like to talk about superfood superfluid these events as a protocol that enables digital negative programmable cash flows right so we allow you to stream money with no capital lock up in a way that is 100 programmable and unchained so at superflu we have this this saying we like to bring up a lot and that is introduced information with blockchain is to value right so one way that we want to bring this idea of value transfer into the age of the internet is by allowing payment to flow in real time right and if you think about this in terms of how the world works right if i provide you a service whether i'm a consultant an employee or just providing some other random service i provide that value to you in real time but the way that it works today is that payment for the service is not connected to the value that i'm providing right if i'm an employee your company i might work for you right now but i have to wait 7 to 14 or maybe even 31 days if it's the entire month to actually receive payment for the work that i've done right so we believe that there should be a direct link between the two and money streaming enables this right so a money stream is a it's a continuous payment so you think you can think of it as a kind of uh recurring payment taken to its extreme that is frictionless and open-ended between two accounts right so this enables that real-time direct link between value transfer and payment for that value right so the other cool thing about this is that once i uh execute a transaction to open a stream that is an on-chain event so what you get out of that is a what's called a flow rate which is how many tokens are being sent per second and that gives you an on-chain proof of payment so that i have less on payment risk no delays and also because it's on chain there's no intermediate area either this is all made possible by the super fluid protocol which is an evm focused smart contract framework that is deployed all over the place on layer twos and side chains and it's made up of these three high-level parts right so you can see we have that big thing down there at the bottom the superfluid host contract which you can think of as the brain of the protocol we have a library of agreements that allow balances to be changed in really novel and unique ways and we have a new primitive that extends the erc20 and years 277 777 standard called the super token that really makes all of this possible and ties it in so what are super tokens right these are again an extension on the ear c20 and erc777 standard that are plugged into the superfluid protocol right so the erc20 compatible meaning they have all of these standard methods you're used to on an erc20 token contract right there's the approved function transfer from symbol total supply all that good stuff however the way that balances are calculated on them are a bit novel right so because they're plugged directly into the protocol they can have balances that change according to superfluid agreements right so the there's an agreement contract there's actually multiple agreement contracts but one that i'm going to focus on today is the one that will enable money streaming right so there's an agreement contract called the constant flow of agreement that i can engage in as someone with a an ethereum address or if i had if i was building smart contracts which would do later i could use this market contract address if i call the function which creates a stream what that will do is it will uh hook me into an agreement right that is completely open-ended that i have full control over right because this is web three right i have full custody over all of my funds and control over what happens but this agreement will connect me to a separate address and when i create a stream what that will do is define using that agreement a number of tokens that i will send per second to a different address right and this amount using this agreement is reflected back into this thing called a real-time balance right so with the real-time balance what what we're doing is instead of just checking discrete amounts to see how many tokens you have in your one at any given point in time we're also taking into consideration all of those agreements you have set up both in the receiving end and an ascending one right so what we get from each agreement you're engaged in is this thing called the flow rate and we can just aggregate those things together to get a net flow rate so imagine i'm sending one stream that's 10 tokens per day and then on the other end i'm receiving one stream at five tokens per day what we get there is five coming in at 10 going out so we have a net flow rate of minus five tokens per day right so this will tell the protocol okay five tokens should be going out of sam's balance every day and what we can do is cheat a little bit and use the block.timestamp value that we get at every single new every single new block to calculate each time maybe i want to create a new stream or check what my balance is we can calculate the number of seconds that have passed since that net flow rate has changed and because that amount is defined at a per second value we can always know how many tokens each individual address should have right so if you have any questions on that please let me know that's uh me taking it from like the high level all the way down to the low level explanation of what's going on but i'm happy to answer specific questions about this in the q a but that's really how the the streaming is working the other cool thing about superfluid beyond the streaming and all that good stuff is that it's programmable right so you can build these contracts that are also plugged into the protocol right i can build a thing called the super app that i register with the protocol and we saw back here there's this thing called the superfluid host contract there at the bottom i can register a contract with the host and have it be aware of all the the different money streams being sent into it and sent out of it as well so i can create really cool stuff where i can take in incoming streams and then automatically do things with those incoming streams right we have this example in our super examples repo that i'll point to later called the money router that can take in streams and instant streams out and we also have an example called the tradable cash flow that i'll demo using some code here in a few minutes that will show how these callbacks on these contracts work right so they can take in streams and send them elsewhere right in a programmatic way that you can define so again these are called super apps we'll get into them later uh there are three callbacks that i'll reference in in a bit one of them can react to streams coming into the contract another can react to those streams being updated and then another can react to any deletions that happen right so this is a way to build applications that connect with other applications and what you get once you start chaining these things together what we already have is this really really cool network of cash flows right so we have this is actually live right now of what's recording it's not fully fully live but this is the polygon network as of a couple of months ago with all the different super fluid activity taking place on it right so you have each individual node here is one of those green balls and then all the connections are cash flows and you can see the clusters are all smart most of them are smart contracts where there are different applications people are running that are taking in streams and doing really cool things with them right so you as someone working in this hackathon if you want to build on superfluid you have the opportunity to plug into this entire network of accounts and addresses already interacting with superfluid and make it bigger while also having a potential interesting user base to start with right so i employ you i implore you to help grow this network again if you're building on superfluidity online i want to highlight some of our prizes we have about 20k worth and prizes of this hackathon i think this is the most money we've ever put out at a hackathon so we're pretty excited about it should show you that we're pretty bullish on on ethon line 2022 but uh it ranges here from 4k to the best overall project both on superfluid down to you know if you if you submit a super fluid-based project and don't qualify for these other prizes you can still be someone that wins a portion of our prize pool right so we have a four thousand dollar prize pool for everyone that doesn't with one of these amazing prizes uh if you submit a super food project can you actually use superfluid in some way don't you just say you did but actually implement our sdk or our contracts you'll qualify for one of these things so build on us right we're really really helpful and uh we hope you you consider uh uh exploring our tech but okay that was a mouthful that was a lot let's get into actually building on superfluid um how do we do this well there are a few things you should know about before we actually get started building right one of those things is the superfluid developer console this is a really really useful tool for exploring different activity on superfluid network and also tracking your own streams right there's also a really cool subgraph explorer thing that we'll go through later as well there's a superfluid dashboard this is actually an outdated pic we have a really really cool new version of our dashboard dashboard v2 that is live now you can use that to get tokens and create streams in a no-code way and then we also have our sdk core right this is an sdk that will allow you to create streams in as little as five to ten lines of code total uh and we'll go through an example of how to use this in a few minutes but let's go to the basics some basic low-level things you'll need to know uh before we actually get into like specific examples and contracts so when you go to interact with superfluid remember there are three general classes of contracts you need to be aware of i went through these a bit a bit earlier but they are the superfluid host contract they are superfluid agreements and super tokens most of the time the only thing you really need to be aware of are like how to interact with super super tokens directly because our sdks and libraries will distract some of the other low-level stuff away from you but when it comes to super tokens themselves you should know that these are most of the time if you're if you're interacting with a token that is a rapper contract meaning if it's a token like die that you want to stream or usdc or eth you're interacting with something that's a wrapper around that underlying asset right so there's this thing called an erc20 wrapper token you can deploy if you want you can you can take any erc20 token and deploy your own wrapper but you know if i want to stream die what i'll need to do is upgrade my existing erc20 die to the super token version and there's a function on the super token contract called upgrade that will allow you to do that what that's doing under the hood is just depositing your erc20 die into the super token ui contract and minting you back a corresponding amount of the super token die right it's just a one to one amount and the inverse of that operation is the downgrade method right so if i call downgrade what that's going to do is burn the super token asset and just give me back my underline okay that's super tokens the final thing i'll go through just in terms of like diagrams of how calls are working in things is this diagram here right so under the hood what's happening when i create a stream well let's stay on the sender there in the bottom left-hand corner what's happening under the hood is i'm making a call to the superfluid host contract okay the host contract will then take in a call that specifies uh as a part of that that transaction an action that i want to take using the agreement right so under the hood let's say i want to create a stream to create for one token per day i would define this as parameters there and then that call will be passed through the agreement and create the stream from me the sender to create the receiver right pretty simple again you don't have to really keep in mind at all times that it's the host making the call to the cfa but it's worth having that as some just baseline context right in case you're curious when you're looking at ether scan like why am i calling this host thing why is it using call agreement this is the explanation as to why it looks like that okay so it's time to build all right let's actually get into some code okay let's see how much time i have left here i have maybe about 10 minutes for actual code i'll go through this relatively quickly okay so the first thing that i'm going to do is go through an example using our sdk all right let me just pull this down slightly here inside of our docs let's go to the superfluid docs all right let me stop real quick just to make sure i don't have any outstanding questions everything is all good uh we just have some sheesh good emojis let's go people are excited all right nothing too crazy in the in the chat uh shivali i will answer your question in just a bit your short answer there is you don't need to deploy any contracts to do that right but i'll go deeper into that in just a few minutes all right so i'm in our docs these are really helpful i highly recommend you go to docs at superfluid.finance before you start building it's useful stuff there we've worked pretty hard on them but the first example i want to show you is an example that exists inside the interactive tutorial section under money streaming okay so this money streaming page is going to show you how to create update and delete money streams using our sdk okay the first few examples will you use just an unlocked private key to show you how things are working if you interact with these put in your address and create a stream to yourself it will send you some free tokens right if you need free tokens as well we have a faucet you can use i recommend that it'll give you a bunch of fake dye which is fun but the example that i specifically want to show is this one where i have a connected wallet okay let me move around this little zoom thing and i'll show you this okay so we have on this page a really simple example with a really ugly ui of a creative flow modal right what this lets you do is connect the wallets and create a stream okay so here i have the code sandbox up you're welcome to fork this and using your own project i recommend you make it prettier than this because again i'm not a i'm not the greatest frontend developer in the world but i can do it um but here i just have a simple example to use their sdk to create these streams right so what i'm doing here is importing this framework object from the sdk core which is important for initializing the sdk and then what i'm doing is i just have this function that will run when i click this button to create a stream that does the following okay so the first thing is it will set up the sdk here we have this initialization right so this await framework.create will create a framework object that i can call to open a stream and all i need to pass to it is the chain id that i'm using right so what i'm doing up top here is i'm i'm getting the chain id from metamask and then i'm also just getting the the provider from metamask as well you could use an rppc provider here for this section if you like but here i'm just using the window.
ethereum little thing here then what i'm doing is i'm getting i'm loading the dyex contract from the sdk i can do this just by calling s theft.loadsupertoken and i paste in the symbol there keep in mind that if you are using a test token right it is called f diax to make it sound fake right f x is the token i'm using here i'm getting the address of that and then i had this operation that is going to create a flow for me okay so here in this create flow operation i'm just going to call sf.cfav1.createflow and i'm going to pass in a few parameters okay the first one we need is the recipient right so in the example where i'm sending a stream to cree i'm just going to have pre's address in here in this case i would just you know i paste it in here to the form i also need the flow rate okay so the flow rate is denominated in way right so you can imagine these amounts having 18. imagine if they had like 18 decimals right it's going to look like a very large large number but this is going to be the number of tokens i want to send per second okay so again it's in um away so a large number is actually really going to correspond with small amounts but that's my flow rate and the final thing i need here is the super token that i want to use in this case it's going to be di x okay so this x here at the end is the denomination we use for for super tokens right if it's a wrapper super token we'll call it die x okay so you have di-x here um if it was ethex that'd be wrapped eth and so on okay then what we're going to do is execute this operation using the createflowoperation.
exec here and i'm going to pass in my signer in this case it's going to be the connected wallet to metamask but that could be a signer and hardhat or something like that as well all right so we can create a stream like this let's say that i want to go to this end to end connect a wallet i'm going to pick a kind of obscure address here that i have not used let's just go to the console and grab a random address i'm going to send a stream to this is the super fluid console by the way guys it's a pretty useful tool but if i go to home in the console i'll see all the recent streams created on any network here i'm just going to take a random address and i'm going to give some really lucky random person uh a stream all right a fake die on gwearly okay let's make sure i'm angrily yep i'm on gowerly wonderful send a stream to this person and then i'm going to create a flow rate right so this is going to be 25 die per month right again it looks like a pretty large number per second but it's actually denominated in way so in reality it's quite small so i'm going to create a stream let's hope this works we're live all right wonderful modal pops up looks good all right our stream is in the process of being created okay so what i'm gonna do is i know which address we have here hang on a minute it's this one and we can see here if i search this address this address has a lot of streams okay what you can do once you have a stream open is see a bunch of different incoming and outgoing streams we have here right you can see here i have a ton of outgoing streams right like this one for example like it it'll take a few seconds for the previous stream i just created to show up here but what's going to happen here is it'll look like this right i could i could search it it'll show me what the flow rate looks like it'll show me how much has been streamed and shown me when it was last updated and you'll be able to see for any given address all the incoming and outgoing streams that any address has right so the console is very cool you can also do this and look at it actually on the subgraph as well it'll give you a specific subgraph query you can use to search this right which is pretty awesome especially if you need to get data in for your application okay so that's a really simple example to show you how to create update delete money streams it's pretty useful for you what i'll do now we're running low on time but i'm going to give you a quick overview and at least show you where to go to play around with the more advanced smart contract example okay so there is a contract in our examples repository called the tradable cash flow contract i'll reach here here so we went through this one with our beautiful ui but this tradable cash flow contract what does this do because this is one of those super apps right that can automatically react to changes in streams with regard to that contract right so we can deploy one of these contracts register it with the super fluid protocol and then any time that a stream is sent into this contract anytime it's updated or any time that stream is deleted we can do things automatically within that contract inside of these callback functions right so in this case what the tradable cash flow does is it receives streams it's also an nft contract at the same time which is pretty cool and it can automatically send 100 of the info into the contract to the owner of that nft contract right so it is literally a tradable cash flow anytime the stream is stopped the outgoing streams are stopped um anytime the nft is traded all the cash flow will follow that owner your follow yeah it will follow the nft to the new owner which is which makes it literally like an asset that can be plugged in the rest to d5 okay i highly recommend playing around with this many of you that are here repeat builders on superfluid have probably seen this deal with the tradeable cash flow before but it's a very powerful primitive that can unlock a lot of really cool things and gives a lot of people a light bulb moment as to how programmable superfluid actually can be okay so if you ever want to fork this example what you can do is go to our examples repo and i'll just do that for you here live to show you where you can find it but it is in the super examples folder okay so if you go to github.com superfluid finance and go to super examples you can see it in the examples folder right here okay so you want to go right here i'm literally going to put this exact link in the chat for you guys so you can see it yourself in case you're curious all right so go click on that and then i'll just quickly go through like the architecture of the contract and then we'll stop for some q a okay so if i go into the contracts themselves you see there's two right one of them is this tradable cash flow that's sold this creates a contract which which does two things one it inherits from the nft contract that we're all familiar with right the erc721 and it also is going to use this before token transfer hook that is optional that you can implement on nft contracts themselves you can read about the before token transfer hook in the open and zepplin docs but it does this really cool thing where it allows me to insert some logic anytime token is transferred okay that's what that's going to do and then we're also going to inherit from this redirect all contract which has all of our cool super fluid logic in it all right so you you'll want to go into the redirect all contract read through it and you'll want to notice really two key things okay the first thing is that we're implem we're importing and setting up this cfa library okay this cfa library makes it really easy to create update and delete streams for us and you can see its usage all over the place so if i just look up cfav1 we can see that it is being used to let's see here i'll find a create example yep it's being used to create a flow right here it's being used to delete flows you want to get familiar with the cfa library if you're using solidity to create streams we have lots of docs on it look up cfav1 library in our docs okay that's thing number one thing number two is that you'll also want to check out these callback functions so look for this little after agreement keyword all right so we have asked agreement created after we've been updated an app agreement terminated inside of these functions i can insert any arbitrary logic i want such that that logic will run each time a stream is created updated or deleted into the contract okay you can put anything you want in here get creative as you'd like do something weird we'd love to see it but the only thing that's running is this update outflow function which will if i search that this go back up one this is going to facilitate all of the logic where we look to see all right is there a stream now coming into the contract okay what do we know about that stream we need to make sure that 100 of that stream is getting sent to our owner and this is just handling each of those cases right so i'll kind of pause there if you have any questions specifically on how this function works and how all this stuff works please let me know reach out to me i'm happy to help you in discord uh but for now if we have a matter of the minute or two uh it sounds like free might be giving me that little uh time to stop but let me know and we can uh we can answer some questions here live awesome no you're good for about like another minute or two if there's anyone that has any questions okay so i will go into the chat here and i will officially answer shivali's question do we need to deploy all three contracts host agreement and super token all these contracts are actually already deployed you can interact with them pretty easily either you're using our sdk like i went through that cfav1 library i just briefly showed and in case you need to actually set things up or you need the addresses you can go to the console and go to this protocol section right this was the place where you'll get all the addresses for these contracts when you want to interact with them okay so check those out there and then the last question here is good day sir man please ask what happens in the case when an asset you were trying to create a stream before is not 18 decimal places such as usdt is there a specific list of supported assets and secondly can you confirm if the contract's addresses deployed for the protocol or proxy contracts number one yes there are proxy contracts they are upgradable um in terms of like how we upgrade them we have a governance process we very rarely upgrade things except for extremely technical reasons uh but you're welcome to ask me additional questions on that the deployed contracts are going to be found in that protocol link that you'll want to go to that i just pasted in the chat and all super tokens as they're deployed right now have 18 decimals right so if you have a contract i think usdc doesn't have 18 decimals but the the usdcx contract the super token version will have 18 decimals that is a bit of a standard we have arbitrarily chosen to try to keep things simple within super tokens if you want to deploy your your own new erc20 token wrapper we'd also ask that you you make your decimals 18 right i don't believe it's hard-coded uh but obviously because you know if you're gonna deploy your own contract you can do whatever you want but um you know keep it 18 decimals for simplicity please can you go into a bit of detail on how the money streaming itself works on a technical level okay so i try to do that a little bit earlier with the real time balance explanation i'm not gonna have time to probably go through it again here unfortunately but hit me up in discord go to the super fluid support channel and discord in the east global server uh just paste the same question there and i'll get into it more in more depth um thank you so much sir i got you thank you for the explanation okay and then last question i'll answer here can you interact with the smart contract directly on the back end yes you can so all the contracts again are available at those addresses in the protocol section if you click on any of those or if you search them on etherscan polygon scan all the different block explorers they're all they're all verified you'll be able to see all the functions there on on the block explorers if you want to interact with them directly there you can i recommend going to our docs first and reading about how each thing's working first um but yeah it's all it's all open it's all open source code you can do whatever you like with the deployed code there and i think that's it yeah i think so awesome thanks so much sam always always super informative um you always know like someone is super smart if they can explain complicated things in a way that i understand so uh thanks again awesome all right everyone yeah go ask some more questions in this discord and thanks for coming cool thanks for hanging thanks everybody have a good one you
Automatic transcript — names and jargon may be misspelled.