Learning How to Ape - Jules Smith
Ethereum Denver·Sun, Oct 9, 2022, 12:00 AM
Transcript
all right hello i'm jewel smith i work for ape works and i'm going to be teaching you how to ape today so what is ape abe is a python development framework uh very much like hard hat or truffle except in python if you've ever used brownie it's a lot like that except we kind of take it next level where we are very plug-in oriented and i'm gonna go into what that means in a little bit so why python so python is really easy to read and write as you probably know it errors very loudly and for those two reasons uh it's more secure it also integrates better with existing cyber security and data science tooling so now that we have um kind of a a really nice framework in python that we can easily integrate into these other spaces and we can make use of these existing tools and a lot of um security tools already in the ethereum space are written in python by the way so okay so ethereum there's a lot of abstractions there's a lot of interfaces like token interfaces but even you can take it past that word like signing a message what do you use you use your ledger wallet do you use your meta mask in the end all we care about is you have a signed message um ecosystems in general are kind of abstract so maybe you want to use the ethereum chain maybe you want to use an l2 or another chain does it really matter if they all kind of follow similar enough abstract interfaces and that's kind of that's kind of where apes shines we um we have a python plug-in ecosystem where you can pick and choose which parts that you want to use and install them for your project and you can use the same framework no matter which chain you're on no matter which accounts you want to use if it's a simple nft project or like a more complex defy project you're gonna you're gonna install different plug-ins based on your needs and the best part about it is if those plug-ins don't exist yet you can just make them okay so stepping back a minute uh projects in ape look kind of familiar like you have contracts you have tests you have scripts you have a configuration file and now i'm going to show you a little project i'm working on this is just a demo project but it's going to show you kind of how to use ape so i have this repository here and in it i have a demo project and i have a demo plugin so they're in the same spot and i'm going to link to the the project on github at the end okay so the demo project we have a contract in the contracts directory this is called downstream let me make my terminal smaller this is called a downstream erc20 just for demo purposes it just requires you to have a separate token in order to have this token so you can see there's this modifier has upstream which takes the argument two this is solidity and it'll only let you get mint that token if you have the other one so that means we're going to need to probably have mainnet fork in our testing so let's let's look at the configuration file here so the ape config file um probably the first part you want to notice is this plug-ins list here this will specify all the plugins that this project requires to run so we're going to need solidity for compiling the contracts we're going to use hard hat for its node so we can run tests using the local hard hat node chain we're going to have to use the alchemy plugin and this is going to allow us to do mainnet fork which we're going to need because it's a it's a downstream token so we need another token it's just easier to use an existing token we also have this tokens plugin which just really makes it really easy to use tokens in your scripts and um i'll show you how that works and okay so i will get more into this file but first a simple command to see what plugins you have eight plugins list nothing should be installed yet because this is fresh but to install all the plugins i have listed here in my config file so if you do ls you see i have my config file there so ape plugins install dot for the directory of where the config file is you can also give it a list so if you want the ems plugin in the inferior plugin you can do that but if you just want to point to a project you can just put the path to the project and it should go and through and install those plugins and that will take a little while so while that's happening i'll just explain more in this config file so we're saying dependencies to make it easy you might have noticed in my contract i'm just importing from open zeppelin and that's what this declares here this will make it uh so this will allow me to download the open zeppelin dependencies from github and i can specify version and then when i'm configuring my solidity plugin i'm going to set import remappings to map to this dependency i've set and i'm also going to set some default networks so i want to use mainnet fork for all my testing and i want to use uh alchemy as my mainnet provider and i'm also setting the upstream provider in hardhat okay so it looks like my plugins got installed so now if they should show up in the list but just be sure all right so eight plugins list you can see i have some plugins installed another really nice thing about ape is you have this console tool so if i just do a console this will launch me into an interactive blockchain environment starting up the hard hat node in the background okay so now uh if i do you have some things i'm automatically in your space like network manager networks.activeprovider.name you see it's hard hat that's good and quickly i'm going to show you that tokens thing right quick so from ape or from ape tokens import tokens and let's say i want the die contract address i can just do die dot address so very easy to work with tokens in your scripts okay so i exit out of that that'll shut down my hardhat node and we're good so now i want to run some tests but let's look at the tests so our ape test tool integrates with pi tests so it just if you've used python and pi test before this will be a little familiar but basically you have these python fixtures so we're going to define a few fixtures we have an owner of the contract we have some rando and we have a a west wief holder and that's going to be our upstream token and we're also going to deploy the contract for our downstream token and you can see here we're actually using the tokens plugin that i showed you in the console all right so into our test we have two very simple tests we're to make sure that upstream holders can have the downstream tokens and then we're going to make sure that when we try to mint the token without having the downstream tokens like some rando that it's going to revert and that's using this ape reverts tool okay so to run your tests all you do is ape test and i've already specified my uh preferred network in my config file so it's going to use hardhat okay it's also i didn't compile so it's going to download my dependency first because that does not exist and only does this once and it only compiles it once and so we're going to watch that happen sorry about the wi-fi here but we're wait for that so it downloads the dependency it stores that in like a data cache and then in your project when you specify the contracts it'll pull those in and use them and it won't have to compile them each time okay it's running the test now i'll show you more on compile in a minute here but it looks like my test passed um so i just compiled so if i run a compile without doing any changes at all nothing happens because it does not need to do anything but if i want to force or recompile i can do dash f and it will just recompile my one contract it does not need to recompile the dependency again because that's already there okay so we are good um once you're satisfied with your project it's well tested uh you might want to create like a deploy script so ape supports that you create a scripts directory and then you create your scripts in there and this will look a lot similar to what i was doing in those in those pi test fixtures where we um we have the the upstream and then we deploy the contract using an account and we specify the address of the upstream and this this little helper here get user selected account this comes from our ape cli namespace which is just full of odds and ends to help you have like reusable code for like getting accounts or selecting contracts or just anything that you you will find useful when making scripts as well as plugins so just that's where that's coming from and i'm just going to run this run deploy starts at my node it's going to prompt me for my account that's this line here and i'm going to pick then you just put the number you can also type it out it works either way thank you sorry all right yes okay so that's a deploy script all right so what just happened so in ape there's all these um commands as you saw we have ape compile ape accounts and this is sort of like our cli entry point into the framework and then inside of ape core we have all these abstract classes so we have a compiler api this is an abstract class this is an abstract class and basically how this works is we define these apps our classes and then plugins which are other python packages that use ape they implement these abstract classes and then ape knows about them and then ape will use the plugins based on certain criteria so if like you're compiling a viper file and you don't have viper it can't compile it but if it notices you do have the viper plug-in installed now it's able to recognize that these are source code files for your contracts and now you can compile your viper files and the same thing with accounts you can manage accounts you know through hardware wallets we have a ledger plug-in we have a tracer plug-in uh we even have like local accounts uh natively in nape but then you you can basically use you can make a account plug-in using any sort of like private key storage if you really wanted to like a key ring is exactly that okay i didn't want to show that yet but now i'm going to show you a little bit about implementing an abstract class so i have this plug-in project in the same directory and there's a few files in the main thing we want to look at first is we can import from the ape api namespace these abstract classes and there's one called provider api but there's also this half implemented one called web3 provider and this will get you pretty far and you don't have to use this at all all it needs to do is implement the provider api but if you want like a little bit if you want to use the web3.pi framework then you can just use this one and it'll kind of save you a little bit of time because it is kind of reusable and there's uh different methods that must be implemented or else you will get some warnings april get mad so this is a connect method this is required but i'm just going to temporarily destroy it first i'm going to actually install so these are just python packages plugins it has a setup.pi file so you can install it using pip plug in okay so now we have a local plugin installed if i do a plugins list i'm gonna show up and if i type ape let's see what happens i get a warning chain stack from ape chain stack is not fully implemented remaining abstract methods connect okay so it did not like that we didn't have the signature correctly here so i'm going to fix that save again all right uh does that work yes okay so we're all good we now have if i run ape networks list this is another command just for reading what providers you have available you can see here that we have chain stack by the way i'm making a chain stack provider as part of this demo so we have chain stack which i just installed here locally and that's part of we have rob scene maintenance and looks like rink rinky so different providers support you know different networks so you don't always get everything so now let's try to use chain stack i'm going to launch that same console and i'm going to use rink b this time i'm going to use alchemy because that's installed so you can always change the network you don't have to use the ones in the config you can use the network option and specify uh so the first part here would be ethereum it's kind of assumed if you don't put it so ethereum colon wrinkby colon alchemy so ecosystem network provider name is the pattern there so now let's see what's going on we have networks uh active provider that name looks like it's alchemy i forgot i was going to use chainstack i'm so sorry okay let's use chainstack this time our new plugin that we just made and chain stack okay so one other piece that's missing here is this init.
pi file this is where you will register your plugin so every plugin needs to import from ape import plugins and then you import whatever classes are part of your plug-in spec and then we have some east networks here hard coded that chain stack supports and then we loop through the networks and then we yield this provider and that allows it to show up in that network's list and allows it to be used by specifying it this way okay so chain stacks registered we're in an interactive environment um let's just let's just maybe get a block all right so it seems to at least work a little bit and this is this would be a good start if you wanted to build the chain stack plug-in it's already kind of working right here you can exit out of that and when you when you call exit that'll call this disconnect method and all that does is set this variable to none really and that's basically it so we have a couple bounties we have a bounty for best project using ape and we have a bounty for best plugin using ape so if you want to integrate with something or you want to try to use python ape in your projects then come talk to me and i'll help you get set up or we have uh we have lots of documentation uh we have banana jam bounty information and then also those the two projects i just showed you are also available and i guess that's all i really had you want to look at the okay thanks again here i'll make it big our table is just on the third floor i don't remember the number but we have a lot of bananas it's the table with all the bananas come find us m36 all right thank you we actually have about five minutes if you want to take any questions from the audience yeah hi is it possible to um run a test without having to recompile the whole project again like brownie does yes so it only compiles your contracts when it notices there's a change in it so if there's no change in the contracts it won't recompile it just makes sure it's up to date before it runs the tests any other questions if not then please give a warm round of applause to jules [Applause]
Automatic transcript — names and jargon may be misspelled.