<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by DeFi Wonderland on Medium]]></title>
        <description><![CDATA[Stories by DeFi Wonderland on Medium]]></description>
        <link>https://medium.com/@defiwonderland?source=rss-ec0633ca8a86------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*j3BDvizqYkNSFSrJv4BpOg.png</url>
            <title>Stories by DeFi Wonderland on Medium</title>
            <link>https://medium.com/@defiwonderland?source=rss-ec0633ca8a86------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 12:20:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@defiwonderland/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Upkeeping the protocol]]></title>
            <link>https://defiwonderland.medium.com/upkeeping-the-protocol-a654b57b7d06?source=rss-ec0633ca8a86------2</link>
            <guid isPermaLink="false">https://medium.com/p/a654b57b7d06</guid>
            <category><![CDATA[keepers]]></category>
            <category><![CDATA[keep3r]]></category>
            <category><![CDATA[dao]]></category>
            <dc:creator><![CDATA[DeFi Wonderland]]></dc:creator>
            <pubDate>Wed, 06 Apr 2022 13:59:17 GMT</pubDate>
            <atom:updated>2022-04-06T13:59:17.877Z</atom:updated>
            <content:encoded><![CDATA[<p>Since the launch of Keep3r V2 in early December, we’ve been monitoring the network and working towards improving the keeper’s experience. We are committed to creating a protocol that is <strong>permission-less, immutable and credible-neutral</strong>.</p><p>During this period, we found a series of bugs that difficulted the proper functioning of the network. It is important to clarify that funds were never at risk. In order to implement the required fixes, two contract migrations where performed (details below):</p><pre><strong>Keep3r Core:</strong></pre><pre>Keep3rV2.0: 0xdc02981c9c062d48a9bd54adbf51b816623dcc6e<br>Keep3rV2.1: 0x4A6cFf9E1456eAa3b6f37572395C6fa0c959edAB<br>Keep3rV2.2: 0xeb02addCfD8B773A5FFA6B9d1FE99c566f8c44CC</pre><pre><strong>Keep3r Helper:</strong></pre><pre>HelperV2.0: 0xc07c37a0a1e557e0d1704c90d966b37a67a45dd4<br>HelperV2.1: 0x12038d459166ab8e68768bb35ec0af765a36038d<br>HelperV2.2: 0xd36ac9ff5562abb541f51345f340fb650547a661</pre><pre><strong>Keep3r Proxy:</strong></pre><pre>Keep3rV1Proxy: 0xfc48ac750959d5d5ae9a4bb38f548a7ca8763f8d<br>Keep3rV1ProxyV2: 0x976b01c02c636dd5901444b941442fd70b86dcd5</pre><pre><strong>Uniswap V3 Pair Manager:</strong></pre><pre>FactoryV1: 0x005634CfeF45e5a19C84aEdE6f0af17833471852<br>kLP KP3R/WETH V1: 0xfbba1784163212e7b639ed9e434e3aed48036b34</pre><pre>FactoryV2: 0x053D7DD4dde2B5e4F6146476B95EA8c62cd7c428<br>kLP KP3R/WETH V2: 0x3f6740b5898c5D3650ec6eAce9a649Ac791e44D7</pre><p>All the fixes have been audited by <a href="https://twitter.com/peckshield">Peckshield</a>. While audits don’t represent an insurance against potential bugs, we do believe that its fundamental to have outside reviewers.</p><p><em>Also remember that Keep3r Network has a bug bounty program live on </em><a href="https://immunefi.com/bounty/keep3rnetwork/"><em>Immunefi with up to $1M in bounties</em></a><em>.</em></p><p>In the following sections, we will present the list of fixes and improvements added to Keep3rV2 in the migrations:</p><h3>Bug Fixes</h3><h4>V2.0 quoting bug</h4><p>After the first work() transaction in Keep3rV2.0 <a href="https://etherscan.io/tx/0x07c5e3f5f1eb2f2887ca7ec4550b2cfca2c594846582fe72c216be1d622f3806">0x07c5e3f5f1eb2f2887ca7ec4550b2cfca2c594846582fe72c216be1d622f3806</a>, the team realized that the job was underpaying the keeper by a factor of 50%.</p><p>After reviewing all transaction logs and traces, we discovered two main issues with the quoting calculation:</p><ul><li>Keep3rHelper.sol: boost was miscalculated (<strong>minor bug</strong>)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*J14lZDs1rAPA9wiG.png" /></figure><p>If a keeper has 50KP3R bonded (out of a target of 200), they should perceive a boost in between MIN and MAX (25% above MIN). Using the outdated calculation, MIN would always be used until the keeper had ~90% of TARGETBOND (and MAX * 90% &gt; MIN).</p><p>Calculation changed to:</p><pre>uint256 _cap = Math.max(MIN, MIN + (MAX-MIN)*_bonds/TARGETBOND)</pre><ul><li>Keep3rJobWorkable.sol:: gas quoting to KP3R was miscalculated (<strong>critical bug</strong>)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zxN2Wlr0qiKCx3L1.png" /></figure><p>L59 showed _payment calculation, using _quoteLiquidity function. This function was used to calculate, given an amount of kLPs, the amount of underlying KP3Rs of that liquidity:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3namz2152TDz0lEM.png" /></figure><p>When calculating _payment, the desired calculation should not be getKP3RsAtTick, but rather getQuoteAtTick, since the payment calculation should convert the gas cost of the transaction (in ETH), to the amount of KP3Rs to pay the keeper.</p><p>Bug was solved using the following functions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DBfD5qNQn1Ifq7tG.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2deSXeRMRDiYp2g5.png" /></figure><h4>UniswapV3 PairManagerFactory</h4><p>The PairManagerFactory was designed to deploy a full-range liquidity position and fractionalize it as an ERC-20. Since UniswapV3 pools have different tick spacing depending on their fee tier, some of the fee tiers (and possible future ones) were not being able to deploy, as the full-range was indicating to have a lowerTick and upperTick that would not exist for those fee tiers.</p><p>Previous ticks declaration:</p><pre>int24 constant MAX_TICK = 887200;<br>int24 constant MIN_TICK = -887200;</pre><p>Fixed ticks declaration:</p><pre>int24 constant _MAX_TICK = 887272;<br>int24 _tickSpacing = IUniswapV3Pool(_pool).tickSpacing();<br>int24 _tickUpper = _MAX_TICK - (_MAX_TICK % _tickSpacing);<br>int24 _tickLower = -_tickUpper;</pre><h4>UniswapV3 PairManager</h4><p>A factory() view function was added to point to the deployer of the PairManager.</p><h3>Improvements</h3><h4><strong>Gas measurements with EIP-150</strong></h4><p>In Keep3rV2, gas is registered and logged in the work events parameters. This allowed us to identify that gas measurements was not having into account the implications of the EIP-150 subcalls specification, where each subcall carries only 63/64 of the original gas. Gas used by keepers was measured using:</p><pre>// Initialize<br>_initialGas = gasleft();<br>...<br>// Calculate gas<br>_accountedGas = _initialGas - gasleft();</pre><p>Since Keep3r (in a work scenario) is always called (at least) on a 2nd level of depth, as keepers should call the work() function at the Job contract, gasleft() was always bringing a measurement 1/64 below the real used gas by the keeper on the job transaction.</p><p>This issue was addressed by changing gasleft() for an internal function that would compensate the gas reduction on the subcall:</p><pre>function _getGasLeft() view returns (uint256 _gasLeft) {<br>    _gasLeft = (gasleft() * 64) / 63;<br>  }</pre><h4>Gas measurements on small job transactions (under 500k gas)</h4><p>We were also able to identify that keepers were spending unavoidable gas before the _initialGas measurement, and after the _accountedGas measurement. The 21.700 gas used on the initial function call could never be a part of the total gas accountancy, nor could the gas used after the accountancy to perform the payment to the keeper.</p><p>Although all work transactions were susceptible to this unaccounted gas, its effect was discovered to have a considerable impact on smaller transactions (those under 500k gas units).</p><p>A gasCoefficient measurement was designed to track this behaviour, with 1 being the ideal number, and higher values picturing inconvenient scenarios for keepers (as they pay for gasUsedand are rewarded for accountedGas):</p><pre>gasCoefficient = gasUsed / accountedGas</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6z8AMRblcqRFJsr2.png" /><figcaption>Old Payout Coefficient Curve</figcaption></figure><p>To mitigate this effect, a new gas profiling formula was designed. We are now able to manipulate the accountancy by applying a linear formula aX + b (being X the accounted gas, and a &amp; b configurable constants).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/1*rIlhCg37cuneMC0EgI6DWA.png" /><figcaption>New Gas Coefficient Curve</figcaption></figure><p>Although gas expenditure on the EVM is highly unpredictable, the new gas profiling formula has shown an improved precision on all tx size ranges, allowing jobs to use the protocol for low-size maintenance transactions without the risk of being unprofitable (and not upkept).</p><h4>Payment quote</h4><p>One of the key features of Keep3rV2 was the implementation of TWAP quotes for the provision of the job’s liquidity and the calculation of rewards for keepers.</p><p>Our first design approach was to use a 5 day TWAP (the same one) for both LPs and work transactions. The TWAP calculation showed an improvement on the UX for job owners, as their credits will automatically adapt to new values of the tokens: should KP3R devaluate, they would mint more KP3R credits to compensate; should it increase in value, they would mint less KP3Rs as they should require less to pay for jobs.</p><p>On the other hand, when KP3R had a lower value than the 5 day TWAP, even with the +10/20% boost for working a job, keepers would be getting a much better quote by swapping ETH for KP3Rs on the market, instead of working. For this reason, payments to keepers implement now a 10 minute TWAP instead. This reduces notably the previous possibility and creates a more robust incentives schema.</p><h3>Call to action for keepers and job providers</h3><h4>Migrate your bonded keeper</h4><ul><li>unbond your $KP3R from Keep3rV2.1 [ Keep3rV2–1.unbond() ]</li><li>withdraw your $KP3R from Keep3rV2.1 [Keep3rV2–1.withdraw() ]</li><li>bond your $KP3R on Keep3rV2.2 [ Keep3rV2–2.bond() ]</li><li>activate your Keeper on Keep3rV2.2 [Keep3rV2–2.activate()]</li></ul><h4>Migrate your job</h4><ul><li>unbond your old kLP from Keep3rV2.1 [ unbondLiquidityFromJob() ]</li><li>withdraw your old kLP from Keep3rV2.1 [ withdrawLiquidityFromJob() ]</li><li>burn your old kLP from the deprecated UniswapV3PairManager <a href="https://etherscan.io/address/0xfbba1784163212e7b639ed9e434e3aed48036b34#writeContract">contract</a></li><li>mint your new kLP from the new UniswapV3PairManager <a href="https://etherscan.io/address/0x3f6740b5898c5D3650ec6eAce9a649Ac791e44D7#writeContract">contract</a></li><li>add your job on Keep3rV2.2 [ addJob() ]</li><li>bond your kLP on Keep3rV2.2 [ addLiquidityToJob() ]</li></ul><p>If you are a keeper or a job provider and have any questions regarding the migration, please do contact us through Twitter and/or Discord and we would be more than happy to help with the process.</p><h3>What’s next?</h3><p>As we’ve commented on the announcement of the launch of Keep3rV2, for the past few months we have been working together with <a href="https://www.nomad.xyz/">Nomad</a> and <a href="https://www.connext.network/">Connext</a> on bringing Keep3r to every single EVM compatible chain.</p><p>We are now in the final process of development for Keep3r Multichain. We plan to start live testing on mainnet within the next few weeks. If you would like to learn more about this, you can follow us on our <a href="https://twitter.com/DeFi_Wonderland">Twitter</a> or <a href="https://twitter.com/thekeep3r">Keep3r’s Twitter</a> where we will be posting and article about the design infrastructure in the near future.</p><p>About <strong>Keep3r Network</strong>: A decentralized Keeper Network for projects that need external devops and for external teams to find keeper jobs.</p><p>Website: <a href="https://thekeep3r.network/">https://thekeep3r.network/</a><br>Twitter: <a href="https://twitter.com/thekeep3r">https://twitter.com/thekeep3r</a><br>Discord: <a href="https://discord.gg/9JBxTWR4nZ">https://discord.gg/9JBxTWR4nZ</a></p><p>About <a href="https://defi.sucks/"><strong>Defi Wonderland</strong></a> is an activist fund with an army of Solidity, UI/UX devs and researchers.</p><p>At Wonderland, we invest into projects and strongly commit to them from a technical standpoint. Our approach to projects consists in lending a hand to improve the current situation, we do not take over the protocols.</p><p>Website: <a href="https://defi.sucks/">https://defi.sucks/</a><br>Twitter: <a href="https://twitter.com/DeFi_Wonderland">https://twitter.com/DeFi_Wonderland</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a654b57b7d06" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Keep3r V2: decentralize & conquer]]></title>
            <link>https://defiwonderland.medium.com/keep3r-v2-decentralize-conquer-36d65f57d26a?source=rss-ec0633ca8a86------2</link>
            <guid isPermaLink="false">https://medium.com/p/36d65f57d26a</guid>
            <category><![CDATA[v2]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[defi]]></category>
            <category><![CDATA[keepers]]></category>
            <category><![CDATA[keeper-network]]></category>
            <dc:creator><![CDATA[DeFi Wonderland]]></dc:creator>
            <pubDate>Tue, 14 Dec 2021 18:08:27 GMT</pubDate>
            <atom:updated>2021-12-14T18:08:27.275Z</atom:updated>
            <content:encoded><![CDATA[<p>Earlier this year, we <a href="https://gov.yearn.finance/t/keep3r-v2-optimizing-the-protocol-ability-to-grow/10938">proposed</a> &amp; started the adventure of developing the V2, after 5 months of work we are happy to reveal the final release candidate for <a href="https://thekeep3r.network/">Keep3r V2</a> that has been <a href="https://github.com/defi-wonderland/keep3r-v2/blob/dev/audits/PeckShield-Audit-Report-Keep3rV2-v1.0.pdf">audited by Peckshield</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vrGnNTmry-WNFzNcN4WwjA.png" /><figcaption><a href="https://thekeep3r.network/">https://thekeep3r.network/</a></figcaption></figure><h3>Time based credit minting</h3><p>In the first version, jobs were funded by providing liquidity to the protocol, bonded for a period of 2 weeks, ensuring that all the credits minted were always backed by underlying liquidity. This setup, required the job to bond and unbond the liquidity every 14 days to keep on minting credits, incurring in unnecessary transaction costs, and requiring the provider to be aware and trigger those transactions when the time comes.</p><p>In Keep3rV2, we kept the same minting principle, every minted KP3R is backed by an underlying KP3R bonded to the protocol to ensure its value. The difference is that KP3Rs are minted gradually with time, and require no further transactions from the liquidity provider.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fd6GjGpbyWZEPtbz.png" /><figcaption><em>Comparison between Keep3rV1 and Keep3rV2 funded with 10 KP3R running the same job</em></figcaption></figure><p>Each period, a job should be rewarded an amount of credits equal to the underlying KP3Rs of the KP3R LPs provided, multiplied by the proportion between the period length and the configurable inflationPeriod:</p><pre>underlying KP3R of the kLPs * rewardPeriod / inflationPeriod</pre><p>To avoid KP3R honeypotting, minted credits are only to be used within a reward period, and old unused credits burnt.</p><p>Updating a v2 job requires only deploying a new job contract, registering the new job address, and triggering a migration from the older job contract address to the new one. Credits and kLPs will be seamlessly migrated, and the new job ready to be worked.</p><h3>Uniswap V3 Pair Manager Factory</h3><p>The UniV3PairManagerFactory is a factory that allows anyone to create a full range position on any Uniswap V3 pool and tokenize the ERC721 full range position into ERC20 kLPs.</p><p>To tokenize the liquidity into ERC20 kLPs, a Pair Manager contract is deployed, allowing providers to deposit TOKEN A and TOKEN B, and it will handle the funds into the Pair Manager’s full range position.</p><p>To incentivize KP3R liquidity, a set of approved kLP liquidities will be accepted as collateral in Keep3r V2 to mint new Keep3r credits, as a requirement, one of the 2 tokens must be KP3R.</p><h3>Uniswap V3 TWAP Oracle</h3><p>To efficiently calculate how many KP3R are bonded to a certain kLP, Keep3rV2 observes and stores the tickCummulative value of the underlying Uniswap V3 Pool at the timestamp of the last period start. When the first job of the period gets worked, its keeper will also perform this pool observation, then calculate job credits, and finally get rewarded for the gas cost of the whole transaction.</p><p>Since jobs are rewarded for the gas * baseFee costs in ETH, a KP3R/WETH pair is natively quoted using the KP3R/WETH 1% UniV3Pool: <a href="https://etherscan.io/address/0x11B7a6bc0259ed6Cf9DB8F499988F9eCc7167bf5#readContract">0x11B7a6bc0259ed6Cf9DB8F499988F9eCc7167bf5</a></p><p>To reduce the impact of KP3R spot price on the speculation of a job’s profitability, rewards and credits are quoted at the same time, under the same principles. This means that within a certain period, both quotings will remain stable, ensuring the job a certain available amount of gas * baseFee to reward to the keepers.</p><h3>Keep3r-CLI</h3><p>As we stated on the proposal and while working on the infrastructure of Keep3r V2, we realized that there was a huge lack of adoption from the keepers’ side, so we decided to develop a client so anyone can run their own keeper with little to no technical knowledge.</p><p><a href="https://github.com/keep3r-network/cli"><strong>Keep3r-CLI</strong></a><strong> </strong>provides an easy-to-use CLI along with all the necessary code required to run your own keeper and start working on jobs. This is a beta release, proceed with caution.</p><p>A job board will be curated on the Keep3r-CLI repository, with description and the required information for each job, so the protocols have a way of broadcasting their jobs through pull requests to the public repository (in a similar fashion as <a href="https://github.com/flashbots/mev-job-board">MEV Job Board</a>).</p><h3>Multi-chain future</h3><p>Looking into the short-term future, we want to expand support of the Keep3r Network to as many blockchains as feasible. While our design of V2 has in mind the multi-chain approach, we have to standardize the approach of how we expand to different ecosystems. This is currently a work in progress but once the framework has been finalized, its a matter of working on the specifics for any chain that we want to expand towards (including but not limited to oracles, incentives &amp; adoption)</p><p>About <strong>Keep3r Network</strong>: A decentralized Keeper Network for projects that need external devops and for external teams to find keeper jobs.</p><p>Website: <a href="https://thekeep3r.network/">https://thekeep3r.network/</a><br>Twitter: <a href="https://twitter.com/thekeep3r">https://twitter.com/thekeep3r</a><br>Discord: <a href="https://discord.gg/9JBxTWR4nZ">https://discord.gg/9JBxTWR4nZ</a></p><p>About <strong>DeFi Wonderland</strong>: An activist fund meets a Solidity army.</p><p>Website: <a href="https://defi.sucks/">https://defi.sucks/</a><br>Twitter: <a href="https://twitter.com/DeFi_Wonderland">https://twitter.com/DeFi_Wonderland</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36d65f57d26a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Smock — An Optimistic Adventure in Wonderland]]></title>
            <link>https://defiwonderland.medium.com/smock-an-optimistic-adventure-in-wonderland-1eec36c3e0dd?source=rss-ec0633ca8a86------2</link>
            <guid isPermaLink="false">https://medium.com/p/1eec36c3e0dd</guid>
            <dc:creator><![CDATA[DeFi Wonderland]]></dc:creator>
            <pubDate>Wed, 11 Aug 2021 16:46:54 GMT</pubDate>
            <atom:updated>2021-08-11T16:47:55.000Z</atom:updated>
            <content:encoded><![CDATA[<h3>Smock — An Optimistic Adventure in Wonderland</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ku2ZrsG3xsE6ljJ-_y8yTA.gif" /></figure><p>As many of you know, Smock is a mighty tool for mocking solidity contracts developed by <a href="https://twitter.com/kelvinfichter">Kelvin Fichter</a>, one of the mad people behind Optimistic Ethereum (Optimism).</p><p>At DeFi Wonderland we <a href="https://twitter.com/DeFi_Wonderland/status/1401913210631036932?s=20">have long ago decided to partake</a> in this adventure of making Solidity Testing as easy and powerful as it can get, that’s why we’ve partnered with Optimism to develop Smock 2.0 together.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/0*7z4gDfuKv-Do6YIC" /></figure><p>So… what does the new smock have to offer?</p><p><a href="https://github.com/defi-wonderland/smock"><strong>smock</strong></a> makes the process of testing complex smart contracts significantly easier. You’ll never have to write another mock contract in Solidity again.</p><ul><li>Get rid of your folder of “mock” contracts and just use JavaScript.</li><li>Keep your tests simple with a sweet set of chai matchers.</li><li>Fully compatible with TypeScript and TypeChain.</li><li>Manipulate the behavior of functions on the fly with fakes.</li><li>Modify functions and internal variables of a real contract with mocks.</li><li>Make assertions about calls, call arguments, and call counts.</li><li>We’ve got extensive documentation and a complete test suite.</li></ul><h3><strong>Why do we test?</strong></h3><p>If you are into Ethereum, then you probably know that contracts are immutable, and that developers must spend most of their time testing their own code. If a developer, by some intricated coding or few hours of sleep, broke some functionality of his contract, he needs to be warned somehow. That’s what tests are made for.</p><h4><strong>So what’s a mock contract?</strong></h4><p>Let’s say we have the following contract, not so different from the common Greeter that you’ll find all around Solidity tutorials, with the only difference that will come handy for this explanation: after the 10th greet change, it will change it no more.</p><pre>//SPDX-License-Identifier: Unlicense<br>pragma solidity ^0.8.4;</pre><pre>contract LazyGreeter {</pre><pre>  string public greeting = ‘Hello World!’;<br>  int24 public count;</pre><pre>  function greet() public view returns (string memory) {<br>    return greeting;<br>  }</pre><pre>  function setGreeting(string memory _greeting) public returns (bool _changedGreet) {<br>    if (count &lt; 10) {<br>      greeting = _greeting;<br>      _changedGreet = true;<br>    count += 1;<br>  }<br>}</pre><p>As you can see, there’s a setter function for the greet, but there’s not a setter for the count. But the count is easily predictable, if you call the setter function, the count will get +1, until it reaches 10.</p><p>So performing a test on this SUPER simple feature should be quite easy: we just call the contract setGreeting function 11 times and see what happens! That’s called an end-to-end test, we interact with the contract as we would on the EVM, just calling it’s functions, and we may be able to enjoy some of the advantages of running this contract in our own local node (like having A LOT of ETH to play with, gas free transactions, or being able to fake other people’s signatures, just for the sake of testing).</p><p>In an end-to-end test we have several disadvantages, to see what happens after greet change n10, we should need to make 10 transactions. So if any of those transactions would fail before we’ve got to the sweet spot, we are going to have a hard time trying to debug it (because the test was not supposed to fail in the way it failed). And what if the limit for the counter was more than 10? Let’s say… 10¹⁰ (number completely possible). So then we would need to make 10000000001 transactions to get a glimpse of our precious feature. That can take AGES.</p><p>So, as with any other problem in life, there’s always multiple approaches to it:</p><h4><strong>Approach A “The just call it 10 times”</strong></h4><pre>   greeterFactory = (await ethers.getContractFactory(&#39;LazyGreeter&#39;)) as LazyGreeterForTest__factory;<br>     greeter = await greeterFactory.deploy(&#39;Hello, world!&#39;);<br>[...]<br>it(&quot;Should not change the greeting more than 10 times&quot;, async () =&gt;{<br>     await greeter.setGreeting(&#39;Hola, mundo!&#39;);<br>     await greeter.setGreeting(&#39;Hola, mundo!&#39;);<br>     await greeter.setGreeting(&#39;Hola, mundo!&#39;);<br>     await greeter.setGreeting(&#39;Hola, mundo!&#39;);<br>     await greeter.setGreeting(&#39;Hola, mundo!&#39;);<br>     await greeter.setGreeting(&#39;Hola, mundo!&#39;);<br>     await greeter.setGreeting(&#39;Hola, mundo!&#39;);<br>     await greeter.setGreeting(&#39;Hola, mundo!&#39;);<br>     await greeter.setGreeting(&#39;Hola, mundo!&#39;);<br>     await greeter.setGreeting(&#39;Hola, mundo!&#39;);<br>     expect(await greeter.callStatic.setGreeting(&#39;Hola, mundo!&#39;)).to.be.false;<br>  })<br>});</pre><p>Notice that our tests are Typified, that’s a really helpful interaction between the IDE and our contracts, predicting us which functions are available to use, or not even starting to run the test if something is just not right. If you like to test this typifying, I’d suggest you take a look at our <a href="https://github.com/defi-wonderland/solidity-boilerplate">Solidity Boilerplate</a>.</p><h4>Approach B “The use another, but similar, contract”</h4><p>So this is where a unit test comes handy. In unit tests we give ourselves the permission to modify a little bit our contract, to be able to focus on that particular function, and not the whole environment around it. So up until Aug-2021, this is what we were supposed to do: Mock Contracts.</p><pre>//SPDX-License-Identifier: Unlicense<br>pragma solidity ^0.8.4;</pre><pre>import &#39;./LazyGreeter.sol&#39;;</pre><pre>contract LazyGreeterForTest is LazyGreeter {<br>  constructor(string memory _greeting) LazyGreeter(_greeting) {}</pre><pre><br>  function setCounter(uint256 _count) external {<br>    count = _count;<br>  }<br>}</pre><p>So now we have a different contract, it’s called LazyGreeterForTest, it imports LazyGreeter (with its variables and functions), and adds a piece of code that is definitely NOT going to be in the deployed contract: a setter for the count!</p><pre>greeterForTestFactory = (await ethers.getContractFactory(&#39;LazyGreeterForTest&#39;)<br>greeterForTest = await greeterFactory.deploy(&#39;Hello, world!&#39;);<br>[...]<br>it(&#39;Should not change the greeting more than 10 times&#39;, async () =&gt; {<br>    await greeterForTest.setCounter(10);<br>    expect(await greeterForTest.callStatic.setGreeting(&#39;Hola, mundo!&#39;)).to.be.false;<br>  });<br>});</pre><p>As you can see, it’s fairly easy to recognize that the contract is quite the same, so the feature we’re looking after should behave the same as well. But when contracts start to get rather Messi, we can end up having more ForTest contracts than actual contracts!</p><p>So this is where <strong>Smock</strong> comes into play.</p><h4><strong>Approach C “The just Smock it!”</strong></h4><pre>     const greeterFactory = await smock.mock&lt;LazyGreeter__factory&gt;(&#39;LazyGreeter&#39;);<br>     greeter = await greeterFactory.deploy(&#39;Hello, world!&#39;);<br>[...]<br>it(&#39;Should not change the greeting more than 10 times&#39;, async () =&gt; {<br>    greeter.setVariable(&#39;count&#39;,10);<br>    expect(await greeter.callStatic.setGreeting(&#39;Hola, mundo!&#39;)).to.be.false;<br>  });<br>});</pre><p>Notice that the setVariable is a functionality of Smock, and not the contract!</p><p>Advantages:</p><ul><li>One does not need to test his contract through another similar one</li><li>One does not need to test the ForTest contracts (Smock is fully tested)</li><li>One does not need to code extra .sol files</li><li>One could program a whole function response instead of it’s internal variables</li><li>One could program a different response for each call</li><li>One could assert that a certain function has/hasn’t been called in the interaction</li></ul><p>And this is just the tip of the iceberg, go check out the docs!</p><p><a href="https://smock.readthedocs.io/en/latest/">https://smock.readthedocs.io/en/latest/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1eec36c3e0dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>