Hacking on Superfluid

Francesco Renzi
Superfluid Blog
Published in
4 min readOct 7, 2020

Over the weekend I decided to take a break. Very nostalgic of ETHGlobal events from last year I made a quick and dirty hack on Superfluid.

As a fan of crypto games, I designed a simple mechanism that we could build over the weekend to showcase how easy it is to build on Superfluid and its App Framework.

FlowLottery

Just a simple lottery, let’s call it FlowLottery. Users can join the game by sending a stream of fDAIx (Superfluid Fake DAI) to our contract. All incoming streams are summed and the contract chooses a winner among the participants who receives the resulting reward stream… until a new one is chosen!

A new draw is triggered every time a player joins or leaves, so if you keep streaming you will get lots of chances to win. As the game grows, the reward stream also grows. If the reward stream reaches 1M$/month, being the winner even for just one hour will give you 1400$ (of testnet Fake DAIx! 😜)

The contract took just a few hours to build. There’s only about 3 functions needed for this lottery to work. Everything else is built into the SuperApp Framework itself. The UI took a bit longer and is still very glitchy and unpolished. 🔧 Hackathon style punk 🔧

The code for smart contract and dapp are both available on our github! Just clone our preview repository and you are good to go.

It’s deployed on Goerli, so you’ll need some testnet ETH to try it out! Ping us on Discord if you need some help. You can check it out on IPFS at flowlottery.eth, just follow the steps!

How it works

No balance contract

Every second, the FlowLottery receives money from the participants (in streams) and redirects all of the incoming to the winner. At any given point in time, the contract doesn’t hold the funds streamed by the players.

Over the course of a month, the FlowLottery app could process huge amounts of money streams. At the same time, if it’s ever hacked, the worst that can happen is the hacker could redirect himself the winning stream until the game stops, or everyone leaves!

FlowLottery is a streaming money router. Every second it receives streams of money and redirects the full amount to a different account.

It doesn’t break

All you have to do to leave the lottery is close your stream using the deleteFlow() function. That’s it.

This means as a user you can close your stream from anywhere, you don’t need the dapp’s website to work.

“If you leave, won’t that mess up the lottery? There’ll be 10$ less per month incoming, so the outgoing stream will break!”

No it won’t! Everything is taken care of!

When the user calls deleteFlow(), thanks to our SuperApp Framework the lottery will trigger a callback, updating the reward stream to the new value, and choosing a new winner, automatically

Superfluid callbacks automate interactions, improving UX

Batch calls

If you tried the game, you may have noticed a big button called join lottery. This button hides a lot of magic. By clicking it, you are executing 4 transactions in one, built in javascript. Check out the code:

Build complex transactions straight from javascript

BatchCall() is an amazing functionality that allows you to build complex calls across the superfluid smart contracts and beyond. All Superfluid Token goodies work out of the box, so you can approve, transfer, createFlow, deleteFlow, etc..

Compose a call in javascript, and sign one single transaction in the front-end. Amazing UX!

External contracts can also be called, but they need to support Superfluid’s context. This is a way to tell the receiving contract who the real msg.sender is, allowing the Superfluid host contract to act as a relayer.

Want to test this out? Check out the smart contract, and if you have any doubts, we’re always available on Discord!

🚩 Disclaimer
This dApp is a mere demo. It is not meant for gambling, it was built for fun and to play with the Superfluid protocol. Since everything is deployed on the
Goerli testnet only, the currencies in use are Goerli ETH and fDAI (fake DAI. You can mint it directly in the UI). No real money are at stake.

Hack on Superfluid protocol

It’s time to BUIDL real-time finance!

- Join our discord
- Check out our repository
- Read our docs
- Our Coolest SuperApp bounty on Gitcoin
- Check out some cool ideas on what to build on Superfluid

Some of the teams hacking on Superfluid still have openings, join our Discord to check them out!

Happy Building! 👨‍💻

--

--