New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Make Interactions with Smart Contracts a Pleasure with eth sdk

ETHGlobalSun, Oct 9, 2022, 12:00 AM

Join Kris Kacsor for a talk titled, "Make Interactions with Smart Contracts a Pleasure with eth sdk," at the DevTools Summit, part of ETHOnline 2021, a month-long virtual hackathon with over 1,000 engineers, designers, and creators building new protocols, learning at summits and workshops, and gaining valuable feedback from ecosystem professionals along the way. ---------------------------------------------------- Follow us: https://twitter.com/ETHGlobal​ Check us out: https://ethglobal.co​m Email us at: hello@ethglobal.com

Transcript

[Music] [Applause] [Music] move on to our next talk and next up we have chris and chris is going to talk about the sdk and how to make development a lot more pleasurable so without further ado welcome [Music] hello uh let me share my screen okay i think it works now so uh hello yes uh my name is chris catcher and during the day i worked i work as a software engineer for maker dao working particularly on layer 2 stuff and during the night i'm part of a deaf organization where we're trying to make ethereum developer experience better today i want to talk about if sdk a new this is actually a world premiere so guys free alpha incoming a new tool that is a generator of lightweight and typesafe as the case for ethereum smart contracts and i'm gonna unpack this in a bit now let's talk a little bit about the ethereum dev landscape and how it evolved so you know if you were using ethereum a few years ago uh everyone was using web3.js and it was pretty raw it didn't even support promises back then these days eater's js is a king a similar thing happened to truffle and heart hut and i i'd like to point out a more uh profound uh change which is which is a change in language that we use a couple of years ago everyone was using javascript now more and more people use typescript and not only blockchain community like you can see this rise of typescript everywhere and uh you know some time ago you could think about java typescript as some kind of add-on to javascript these days typescript is more popular than you know full-fledged programming languages like ruby and you know now these days a lot of libraries were rewritten in in types so for example ethers.js it's written in typescript already as richard mentioned it's going to be even better supporting typescript soon but still in some sense webfree.js with typescript is broken and it breaks exactly in the moment when you start interacting with uh with smart contracts deployed on ethereum the problem is that to interact with a smart contract you need an api and this is a piece of uh dynamic piece of json javascript that you basically inject into your blockchain access library and then you get like some sort of uh object created during run time that can talk with the smart contract and the problem is that you cannot type it you cannot prepare typings to this in a usual way and i recognized all of these issues some time ago uh like you know the rising popularity of typescript and the problems related to webfree uh interface word and created a tool called type chain so type chain takes your json apis files and takes information about your target so do you use traffic to use ethers maybe web3.js and it generates typescript typings as a sim as a separate uh build step so this way you have excellent id support you can uh you know just browse for the methods that that that are available on the smart contract uh we support as well not spec that was already uh mentioned uh during this dev summit um as i mentioned it supports officially uh many different targets ethers jsv45 webview.

js truffle but uh recently we also added a support for hardhat which is kind of great like if you use hardhat you should that i give it a try uh it's typing things like get contract at and so on and i feel like typechain uh does uh like accomplished a lot like a lot of well-known companies like uni swap maker obviously optimism uh uses it uh but uh you know it it makes interaction with smart contracts a breeze but it doesn't solve all of the problems that a web feed developer can can encounter especially during setting up like the scene for a smart contract development for smart contract interaction rather so you can imagine that the typical day in the web free uh developer uh [Music] life looks more or less like this like imagine that you get a new assignment to write some kind of script that gathers data or reads some events from multiple multiple smart contracts or maybe you're developing a front end or back end it doesn't matter first of all what you probably do is you gather smart contract addresses and maybe use etherscan for that maybe you browse and you know you just type in etherscan search to find the addresses maybe use github and or docs of a given project to find all the addresses but then you need to find apis maybe if you're lucky you can you can find the api somewhere in the on the github but then you need to wire apis with the addresses uh in code like create ethers.js contract wrappers or something similar then you need to generate typings if you if you want to have like you know good developer experience and type competition and only then you can start actual work so if sdk tries to make this ceremony much more easier and enable rapid smart contract to enable rapid interaction with smart contracts so i like to think about it as a love child of eater's js type chain and ether scam and you know talk is cheap so let's go and try to use it in a hardhat demo so here's a boilerplate code for a hard hat example and this is a very simple code that it's configured to fork mainnet from a particular block and we can start writing code note that there are no you know useful contracts here we're going to interact with uh mainnet contracts and the only reason we use hardcast is for you know fork features and what we want to do here imagine that it's i guess one week ago and compound hack just happened and you're a white hat or just person curious and you want to you know realize what happened you don't know too much about compound uh smart contracts but you still have you know addresses of the uh of the contracts and you want to you know dive right into it and try to and try to you know play with it uh try to reproduce the uh original claim that caused like meaning a lot of comp tokens and you know try to play with the whole setup so usually now you would start dumping apis in your code and so on and so on and it would take you some time but now we can use if sdk and to do that we can go right to the github and grab a command to install it it's to the dependencies and once it's it's installed uh we can use it right uh right now so first of all we need to create a config file so if sdk will read all contract information from contracts.json config files so in this file we define all of the contracts that we want to interact with and i already have this piece of code ready to copy so let me just dump it yeah so first we need to specify a network that we want to interact with here it's mainnet and then we we can start writing arbitrary nested list of smart contracts so we can have these namespaces like tokens but at the end of the day is name of the of a smart contract and address uh that it leaves uh on on the particular network and we do the same for compound for other compound smart contracts like comptroller which is responsible for dealing out the rewards it's a proxy so it has controller implementation and the treasury so there's another uh interesting uh bit about this attack and that's why we need uh treasury so let me now it's the moment when we can actually run f if sdk and see what happens so now it read a config file and it's getting apis automatically and it's generating client and notice that client was generated directly to node modules and now we're good to go to start interacting with the smart contracts that we defined so uh let me import the sdk so this is the if sdk client package and we have get main.sk and this function who is tailored based on the config that we just created so here we gonna call it and we're gonna pass signer and you can see that it matches the shape of this object matches exactly the shape of a config file and if we go inside we see that there are a bunch of typings and uh in the runtime these are js contracts uh that are already configured to be used so um the interesting bit about the compound attack was that there there was a treasury contract that could be used to uh drip even more compound tokens into treasury so just to test that let's let's let's actually test first the drip function so we're gonna uh we're gonna add some logging here uh let's say before drip and we're gonna call some methods uh so we're gonna check the balance of comp token on on the on the comptroller address so if drip function works correctly we should see that the balance was increased uh we need some formatting and we need to import this function okay now we can call a drip function and no notice that i'm not an expert in compound contracts but thanks to no type competition i can write my code pretty quickly and here again i forgot to handle promise but since everything is typed correctly i got immediately you know an error in my idea about this so now we're going trip function and after drift this this balance should uh increase if drip works correctly so here i have a script to to to execute the code that we just developed and yes we can see that drip is causing like you know a lot of new comp tokens available now we can try to replay the what original maybe not attacker but the original claimer did so we want to claim tokens and receive much more much more rewards than we should receive right so again we're gonna we're gonna print out balance but this time uh before claim and we're gonna print out balance of the eoa that we're interested in um now we can call up so there's another thing that we need to actually initialize uh controller because it's just a proxy so we need to take the implementation and attach it to to the comptroller uh address okay with this we can do claim and notice that there are a bunch of um overloads but this is the one that we're interested in the holder is again our eoa and the c tokens is the list of c tokens that the rogue claimer claimed and uh finally we can print out the balance again of the eoa so if things go well we should see like this claim causing a lot of rewards to be sent to the eoa let me run it again it's a little bit slow because it's work mode but yes we can see that there was a bunch of comp reward uh sent to the user i don't know we can play it with this code but what i wanted to present here is that we were able to start interacting with a system that we know almost nothing about and you know write a script that does something meaningful meaningful very quickly and uh writing so it's funny because if sdk wasn't ready really one week ago as i told you it's a word premiere and i was writing similar code and i made the typo while copying one of the addresses and i was exiting i was interacting using the wrong api to the wrong address and i spent like half of an hour trying to debug it so with if sdk it's all like very simple to to to code now continuing my presentation [Music] so as you could see uh if sdk generates this lightweight uh type safe ready to use sdks it works for different networks so even so uh we care about the network config because we need to have a way to connect to etherscan and and get verified contracts for this for these uh for this particular network so that's why that's why we support xbctl these networks but it's pretty long including things like optimism mainnet we generate sdks directly to node modus so if you're familiar with prisma this is exactly the same approach i think it's quite nice because the import paths are always the same and they are like short and as i said it's perfect for writing any scripts one time one one-time scripts or front-ends or back-ends uh diving a little bit more into details um apis that we gathered are stored in the if sdk apis directory and this directory uh should be actually committed to the git repository to avoid uh querying contracts again when you execute if sk again another trick is that you could uh add executing here and uh executing um if sdk as as a post insta script so it's uh much easier to use because you always need to generate the client code client generation is a little bit complicated because first we generate first we generate typescript code we generate typings and only then we run typescript compiler to get javascript code that you can easily place in the node modules directory and node modules insertion i already mentioned so what are the future plans so um we will we would like to have a first class support for hard hat so right now like you you you saw like it's a quite an opinionated tool if it comes to what you're using if you could use it with you know whatever you want but we can improve the integration for some of the frameworks so for example for hardhat we could register the api so hard hat knows more about them and can print meaningful error messages and so on we would love to uh integrate if sdk with used up that mark uh mentioned today and the the bit that i didn't like during demo you could notice that we users were users are forced right now to reconnect proxy with implementation so we could also automatically query proxies to get always the valid implementation so if you recognize that the given contract is a proxy we can always query implementation and then query api for this implementation and then merge this api so this would be i think a pretty nice feature for end users finally we uh we are planning on supporting alternative api sources so sourcify was mentioned here today already and i think it's an excellent um tool that we should integrate with um because you know etherscan is pretty great but it's a closed source for perfect company so it's always nice to have like a decentralized alternative um and then we're thinking about supporting dynamic addresses for for example for local network when you can provide them by hand and just enjoy the typed shell and that's it more or less you should join our discards so at def we're focused on uh improving ethereum uh developer experience there's a couple of people involved in the organization uh here are social media and i'm open to any questions right now thank you thanks so much chris um no this is really great i think uh kind of a couple questions the first one is just as a confirmation are you getting the abis per meter scan or somewhere else and uh how does that work uh yeah so i i think i i answered it at the end but yeah right now um we use ether scan api there's actually a key hard-coded somewhere deep in if sdk so i hope you guys don't uh don't do the os it and but yeah the better idea would be to use uh cersify or at least have you know some uh like multiple backends but for now uh eaters count was a way to go no that makes a lot of sense and i think uh one super cool part of this is is that so many talks today they're all interconnected and they're just helping uh basically modularize each other so it's just great that i saw use tab at sourcify and everything here i know you already touched on this thing but what are some of the feature things that you're trying to integrate and uh or add features for and how do you think this will evolve over the next six to 12 months yeah so uh i think like a single biggest feature that we're looking at is you know integration with other uh libraries tools in the ecosystem particularly the ones that you would use on front end so magnetic was showing today used up and use that piece is great but if you would if you were paying attention you would notice that smart contracts interactions right now at used up is totally untyped right i bet that the whole thing is written in in typescript but again as i mentioned at the beginning interacting with smart contracts it's a bit more difficult and so we would like to you know integrate with tools to provide like this first-class support for uh for you know typed smart contracts and you know it's it's not something that uh you do um you know the the reason why we're doing is that because it saves you time in the longer term right like now these days if i write some code without uh you know having type information and basically you know guessing i and to go again to check the documentation or check the source code of the smart contract uh to you know double check the arguments or or whatever like you know having all of this in the id is just a game changer and it makes you know writing codes so much faster and debugging and that's just about yeah yeah yeah and this bit uh like last thing this bit that that you could show that you could see that i forgot about 08 in one place and you know id you could you know literally spot it right there and just say you know hey you forget away this is exactly the thing that that we're missing when when uh you know coding just in javascript absolutely um and then just kind of lastly to close um are there some immediate things that people can help with uh in terms of like contributing to the project directly or how is that structured and how can people get involved yeah so i would uh encourage all of you to just jump on our discord channel and uh we're trying to it's it's a very fresh channel so uh not so much uh going on there now but uh this would be a perfect place to to try to coordinate and if sdk is in quite experimental stage right now where uh we're just trying to figure out what's the best way for example to define the config or to define some uh you know other things like i don't know the cli interface zones so i would uh encourage anyone uh to you know just just drop us some feedback and so on awesome well hopefully that was helpful for everybody too and uh it's a good reminder for everybody to join the discord and make it more lively uh chris thank you so much for that amazing demo and presentation and uh can't wait to uh myself use the sdk and have others try it out too thank you amazing all right [Music] [Applause] [Music]

Automatic transcript — names and jargon may be misspelled.