Compound: Building on the Compound Protocol
ETHGlobal·Sat, Oct 9, 2021, 12:00 AM
Join Adam Bavosa of Compound in a workshop for building on the Compound protocol, all part of HackMoney 2021 - ETHGlobal's virtual 3-week hackathon for hackers looking to build decentralized finance applications. HackMoney 2021 runs from June 18th to July 9th, 2021.
Transcript
well let's start over hi welcome to the building on compound protocol uh we have adam bavosa here from our sponsored compound um and he's gonna be running this workshop today um adam i'll leave it to you to introduce yourself and get started sweet yeah thank you i'm happy to be here uh happy to be back at hack money for uh the second one and happy to be at uh e global hackathon once again uh so yeah i'm adam and i working at compound labs and today i'm going to be giving a little code workshop on uh how you can build on top of the compound protocol and we have some sponsor bounties so these uh this workshop will help you go towards those bounties give you some education there so you can get started also some open source code which is great so i'm going to share my screen and i'll have a little presentation for you all all right okay here's my slide deck um great so happy to be back at hack money so i'm adam and i'm the developer relations lead at compound labs i'm here to help you out with your compound protocol related development projects i'm available uh in discord and also on twitter you can ask me questions there i give help to developers all the time and there's a great community in our discord so if you show up there and look for development help there's lots of great people there to help you out so today i'm going to go over our bounties real quick i'll explain what the compound protocol is if you're not familiar i'll show you some developer resources so you can start writing code right away and also give a little dev workshop to give you a preview of what it's like to build on compound so compound grants is a independent organization they are able to compensate developers for building cool stuff on ethereum and compound so they are sponsoring hack money this year uh company's awesome organization you can apply for grants at any time they're super cool they are sponsoring hack money so the values are thanks to compound grants so the bounties are uh the most the project with the most original use case for supplying assets the compound protocol uh also the most original use case for borrowing assets from the compound protocol and also the most original open source interface for compound all these bounties are for two and a half thousand dollars and uh there's some more details on the compound grants website at compoundgrants.org great so compound compound is a protocol on ethereum and what it enables is for developers to build applications so the users of those applications can supply their crypto assets and earn a interest rate on those assets so currently the protocol supports 11 assets and users can supply them for any number of ethereum blocks and earn interest on their assets so at the current time there is about 12 billion dollars worth of assets applied to the compound protocol and the other side of supplying is borrowing assets so users that supply assets as collateral can also borrow assets from the protocol and the interest that borrowers pay for borrowing is what the suppliers earn as interest for supplying so let's say you have a whole bunch of wbtc you can supply that to the compound protocol and you can borrow other assets like usdc against your wbc wbtc by using compound so if you supply asset you earn interest and if you borrow an asset you pay interest so that is the gist of the compound protocol and how it works there are 11 supported assets at the moment there were two added very recently those are chain link and true usd the stable coin um all of these assets are able to be supplied to the protocol but only some of them are able to be used as collateral to borrow assets so you can borrow any asset and supply any asset but only some assets are capable to be used as collateral at the moment uh and there is constant changes the protocol through compound governance so yeah here here's uh the current supply in the current borrower so there's about 5.3 billion dollars worth to borrow and of course like i said earlier there's about 12 billion dollars worth of supply and of course as um assets move in and out of the protocol and prices change uh those amounts change all the time uh yeah so at the moment there's about 290 000 suppliers and about 8 000 borrower accounts those are ethereum accounts that are interacting with the protocol so another important concept in the compound protocol is governance so the way that the smart contract code changes in the compound protocol is completely through decentralized governance so it's now been about one year since the compound governance token was created and it is slowly being distributed to all users of the compound protocol so if you supply assets protocol or borrow assets from the protocol you slowly earn a drip of comp token that is accrued for your address and you can claim it at any time and what you can do with this comp token is vote on compound governance proposals so those that have enough comp voting weight at their address can create proposals which are code changes to the protocol so let's say you want to add a new asset to the protocol or you want to change an interest rate model or change a reserve factor something like that you can write the code deploy it to the blockchain and create a governance proposal and then all the comp token holders can vote yes or no for that code change so if a uh compound governance proposal uh has enough votes in favor and it succeeds then uh the code on chain changes based on that new proposal so that is how code changes are made and it is done completely through decentralized community governance so you can check out the smart contracts on github they're available in the compound finance github org it's open source and you can check it out and start building on the protocol right now so like i said there's the comp token which is the compound governance token um here is the flow for change proposals this changed very recently there was a proposal to change the voting delay so once a code change is made a user can propose that the code change be implemented in the protocol and there's a two-day waiting period this is to give the opportunity to all users and um fans of the protocol to check out the code make sure that it does what it says it does there are no bugs uh stuff like that sort of give their feedback on like the forums or in discord and once that two-day proposal wait has passed then there's three days where all delegates and holders can vote so they can vote yes or no on the proposal and once voting ends if the proposal succeeds it goes into the time lock for an additional two days this gives projects that depend on the protocol as infrastructure the opportunity to update their apps to reflect the pending changes to the protocol so in that two-day period folks can update their apps to accommodate the new changes and then after two days anyone can call the execute function which will officially change the code of the protocol to include the newest code change if you go to compound finance slash governance you can check out the governance dashboard which will show you your voting weight if you connect your wallet and also show past proposals a little information about them if they succeeded or failed so you can learn about the history of compound governance if you check that out on the compound finance website there's also a leaderboard which shows the top voting weight addresses addresses that have more than a hundred thousand comp delegated to their address are able to create code change proposals and everyone else can vote on those so if you're familiar with compound you might have heard about gateway uh gateway is uh completely separate from the v2 on-chain protocol it is uh we call it a v3 and basically what it is is it enables supplying and borrowing of assets to go beyond ethereum and create a cross chain worlds where gateway is its own chain and other chains can supply assets is collateral by using a star port so say you have tezos or you have assets on polygon or another chain you can lock them in a star port on that chain the gateway chain will read the events of the locked assets on other chains and have a account of that collateral on gateway so once collateral is locked in the starport users can begin earning interest just like they do in v2 except in v3 it is earned in cash which is a unit of account on gateway it's pegged to one dollar and all interest is earned in cash on gateway and also uh interest is paid in cash on gateway so gateway is currently in development it is uh in testnet right now connected to ethereum robsten testnet and uh it's it's it's uh looking to go to mainnet hopefully sometime this year uh so i just wanted to give a little preview to gateway uh for you all because we get lots of questions about it but the bounties in the hack money hackathon will be completely for v2 ethereum based compound protocol so here are the resources for today's workshop you can head over to compound finance docs to check out our smart contract documentation all the addresses on each of the test nets and also mainnet for all of our contracts are available there as well as api documentation and compound js sdk documentation in my workshop today i'm going to be using the compound.js sdk uh so all the docs are available there on our website you can check out governance as well like i showed my previous slides if you want to join the conversation in discord uh come to compound finance discord and join our community there's lots of developer guides as well as education resources on the compound medium blog and that is also linked from the docs and of course you can head over to github and check out our github organizations which have open source code and also the compound protocol source code so you can check that out here is a link to the repository for the workshop i'm going to be going through today so you can visit that url to go to the repository or you can scan this qr code and head over there so i'm going to give everyone an opportunity to take a pic of this qr code real quick so you can head over to the repository all right so before i continue i just want to say thank you again one more time to compound grants they are the ones sponsoring hack money and their great organization helping developers get compensated for creating their uh code and apps and protocol changes for compounds so head over to compoundgrants.org there's a request for proposals there so if you want to find something to work on there's a list there so very awesome organization happy to work with them great so that is my slide deck next i'm going to go through my workshop so here is the github repository i'm going to be building a web app a simple html css javascript app of um an app that allows users to supply uh assets to the protocol and also redeem their assets later once they want to take their interest so here's the repository i already have it on my computer so i'm going to pull up the code now so we've got html i'm going to run a local web server so we can check out the app so i'm going to run http dash server this spins up a local web server and at this address i can go to my browser and check out the app so i have my page here here's my interest rate app it has a button to enable ethereum and also a way to supply and redeem ether so i also have my developer console open so we can see any errors that pop up so i'm going to connect my meta mask to the app so um instead of using ethereum mainnet i'm going to use uh localhost 8545 which is my ganache so this way we can have a local blockchain running on my machine and use that as like a test environment so i need to run that first so i'm going to use ganache cli i'm going to head over to my terminal make another terminal window and i'm going to run this command i have saved here uh it's ganache cli i'm going to fork mainnet you can grab a json rpc url to connect to the mainnet blockchain you can grab that for free at infura.
io or alchemy.com i also have a test mnemonic of ethereum accounts that i only use in my test environment pointing to mainnet so i'm using an uh chain id of one and also i'm unlocking uh the cusd address but i will explain why later it's not important right now so i'm going to start up this localhost chain and each of my accounts here have a 100 test ether so i can do stuff in my dev environment so i'm going to try to connect again nice okay so my localhost account has a hundred ether uh great so let's check out the html that makes this project so we've we're importing some css which i'm not going to go over we have our interest rate app we have our enable button which is that big blue button i already have the account connected but if i were to disconnect it and refresh the page i get an error in the console because the javascript is trying to use the blockchain but it can't because uh ethereum hasn't been enabled in metamask yet so recently the metamask requires you to have a button like this to enable ethereum so i'm gonna enable it choose my wallet of choice click next connect and now if i refresh the page no errors in the console so that's how the ethereum button works i'll show you the javascript soon i also have a box here where i enable users to supply their ether so there's a label of eth we show the current apy of eth the percentage that you earn per year and also we have a button and an input for supplying and also redeeming of c tokens so you can see that here we have our apy it's currently 0.14 for ether and we can supply or redeem as well so let's head over to the javascript which is the fun part um at the top we have a reference to compound.js i forgot to mention in the html we're using compound js which is the sdk for javascript for doing things on the compound protocol gives you nice one-line methods for doing stuff like supply and redeeming so i'm creating an instance of the sdk in my app um i'm using the ethereum provider window.ethereum then i'm quickly grabbing each of the html elements so i can reference them in javascript here's my ethereum button when i click that enable ethereum button that big blue button we're sending a request that enables us to access the accounts in metamask next we have event handlers for our buttons so if we want to supply we would hit that supply button we'd fill you know a number in here and hit the supply button the amount comes from that text box and we're calling the supply helper function i made so basically what it does is it makes sure that there's a number in that text box not a letter or anything else um and then we do a transaction so uh we're creating an ethereum transaction by calling compound.
supply we pass the asset type which is ether and also the amount so once the transaction goes through we log it in the console so the user can check out their transaction if something goes wrong we alert it so they're aware that their transaction did not succeed there's also an equivalent redeem function um one liner nice and simple again so that would happen when i hit the redeem button and there's one more function a calculate apy so compound js allows uh users to do not only compound protocol functions but also ethereum based functions so we can read from the blockchain we're able to pass an address an api that we can type out and also parameters for the function and we can pass a provider so this will read the supply rate per block directly from the c token contract so that's how much interest is earned per block and we can use this equation here where we scale up to uh you know weigh so 1 times 10 to the 18. the amount of blocks per day based on 13.15 second blocks and we have 365 days per year if we use this equation we can find the apy percentage and this function will return it so when the page loads we're running our event listener for page load and we grab the apy and we put that into the text box on the page so uh when our ethereum provider was not connected this number did not pop up but once we're connected to the network we can calculate the apy and that's 0.14 so if we want to check to make sure that that's correct you can head over to compound.finance and if you click on markets at the top you can head over to the markets page and see that the current apy is 0.
14 so our app is correct in calculating that so i'm going to quickly supply some assets show you how that works so we have 100 each i'm going to supply 5e so i'm going to hit the supply button and um metamask creates a transaction that this is powered by compound.js so we're going to send five ether to the compound protocol using the mint function and there's a little bit more than five for the gas feed so i'm gonna hit confirm uh we currently have an error because ganache is um just got booted up so the account is confused so i'm going to re reset the account in metamask so the nonce is correct okay i reset it so i refresh and i'm going to try that again and try and supply five eth at our transaction confirm and now it actually worked so if i pull up my ganache cli you can see the transaction happening and we should see that i now have a little bit less than 95 ether and my amount of c ether should go up so c ether is about 50 to 1 so that ratio is always changing such that users can redeem more ether for their fixed amount of c ether so as the block height goes up they can redeem more of their assets so they're earning interest so we get about 250 ce in return and of course i can redeem that so i'm going to redeem 200 of the ce uh metamask is going to pop up again thanks to uh that that redeemed function in our javascript code and i'm gonna hit confirm and we get another transaction logged here another transaction in ganache cli and we're gonna see my ether amount go up and my ceas amount go down so metamask needs to cook for a second and make its requests to ganache so it looks like we're getting some balance yes okay so my ether went up to 100 almost 100 and my ce should go down now it's down to 50 went from 250 to 49.44 great so uh the next part i'm going to show is i as you know there are 11 assets that work with the compound protocol uh i'm going to show how to use it with an erc20 token so quickly i'm going to go back to my repository i've already written the code for this change if you go to pull requests and support usbc in the app we can see that with a little bit of code changes we can support usdc so i know i'm running low on time so i'm going to do this pretty quickly i'm going to copy this html and put it in the html here so i'm just making another box uh so you we have this ui here we're gonna have another one beneath it for usdc so yeah we have another box we're going to calculate usdc apy and have a supply and redeem button the javascript side we're going to wire up those buttons i'm going to grab uh references to them so now we can access each of those dom elements we're gonna add uh some button event handlers just like we have for the the ether ones so that way we're able to supply and redeem our cusdc and the last thing we're going to do is calculate the apy when the page loads so i'm going to go down to that event handler for the load and in addition to eth we're going to get the usdc apy so i'm going to refresh the page now we can see that we have usdc as well so we have 1.91 apy on usdc so if i go to my metamask wallet you'll see that i don't have any usdc so i'm going to do something that we can only do on localhost i'm going to take some usdc from an address you can see that i initially unlocked the usc usdc address i'm going to take some usdc from there to use in my app so i could do that by going to um one of my other repositories the supplying assets of the compound protocol repository it's linked in uh this repository read me so uh we can head over there and i have it pulled up so i'm just going to run the usdc script so basically what that does is it um takes 50 usdc and puts it in my account so if i go over here to metamask i now have 50 usdc yep it's going to pop up here so now i can supply so i'm going to supply 25. i get two different transactions so first one is going to be the erc20 allow so i allow users to have their assets sent to the the protocol i'm going to confirm that as soon as it mines it's going to pop up another one for mint so there's zero ether this time except for the gas fee and we're going to mint um cusdc so if i come back over here this number will go down to 25 and my cusdc number will go up i'll pull up my ganache cli again there we go okay so we have 1133 cusdc and 25 usdc so yeah that's how you build a interest rate app on the compound protocol that supports both ether and erc20 tokens i know i'm running low on time so that's the end of the workshop i can answer questions now quickly if anybody has any questions about code or projects okay i'm going to pull up the chat okay so i see ian is in the chat um definitely use in environment variables um so you don't reveal any secrets of course and yes ian's frequent in our discord so i'll be seeing him there okay looks like uh there aren't any other questions [Music] okay if there's no more questions um i'd like to thank a compound one more time and adam uh for running this workshop thank you guys for coming uh thanks for having me great day he has nowhere to find him in the discord i'll be there bye buddy bye everyone
Automatic transcript — names and jargon may be misspelled.