<?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 Massil Achab on Medium]]></title>
        <description><![CDATA[Stories by Massil Achab on Medium]]></description>
        <link>https://medium.com/@machilassab?source=rss-858ea665a5b8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1RV2wXQv3RABLzoGdfofag.jpeg</url>
            <title>Stories by Massil Achab on Medium</title>
            <link>https://medium.com/@machilassab?source=rss-858ea665a5b8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 22:46:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@machilassab/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[Aave <> StarkNet Bridge]]></title>
            <link>https://medium.com/nethermind-eth/aave-starknet-bridge-9de6797c4ada?source=rss-858ea665a5b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/9de6797c4ada</guid>
            <category><![CDATA[aave]]></category>
            <category><![CDATA[defi]]></category>
            <category><![CDATA[starknet]]></category>
            <category><![CDATA[starkware]]></category>
            <category><![CDATA[nethermind]]></category>
            <dc:creator><![CDATA[Massil Achab]]></dc:creator>
            <pubDate>Tue, 06 Dec 2022 12:50:34 GMT</pubDate>
            <atom:updated>2022-12-08T08:54:22.108Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZJEy_0JhAWLsoS32B69-TQ.png" /></figure><p><em>by </em><a href="https://twitter.com/machilassab"><em>Massil Achab</em></a></p><p><em>Thanks to </em><a href="https://twitter.com/quixotic_eth"><em>Soufiane Hajazi</em></a><em>, </em><a href="https://twitter.com/mempoolsurfer"><em>Jorik Schellekens</em></a>,<em> and </em><a href="https://twitter.com/swp0x0"><em>Swapnil Raj</em></a><em> for helpful comments and revisions.</em></p><p>We are excited to announce the activation of the Aave &lt;&gt; StarkNet Bridge! The bridge lets you hold and trade Aave aTokens on StarkNet while still accruing Aave rewards from Ethereum. This collaboration between <a href="https://bgdlabs.com/">BGD labs</a> and <a href="https://nethermind.io/">Nethermind</a> marks the first step of Aave into the StarkNet ecosystem.</p><h3>Ethereum &lt;&gt; StarkNet bridge for aTokens</h3><p>A user can deposit their aTokens to the bridging contract on Ethereum. The bridge locks the aTokens, wraps them in staticATokens (explained below), and uses StarkNet’s <a href="https://docs.starknet.io/documentation/architecture_and_concepts/L1-L2_Communication/messaging-mechanism/">secure messaging mechanism</a> to mint the equivalent amount of staticATokens on StarkNet. The cool thing about StarkNet messaging is that messages sent to StarkNet are verified as correct on Ethereum when the recipient StarkNet block is verified, rendering messages unforgeable!</p><h4>What is a staticAToken?</h4><p>The holder of an aToken earns their fees in the held aToken. As such, an aToken grows in balance. This constantly changing parameter is hard to bridge over continuously: staticATokens solve the bridging problem by no longer dynamically changing its internal state. A staticAToken represents the share of the aTokens a holder is entitled to, which stays constant. Working with staticATokens, we replace a continuously increasing number, their amount, with a constant number, the share, and avoid numerous updates between networks. Note traders and markets will still need to ascertain the staticAToken’s actual value; however, this falls under the standard set of problems solved by oracles and arbitrage.</p><h4>Reward accrual</h4><p>StarkNet users will receive the same rewards as if they held their aTokens on Ethereum. When Aave’s reward mechanism is active, the Aave protocol awards the bridge contract for the locked aTokens. Holders of staticATokens on StarkNet can claim these rewards according to the staticAToken’s accounting of rewards. Aave rewards can be bridged back to Ethereum at any time by the holders. This way, rewards are fairly distributed proportionally to the time a user holds a staticAToken on the StarkNet side. Trading a token before claiming its accrued reward will result in a record of the outstanding reward due to the holder.</p><h4>Governance</h4><p>Governance still happens on Ethereum for this project. The Governance relayer can execute Governance actions on Ethereum on StarkNet. We use two Ethereum contracts from Aave and one StarkNet contract from the <a href="https://github.com/makerdao/starknet-dai-bridge">StarkNet DAI Bridge</a>. For instance, the execution of the activate_bridge spell, whose address is below, activated the bridge. The reader can find more details in this Ethereum-side <a href="https://app.aave.com/governance/proposal/?proposalId=127">AIP proposal</a>.</p><h4>Diagram</h4><p>The following diagram provides an overview of the system and the interaction between the contracts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*NgCkzJymw7sssEMjF4Zztw.png" /></figure><h3>How to bridge without coding</h3><p>This section explains how to bridge Ethereum aTokens to StarkNet staticATokens using Etherscan UI and wallets on both networks.</p><h4>Step 1 — Hold tokens that can be bridged</h4><p>You need an Ethereum-compatible wallet funded with one of the six following tokens: <a href="https://etherscan.io/address/0x6B175474E89094C44Da98b954EedeAC495271d0F#code">DAI</a>, <a href="https://etherscan.io/address/0x028171bca77440897b824ca71d1c56cac55b68a3#code">aDAI</a>, <a href="https://etherscan.io/address/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48#code">USDC</a>, <a href="https://etherscan.io/address/0xbcca60bb61934080951369a648fb03df4f96263c#code">aUSDC</a>, <a href="https://etherscan.io/address/0xdAC17F958D2ee523a2206206994597C13D831ec7#code">USDT</a>, or <a href="https://etherscan.io/address/0x3Ed3B47Dd13EC9a98b44e6204A523E766B225811#code">aUSDT</a>.</p><h4>Step 2 — Get the tokens’ balance</h4><p>Select the amount you would like to bridge (note that the tokens above do not have the same number of decimals). To do this, go on the Etherscan page corresponding to your tokens, e.g., <a href="https://etherscan.io/address/0x6B175474E89094C44Da98b954EedeAC495271d0F#code">DAI</a>, click on the tab “Read Contract” or “Read as Proxy,” and call the function balanceOf with your wallet address.</p><p>If you hold $50 of DAI / aDAI, the output of balanceOf should be approximately 50000000000000000000, and if you hold $50 of USDC / aUSDC / USDT / aUSDT, it should be about 50000000.</p><h4>Step 3 — Let the bridge transfer your tokens</h4><p>Before depositing your tokens to the bridge, you should allow the bridge to spend your tokens. To do so, go on the token’s Etherscan page, click on “Write Contract” or “Write as Proxy” tab, and then on “Connect to Web3” tab to connect your Ethereum wallet to Etherscan. Click on the function approve, and fill in the bridge&#39;s address (0x25c0667E46a704AfCF5305B0A586CC24c171E94D) as spender and add the amount you would like to bridge as amount. Finally, click &quot;Write&quot; and accept the transaction on your wallet.</p><h4>Step 4 — Deposit your tokens to the bridge</h4><p>To deposit tokens to the bridge, go to the bridge contract Etherscan <a href="https://etherscan.io/address/0x25c0667E46a704AfCF5305B0A586CC24c171E94D#writeProxyContract">page</a>. If your wallet is disconnected, click on “Connect to Web3” tab again, and click on the deposit function to display its arguments. You should then enter the following inputs:</p><ul><li>l1AToken: Fill in the address of the token you would like to bridge. For DAI, write 0x6B175474E89094C44Da98b954EedeAC495271d0F.</li><li>l2Recipient: Fill in your StarkNet wallet address, converted to decimal. Use <a href="https://www.rapidtables.com/convert/number/hex-to-decimal.html">this website</a>, or BigInt function in JavaScript to do this. For instance, if the StarkNet wallet address is 0x01270059Ea5843794F1130830800EcEF60B7D1AFd195f1847a884223a5B94f4A, you should fill in 521222308224262530654458833061745344984501837223744122628617462097842360138.</li><li>amount: Fill in the amount you would like to bridge. This amount should be lower or equal to the amount you have approved in the previous step.</li><li>referralCode: Fill in 0. This argument is proper to identify future integrators.</li><li>fromUnderlyingAsset: Fill in false if the token you bridge is an aToken (aDAI, aUSDC, aUSDT); otherwise, fill in true.</li></ul><p>Finally, click on “Write”, accept the transaction, and wait for Ethereum and StarkNet transactions to finish.</p><h4>Step 5 — Import token to your StarkNet wallet</h4><p>In your StarkNet wallet, click on “+ New token” for Argent X or “+ Add token” for Braavos, and fill in staticAToken’s address that corresponds to the tokens you have deposited on the Ethereum side — see the section below for deployed contracts’ addresses.</p><h3>Deployment on Ethereum and StarkNet</h3><h4>Ethereum</h4><ul><li>Bridge: <a href="https://etherscan.io/address/0x25c0667E46a704AfCF5305B0A586CC24c171E94D">proxy</a> and <a href="https://etherscan.io/address/0x69F4057cC8A32bdE63c2d62724CE14Ed1aD4B93A">implementation</a></li><li>CrosschainForwarderStarknet: <a href="https://etherscan.io/address/0x8c598667A5a6A14F04172326e62CE143BF8edaAB">implementation</a></li><li>AIP payload: <a href="https://etherscan.io/address/0x4919E176f02142C20727da215e8dc1b3d046D026">implementation</a></li></ul><h4>StarkNet</h4><ul><li>bridge: <a href="https://voyager.online/contract/0x0434ab0e4f2a743f871e4d57a16aef3df84c1a29b61565e016da91c1f824b021">proxy</a> and <a href="https://voyager.online/class/0x77cb72a5e969d13753eb4f999219811cd96b703586d6d1de8af7b6679f82a96">implementation class</a></li><li>L2 governance relay: <a href="https://voyager.online/contract/0x07bbb769e53d886f77792d59b9cd65a2eb14a84c49a0942ba9577e291deefcec">proxy</a> and <a href="https://voyager.online/class/0x0431f8e4ac4298966bdf1d99ea273d9f22e72005874d76498bd630fcb806f605">implementation class</a></li><li>Static Aave v2 Ethereum aDAI: <a href="https://voyager.online/contract/0x04212f12efcfc9e847bd98e58daff7dc588c4896f6cd320b74023ad5606f02fd">proxy</a> and <a href="https://voyager.online/class/0x60f3a90b235b6bed4cd55c36a0b48b8e6d075425bfa50105afca6ff1f45a09f">implementation class</a></li><li>Static Aave v2 Ethereum aUSDC: <a href="https://voyager.online/contract/0x014cdaa224881ea760b055a50b7b8e65447d9310f5c637294e08a0fc0d04c0ce">proxy</a> and <a href="https://voyager.online/class/0x60f3a90b235b6bed4cd55c36a0b48b8e6d075425bfa50105afca6ff1f45a09f">implementation class</a></li><li>Static Aave v2 Ethereum aUSDT: <a href="https://voyager.online/contract/0x02e905e3d2fcf4e5813fef9bfe528a304e8e5adc8cbdc247b3980d7a96a01b90">proxy</a> and <a href="https://voyager.online/class/0x60f3a90b235b6bed4cd55c36a0b48b8e6d075425bfa50105afca6ff1f45a09f">implementation class</a></li><li>RewAave: <a href="https://voyager.online/contract/0x047cd265d4ebf1daacd1cca98c0ecb018c3da5cec73a8638bd94d5acfdd78ec1">proxy</a> and <a href="https://voyager.online/class/0x22196a57f69dc51dc5087cb6c288db442a6bc45804502181f1e43da2c92679a">implementation class</a></li><li>activate_bridge spell: <a href="https://voyager.online/class/0x00be3e7fe64939ef463bc80b76703b93c10a61944de34df5bb2dbc7b734e3159">implementation class</a></li></ul><h3>Audits</h3><p>Nethermind, Peckshield, and Certora audited the codebase. Please note that there is a Chinese wall between the Nethermind development team and the Nethermind audit team. We want to thank all the auditors for their meticulous work and remarks, which greatly improved the code.</p><p>Audit reports are available in the project repository here: <a href="https://github.com/aave-starknet-project/aave-starknet-bridge/tree/main/audit">https://github.com/aave-starknet-project/aave-starknet-bridge/tree/main/audit</a>.</p><h3>What’s next?</h3><p>In the upcoming weeks, a discussion will start on the Aave governance forum to port the protocol Aave v3 to StarkNet, the project’s second phase. Please participate if you want this protocol to come to StarkNet.</p><p><strong><em>Disclaimer</em></strong><em>: It is the user’s choice to utilize Aave &lt;&gt; StarkNet bridge contracts. Nethermind does not accept any liability for the risks associated with using the bridge, nor does it accept liability for any other risks typically associated with the holding and trading of tokens.</em></p><p><em>Nethermind is a team of world-class builders and researchers. We empower enterprises and developers worldwide to access and build upon the decentralized web. Our work touches every part of the Web3 ecosystem, from our Nethermind node to fundamental cryptography research and application-layer protocol development. Our extensive Cairo expertise is applied in securing and building the StarkNet ecosystem: </em><a href="https://nethermind.page.link/8a9f"><em>Warp — the Solidity to Cairo compiler</em></a><em>, </em><a href="https://nethermind.page.link/25ee"><em>Cairo Smart Contracts Security Auditing</em></a><em> and </em><a href="https://nethermind.page.link/Mctx"><em>Formal Verification</em></a><em>, as well as </em><a href="https://nethermind.page.link/htwC"><em>Voyager, a StarkNet block explorer</em></a><em>. We’re always looking for passionate people to join us in solving Ethereum’s most difficult challenges. Are you interested? Check out our job board </em><a href="https://nethermind.page.link/careers"><em>https://nethermind.io/company/</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9de6797c4ada" width="1" height="1" alt=""><hr><p><a href="https://medium.com/nethermind-eth/aave-starknet-bridge-9de6797c4ada">Aave &lt;&gt; StarkNet Bridge</a> was originally published in <a href="https://medium.com/nethermind-eth">Nethermind.eth</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cairo Coding Guidelines]]></title>
            <link>https://medium.com/nethermind-eth/cairo-coding-guidelines-74eb6f4ee264?source=rss-858ea665a5b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/74eb6f4ee264</guid>
            <category><![CDATA[starknet]]></category>
            <category><![CDATA[cairo]]></category>
            <category><![CDATA[eth-scaling]]></category>
            <category><![CDATA[zkrollup]]></category>
            <category><![CDATA[cairo-lang]]></category>
            <dc:creator><![CDATA[Massil Achab]]></dc:creator>
            <pubDate>Fri, 23 Sep 2022 09:27:26 GMT</pubDate>
            <atom:updated>2022-11-24T10:21:38.068Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xlZCTpcxEQqYLQ5pv-msTQ.png" /><figcaption>Cairo Coding Guidelines by Nethermind</figcaption></figure><p><em>by </em><a href="https://twitter.com/machilassab"><em>Massil Achab</em></a><em> and </em><a href="https://twitter.com/kalzakdev"><em>Kalzak</em></a><em>.</em></p><p><em>Thanks to </em><a href="https://twitter.com/eniwhere_"><em>Mathieu Saugier</em></a><em>, </em><a href="https://twitter.com/quixotic_eth"><em>Soufiane Hajazi</em></a><em>, </em><a href="https://twitter.com/kaliberpoziomka"><em>Krzysztof Szubiczuk</em></a><em> and </em><a href="https://twitter.com/EdgarBarrantes"><em>Edgar Barrantes</em></a><em> for helpful comments and revisions.</em></p><h3>Guidelines to write Cairo</h3><p>Writing Cairo can be challenging, mainly for two reasons: smart contract developers are tempted to use patterns from Solidity code, and Cairo is a constantly evolving language with new syntax and API changes every few months. Still, ZK-rollup technology is here to stay, and an increasing number of developers onboard every week to learn StarkNet and Cairo. At Nethermind, we have gained experience with Cairo across several projects. We want to share our list of patterns, tips, and recommendations that we use to standardize our Cairo code.</p><h3>Foreword</h3><p>As a team, following a fixed set of rules is a convenient way to maintain a consistent code style across a codebase. This requires all team members to commit to the same code style while writing and reviewing. A benefit to a consistent code style is improved readability, which is crucial during an audit process. The more readable your code is, the easier it is for auditors to understand your protocol, allowing them to focus on more technical security aspects.</p><h3>Code</h3><h4>Composition over Inheritance</h4><p>One of the many differences between Solidity and Cairo is that you can’t use inheritance with Cairo. Instead, you can use the <a href="https://en.wikipedia.org/wiki/Composition_over_inheritance">composition over inheritance principle</a> — called “Extensibility pattern” by OpenZeppelin — to define a class by reusing the code of other classes. We propose a file structure at the end of this guide. More precisely:</p><p>for each contract, we define:</p><ul><li>A library file (or logic file), named my_contract_library.cairo, contains the logic code of the contract. Namely, it contains: (i) internal and external functions encapsulated in a namespace, and (ii) storage variables and events defined outside the namespace.</li><li>A contract file, named my_contract.cairo, exposes external functions from its corresponding library file and other library files. For instance, an implementation of cToken that inherits from an ERC20 contract would expose both functions in c_token_library.cairo and erc20_library.cairo.</li></ul><p>for each abstract contract, we define:</p><ul><li>A library file, following the same naming convention than above. Indeed, an abstract contract should not expose functions but provide code to other contracts.</li></ul><h4>Naming conventions</h4><p>We follow most, but not all, recommendations from OpenZeppelin, detailed on their page about <a href="https://github.com/OpenZeppelin/cairo-contracts/blob/main/docs/modules/ROOT/pages/extensibility.adoc">Extensibility pattern</a>. More precisely:</p><ul><li>We use snake_case for filenames, functions (in library files) and variables.</li></ul><pre>// filenames<br>// example 1: contracts/pool_config.cairo<br>// example 2: tests/test_pool_config.cairo</pre><pre>// functions<br>func config_of{...}(user : felt) -&gt; (config : MyConfig) {<br>    ...<br>}</pre><pre>// variables <br>let (user_config) = config_of(user);<br>let user_balance = user_config.balance;</pre><ul><li>We use snake_case for view and external functions (functions in contract files).</li></ul><pre>// view function<br>@view<br>func get_name{...}() -&gt; felt {<br>    ...<br>}</pre><pre>// external function<br>@external<br>func set_name{...}(name : felt) {<br>    ...<br>}</pre><ul><li>We use UPPER_SNAKE_CASE for constants.</li></ul><pre>const CAIRO_FIELD_ORDER = 2 ** 251 + 17 * 2 ** 192 + 1;</pre><ul><li>We use PascalCase for struct, namespaces, interfaces and events:</li></ul><pre>// struct<br>struct MyStruct {<br>    ...<br>}</pre><pre>// namespace<br>namespace MyNamespace {<br>    ...<br>}</pre><pre>// namespace (interface)<br>@contract_interface<br>namespace IPool {<br>    ...<br>}</pre><pre>// event<br>@event<br>func AssetUpdated(asset : felt) {<br>}</pre><ul><li>We use PascalCase_snake_case for storage variables, where the PascalCase part is namespace’s name:</li></ul><pre>// storage variable (defined for namespace MyNamespace)<br>@storage_var<br>func MyNamespace_user_config(user : felt) -&gt; (config : MyConfig) {<br>}</pre><h4>Import order</h4><p>Regarding Cairo imports, we group them into three categories separated by a newline and alphabetically sort each category. Sorting imports enables fast retrieval for the reader. The three categories are:</p><ol><li>Starkware imports</li><li>External libraries imports (usually cairo-contracts from OpenZeppelin).</li><li>Internal code imports</li></ol><h4>Error messages</h4><p>When using with_attr error_message(...) statement, make sure only one expression in the statement can fail.</p><p>Otherwise, if an expression fails and the error message doesn’t correspond, the developer can spend a lot of time trying to debug.</p><h4>Calldata</h4><p>With Cairo, to pass an array of felt to a function, the usual pattern is to pass a pointer of felt and the array’s length. We recommend encapsulating this array in a struct MyStruct and use MyStruct.SIZE for the array length.</p><p>Otherwise, if you hardcode the array’s length in a function — say IProxy.initialize(pool, 2, new (name, value)) - and want to add an element decimals to the array, you may forget to increment the array length at the same time. Using structs for this is a way to use a dynamic length instead of a fixed number.</p><pre>// using array as calldata<br>IProxy.initialize(pool, 2, cast(new (name, value), felt*));</pre><pre>// using struct as calldata<br>struct InitializeInput {<br>    name : felt,<br>    value : felt,<br>}</pre><pre>IProxy.initialize(pool, InitializeInput.SIZE, new InitializeInput(2, 22));</pre><h4>Guards</h4><p>We recommend calling guards in the contract file. In Solidity, we usually define them with modifiers. Indeed, calling it in the library file can result in reusing it without doing so on purpose.</p><p>Example:</p><pre>// in my_contract_library.cairo<br>namespace MyContract {<br>    func set_name{...}(name : felt) {<br>        MyContract_name.write(name);<br>    }<br>}</pre><pre>// in my_contract.cairo<br>@external<br>func set_name{...}(name : felt) {<br>    Ownable.assert_only_owner();<br>    MyContract.set_name(name);<br>}</pre><h4>Booleans</h4><p>Since Cairo doesn’t have a separate type for booleans, we use felts. It can be misleading for a newcomer to read the following statement: assert bool_1 + bool_2 + bool_3 = 3. Instead, we recommend using a library for comparing and combining boolean variables, like <a href="https://github.com/aave-starknet-project/aave-starknet-core/blob/main/contracts/protocol/libraries/helpers/bool_cmp.cairo">BoolCmp</a>, for better readability.</p><h4>Loops</h4><p>Cairo doesn’t provide support for loops. The alternative is to define recursive functions that operate on arrays instead. For each loop, we recommend defining an internal function suffixed with _inner or _loop to do the job.</p><p>Example:</p><pre>func sum_array(array_len : felt, array : felt*) -&gt; felt {<br>    let sum = 0;<br>    let (res) = _sum_array_inner{array_len=array_len, array=array, sum=sum}(0);<br>    return res;<br>}</pre><pre>func _sum_array_inner{array_len : felt, array : felt*, sum : felt}(current_index : felt) -&gt; felt {<br>    if (current_index - array_len == 0) {<br>        return (sum);<br>    }<br>    let sum = sum + array[current_index];<br>    return _sum_array_inner(current_index + 1);<br>}</pre><h4>Storage variables</h4><p>Storage variable names could conflict for Cairo version prior to 0.10.0. In case of a conflict, it now raises an error at compilation time. We recommend prefixing storage variable names with the namespace to avoid a collision.</p><pre>// in my_contract_library.cairo<br>@storage_var<br>func MyContract_name() {<br>}</pre><pre>namespace MyContract {<br>    ...<br>}</pre><h4>Asserts</h4><p>One can assert several equalities at the same time using structs. Let’s say that a function simple_func returns three values a, b and c. You can use the following:</p><pre>// without struct<br>let (res : SimpleFuncResult) = simple_func();<br>assert res.a = a_true;<br>assert res.b = b_true;<br>assert res.c = c_true;</pre><pre>// with struct<br>let (res) = simple_func();<br>assert res = SimpleFuncResult(a_true, b_true, c_true);</pre><h3>Math</h3><p>Cairo standard library currently provides misleading utils functions to compare felts; see <a href="https://github.com/OpenZeppelin/cairo-contracts/issues/433">this issue</a>, for instance. We have written two libraries to work safely with signed and unsigned felts: <a href="https://github.com/aave-starknet-project/aave-starknet-core/blob/main/contracts/protocol/libraries/math/safe_cmp.cairo">SafeCmp</a> to handle felt comparisons and <a href="https://github.com/aave-starknet-project/aave-starknet-core/blob/main/contracts/protocol/libraries/math/felt_math.cairo">FeltMath</a> for math operations.</p><h4>Unsigned felts</h4><p>Cairo’s native integer type belongs to a finite field of order <em>P = 2^251 + 17 * 2^192 + 1</em>. A way to think about it is to apply integer division by <em>P</em> after every math operation. In particular, integers can take values from <em>0</em> to <em>P-1</em> and then go back to <em>0</em>. That is: <em>P = 0</em> is a true statement in Cairo.</p><p>Thus, one can represent unsigned felts with values <em>{0, 1, …, P-2, P-1}</em>. If a math operation’s result is strictly greater than <em>P-1</em> (resp. strictly lower than <em>0</em>), it can be considered an overflow (resp. an underflow).</p><h4>Signed felts</h4><p>Cairo natively supports signed felts by casting them to <em>{0, 1, …, P-2, P-1}</em>. Recall that <em>P = 0</em> in Cairo, then <em>-1</em> is simply regarded as <em>P-1</em>. With that in mind, we can uniquely represent integers of the interval <em>{-(P-1)/2, -(P-1)/2 + 1, …, (P-1)/2–1, (P-1)/2}</em> using felts. In particular, it means that the maximal signed felt is <em>(P-1)/2</em>, and that the minimal signed felt <em>-(P-1)/2</em> is represented by <em>P-(P-1)/2 = (P-1)/2 +1</em>.</p><p>The two libraries mentioned above are built on top of these findings.</p><h3>Contract structure example</h3><h4>Library file</h4><p>my_contract_library.cairo</p><pre>// imports<br>from starkware.cairo.common.uint256 import Uint256</pre><pre>// storage variables<br>@storage_var<br>func MyContract_name() {<br>}</pre><pre>...</pre><pre>// events<br>@event<br>func Initialized() {<br>}</pre><pre>...</pre><pre>namespace MyContract {<br>    // internal functions<br>    func _first_func{...}(...) {<br>        ...<br>    }</pre><pre>    // getter functions<br>    func second_func{...}(...) {<br>        ...<br>    }</pre><pre>    // setter functions<br>    func third_func{...}(...) {<br>        ...<br>    }</pre><pre>    // more complex functions writing data<br>    func fourth_func{...}(...) {<br>        ...<br>    }<br>}</pre><h4>Contract file</h4><p>my_contract.cairo</p><pre>// imports<br>from openzeppelin.access.ownable.library import Ownable</pre><pre>from contracts.my_contract_library import MyContract</pre><pre>// view functions<br>@view<br>func get_name{..}() -&gt; felt {<br>    return MyContract.get_name();<br>}</pre><pre>...</pre><pre>// external functions<br>@external<br>func set_name{..}(name : felt) {<br>    Ownable.assert_only_owner();<br>    return MyContract.set_name();<br>}</pre><pre>...</pre><p><em>Disclaimer: Please note that the above guidelines are the product of our experience writing Cairo here at Nethermind. We do not claim they represent the only correct way of approaching this issue. We are simply sharing our best practices for you to follow or build on, should you wish to do so. We cannot and are not making any guarantees about the quality of the code should you implement our recommendations.</em></p><p><em>Nethermind is a team of world-class builders and researchers. We empower enterprises and developers worldwide to access and build upon the decentralized web. Our work touches every part of the Web3 ecosystem, from our Nethermind node to fundamental cryptography research and application-layer protocol development. We’re always looking for passionate people to join us in solving Ethereum’s most difficult challenges. Are you interested? Check out our job board</em> <a href="https://nethermind.io/company/">https://nethermind.io/company/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74eb6f4ee264" width="1" height="1" alt=""><hr><p><a href="https://medium.com/nethermind-eth/cairo-coding-guidelines-74eb6f4ee264">Cairo Coding Guidelines</a> was originally published in <a href="https://medium.com/nethermind-eth">Nethermind.eth</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>