Andrey Babushkin - Uncover Hidden Bugs with Fuzzing
EthereumZuri.ch 2024 · April 2024 · 1:00:37
Andrey Babushkin leads a hands-on fuzzing workshop at EthereumZuri.ch 2024, arguing that many web3 developers deploy smart contracts without fuzz tests. Using Solidity code optimized with Yul that hides unobvious errors, the workshop compares fuzzing approaches and tools with a focus on guided fuzzing, writes a fuzz test from scratch, runs it locally and evaluates the output, then discusses how fuzzing applies to bigger projects as an extra layer of security.
Speakers
Topics
From EthereumZuri.ch 2024, hosted by EthereumZuri.ch.
Transcript
so yeah hi everyone glad to see you here and uh today's talk is about fuzzing I have first of all it's not a talk it's a workshop so let's make it interactive and uh if you have any questions just raise your hand feel free to interrupt me ask ask questions and so forth and my name is Andre babushkin let me introduce myself so it's me I think because we attack people it's very difficult to find a good photo and I was stress really to put it there because it's my only good photo there I'm an auditor in AK blockchain and the company we do audit smart contract and uh we do audit in of solidity smart contracts on
ethereum and on salana so also we develop security tools uh maybe you have already heard about wake wake is our product and it's not open source tool that's finds bug in your code that's find some vulnerabilities and uh also we on board new developers we provide School of salana we also do studies in the Czech Technical University for students so we educate also so our partners are these companies uh I guess uh many of you heard about some of them 1 in Lio maneria XLR so and we have
received several grants from ethereum foundations coinbase and Salon foundation and uh let's talk about fuzzing first what is fuzzing I think uh you already some of you know fuzzing is like putting random input data into contract why why should we do it because it sometimes helps to find uncovered bug and vulnerabilities why because when we write unit tests we think about very usual ways how the contract will be used however we may miss some edge cases and these edge cases only can be found by either hackers unfortunately or fs and um these edge cases are extremely
extremely important to test However unfortunately fuzzing is often overlooked in development there are not so many contracts that has fuzzing implemented and that's a problem that may be problem and actually there are some several examples because yesterday there was a talk about decimals about floating Point arithmetics and problems with rounding errors and uh kyber kber kyber I don't know kyber balancer ra wise these are extremely known hacks companies lost millions of dollars like tens of millions and it can be extremely extremely dangerous for example one example from the web 2 world world uh
there is a Google project called OSS fuz it's a project that tests several open projects with fast test it's runs fast test and these projects founded found more than 10,000 bucks developers are clever developers are smart however they often miss some edge cases and this is this a problem so uh in my talk description I wrote that we would have a highly optimized uel contract however I changed my mind a bit I I want to introduce something more interesting to you it's a real use case from our um experience and it's
Lio if you know what slider is I will introduce this company later so we'll explore the use case what went wrong we'll understand how the code Works what is the architecture of lios songs next we will write some fast tests in Python if you want to follow me alone no problem I will show everything I will be quite I'm not going to be fast so you'll manage it next uh we will identify a vulnerability in the code of course we'll fix it I will'll show how fuzing helps and of course you we will have to enjoy the rest of your day after this talk so let's go on for now any
questions not okay great so what is Li STS as you may know Lio is a very big company actually its uh evaluation is about three billions of dollars and uh it's the same as uh gross product of Andora for example the whole country and laa is an extremely extremely large company it's a DA actually and uh it was our client and they introduced Tong stons is a smart contract it's like a set of contracts and I will explain everything don't worry so how St work like the treasury it's a dow so there is a treasury they want to
sometimes exchange some Stak ether their own coin to some stable coins and back they want to do it automatically when Lio creates a new like swap they need to initialize stons this is the first step when Dow proposal is accepted it's automatically initializes stons next stons creates an order order is a an order a swap order for the cow protocol it's a swap protocol actually and uh the order contract emits an event an offchain component read this event and
offchain uh creates a new transfer to cow protocol when cow swaps this it sends swap tokens back to Treasury and uh the order calculates data calculates the price based on chain link fins so this is how light works actually it's a very simple set of contracts it's a a very small application of course we will not test everything here cow protocol is very difficult to explain chain link also it's a it's not difficult however there is a lot of code so we'll simplify things a bit first of all uh we don't use light da of course we just just
writes test for this just creates new stonks next As for chain link it's just omited and we create a very very simple contract that just always reports the same price with some deviations next uh we don't have C protocol because it's again we don't need it so actually we are interested only in two smart contracts it's tones and order and we will write a test for this we will we will verify that the event order created is complete it's correct so it's going to be this simple and uh actually this is it so this is the introduction and uh I want you if you want to follow me just scan these
codes this is the GitHub repository with uh everything we need and uh get wake is a website for the Wake tool we will be using it we will write Tools in vs code so we will write test in vs code sorry and um getting so get ready just prepare the vs code if you don't have installed installed it please and uh let's switch to GitHub I will show you right away what we're going to need I described everything here so this do you see everything so the font is good okay great so the sorry
okay so this is it just yeah I'll give you some time to scan it so we get up repository when you're ready just tell me so good everyone's ready great we go we can go on I think so this is our repository it contains all the code and for now just uh clone it everything is described over here actually everything that I described is over here and uh with a bunch of links for you to follow later
for example if you want to uh the agenda is very simple so we'll clone this repository uh please use recurse sub modules because uh I have a Foundry toolkit I will show the test written in Foundry and I will rewrite this test in week I will show you why why it's why it matters and uh just enter this directory if you want so I will give you some time and I will switch to the VSS code so again let's open the link here for you for newcomers for example if you want to follow I'll give you
some yeah you you don't you don't have to install Wake for now uh just be sure that you have vs code and be sure you have python because wake is based on Python and if you don't just uh go to python.org I think downloads or something like that and install python please for your system because uh the installation May differ from system system of course so um does anyone need some time some more time no good great so here it is here is the repository the repository is simple first of all you do not have to
understand the code really this is the power of fast testing actually and testing it's like ex there exists test driven development actually there exist 10 driven test driven auditing because you don't have to understand code you can write test right away when you boun to some bounce into some for example problem okay you have look you look at the code you try to fix it however you just read the documentation describe some use cases and write test for these use cases right away with fuzing I will show you and uh here's the vs code let's just explore the main contract actually there are two of them first of all it's
stons the stons contract as I said just creates orders and that's it this is a very simple contract we just somehow somehow we create this contract somehow we do some magic here we don't need it and then there is a function it's called place order we will test this function today so otherwise there are some estimate tra outputs again to calculate the price of the swap again we don't need it just skip it no problem and some other view functions again we don't need it and the order contract so the order works like this it's a big bigger contract however again we skip
everything we just see that the Constructor needs some parameters okay we will find them no problem so then we will go to initialize this is the main function here because when the contract is initialized it's MSD event and we will test this event so we see that there are some initializations techniques okay something's going on here so some mathematical okay no problem just GPT some structure maybe I don't know what's this the structure is some other parameters like uh approve for in emit the event so great and that's it that's all what I need for example if I if I was looking at this code for the first time I just said okay
I understand this structure because I understand what's going on however I don't have any suggest any idea how it works I don't need it for now so I will start writing the test for now just skip the foundry parts we don't need Foundry for this I will show you the foundry test because it's our reference however it's not that important to understand it's not important to know Foundry it's not important to just write this test it's already predone for you however I will explain how it works so this is the contract the first Foundry thing is that we write tests in solidity so we we write new smart contract to
test our existing smart contract maybe that's a good thing however yeah I will show you why this matters so we just Define several initialization just again skip it I just wrote this quote no problem we have a setup function what setup does it's before every run I will explain to you before every run Foundry calls this setup function what this setup function does you can deploy contracts for example you can set up this contracts just prepare the environment what's are runs when you try to fast test you create a lot of input data like a lot you for example you
generate it know several thousands of input data through vectors I would say I'm from AI sorry I know it's math so you just create several vectors of data and you test every use case again and again and again and again with random data and you verify that the data that is outputed from the contract is what we expect to see and uh so in this setup function we just create an agent it's a parameter is just a random address a manager is a random address no problem so we need some to something to randomize so here we randomize using as you may know there is a problem with random data and solidity however
there are there are things that we can use and one of them is black preand it's like I don't want to explain it and just a time stamp it's like a source of our Randomness so we just Define the order duration how order is uh how much time can this order is valid and uh we Define the margin margin in stons is like uh how much fee do we pay for these uh operations of of swapping and price tolerance it's like to prevent uh spikes in prices so we don't sell for very unfortunate values so yeah we create the amount converter amount converter is a
very abstract class it's very simple it's it was it actually uh replaces the chain link it's a contract that is extremely extremely simple this contract just set the conversion rate it just Returns the conversion rate and calculates the price when you put one token to another it's very simple I just do everything manually here I don't need any real data here for testing so yes and um and that's it I think we can go back to Foundry so it's in test Foundry so we initialize we create stons stons is created for every trading pair for every pair so use the C for used the
St ether for used the several stons contracts so for every pair here for sell tokens buy tokens these are variables defined here is just addresses of smart contract existing on chain we create a St contract and that's it this is the setup function actually everything this goes go goes on here we just create SS contracts we prepare the environment with some random data already so next uh there is a mint function uh because we don't have our treasury we have to Mint we have to Mint some tokens to use them and uh this function does a very simple thing it changes the storage of the contracts because we Fork the chain we can change
the storage right away and to use the change values so this function just does this thing is just reads the storage and changes the values of the storage without calling the minan function actually of course you cannot do that on chain of course you cannot for testing purposes yes we can and uh so this is a minan function some mathematics in fact it's a copy paste from the contract uh from the stons or order you remember actually and yeah here is the function test place order order so Foundry needs test these test are functions that has that have test in the name of the function and uh so here it does it so we calculate uh some input parameters when
we specify input parameters for the test Foundry uses this as a fast test because these parameters are random when F when Foundry runs the test it just specifies random parameters to these uh arguments so however we need to bound these parameters of course of course we cannot use them right away because for example if we need an index in the in a field in an array we need uh only indices in this in this array so we bound these parameters in impute data and uh we yes we specify the sell token the buy token to ston we need to obtain the correct St ston contract so we make some uh assumptions so the sell token is not
equal to buy token if it's if they are equal Foundry just skips this data because it doesn't it will not work and uh then we Define some mathematical stuff again you don't have to understand this and um okay let's we mean tokens we compute the buy amount the minimum amount uh it's for the minimum price for the price so it's again it's a protection against spikes in prices and uh so here it is yeah we just uh specify test use case for example if the cell amount is less than 10 it the contract should revert why because these are dust traes is maybe insecure for ston it's just the
logic of ston contract just take it as as it is so next uh if the amount without margin again margin is the fee if the amount without margin is equal equals to zero so again it should revert next we obtain logs from The Foundry we obtain logs we check everything so VM loocks Foundry is very strict and again why because it's statically typing typed language because we writing solidity you have to specify all the input data you have to specify all um uh types of every variable you have to specify this long long string to
compute the correct uh signature of the event because you need to find these events in the locks so and when you find the correct loog you just check is that everything is right and here are these checks so first of all you expect the amount that we want to sell and the amount that we want to buy our computers correctly first of all next we try to recover tokens from the contract when the duration of the order is still valid that means that the uh order is not still yet executed if we try to take these values from the take the tokens from the order
it should revert because the order is still valid and it should revert here next uh we set the time stamp to order duration plus one that's that means that this order is not valid anymore and it should not revert in this case we can extract tokens from the contract and uh so yeah VM start Bron it's a very weird name in my in my opinion however we change the color address so when next time we we call a contract it will be called with message sender equals to random actor here just some random value and yeah we call order recover from token from it should not
revert here because the order is invalid and that's it so again what happens here I know that's that's much really you do not understand the code however you don't have to what we checked that we just took the initialized songs contract with some values with for example with some random duration of the order we created the order we check that this order has corre some correct data in it and these that the functions inside the order are valid I mean these functions represent the correct state of the contract for example because of the duration of course we can check many more things however I think it's enough I think it's
enough so this is The Foundry chest it's long it took me some time to write it really because it's Foundry I will show you how easy it is to write in Python and I will explain it later and for now for now just please uh if you have the Clon repository if you have the vs code open and if you have opened the project so please install the extension tion the extension is called tools for solidity tools for solidity is the extension from a blockchain here this tool installs wake this this framework we will need it before because we need to test with this framework the code so
we'll install the tools for solidity it automatically installs wake wake is based on python so be sure that you have python ready and uh that's it Let's uh just do that so I'll wait for you before so if you install these tools if you have everything right so for those who follow just please do that and I will wait if you still follow of course yeah another in um the tested contract is the
no no no it's it does not inherit we we just take the contract that's just the system of lios songs and we just create another contract that implements this logic of uh in instant instantiation of initialization and uh we create some new ins in these variables inside this tested contract we create some variables for STS and uh against these uh uh instances of these song contracts we verify if everything was correctly so there is uh actually no inheritance between these contracts so we don't have to do that for Foundry and uh for example uh like for example
when I showed the means function it's like mocking like mocking for example I got rid of chain link I mocked I just created another contract it's like mock also so it's uh depends on the use case what to Mock and what not because for example for stons and for the order I didn't mock anything because I don't need to yes these contracts yeah these contracts are tested in the local chain so it's uses wake uses Anvil s for Foundry I'm not sure maybe Anvil 2 I I guess Anvil 2 so yeah this are local test chain uh yes you you can do that that however
you can just modify the storage right away because it's a local chain you can do anything you want you you of course you can change some logic however you don't have to at least in this case for a more complex logic maybe you you will have to so other questions no okay so I guess everyone has tools for Solarity installed so let's start coding and uh what I'm going to do here I will open The Foundry test I will put it here and I will open the test fast it's for Python and uh I will split the screen here so why do we use
Python um let's be honest solidity is made for smart contracts it's a great language but it's made for smart contracts it's not made for testing and the first issue that you may uh experience is that solidity has the limitation on the stack size you cannot have more than 16 local variables it's not very fortunate you know when you create a large test you cannot do that and this is a problem next um as for solidity you will have to sometimes you will you want to test the arithmetics solidity does not have floating Point values and every Dev will
do one thing devs just copy and paste the code with arithmetics from the from the source contract to the test contract it does not test anything when you it's called differential testing when you provide at least two different implementations and you compare the outputs when you use Python it's very straightforward you just rewrite the logic but with using floats in point numbers it's much more precise and next uh when you want to iterate over arrays when you want to use some algorithms for example just like for iterating over array good luck solidity does not have it you just you you just need a lot of boilerplate code for
solidity you cannot use algorithm algorithms python is a very very general purpose language it just takes uh it took a lot of time of other developers to develop tools that we can use with one line of code so solity again it's very difficult so actually python is just more general purpose language and solidity is specific to Smart contracts and our develop our time of developers is expensive extremely expensive python is more easier to learn so let's have a look how the python test works so it's over here we have the Min function is
actually the same absolutely the same we have some uh defined contracts as you can see we just take the addresses of these contracts and use them right away uh wake the the framework pulls the code from uh ether scan and uses it so it's like real actual contracts of Stak ether D usdt usdc so we just need this address for the SS contract it's uh for cow again we don't need it so as for the mean function it's absolutely the same as I explained the St test again wake runs sequences and has flows what is sequence sequence is just one pass of a
fast test the flow is just one call of a function in this fast test when Wes sees several functions that wake can call these functions are called in a random manner with different random data and you can specify how many flows you want for example you will have in one sequence you will have several hundreds of flows and uh you can have for example several hundreds of sequences just runs of these tests and uh pre-sequence is like a setup function is Foundry it's actually the same so and again here we create an agent it's just a random account just new account so default chain accounts it's for for the devel for testing chain
we take the accounts the first accounts it's the main deployer and we say that this account is a manager and here's the thing random it it's like true random in it's like random so in Python is very simple you just specify from which integer you want uh the random number to which integer you want the random number number it's very simple so we specify some random values here and other parameters uh we said the amount converter as I said it's very simple contract that just specifies all the all the time specifies the same price for every pair uh Market is a contract that uh just is connected to amount converter don't uh it's not important here so yeah
here we deploy the sample order we need this in the stons Constructor and again it's very simple here it's it's the same here it's nothing changed so and here yes here we have a normal iteration over inline array we like doing this in solidity uh so yeah we specify if sell token is equals to by so can we just skip the test we don't need it and we create a st's contract again we have a nice dictionary here it's like a mapping mapping but we can iterate over dictionary which is a nice thing because we use Python it's a general purpose language and we deploy a contract it's absolutely the same like in Foundry so here is uh these functions that compute the price based on decimals
based on the price and uh these contracts these implementation is Rewritten using python so we can use differential testing here it's much more secure I would say from the testing point of view so we rewrite these functions using python it's actually the same and here we place the order so again we choose a random random token here St ether for sell tokens for buy tokens and again we can use these nice python features like a set that we can uh skip the part with ifs it's very quick here and as for sell decimals again we compute the price so we take
the random integer here and um here we call these functions we compute the price that should we should expect here we mean some tokens again we sell uh compute the sell amount again you don't have to follow it really I just explain how it works it uh doesn't matter if you don't remember it in one minute no problem and we can comp The Bu buy amount So Random integer okay so yeah and here is the code we will rewrite this online like from Foundry to solidity oh from Foundry to python I'm sorry my bad so I have it prepared here so I don't make this things
up okay so let's have a look first of all we want to test for dust traes how it works so wake has a very nice feature we can say that with must revert we do stonks so this is our contract minimum possible balance not met and this is a nice feature actually because in Foundry you cannot write this at all in Foundry when you write these expect revers you can specify the selector of the correct
error however if this error contains some parameters you have to compute the catchup of this error with these parameters good luck doing this if the logic is very complex in terms of math because for example if one one of parameters has some weird number in it you have to compute it so actually you specify in Foundry you specify that it should revert but you cannot specify with which error it should revert in Wake we can do that so uh with must revert we sell that we call the function self stones and uh of course it's cell token it's by
token and uh we call the function named sorry call the function place order and this function with mean by amount and that's it so we compare again it's almost the same almost [Music] however uh there is a problem with solidity you should specify a very precise ketu so and we return we tested this use case L if it's uh in other so it's so
let's delete this data if the exped by amount is zero so we compute this and uh how do we do that self compute by amount without margin again sell amount sell amount uh price these sell decimals price decimals buy decimals equals to zero if this value is equals to zero so we do again with must revert again we can specify a very precise uh
error here amount converter invalid valid expected out amount and uh again we call selfs with these values so it's very simple and otherwise everything is good so we tested two use cases here otherwise it's good we can go on so first of all we want to call the function the correct function We call we say transaction so it's a transaction data returned by weight so we say self again Stones sorry self STS and uh sell token
token by token again we call the function it's a correct function and uh order with mean by amounts so this is the call it should work work it should not revert that's why we don't uh use must revert and we validate the events so let's have a look what we have here so it's actually the same as you can see here so we in Foundry we had to specify log arrays and Order hash again with types and so forth we don't have to do that here and uh we just do event and again we use Python very nice python
feature so next e for E event for event in transaction events in all events that's is returned by the transaction we if we use is inance is instance e order or order created just have a look how simple that is that we iterate Over All event here that is the instance of order order created and that's it to craft this string it took me like a couple of
minutes to to obtain all the parameters that I needed so it just took some time here we just need the Declaration and um here we specify order order order e order so it's we took the order from the event again it's very simple here we don't need to use this one it's very weird you know so okay and uh we comp put order hash and specific to Stones e order hash we just extracted from the event that's it we just extract several
values from the events right away we use the type order here it's very very simple here it is very straightforward what you need to do next we compute several amounts cell Amounts is equal to e order data sell amount again we extract these from the event and buy amount equals to maximum again we use differential testing here self compute by without by amount it's correct and so we use sell
amount price sell decimals by decimals again it's very straightforward here yes and we check in Python it's very it's very simple we just called it assert assert that buy amount is correct eorder data by amount you know I just realized why I don't have uh code completion because I didn't run the wake in its command so because we use Python and because we
need to generate uh some classes from solidity to python we use wake in it by types and what is by types it's types it's python types for solidity contracts and we can use this like classes we use this we use them so we call if we open the terminal and we call wake in it by types so it compiles the contract and it generates P types what is p types let's have a look P types is a very special for wake it has the same structure and is you can see we have the
same contracts here so for example if I open the order it has every every single function from the order contract so I can use it with python for example these um events and if I scroll down for example I have this function is valid signature initialize so get order details and so forth so P types allow me to use solidity classes in Python like in a straightforward Manner and um so now I guess it should work because it's it's going to be much more quicker so validate the signature so again assert sorry assert we validate the uh order is valid
signature order hash just uh bites from xx and this is a magic number actually it's very simple here it just I can to it take it right from the contract if I open the order it's over here it's from the implementation this by string it should be this so the order is created and not expired so we should revert with must revert order again we can use uh
expired the error with order expired recover recover from token recover token from sorry recover token from so must revert uh great so we now just have to do one last step is just to set the block Time s to a new value and check if the if everything is correct so uh we mine a new block default chain mine Lambda it's an inline uh anous function
here Lambda uh I will explain in a second what this does block timestamp time stamp plus self order duration plus one what does it mean so we mine a new block and this Lambda function is just a callback this callback sets a new uh it's an optional callback actually however it just sets a time stamp for the next block and the next block time stamp is like transaction block time stamp the current time time that we already have plus order duration plus one so it's actually the same as here it's the same line so and we check that everything works I will just copy
paste this with must revert order order not order expired already Okay signature order hash again bytes so I'll check this the same and um recover token from from again give me a second run then
account so again this is the Wake part we just specify in every call we can specify from which account we want to call this function we don't we don't use VM pranks and uh we don't prank anyone here so we just specify from which account we call this function we we call this recover token from from the random account straightforward again so um that's it that's much of it I sent however let's just do one thing so first of all let's try to run The Foundry test so we Fork the main net using the public node and uh if you still follow you can use this uh public noes URL so https uh
ethereum rpcu no.com and we will use it in wake and um we run the test it should fail it should fail it takes some time to compile so w init P types was extremely quick in comparison with this so compiler run successful okay so the test reverted it was not successful we'll have a look why however uh if we have a look we have a stack Trace here and we see all these function calls and in Wake we just write wake
test test fast Pi or we can just skip this part just wake test because in our test we have already written the public note again we don't specify this on common line interface it may be problematic because it's not reproducible we just specify it here that we with for the default chain we use these Fork URL and we run one sequence of 100 flows again so the order order create function will be called 100 times and one for one sequence and if it's reverts we just print the whole uh C Trace it's very simple actually again it's all everything is written in code you just have to run WG test and that's
it now it should fail if I wrote everything correctly so uh so no events let's have a look um events so it's here in object is anable oh you see um if as instance yeah it's over here biom mod Max compute biom mod oh yeah I see I forgot to use which wi with which
Maximum um so it's over here validate the signature um validate the signature where this is I'm dumb update values yeah I see mean by amount mean by amount so life coding is not simple you know I'm sorry for this so yeah uh wake also reverts it's correct and we see the St trace the whole St Trace it's uh very easy to follow because uh for every contract wake tells you which function was successful and which reversed with this
check mark we see that uh st's estimate trade output was successful that's great get expected amount was called Somewhere in the contract okay but then something happened let's a look what happened as for the order I have it here after we compute estimate trade output and generate data here we see that uh Dev wrote the just normal appro approv function for erc20 it works generally it works however there are some tokens like usdt Unfortunately they were introduced uh in very early stages of ethereum and
they behave very in a very weird way sometimes and um sometimes you cannot set a new allowance for new for the address without nullifying it before for example if you have a set allowance for one token for for one address you have to set it before to zero and then to set to a new value it's like prevent front running however it's a very weird case and uh these tokens just revert in this case so here is the back back actually wake helps to find it and uh if we reite it to force approve Force approve it's a
very simple function from safe erc20 this function does that it forces the approval through the nullifying it before and uh through the force approve we can fix this error actually this was an error a mistake in ston code and uh if we regenerate P types again because we need to compile this we in it P types we recompile the project and we run the test so wake test yeah it takes some time of course because we are running a fast test again we gen generate 100 inputs for for this um yeah it's running it's it should
finish I think I guess sometime I guess again so what I wanted to show here I know this is uh pretty much for the code and um if you are interested because lier is a very big protocol I would say if you are interested take a look at the read me take a look at the code it's very simple you will learn from this and um yeah sequence pass as we can see wake finished successfully so the error is fixed and what I wanted to show to you why I used Foundry before Foundry is a very very cool testing framework really it works great however the main problem here so I think we can stop the test
right now however the main problem with Foundry the tests in Foundry are written in solidity and I have already enumerated many problems of solidity in general it's a it's not for example why should we care about gas in tests why should we care about local variables python is a very straightforward language it's just a main advantage of wake and it's very simple it's very straightforward it works faster actually if you visited the talk today it is uh faster than uh Foundry and um it's highly extendable for example I can show some features of wake in the code if we set it back
approve and uh delete this magic line that's just disable the test because uh I wanted to show it to you wake underlines because tools for solidity underlines underscores this code and says that for example first of all it's possible re-entrancy here be careful because you em at the event after calling the external function and next you see that's wake suggest that it's in a safe variant of erc20 call so wake detects this vulnerability right away in the codes when you write the codes and tools for solidity display displays it in your IDE it's very very very convenient and here we have an output of different uh
vulnerabilities potential vulnerabilities and as we saw right away this unsafe function call and say variant of erc20 call is a real vulnerability that is detected by wake if only lied the developers used wake before now they use it I guess so let's get to the question part I know you may have a lot of questions I know that these quotes is very difficult to understand right away in in one hour I tried my best but my main goal was to compare to give it to you the information about using different testing Frameworks so if you have any questions I'm happy to help I'm happy to answer M
MH we uh we take tokens from the treasury or from our Min function we swap them no we don't swap we just we just uh calculate through the chain link feed we calculate the price of the swap we calculate the amount that we want to buy and we create an order no after that uh for Li of STS for the actual implementation an offchain component read reads these event and creates an order for the cow protocol where the swap actually happens uh we didn't swap here we just
calculated prices here we just throw it away we yes yes and we calculate the prices yeah and we thees do yes exactly we calculate the price because we don't care actually about real swapping because we will swap for these prices that we calculated here we will swap for these amount of tokens that we calculated here the swap is actually just one more step it just swaps for these amounts and just sends tokens back to the treasury yes yes yes exactly that's it we just created because we had to calculate it
correctly yes exactly this is the problem and why I say that these uh bugs are really really difficult to find because it's a very straightforward line you just approve the token it's a correct line actually however uh when we audit for example the codes you know what Auditors not every auditor may just know and or developer may not may know that the usdt token is very weird so we just uh with random data with random pairs of tokens we just spef specify the B buy token sell token and we find that with random pairs for where us it figures so we find that this is a
problem and for example if we had a problem with math in solidity because the test in Foundry is just a copy paste of this the same math we wouldn't find this problem if we had a problem with math and we used the implementation in Python we will find that these values are not the same something is wrong as this is the power of differential testing so questions more questions yes of course yes no hard hardhead is just another
language is just in typescript and JavaScript so you will have to rewrite it to another language uh yes I guess it's possible yeah because you can reuse the already deployed local net local chain and the deployed contracts over there you can use it however you know from my just my personal opinion not from AK blockchain you know I just try to use hard head I tried to learn it I tried to use it so much boiler plate code really you have to write so much code to test a very
very simple function and uh personally and only my opinion I just don't use I don't like hard hat and uh for me Foundry is a good choice however it has limitations wake is the best choice in my opinion not because I'm Shilling it not because I'm selling it really I try these Frameworks I because I'm an auditor I'm not a developer I have to teste every framework to be effective to be productive for example what if wig doesn't do anything doesn't do something I I trust uh I tested these Frameworks uh it's a different thing P types actually is uh so wake compiles
contracts it's deploys on the local netk local net and also it creates for you to use these contracts in Python is creates by types so it's like classes representing these uh contracts and when you call a function in Python so these P types just uh wraps this function call and send to the local local net yeah so any more questions uh I will publish it no problem I will
publish it right away because I didn't want to like spoiler to create spoilers I will publish it of course actually the whole code for whole songs is there it's a link for it's open source too like for the whole protocol we WR we've written the fast test however yeah the simplified version is here too so if you don't have any questions uh no more questions yeah so please follow us so I'm trying to start my Twitter you know please follow me I will be very grateful I try to post something interesting from my uh experience from auditing from some contracts so it will be helpful I don't sh post I promise I hate social networks but we need it in today's like realities you know and uh
this is the Twitter of blockchain also follow us you'll find many many interesting things over here so if you don't have any more questions we we're running out enough time so yeah thank you thank you and see you at the bar if you have questions so yeah [Applause]