Compound 🛠️ Building on Compound III - Adam Bavosa
ETHGlobal·Mon, Oct 9, 2023, 12:00 AM
Join Adam Bavosa from Compound for a workshop titled "Building on Compound III". Welcome to ETHGlobal Waterloo, where it all began! Returning to Canada's tech hub, we're thrilled to bring together international enthusiasts and local talent for an unforgettable hackathon experience. Don't miss out on this exciting opportunity to showcase your skills, collaborate, and innovate with the power of Ethereum. Be a part of the journey that continues to shape the Ethereum ecosystem. Secure your spot today and let's build together at the heart of Canada's tech scene! ----------------- Follow us: https://twitter.com/ETHGlobal​ Check us out: https://ethglobal.co​m Email us at: hello@ethglobal.com
Transcript
foreign [Music] hello everyone and welcome to the compound grants developer workshop for youth Global Waterloo 2023 my name is Adam bavosa and I am the developer relations lead at compound Labs you can find me on Twitter at adampevosa and also in Discord I'm Adam B I'm here to help out with protocol related development projects whether you're building something in solidity or JavaScript you are integrating the compound protocol or building on top of it or developing an upgrade for Community governance of the protocol I can help you out with your code and also brainstorming your idea so what we're going to go over in this Workshop is the hackathon bounties for compound grants I'll tell you a little bit about the compound protocol in case this is the first time you're hearing about it I'll highlight key differences between compound V2 and compound V3 compound V3 came out in summer of last year I'll go over the governance system for protocol Community governance and also share some developer resources so you can get started working on your project immediately so the compound grants bouncy price for this year is five thousand dollars USD to the best integration of compound three the project must interact with smart contracts or Json RPC with any instance of the V3 protocol or be a supportive project that is specifically created for the compound communities use so a little bit about compound in case you're not familiar I work at compound Labs we are the authors of the compound protocol a protocol made up of several smart contracts running on the ethereum mainnet since 2018 our mission is to create efficient algorithmic money markets and what gets me excited to work in the defy space is that we author and continue to Steward decentralized free and open crypto tools for everyone with an internet connection including the banked the underbanked and the unbanked this is a incredibly huge amount of potential Market to serve globally if you think about it anyone on Earth with a smartphone and an internet connection can use defy and can use compound historically we've seen interest rates for suppliers as high as 15 apy on usdc as well as all the other supported assets that I'll share in a little bit I love that what we're doing is different from crowdfi because D5 systems like the compound protocol are transparent and voluntary systems with public ledgers where code is law with defy you instead get decentralization and transparency unlike centralized Banking and Financial systems that's what gets me excited to work in D5 and also in the ethereum space what is the compound protocol it is simply put crypto asset money markets we refer to it as the compound protocol it runs on ethereum and also some other chains some other l2s like polygon and arbitrum it is decentralized which I'll explain shortly in the governance section the main use case is that it allows users to earn interest on their cryptocurrencies or permissionlessly borrow other cryptocurrencies so instead of an asset like ethereum or usdc sitting idle in your wallet you can supply it to compound and earned a varying interest rate users can supply crypto collateral and borrow a different crypto asset against their collateral the interest that suppliers earn is driven by the interest that the borrowers pay those are the two basic uses of the protocol for everyone earn interest on crypto or borrow other crypto assets and you can do that completely permissionlessly and no single entity controls any of those assets that are stored in smart contracts which is controlled by Community governance at this time compound can only support crypto assets that are tokenized on ethereum or other evm chains as erc20s and it allows suppliers to earn a variable interest rate on their assets and an individual that uses adapt like the ones listed on the compound home page or their own code that they write can use every feature of the protocol developers can build their own apps that use the protocol as interest earning infrastructure users or organizations can use interfaces to earn interest to borrow assets or participate in community governance so use cases we see are crypto wallet applications using the protocol to offer interest earning for their users also any business that holds assets for a period of time they can use the protocol to supply and earn interest for a period to create some income instead of letting those assets sit idle and here is a example of a supply operation let's say you have uh one of the many interface applications from the protocol and you want to supply assets to compound when a supplier supplies they get a fixed amount of C tokens which you can think of like a code check for your underlying assets and as time progresses more underlying can be redeemed for those same amount of C tokens in compound V2 so as time passes interest accrues to the C token Alder in compound V3 C tokens are only minted for supplying the base asset not for collateral and those v3c tokens are one to one rebasing so the balance will increase in your wallet as the supplier earned interest so there's more differences between V2 and V3 which I'll share later assets are stored in smart contracts that no individual controls there is an on-chain governance system that will be covered later C tokens are redeemable at any time users can supply for as little as one ethereum block and interest accrues every ethereum block which is about 12 seconds nowadays this enables on-demand supplying and redeeming from the Protocol no need to wait for a lockup period you can earn interest for supplying to the protocol for even just one minute so the other key feature of compound is borrowing the protocol enables over collateralized borrowing of assets so a user must Supply collateral before they can borrow and they can only borrow up to a certain amount of the USD value of their collateral so their total value of their collateral is always worth more than their total borrow relative to USD so an example would be that a user supplies collateral like wbtc or eth and then they can borrow a smaller value of an asset like die or usdc and keep it such that their account is over collateralized so the limit of the borrow depends on the collateral asset there's a percentage called the collateral factor which is set for each individual collateral token type Ethan wbtc have different collateral factors and those factors are chosen by the community members and are selected based on perceived risk of the value fluctuation in order to borrow a user must Supply a supported collateral asset and they can then borrow any asset supported by the protocol and the borrowed asset will subsequently be held in the user's wallet with no strings attached in V2 borrowers earn interest on their supplied asset Just Like A supplier does however their borrowed asset accrues interest as well this is not exactly the same in V3 I'll cover it more in depth soon if an account becomes under collateralized like when the USD value of the collateral goes down or too much borrower interest accrues the user see tokens of collateral can be clawed back to the protocol when a liquidator performs a Liquidation on the account supplying only is safer than borrowing you can't get liquidated if you only Supply and there's much less risk involved when a user supplies and does not borrow Liquidators keep the protocol safe and collateralize they are incentivized to do so so borrowers that become undercollateralized are subject to liquidation of their account they can lose some or all of their collateral and due to the autonomous nature of the protocol the liquidation system is much harsher than in traditional Finance there's no negotiating there are no delinquent repayments so compound V2 has 18 supported assets that can be supplied to earn interest or they can be borrowed not all of them can be used as collateral some assets have a zero percent collateral Factor so you can't use them as collateral to borrow a different asset and I will go over V3 as well uh this version of the protocol is more gas efficient and capital efficient it streamlines the most popular use case of V2 which is to supply a volatility priced asset like wbtc and borrow a stable priced asset like usdc this version has additional safety for liquidations a higher threshold for the same collateral factor and one asset can earn interest or be borrowed per deployment so this asset is called the base asset it makes opening positions in V3 have less stacked risk than V2 currently there are two deployments of compound 3 on ethereum mainnet there's usdc and with so you can earn interest on usdc or week if you use one of those deployments of compound V3 uh there of course are other deployments on other chains there's polygon and arbitrum they both have their own usdc deployment so the key differences between V2 and V3 are that V3 has less risk less gassiness and the V3 protocol can be deployed anywhere that salinity is supported in V2 you can borrow any supported asset but in V3 you can only borrow the single base asset per deployment so compound V2 and V3 coexist compound V2 is not going anywhere there's no deprecation schedule because the use case of V2 is somewhat different from V3 users are still using it in high volumes on ethereum mainnet so here's a list of differences between compound V2 and compound V3 the C token system for v2 is a little bit more confusing the assets that you supply you get about 50 to 1 ratio of C tokens return in return for minting and the redeemable amount of underlying that comes when you redeem a c token in V2 is always changing so as the block height goes up you get more underlying for your same C token so that's the mechanism in which interest accrues to the supplier and in compound B3 the C tokens are a little different there are one to one and rebasing so if you supply five usdc you get 5c usdc V3 in return and then amount will go up in your wallet so in combat V2 you can borrow any supported asset like I showed on the screen earlier and in V3 you can only borrow the single base asset per deployment So currently there are two different kinds of deployments uh deployed across three chains there is withe and usdc so you can only borrow with or usdc compound V2 is a single system with many assets it's a monolith running on ethereum mainnet only and compound V3 has a single asset Focus per deployment and can be deployed on any evm chain uh the max borrow and liquidation are the same in V2 so if you borrow the maximum amount Allowed by the collateral you supplied to the protocol you will get liquidated in the next ethereum block which has left some users unhappy because they immediately got liquidated and they didn't realize that they shouldn't have borrowed that much compound B3 has a little bit safer mechanism built in the maximum borrow and the liquidation Point are separate so if you borrow the maximum amount Allowed by the the collateral you supplied you won't get liquidated immediately there's like a no new borrows period in between those two points so there's some time to either top off your account or repay your borrow before getting liquidated so in compound V2 you earn interest on any supported collateral asset that is supplied in V3 there is no earning of interest on the supplied collateral you can only earn interest on the base asset So currently you can only earn interest on usdc or wheath in V2 some assets are not valid collateral but they can be supplied to earned interest V2 has an integrated comp distribution into the protocol contract code for the Comptroller which is also in charge of supplying and borrowing as well in V3 this is more encapsulated into a special contract called the rewards contract so any reward token can be added to compound V3 and in compound V2 there is one account controller the self uh so only Xerox ABC can control 0x ABC's position in compound V2 but in V3 there is a system for allowing account managers to say you have a special contract that is a team contract it could be controlled by other users outside of it like a list of allowed managers that the account needs to approve or can revoke privileges for manager so they can do things like Supply borrow or transfer within the protocol as an allowed manager so let's go over the compound governance system it was the decentralization of the protocol which occurred in 2020 the contracts for compound governance are available on GitHub in the compound Finance organization and uh the governance is controlled by the comp token holders and Community proposals are what manage upgrades of the protocol the comp token is a governance token it can be used to vote yes or no on proposals that are created by community members and if you are a Community member that has more than 25 000 comp delegated to your address you can create proposals and those proposals are literal code changes to the salinity code or the parameters for the contracts and anyone can create those if they have the delegation and anyone can vote if they have comp tokens so here's the governance proposal process there's a two-day review period once a user creates a proposal so other community members can investigate this proposal make sure it's safe make sure it does exactly what it's saying it's doing and one of the consequences once it is executed uh like other apps that depend on the protocol what would they have to change to support this new protocol code change there is a three-day voting period where comp token delegates can vote uh yes or no on the proposal if there is a vote that exceeds the Quorum of 400 000 votes and also is uh in favor instead of uh no vote it will succeed there'll be a two-day lockup period where apps that depend on compound can prepare for this upcoming change that will be executed in two days so then the proposal is queued and executed by anyone in the community you can hit those Governor smart contracts yourself if you want to be the one that accuser executes that's a little bit about governance here are some developer resources that you can use to get started working on your project right away the developer documentation is at Doc stock compound.finance you can find soliditysmart contract documentation there you can find examples of Json RPC calls in ether's Js and you can also find SDK documentation there as well if you have questions you can always come into the compound Community Discord all of the engineers that work on compound are in that Discord they can help you out with your protocol related development projects you can go to compound.finance Discord to get that Discord link the grants program for compound is at questbook dot App Quest book is managing the grants program for the compound Community you can apply for a grant at any time to build something on compound Quest book has given out over two million dollars worth of Grants to community members so far you can head over there and apply for a grant immediately GitHub is a great developer resource we have two organizations compound developers and compound Finance you can find protocol code and also examples for building your own application in those GitHub organizations here is a repository for the developer Workshop that I'm going to go over today so you can get out your phones and scan this QR code or you can just visit that URL on your laptop and you could see all the code that I'm going to walk through in this presentation so that's that's the portion I'm going to go through next again if you want to apply for a grant remember you can go to questbook.app and apply for a compound Community Grant today if you still haven't come up with an idea for your hackathon project we can help you out with this blog post please scan this QR code and take a look at our hackathon project ideas that we have ready for you to go and help you get your Bounty prize in this hackathon next I'm going to get into some code I'm going to show you how to build an app on top of compound that you can use as a starter point to start building your application for the hackathon bounties today or as a starting point for your project that you'll build on compound and you'll get a grant for it using compound grants all right so I've got a repository open here where I have some files missing I'm going to write them alongside with you today in this Workshop if you go over to the repository on GitHub these files are already filled in and that project is fully working all you need to do is npm install and add your provider URL in there and that project should work if you follow the instructions in the readme to get it running and in a few terminal line commands I'm going to walk through installing dependencies and also writing some of the important files in there so you can get started with your hackathon project right away all right to install the dependencies we're going to use npm install and we're going to install ethers and we're going to install a specific version which is 5.7.
2 we're also going to install express.js and hard hat we're going to run an Express HTTP server to serve our HTML CSS and JavaScript in a front-end app and we're going to use hard hat to Fork ethereum mainnet to our local machine so we can test out our application and make sure it works we'll be able to do that with our Local Host metamask account so once I've got my dependencies installed I'm going to go over to the package.json file and I'm going to add a start script which is just node server.js we're going to run a script called server and that's going to be our Express server and also where we Fork ethereum mainnet using hard hat and we're also going to seed our localhost account with some erc20 tokens so we can use them in our interface to supply and borrow from compound so here is my hard hack config I've got some environment variables here where I store my provider URL as well as my development ethereum mnemonic so the accounts that appear in my hard hat localhost node will be using the accounts for that mnemonic and we'll be forking ethereum mainnet using my provider URL which I use infuria we have some settings for our hard hat Network like a chain ID of one to match mainnet and I also got rid of some gas things that might cause issues in our in our test environment and I forked our accounts for our Dev mnemonic so they will each have 10 000 ether in them that we can use on our test uh localhost node okay so I'm going to open server.js and I'm going to import requirements like ethersjs I'm going to import hard hat as well as the task to create a hard hat server which will fork mainnet and run a local node for us and I'm going to initialize and express app as well this is an HTTP server running on my local machine I will be able to access our front-end assets from this web server I'm going to make it so everything in the public folder where we have our HTML CSS and JavaScript that will be loaded in our web browser and we're going to listen on Port 3008 we're going to log that to the console so we can see that the app is running when we run it in our terminal later I'm going to create a fork up mainnet using hard hats task note create server and we're going to run that on Port 8545 and that way our our metamask will know to connect there when I set the localhost network in there I'm going to seed my first localhost account with some erc20 tokens so I'm writing a method later called seed localhost Dev account and I'm going to pass the asset address add the source address to take tokens from the destination which will be my first account and the amount of tokens without any decimals will do an integer so I'm going to write that method for seeding the localhost dev account I need to I need to run a specific node RPC command that is special for a hard hat node you can't do this with Geth on your real ethereum node what we're doing is we're going to impersonate account so that way we can act as any ethereum account and I'm going to move assets from addresses to my address like some liquidity pools so I can start out with some seed tokens like some wbtc some with and some usdc so I'm going to get the signer for that for ethersjs and I'm going to point to my local house node and I'm going to create a token to transfer assets so I'm going to act as that signer and transfer the tokens to my address so now that this method is written we can insert some addresses for the seed localhost Dev account call I'm going to put the wheat address there is some accidentally sent weath in there usually so I'm going to take some of that for my localhost account and I'm going to use the compound V2 pools uh we call them the C token contracts for compound V2 I'm going to take assets from there by impersonating those C tokens and I'll insert those addresses here and I'll put in the amounts that I'd like to take so now when I run this script it will boot up my Express server on my local machine running at Port 3008 so we can serve our HTML CSS and JavaScript and after it sets up the express server it runs a four cup mainnet using hard hat on Port 8545 and it seeds my account using the method we just wrote to give us some with wbtc and usdc once that's successfully run we can continue to the next part where we're going to build this application we see it is a it's called interest rate app it is some simple HTML CSS and JavaScript no Frameworks besides ethers Jas and these files are already in the repository on GitHub but I'm going to write them here in this Workshop so you can see what it's like to write them while you're working on your hackathon project so I'm going to go into my public folder and create a new file I'm going to call it index.
html this is where the browser will navigate when I go to localhost Port 3008 I'm going to use the Sublime Text autocomplete to fill in the head tag and the HTML and body tag stuff like that first thing I'm going to do is create an H1 where I will put interest rate app like you saw in the screenshot from earlier I'm going to create a button called enable ethereum and what that does is when a user clicks it it's going to make a request to the ethereum object in the browser so that the user can connect their men and ask wallet next I'm going to create a select box so you can select the instance of compound 3 on ethereum mainnet there are two instances of compound three we've got cosdc V3 nc3 so I'm going to have the user be able to select the instance there and then they can supply and borrow using the interface next I'm going to create that little model box and I'm going to have a place where you can supply or borrow the base asset we're going to display the supply APR and the borrow APR for the selected compound 3 instance and we're going to put an input box as well as a button to supply or withdraw now in compound V3 you supply to supply or repay a borrow and you withdraw to withdraw your supply or to borrow okay I'm going to run my web server so we can see what our HTML looks like in the browser once it's ready I'm going to open up localhost 3008 in Chrome and we can see here my unstyled application uh we need some CSS to get this looking nice and clean like we saw in the screenshot earlier I'm not going to go over the CSS that I wrote earlier you can just import it using a link tag in the head of the HTML file okay once we import that CSS file using a link tag we can see that the interest rate app looks much more clean and colorful okay next I'm going to make the same model right below our model that we just made and I'm going to make it so users can supply or withdraw collateral assets so they can borrow can reuse most of the code and I'm going to change up some of the HTML tags here so that we can reference them in our JavaScript properly we currently don't have a input for the collaterals we'll draw those on the Fly once a user has selected their compound three instance of choice we'll make that work in the JavaScript later I'm going to make one more modal that shows the user's balance within compound three so we'll update those with numbers once the user is supplying and borrowing tokens last I'm going to import some Scripts first of which is ether's JS so we can make calls in our front-end JavaScript code to our ethereum node Fork running at 85.45 metamask will be able to interact with that so I'm importing second a constants.js file which is basically all of the HTML elements here I'm going to get them by ID using JavaScript and create variables that I'll reference in my script so to keep things clean I'll put the constants in one file and my code that actually does things in app.js and my constants will also hold some JavaScript objects that have data for compound three instances like the contract addresses the number of decimals for each asset asset addresses stuff like that you can check out the constants file yourself if you'd like all right next we're going to write our app.js file I'm going to create a new file in the public folder and name it app.
js I'm going to create an event listener that waits for the browser page to finish loading before it executes this JavaScript code I'm waiting for the Dom content loaded event to Fire and then we'll call this callback our callback Arrow function will have all of our code in it we're going to make a variable for the selected account this is the account that the user is selecting in metamask to submit transactions for I'm going to make a place to handle whether or not the user has a web3 interface enabled in their browser so if they don't have metamask installed they will get an alert that says that they should go to metamask.io and install it and if they do have it installed we'll make an ethereum request to the metamasks ethereum object and it'll fire an event called on ethereum connect and when we on ethereum connect we'll we'll do some stuff on the page to load web3 assets for the user so I'm going to reference the ethereum enable button that we wrote in HTML there's a variable for that in constants.js as well as all of the other variables we created in HTML we have some contract API references as well as our comment instance data which I've pre-populated with some data I took from the documentation page remember that if you go to docs.compound.finance and scroll down to the network switcher you can see all of the addresses of the relevant contracts for compound three and you can see them for each deployment of compound 3.
okay next I'm going to make an event listener for my ethereum enable button so whenever a user clicks this this event will fire we're going to run that same on ethereum connect method first I'm going to write a render UI function we're going to select the instance of compound 3 that the user has selected in the select box and that will correspond to the object in the array here by index we'll make a reference to the base asset as well as collaterals in my object in a constants file I have the base asset as the first asset in the array so that's why I'm using address 0 and then the remaining Assets in the array are the collaterals so once I know the collaterals and the instance of compound 3 I'm using I can fill in that collateral select box that we saw earlier that was empty and I'm going to insert each of the tokens that can be used as collateral for that instance of compound three next I'm going to make a button to supply or withdraw the base asset we're going to make a click event handler for those as well as the collateral Supply and withdraw buttons and we're going to call methods called Supply and withdraw respectively we can use the same method for both the base asset and the collateral asset and I'll show that a bit later I'm going to pass the relevant compound 3 instance the index of the asset in my array of Assets in class in constants.js and I'm also going to pass the amount which is the value in the text box that the user input and the last thing I'm going to do in my render UI function is call another function called Draw balances so that will fetch each of the balances of the user's account so they'll know how many tokens they've put in the protocol as collateral or they've supplied or borrowed uh if it's the base asset and we'll draw that on the screen as the last thing in the render UI function okay I'm going to go back and create my on ethereum connect method that we talked about earlier I'm going to pass the first selected account in metamask and I'm going to remove the hidden class from the instant select box select box so when the user loads the page they see that ethereum enable ethereum button and if they've already connected it will skip that part and just show the compound 3 instant select box so they can choose a deployment of compound 3 to interact with whenever they interact with that select box there is an unchange event handler that will fire and we can render the UI once again with our relevant data for the specific compound 3 instance and by default we will render the first index which is index 0 of the UI before they've selected any of the instances foreign we can see that works but our draw balances function is not yet defined we'll write that soon okay next I'm going to write the draw balances function we're going to first write a reference to a get balances function which will fetch the data from uh the blockchain and in the draw balances function will handle creating the HTML elements so we're going to create a label for each asset and we'll post the balance inside of that label as well so they can see the asset and the balance they have inside the protocol now I'm going to write my get balances function so we're going to pass the instance index first thing we're going to do is create a ethers.js provider using the window.otherium object we'll make a contract object for the specific compound 3 instance that the user has selected we'll call that contract comment we're going to Loop through each of the assets for that specific compound three instance and we're going to make an array of promises each of those promises is an asynchronous request and it will um make a Json RPC request to our ethereum node which is running on our Local Host Fork of mainnet and for the base asset we're going to use user basic to find out the balance of the base asset that the user has supplied or borrowed from the protocol and for all the collateral assets we'll use a different method called collateral balance of we'll push each of those requests to our array called balance fetches we'll use the promise.all function to wait for each of those requests to return once they've all returned we can make a subsequent request so the first one is to check to make sure that the user is either supplying or borrowing from compound 3 and once we know that they're supplying or borrowing we can do a subsequent method with a balance of check to see how much they've supplied or a borrow balance of check to see how much they've borrowed once that last request is returned we can Loop through each of the numbers and turn it from an integer into a decimal number and return that whole array back to our draw balances function okay now when we load the page we see that each of our balances is zero for each of the assets for the selected compound three instance next we're going to show how to calculate the aprs for both Supply and borrow and we're going to display those in our app as well we're going to go back into the render UI function and add a place where we display the aprs I'm going to write a function called get aprs where we do the Json RPC fetches I'm going to reference the HTML element and I'm going to input the APR for both supplying and borrowing next I'm going to write a new function that's an async function for get aprs I'm going to pass the specific instance of compound 3 so we can get the relevant metadata for that instance from our constants.
js file we're going to copy and paste our provider creation object for an ether's JS provider I'm going to create a variable for the number of seconds in a year so we can calculate our APR the way to do that with compound three is we need to first get the present utilization of the protocol that is the amount that is supplied and borrowed we also need to get the current Supply rate based on the utilization and also the current borrow rate based on utilization so we can calculate the supply or borrow APR using this formula it's pretty similar for borrow so I'm going to write that in and return an array of the two numbers so once our render UI function runs it will display those aprs so I'm going to refresh the page and you can see here the aprs with a really long decimal amounts I'm going to use the JavaScript 2 fixed function to shorten that down to two decimal places so that looks much cleaner better for a user interface okay next we're going to write our supply and borrow functions these are going to submit transactions to our localhost node to supply and borrow assets on behalf of the user so the first thing I'm going to do is create a ethersjs signer this special type of Provider can be passed to a contract and it can use the user's private key to sign transactions when submitting a transaction I'm going to make a reference to the asset data that we have in our common instances array I'm going to make a scaled up amount which is a integer version of the amount that the user inputs so they could put like 1.1 ether and that'll get Scaled up to 11e 17. because ether has 18 decimal places I'm going to make an Ethernet contract for the specific token I'm going to call that asset we're going to use our erc20 API which only has the approve function in it next we're going to approve the comment instance before we run the supply method we need to do this for any erc20 contract we need to approve the contract is going to move the tokens before we move the tokens next I'm going to make a transaction for the supply method comment.supply I'm going to reference the token address for the asset we're supplying and the scaled up integer amount that the user wants to once that transaction mines at my localhost node I'll get a receipt back and I'll be able to draw balances once again because our balances in the protocol have changed now that we have supplied an asset I'm going to log that using console.log to investigate if there are any issues we see later we'll be able to open the console and make sure that the numbers are correct and the proper logs fired for the contract events I'll wrap all of this in a try catch and make a similar error log in case we do have an error with our supply function which does happen time to time we'll be able to debug a little easier if we log the error next I'm going to create my withdrawal function which is using a lot of the same code from Supply so I'm going to copy and paste I'm going to remove the approve function because you don't need to approve when you're withdrawing or borrowing I'll update this comment to reflect that I'll change to the withdraw method which has the same parameters as Supply and I'll also change out my console log so when they get logged in the console we won't confuse them with our supply method and that's it it's pretty similar to the supply method mostly the same code all right now that we've written our code we can test out our application let's go over to the browser and refresh the page you can see that I've switched my metamask to localhost 8545 and we can see that I have 10 000 eth because we are running our script we also have assets seated we have some erc20 tokens that we should be able to see in our metamask wallet so if I scroll through my assets you can see I have five rapid coin five with and 5000 usdc this is of course only in localhost development test world this is something you likely have to do you have to go into settings and go to Advanced and clear the tab data this will remove all of the pending transactions and clear out the nonce data so that I can make requests to my localhost chain this way sometimes when a mask will get confused and resetting this will fix that I'm going to do it preemptively so we don't see any nonce errors in the console okay first thing I'm going to do is I'm going to try and Supply 1500 usdc I'm going to input that into my text box and click the supply button you'll see metamask appear on the right and it's going to confirm that I want to approve this contract I'm going to hit the max amount this of course is a new feature of metamask it allows users to limit the amount that they're approving I'm going to just approve the max amount and I'm going to confirm the transaction okay we can see that our receipt got logged like we referenced in our code and you can see all the transaction information there and you can see also that our comment balances have changed we now have 1500 positive usdc this means that we've supplied there is a little bit of a rounding error there but if we wait a few blocks we'll see this update to more than 1500 usdc now I'm going to try and withdraw the full amount and now we've withdrawn all of our usdc we left some dust in there unintentionally because the protocol accrued some supplier interest to us um it does it by the block time stamp so a little bit of cruise every ethereum block and you can see that dust is still there next I'm going to supply some collateral so I can borrow usdc I'm going to supply one usdc I'm going to get two transactions one to approve and one to supply now that we've supplied you can see that my comment balance for wbtc is one and now I can use the withdrawal function to borrow the base asset if we head over to app.
compound.finance markets we can see the collateral factor for Rapid coin on ethereum mainnet we could see that it's 70 percent that means I can borrow up to 70 percent of the USD value of my rap Bitcoin as the base asset So based on the current price of rap Bitcoin I can borrow roughly eighteen thousand usdc from the protocol with one rapid coin as collateral foreign now that I've submitted my withdrawal transaction I am borrowing so my comment balance is negative for usdc and you can see I have negative 17 999 usdc as my borrow and if I open my mad mask wallet and scroll to usdc you can see that I now have that plus 5000 which I had initially so I have about 23 000 usdc now in my wallet I'm going to switch the deployment of compound 3 that I have selected in the app I'm going to go to see with V3 I don't have any CBE or steak teeth as uh tokens that I can supply as collateral I don't have any of those in my wallet I didn't see my wallet with them using my seed function and my server so the best I can do here is actually Supply um wreath as the base asset so I can earn interest on weight if you go over to the markets uh instant selector you can see the current APR for withe is 1.89 so supplying those to width I can get 1.89 with the variable APR now if I try and Supply CBE which I have zero of I will have an error in the console because I'm not able to execute that I get the error transfer amount exceeds balance because I don't have any CBE so I can't Supply One CBE so I hope this was helpful for everyone you can build your own compound interest rate interface using JavaScript or you can do so using solidity I have some examples on GitHub for using the protocol with JavaScript or solidity if you head over to docs.compound.
finance you can click on developer resources on the left and it'll scroll down to the developer resources you can click on compound3 developer FAQ you can see here in the readme that there's several frequently asked questions with code examples linked there are JavaScript files that reference each of those examples and you can see two different examples for each one there's an example in JavaScript and there's also an example in solidity if you click on the contracts folder and look at my contract here's where all of the solidity examples live you can see stuff like how to supply or withdraw from compound 3 how to repay a full borrow how to get the APR how to get the reward APR get the borrowable amount based on the liquidity of an account get the borrow collateral factor and several other examples to run these examples you can take a look at the scripts file there are a bunch of JavaScript files that run JavaScript examples as well as the contract solidity examples and we can run that using npm test that will run a test Suite that runs all 28 of those examples in your console you need to reference a provider URL for Alchemy or inferior and you can watch each of those examples run on your own machine and you can walk through the code examples if you need them for your project remember to ask a member of the compound Grant staff if you have any questions about the Bounty prizes or your project that you're building on top of compound we're happy to help we can help you out with your technical questions or help you brainstorm your project idea we're happy to help you out come find us remember to check out our hackathon project ideas blog post which you could find using this QR code and you'll be able to come up with an idea that gets you going towards those hackathon bounties so you can win some cash prizes at eath Waterloo 2023. on behalf of the compound Grant staff I would like to thank you all for the opportunity to participate in youth Global Waterloo 2023 if you have any technical questions want to brainstorm your project idea or have questions about compounds Community grants please come find me or a member of the compound Grant staff we're eager to help you out also come and find us if you'd like your own compound t-shirt or compound sticker happy hacking and best of luck to all the hackathon participants
Automatic transcript — names and jargon may be misspelled.