Superfluid 🛠How to Build Money Streaming Into Your Dapp
ETHGlobal·Sun, Oct 9, 2022, 12:00 AM
Join Samuel Flamini and Francesco Renzi of Superfluid for a workshop titled "How to Build Money Streaming Into Your Dapp." In the first half of this workshop, we'll walk through what Superfluid is and how it is facilitating a world of real time finance applications. Then, we'll show you how to get started with money streaming as a developer using the Superfluid SDK. This workshop is part of ETHAmsterdam, the first in-person hackathon for ETHGlobal since ETHLondon in 2019. Join over 800 likeminded engineers, designers, and creators to build and learn over one full weekend at the Beurs Van Berlage in Amsterdam. ETHAmsterdam is taking place as part of Devconnect, the week-long in-person gathering in Amsterdam. We're back to build, we hope you'll join us. ---------------------------------------------------- Follow us: https://twitter.com/ETHGlobal​ Check us out: https://ethglobal.co​m Email us at: hello@ethglobal.com
Transcript
[Music] [Applause] [Music] hello yeah there it is okay thanks for coming guys uh small crowd but uh we'll make it worth your while i hope so welcome to the superfluid workshop so superfluid is a protocol for digital native programmable cash flows so you can build cash flows on the blockchain it's streaming money so money moving every second it doesn't require capital lockup which means you can add capital as you go to your streams and it's programmable which means you can build very interesting functionalities into money streaming so what is asset streaming so asset streaming is a way of making continuous payments so these are payments that happen every second they're open-ended which means they're perpetual they don't they don't stop and they're frictionless which means you don't have to do anything once you open a stream they connect value transfer and service provision which means if you're paying for a service you pay for every second of it you transfer value for every second of service you use and this is very interesting because it eliminates unpayment risk it eliminates delays and obviously it happens with no intermediaries because it's a open protocol and this is very interesting because if you can eliminate risk in the provision of services by paying for every second of service you can effectively make stuff cheaper and eliminate a lot of the risks that come with you know service provision employment but also stuff like cross-border teams so the basic units uh of superfluid are super tokens so they're just tokens right they're rc20 they're also erc777 this means that you can use them everywhere you can use erc20 they're also plugged into superfluid but again they're usable everywhere in web3 they're just normal tokens so you can use them from a gnosis safe you can put them in a uni swap pool whatever you want you can do so super tokens come in two flavors you have the native ones which means they're issued through superfluid and you have wrapped super tokens so wrapped super tokens are your regular ether usdc whatever that is then wrapped into a super token and at that point you can stream it there is this rap unwrap mechanic that you have to deal with so how do super tokens manage streams this is through what we call agreements so agreements are new functionalities that are built into the token which are perpetual open-ended financial relationships between accounts so normally when you move money when you use transfer right on your c20 all you're doing is moving money from a to b at one specific point in time when you open a superfluid agreement you're connecting two users balances in a way that will last until you delete that agreement right so for example streams are a connection between a and b where you commit to transfer a certain amount of tokens every second right this agreement stays on chain it's readable it's usable in other applications and it affects the user's balance so by being built into the token balance we can do what we call real time balance so the real time balance is basically when you have a super token and you're receiving a stream what you see is effectively your balance tick up in real time as you get paid right because it's built into the token the settlement is instantaneous as soon as you see your balance go up that money is yours it's available and you can spend it now obviously you don't want to you know be there clicking the button every second but what you can do is have incoming and outgoing streams from the same account at the same time so for example maybe you're working for a company that has subscription payments right so their clients are paying subscriptions in streams and then the company is paying your salary in a stream right so the money comes in from from the customers and then it goes out to the employees and this happens in real time and you're left with the net of those incoming and outgoing streams every second right so again the balance in the middle is immediately available settled every second and you can use it as you wish now as an employee you you're then receiving this salary and what you can do is go on and maybe invest it into an application there's an app built on superfluid called ricochet which allows you to dca into assets by uh streaming money to it so what that would look like is basically what we call network cash flows right so you have the customer paying the business the business paying the employee the employee paying the dca protocol and the dca protocol probably paying its own employees right so all of these connected cash flows come together to basically move money in real time across accounts with no friction no transactions and uh again settled every second on the blockchain right so money is just flowing around freely without gas costs now the interesting side for developers is programmability so with superfluid's programmability you can build what we call uh super apps super apps are basically applications that react to streams so you open a stream to a super app and you can execute custom logic for example this custom logic could say i'm receiving funds from users open streams to another account right you can build this into the talk into the contract and then for example if one of those streamers closes their stream you can react again and add other logic for when streams are closed right so you can build these applications that react in real time to money coming in in real time some of the applications are built to work with zero balance so they receive a hundred dollars a second and they spend a hundred dollars a second so you can build these uh applications that basically move money without ever holding money which is uh probably unique to superfluid so for the hackathon we have eight thousand dollars in bounties uh we try to keep these fairly generic so you can kind of build whatever you want for the top super fluid project we have three thousand dollars for the most futuristic one uh 2000 and then we have some prizes for integrations so we've got a thousand dollars for two different integrations and then there's a ten thousand dollar bounty which we're doing with the uma project which is for a specific integration called pcr tokens uh you can find the details on our twitter it's a really really cool system to perpetually incentivize uh any kind of behavior so you can think of it as generalized liquidity mining where you can incentivize literally anything you want so it's really cool product have a look at the at the spec so i'll pause it to sam who's gonna actually tell you how to get your first uh stream hey guys how we doing thank you for joining us i'm sam i help lead devex here at superfluid and it's great that you join because we're gonna give you some alpha on not just how to get your first stream but also how to win some super fluid swag potentially if you play along so we'll go through that and then we'll get a bit more tactical on how to actually use superfluid as a developer to build some of your projects this weekend so we'll go through some examples i'll walk you through some some of our docs to show you how to create streams pretty easily and we'll uh we'll go from there so getting your first stream alright so those of you that are here you received probably a gigantic swag bag with a bunch of swag from different sponsors in it inside of the bag there is a flyer that is branded with super fluid specific branding and we have instructions on how to get your first stream so there's a link there it's friends.superfluid.finance there's going to be a code that's specific to you enter the code at the link right there will be a form that pops up enter the code enter your ethereum address and you will be sent a stream of friends tokens friends tokens are test net tokens they have zero value right this is this is not real money this is fake uh and you'll be able to see that in the superfluid dashboard once that started so there'll be a modal that pops up which says like congrats you've created your first stream go to this link it'll be app.superfluid.finance where you will see that you're receiving some smiley face fake tokens on coven so why is this cool it's cool a because you're going to get your first stream which is great but it's also cool because this is a game that you can use to either get swag and or be minted in nft uh if you are one of our winners so you're going to get the stream of friends tokens and what happens is that throughout the weekend we're going to track to see how many other streams you open so at this link right at app.
superfluit.finance that's our dashboard you'll be able to use that to open streams to other people so find other people at the event enter their address in open up a stream to them the most streams that are opened will win in nft right so we'll look at the top 10 and nfts of the top 10. so that's the game free alpha for those of you that came uh and yeah let us know if you have any questions so beyond the game we need to talk about hackathon stuff and how to actually build on superfluid so we'll go through some basics for building on superfluid and then some specific examples from our docs so in terms of building on superfluid there are a few tools that might be useful one of them is our visual dashboard which is at app.superfluid.finance this is for easy creation of streams and managing your own accounts but there's also our sdk and developer console right so our sdk is what you're going to actually tangibly use to open streams we'll go we'll go through how to actually use this in a bit we also have some solidity libraries to create streams and to work with the instant distribution agreement to make that very easy and i'll go through those in more detail and i'll also show an example using our developer console which is how you as a developer can do things like write really easy subgraph queries and just track streams for different accounts and super apps you're creating so now getting a bit more detailed right what you need to do to work with super tokens is if you're going to use something that's like an asset you're used to using maybe eth or matic or like any native asset or an erc20 token you have to use super tokens so friend walk through what a super token is and how it works in order to get super tokens or to allow your users to get super tokens you can allow them to call the upgrade function on the super token contract which will just mint you super tokens and basically just hold on to your underlying asset until you want to unwrap and to unwrap what you're doing is you just call downgrade and it will burn the super tokens and give you back the underlying asset in terms of opening a flow that's also pretty simple and straightforward using our sdk or our solidity helper libraries it's literally this simple right it's one line of code right you hit create flow pass in a receiver a token and a flow rate and the flow rate is the number of way you want to send per second so for example with 000 per month writer 1 000 tokens per month it's going to look like that right it's going to be a big long uh number because it's in way so creating the flow is pretty easy right those those actual write operations are pretty simple updating the flow looks very similar and so is deleting and flow reading flow state super simple same thing you can write custom subgraph queries if you want like more advanced data and the superfluid developer console will help you do that but to read flow state using our sdk or even you know some solidity to read flows it's like again three variables and we're we'll return the data that you need there so we won't go too deeply into super apps but if you just want to let you know that these exist um we'll go through how to use superfluid using our sdk which is probably the simpler way to do it if you just want to integrate superfluid quickly this weekend but if you want to build something really advanced i'd highly recommend looking through our examples repo in github for some examples of super apps so super apps are really cool in that they allow you to listen for specific operations that happen within superfluid so for example if i have a contract um i can receive funds and do things when a flow is created updated or deleted into my app right so one really interesting example of this is actually something we call the tradable cash flow it's an nft contract which just will take in streams and send 100 of the value of those streams to the nft's owner so then any time the the flow into the nft is changed it will also change to its owner and anytime you trade the nft the flows are sent to the new owner right so it creates a really cool marketplace for tradable cash flows right that's just one very simple example right we see that forked all the time at hackathons so we'll do next let me exit out of this i will go into my browser here and i think i don't know how i actually have to do this i have to drag this over this could get interesting i can do that in my settings i think hang on displays about the bottom right all right well we'll get creative here any mac wizards want to tell me how to not mirror my screen [Music] ah help me out help me out help me out so okay we show mirroring options what do we got here display what's the redness display airplay display off it is much easier on zoom guys you can just throw that to the other thing with this you want to have that there yeah that will this one this browser there that guy there we go sometimes we should be able to just push it up i'd say what i can do hang on all right there it is there you go good no i don't i don't think that dude which window this one i want to show that one yeah there you go there we go and now just make it full screen using using what here exactly uh can you come around to see it all right here we go thank you sir all right so this is our developer console right this is actually the faucet contract which is distributing the friends tokens so if i scroll down here we can see a bunch of streams from people that have already done it right so these people already have the jump on you so you should you should try to catch up to them um but if we go to our docs this is where i actually recommend starting with superfluid you can go to this money streaming section and use this section to take examples you can use in your own hackathon projects right so these are just little code sandbox examples you can open if you want to but you can see here they have very simple react code where you can very easily create a stream right so i have this create flow operation here where i just pass in the flow rate the recipient and then the token address that i want to use and uh we write the transaction using this line and boom your stream is created so if i get one of my ethereum addresses so this is gonna get really let's see if we can pull this off here so i'm gonna i'm gonna copy this here paste this in and then i'm going to enter my flow right here which is again just in wave per second and we'll calculate the formula down here for you to figure out how much this is per month i'm going to enter in an amount something like this we'll see how much this is that's quite a bit let me let me cut that down a little bit and we'll create a stream the console will just register what's happening here this is taking a second or two to create so it's creating right now amazing this was created for me and what i could do is copy this address in and paste it into our console right so this is kind of the workflow you guys could go through as you're developing you can create some streams and then very easily see them pop up here in the console right so we saw this stream was just created looks like 15 seconds ago we're using fake die on kovin and we can see that this is coming to my wallet same thing with updating updating is also really really easy right we just changed one line so if i want to update this stream what i'll do is i will just copy this address here go down to the update paste that in and then you can see here if i scroll down same exact pattern what we're doing with the superfluid sdk is we're initializing our framework right using this simple line up top we create a signer this is a private key with fake money don't don't take my private key you don't have to do this with just an open private key you can use metamask but this is this is creating a signer and then same thing here where i am grabbing the token passing the token is a super token we're using and instead of create flow we're just updating flow just like this very simple and it should work just as well so let's choose a new flow rate this time something different and we can see the the calculation down here for us we'll update this will take a second just to propagate updating our stream boom stream is just updated so if we go back up here same thing i can refresh and we should see that it should no longer be 860 whatever that was per day should be something different yep looks a bit different right and then finally deleting is also really really simple that's not the right to have uh we can we can just delete it flow by pasting in the address here and we see that again pattern is exactly the same we're just calling delete flow and once again this should work just as well while this loads i do want to let you know that i have an example here using a metamask or web3 provider so if you want to have an easy just like wallet connection using the superfluid sdk and then also um do this just using whatever while it's connected to your browser you can do that using this example right so i recommend just taking this one you know it'll connect whatever chain id you're connected to and it will work exactly as these ones work up here so if we go back up we'll see if we refresh one more time that this stream that we had should be deleted yep it was deleted and again this this console is a very useful tool where i can do things like see whatever super tokens i hold right this is very useful in that sense we can see events right so we see all these different flow updated events submitted every time i update a flow and we can even do things like pull up this address in the in the sub graph explorer alright so we have an embedded instance of graph eql in here and i could just click play and get some information on this address so again we use this all the time internally a lot of developers in the superfluid ecosystem use this and i highly recommend you use it as well it's also useful like i use this for all my my subgraph queries so i recommend playing around but that's an overview of using the superfluid sdk again fairly simple examples there but that's just to show you how easy it is to get started and to give you some examples to pull from directly when building your apps this weekend so i recommend coming here to start there's also other good interactive stuff on using super tokens batching transactions together using the instant distribution agreement to send funds in bulk to large groups of addresses and then other stuff on super apps and solidity you can find in here as well so start here dm myself or friend on discord or dm us at all in our discord if you have any questions you can't find us if you're here and you have a question just come to our uh booth which is just the first very first booth when you walk in on the left and we'll be there to help you there as well so thanks for bearing with us while we turned around and did it but uh yeah if you have any questions this is a good time for q a as well yes i have a question so basically if i understand it simply by so okay and you are distributing the form and then you stop at one point and then let's say uh party b has 20 and party a has 80 how do you transfer from this super and token back to the normal token then because you don't have to 100 anymore how do you get the time back so the it's always one like you're not you're not necessarily locking up capital and distributing it that way right so what you're doing when you wrap like let's say an example of die right if you want to stream die all i have to do is just wrap my erc20 die as super token die so when i do that let's say i want to wrap and stream tendai right what i do is i wrap the tendai those 10 are deposited in that rapper contract i'm mint to 10 super token die and i can spend that but i can never spend more than what i have right so if i spend five stop my stream and then unwrap i now have five tokens left right so the the bit with with having 80 or 20 percent of the total value started with i don't know if i necessarily understand what you're asking there because you aren't going to be able to spend any money you don't have or end up with money that you should have that you lost does that does that make sense uh yeah the idea was actually if you have a contract and you say you want to pay i don't know 2000 and you define milestones and he does deliver everything he delivers only 60 and then you have a situation of this 2000 time he has only 200 you have 800 and now how do you close that he gets again cats so basically you pay him partially you don't need to transfer everything so basically if you have a contract it's partly finished and you already super act okay are you kind of referring to like an example that would be corresponding what you're saying i think it's like investing like if i agree to invest you 100 tokens over the course of a year right i have to like you're basically entering into a contract with me where you're expecting me to pay the full 100 tokens right so in this case what you're doing with superfood is you're not you don't work off of the total amount you want to send you work off the number of tokens you want to send per second right so what i would do there is i would pick a flow rate that would add up to that amount but there's nothing stopping me as the sender from running out of tokens if i mismanage it or from just stopping the stream before all the 100 are sent right so or adding and you can also add more right there even like auto top up services you can use now that we've built where you can always make sure that your balance is is above zero but um yeah that's the idea is that does that make sense if you switch the metal model instead of thinking uh and if you want you could set up a vesting contract where you say all right look i'm gonna deposit a thousand tokens into this contract and the contract's gonna stream that one thousand out that would be a way to implement like what you're what you're suggesting right and you'd have proof that okay the contract has this money you as the receiver can cancel the stream but the contract could be immutable and just keep it going can the party exchange it again once you want to imagine like in a situation you are streaming like 20 super tokens per second and then he goes i don't know 500 of them and you stop everything so he has 500 of this super token is he able to convert that back to the normal tokens yep yep they can unwrap at any time right you could do it like the second you get a single die you could you could unwrap it yeah okay the other part you can do that also yep 100 perfect okay that's all whatever cool yeah good question thank you anywhere else something about um idea yeah yeah so the ida we didn't talk about it a ton here but what the ideal idea lets you do is one-to-many like scalable single transfers right so money streaming is all about second by second gradual transfers it's continuous payments the idea what what it allows you to do is create essentially a pool of people that have units in an index right so imagine imagine i create an index where i give 10 units of that index or 10 shares to 10 different individuals what i can do once i've done that is execute a single transaction to distribute funds to that entire pool right so in the example of a pool with 100 units and each person having 10 units if i send 100 if i distribute 100 tokens to that group each person will get 10 tokens right because it's divided automatically right but which is a good use case for things like airdrops or any any kind of repeatable one-to-many transfer you want to make the cost is fixed the only the only times you need to uh thank you appreciate it i'll just go back so the the only time that you will uh pay gas is when you uh issue new units to new subscribers right you need to change the units um after that it's a single transaction to distribute funds to the entire index right so think about it as the way i like to explain it sometimes it's a bit like imagine if a corporation could just issue uh shares in their stock to individual people and press a single button to pay a dividend to everybody in that in that pool right it's it's the analogy to a cap table is a decent one thank you no problem yeah so emitting events is more of like a general thing where you want to listen for that event maybe on the front end and do something in exchange the the thing that's a bit analogous to that in a super app is these callbacks right the callbacks will run on chain in response to certain events right so for example what you can do in a super app is run arbitrary logic anytime a for example a stream is created into the contract updated into the contract or deleted with within relation to the contract right those all happen on chain and what's happening there is the superfluid protocol when you create a super app it's registered in the constructor with the protocol at large right there's just a line of code you insert there and it does it the superfluid protocol will listen for those certain events and execute those callbacks whenever one of those things happen on chain right so that's i think i'd say the cool part the cool part about that is that it's all on chain and you don't have to do any magic with subscribing to events and listening to it on a front end yep do you need super tokens to do ideas yes yes anything within superfluid you need super tokens for one one other thing you can do that that's cool about super tokens you can you can bash transactions together so if i want to open up six streams at once and then also transfer uh super tokens to three different people in the same transaction what i could do to provide a good ux to my user is if i know there's going to be these kinds of group things happening on a regular basis is bundle them all up using a batch call and instead of having to execute like eight different transactions to do all that stuff i can bash them all together into one and it's a single transaction and i pay gas one time to do all of that but you have to have super tokens to do it but you know it's it's an interesting scalability thing and if i want to have the super token of another token um is there a way to know if there is already a super token that token like if i want to the super token of die how do i know it yeah so one way is to look in our um our docs we have a network directory from some of the most used tokens i think you can search by symbol in the console i'm not mistaken so you can search by symbol in the console like when i was type when i pasted in my address to the console you can just type in a symbol and see if it comes up you type in the symbol like the normal ticker symbol and then x after to see if it exists right but to deploy your own wrapper it's really easy so we have instructions in our docs you'll see in our docs there's a guide on super tokens in that guide you can we give you step by step how to deploy your own erc20 wrapper from like etherscan or polygon scan as a block explorer right it doesn't matter if there is yeah so if exactly so if you want let's say for example you decide to mint some like you started dow tomorrow you meant an erc token as your governance token you want to stream it right what you could do there is go to our docs in that example um go to the super token factory contract which lets you easily create super tokens you just need the underlying address of the token and you fill in a couple of other parameters and boom you have your own super token contract created right that's pretty simple cool any other questions or something like that who checks that's not centralized that someone can i will not change so the stream in itself is some it's not launching it's it's our change the section itself right here but how is it that's that's part of the magic right so that's part of the magic right so what's happening there and we could we could go as far down the rabbit hole as you'd like but you can think of the super tokens as a kind of new uh token standard that's an extension under the erc20 standard right so with your metal model the linear c20 transfer there's no way this is possible right because you're going to have to hit transfer over and over and over again but what the super token has on it is a new method called real-time balance of which kind of plugs it into the rest of the protocol right so what real-time balance of does is it can take into consideration like your static balance like how many tokens you have that aren't moving at all and it can take into consideration your dynamic balance which is uh the we call it netflow is one of the parameters there which takes into consideration all the flows you're receiving or sending out to get like at this moment in time are you are you net positive or negative right so if you're sending more tokens and you're receiving you might be your net flow might be negative 50 tokens right that's simplified because it's going to be in way but imagine negative 50 tokens is your net flow what we can do is if you're sending out 50 tokens per month right what we can do is we can also use the block.
timestamp value to calculate how much time has passed while that was your net flow rate right so at any given point in time if we need to within the protocol like if you decide to unwrap your tokens right away we can check your balance by calculating how much time has passed and that net flow rate to figure out what you're actually you know what what you have an even point in time that's how it works okay nice i'm impressed yeah you should try it out you should try yeah be cool cool can i invest in your token one two one two all right i think we're good thank you guys for coming we really appreciate it [Music] [Applause] [Music] you
Automatic transcript — names and jargon may be misspelled.