<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Maakai123</title>
    <description>The latest articles on DEV Community by Maakai123 (@maakai123).</description>
    <link>https://dev.to/maakai123</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F888969%2Fcfd35eac-27e3-490f-8149-fa2b3fc3a8a3.jpeg</url>
      <title>DEV Community: Maakai123</title>
      <link>https://dev.to/maakai123</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maakai123"/>
    <language>en</language>
    <item>
      <title>Aptos Move #Tip 8: Understanding Aptos Objects and ConstructorRef Leaks.</title>
      <dc:creator>Maakai123</dc:creator>
      <pubDate>Tue, 03 Jun 2025 17:54:38 +0000</pubDate>
      <link>https://dev.to/maakai123/aptos-move-tip-8-understanding-aptos-objects-and-constructorref-leaks-2mb3</link>
      <guid>https://dev.to/maakai123/aptos-move-tip-8-understanding-aptos-objects-and-constructorref-leaks-2mb3</guid>
      <description>&lt;p&gt;What Are Aptos Objects?&lt;/p&gt;

&lt;p&gt;In the Aptos blockchain, Objects are a way to represent things like NFTs (non-fungible tokens, like digital art or collectibles), tokens, or other assets. Think of an Object as a digital container that holds data and rules about how that data can be used. For example, an NFT Object might store the image URL, its name, and who owns it.When you create an Object (like minting a new NFT), you use something called a ConstructorRef. This is like a special key that lets you set up the Object and decide what rules or resources (like ownership details) it should have. However, this key is powerful, and if it falls into the wrong hands, it can cause serious problems.&lt;/p&gt;

&lt;p&gt;What Is a ConstructorRef Leak, and Why Is It Dangerous?&lt;/p&gt;

&lt;p&gt;A ConstructorRef is a temporary capability (or permission) created when you make a new Object in Aptos. It allows you to:&lt;br&gt;
Add resources to the Object (like extra data or rules).Create other capabilities, like a TransferRef, which controls who can move or transfer the Object.&lt;/p&gt;

&lt;p&gt;If you accidentally share or expose the ConstructorRef (for example, by returning it in a function), someone else could use it to:Add unauthorized resources to the Object.Generate a TransferRef to move the Object, even after it’s been sold or transferred to someone else.Store the ConstructorRef in a way that lets the original creator (or a hacker) regain control later.This is called a ConstructorRef leak, and it’s a big security risk. Imagine selling a digital collectible (like a rare Pokémon card NFT) and then finding out the original creator can take it back because they kept a secret key!&lt;/p&gt;

&lt;p&gt;Real-World Example: A Vulnerable NFT Minting Function&lt;/p&gt;

&lt;p&gt;Let’s say you’re building an NFT marketplace on Aptos where artists can create (or "mint") NFTs. You write a function that creates an NFT and returns its ConstructorRef. Here’s what that might look like in Move, the programming language used by Aptos:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="mi"&gt;0x42&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;string&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;aptos_framework&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;mint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;creator&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;ConstructorRef&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;constructor_ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;token&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create_named_token&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;creator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nf"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;b"Collection Name"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nf"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;b"Collection Description"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nf"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;b"Token"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nn"&gt;option&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;none&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="nf"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;b"https://mycollection/token.jpeg"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;constructor_ref&lt;/span&gt; &lt;span class="c1"&gt;// Returning the ConstructorRef&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What’s the problem?&lt;/p&gt;

&lt;p&gt;The mint function returns the ConstructorRef, which is like handing over the master key to the NFT.&lt;br&gt;
Anyone with the ConstructorRef can create a TransferRef (another capability) to move the NFT, even after it’s sold.They could also store the ConstructorRef somewhere (like in global storage on the blockchain) and use it later to steal the NFT back or mess with its rules.&lt;/p&gt;

&lt;p&gt;Real-world analogy: &lt;br&gt;
Imagine you sell a car and give the buyer the title, but you accidentally include a duplicate key that lets you unlock and drive the car anytime.&lt;br&gt;
A ConstructorRef leak is like leaving that duplicate key lying around where anyone can grab it.&lt;/p&gt;

&lt;p&gt;How to Fix It:&lt;/p&gt;

&lt;p&gt;Secure Coding Practices to prevent a ConstructorRef leak, you should never return or expose the ConstructorRef. Instead, keep it private within the function and only use it to set up the Object as needed. Here’s how to fix the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="mi"&gt;0x42&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;string&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;aptos_framework&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;mint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;creator&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;constructor_ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;token&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create_named_token&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;creator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nf"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;b"Collection Name"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nf"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;b"Collection Description"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nf"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;b"Token"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nn"&gt;option&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;none&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="nf"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;b"https://mycollection/token.jpeg"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// Don’t return constructor_ref; let it be discarded&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What’s happening here?&lt;/p&gt;

&lt;p&gt;The mint function creates the NFT using create_named_token, which generates a ConstructorRef.&lt;br&gt;
Instead of returning the ConstructorRef, the function keeps it internal and lets it be discarded when the function ends.This ensures no one (not even the creator) can misuse the ConstructorRef to tamper with the NFT later.&lt;/p&gt;

&lt;p&gt;Real-world analogy: &lt;br&gt;
When you sell that car, you give the buyer the title and keys but destroy any duplicate keys immediately. This way, no one can come back and steal the car later.&lt;/p&gt;

&lt;p&gt;Why This Matters in the Real World&lt;br&gt;
ConstructorRef leaks can have serious consequences in blockchain applications. &lt;br&gt;
Here are a few examples:&lt;/p&gt;

&lt;p&gt;NFT Marketplaces: If an NFT’s ConstructorRef is leaked, the original creator could generate a TransferRef and take the NFT back after it’s sold, cheating the buyer. For example, someone buys a $10,000 digital artwork, only to have it stolen back by the creator.&lt;/p&gt;

&lt;p&gt;Decentralized Finance (DeFi): In a DeFi app, an Object might represent a user’s stake in a pool. &lt;br&gt;
A leaked ConstructorRef could let someone add fake resources or transfer the stake to themselves, draining funds.&lt;/p&gt;

&lt;p&gt;Gaming: In a blockchain game, Objects might represent rare items or characters. A leak could allow someone to duplicate or steal those items, ruining the game’s fairness.In all these cases, a ConstructorRef leak undermines trust in the system. Users expect blockchain apps to be secure and fair, and leaks can lead to financial losses or a damaged reputation.&lt;/p&gt;

&lt;p&gt;How Move and Aptos Help (But You Still Need to Be Careful)&lt;/p&gt;

&lt;p&gt;The Move programming language (used by Aptos) is designed with security in mind. &lt;/p&gt;

&lt;p&gt;Objects and their capabilities (like ConstructorRef and TransferRef) are tightly controlled to prevent common mistakes. &lt;br&gt;
For example:ConstructorRef is only created when you make a new Object, and it’s meant to be used immediately and discarded.&lt;br&gt;
Aptos’s Object system ensures that only authorized users (like the creator with a signer) can create Objects, but developers must still be careful not to expose sensitive capabilities.&lt;br&gt;
However, Move can’t stop you from writing code that accidentally shares a ConstructorRef. That’s why secure coding practices, like the example above, are crucial.&lt;/p&gt;

&lt;p&gt;Real-world analogy: Think of Move as a bank vault with a strong lock. It’s secure, but if you leave the vault door open (by returning a ConstructorRef), anyone can walk in and take what’s inside. It’s up to you to close the door&lt;/p&gt;

</description>
      <category>aptos</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Aptos Move Tip #7: Understanding Arithmetic Operations and Division Precision in Move.</title>
      <dc:creator>Maakai123</dc:creator>
      <pubDate>Sun, 01 Jun 2025 12:49:04 +0000</pubDate>
      <link>https://dev.to/maakai123/aptos-move-tip-7-understanding-arithmetic-operations-and-division-precision-in-move-1e25</link>
      <guid>https://dev.to/maakai123/aptos-move-tip-7-understanding-arithmetic-operations-and-division-precision-in-move-1e25</guid>
      <description>&lt;p&gt;What Are Arithmetic Operations?&lt;/p&gt;

&lt;p&gt;Arithmetic operations are the basic math calculations you learned in school: addition (+), subtraction (-), multiplication (*), division (/), and sometimes bit-shifting (like moving numbers around in binary). In programming, these operations are used to calculate things like fees, balances, or rewards in apps, including blockchain applications.In the Move programming language (designed for secure blockchain smart contracts, these operations are critical because they often deal with money or valuable assets. If the math isn’t handled carefully, small mistakes can lead to big problems, like losing money or giving someone an unfair advantage.&lt;/p&gt;

&lt;p&gt;What Is Division Precision, and Why Does It Matter?&lt;/p&gt;

&lt;p&gt;When you divide numbers, sometimes the result isn’t a whole number. For example, 7 ÷ 2 = 3.5. In many programming languages, including Move, division between integers (whole numbers like 1, 2, or 100) doesn’t keep the decimal part—it rounds down to the nearest whole number. So, 7 ÷ 2 in Move would give you 3, not 3.5. This is called truncation.&lt;/p&gt;

&lt;p&gt;While this might sound harmless, it can cause issues in blockchain applications where precision is critical. For example, if you’re calculating fees for a transaction, rounding down might accidentally make the fee zero, letting someone use a service for free! This loss of precision is called a rounding error, and it can lead to:&lt;/p&gt;

&lt;p&gt;Financial imbalances: Someone might pay less (or nothing) when they should owe money.&lt;/p&gt;

&lt;p&gt;Data inaccuracies: Incorrect calculations can mess up records or reports.&lt;/p&gt;

&lt;p&gt;Flawed decisions: Systems relying on these calculations might make wrong choices, like approving invalid transactions.&lt;br&gt;
Safety risks: In extreme cases (like in medical or automotive systems), errors could cause serious harm.In blockchain, where trust and accuracy are everything, these errors can erode user confidence or even allow bad actors to exploit the system.&lt;/p&gt;

&lt;p&gt;Arithmetic in Move:&lt;br&gt;
The Basics Move supports six types of unsigned integers (numbers that are always zero or positive, like 0, 1, 10, or 100). &lt;br&gt;
These are:&lt;/p&gt;

&lt;p&gt;u8: Can store numbers from 0 to 255.&lt;br&gt;
u16: Up to 65,535&lt;br&gt;
u32: Up to about 4.3 billion&lt;br&gt;
u64: Up to about 18 quintillion&lt;br&gt;
u128: Even bigger numbers&lt;br&gt;
u256: For extremely large numbers.&lt;/p&gt;

&lt;p&gt;When you do math with these numbers in Move, the language has built-in safety features to prevent common errors:&lt;/p&gt;

&lt;p&gt;Addition (+) and Multiplication (*): If the result is too big for the integer type (e.g., adding two huge numbers), the program aborts (stops running) to prevent incorrect results.&lt;/p&gt;

&lt;p&gt;Subtraction (-): If the result would be negative (e.g., 5 - 10), the program aborts because unsigned integers can’t be negative.&lt;/p&gt;

&lt;p&gt;Division (/): If you try to divide by zero, the program aborts.&lt;/p&gt;

&lt;p&gt;Left Shift (&amp;lt;&amp;lt;): This operation shifts bits in a number (like multiplying by powers of 2). Unlike the others, it doesn’t abort if the result is too big—it just produces an incorrect value, which can be dangerous if not handled properly.&lt;br&gt;
These safety features are great, but they don’t solve the problem of division precision. Let’s dive into a real-world example to see why this matters.&lt;/p&gt;

&lt;p&gt;Real-World Example: Calculating Fees in a Blockchain Protocol&lt;/p&gt;

&lt;p&gt;Imagine you’re building a decentralized marketplace on a blockchain where users pay a small fee (say, 1%) for every transaction. The fee is calculated using a formula like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Fee = Transaction Size * Fee Percentage / 100

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Move, this might be written as code that uses integers. Let’s say the fee percentage is represented as 100 “basis points” (where 1% = 100 basis points, or PROTOCOL_FEE_BPS = 100).&lt;br&gt;
 Here’s what the code might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;
&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="mi"&gt;0x42&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;PROTOCOL_FEE_BPS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 1% fee&lt;/span&gt;
  &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;calculate_protocol_fees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;PROTOCOL_FEE_BPS&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 100 basis points = 1%&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What’s the problem? If the size (transaction amount) is small, the division can round down to zero. For example:If size = 50 tokens, the calculation is:50 * 100 / 10000 = 5000 / 10000 = 0.5In Move, this rounds down to 0.&lt;/p&gt;

&lt;p&gt;This means a user could make a small transaction and pay no fee at all, which could let them exploit the system by making lots of tiny transactions for free. In a real marketplace, this could lead to thousands of dollars in lost revenue or even crash the system if someone spams it with tiny transactions.&lt;/p&gt;

&lt;p&gt;How to Fix It: Secure Coding Practices&lt;/p&gt;

&lt;p&gt;To prevent this issue, you need to ensure the fee calculation doesn’t round down to zero.&lt;br&gt;
Here are two ways to fix the code, &lt;/p&gt;

&lt;p&gt;explained simply:&lt;/p&gt;

&lt;p&gt;Fix 1: Set a Minimum Transaction Size You can require that transactions be large enough so the fee never rounds down to zero. &lt;br&gt;
For example, if the fee is 1% (100 basis points), the minimum transaction size should be at least 10000 / 100 + 1 = 101 to ensure the fee is at least 1.&lt;br&gt;
Here’s the secure code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="mi"&gt;0x42&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;PROTOCOL_FEE_BPS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 1% fee&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;MIN_ORDER_SIZE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;PROTOCOL_FEE_BPS&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 101&lt;/span&gt;
  &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;calculate_protocol_fees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;assert!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;MIN_ORDER_SIZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Check size is big enough&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;PROTOCOL_FEE_BPS&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What’s happening here?&lt;/p&gt;

&lt;p&gt;The MIN_ORDER_SIZE is calculated as 10000 / 100 + 1 = 101.&lt;br&gt;
The assert! line checks that the transaction size is at least 101. If it’s smaller, the program aborts (stops) with an error.This ensures the fee is always at least 1, preventing free transactions.&lt;/p&gt;

&lt;p&gt;Real-world analogy:&lt;/p&gt;

&lt;p&gt;Imagine a coffee shop that charges a 1% service fee but requires you to spend at least $10. If you only buy a $1 coffee, they won’t let you pay because the fee would be too small (like 1 cent, which might round to zero in their system). This rule ensures they always collect something.&lt;/p&gt;

&lt;p&gt;Fix 2: Check for Non-Zero Fees&lt;/p&gt;

&lt;p&gt;Another approach is to calculate the fee and then check if it’s zero. If it is, you can either reject the transaction or set a minimum fee (e.g., 1 token).&lt;br&gt;
Here’s the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="mi"&gt;0x42&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;PROTOCOL_FEE_BPS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 1% fee&lt;/span&gt;
  &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;calculate_protocol_fees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;fee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;PROTOCOL_FEE_BPS&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;assert!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fee&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Ensure fee isn’t zero&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fee&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What’s happening here?&lt;/p&gt;

&lt;p&gt;The code calculates the fee as before.&lt;br&gt;
The assert!(fee &amp;gt; 0, 0) line checks if the fee is zero.&lt;br&gt;
 If it is, the program aborts.&lt;br&gt;
This prevents users from making transactions that result in no fee.&lt;/p&gt;

&lt;p&gt;Real-world analogy: &lt;/p&gt;

&lt;p&gt;Think of a toll booth on a highway. If your toll calculates to zero (maybe because your car is super small), the booth operator says, “Sorry, you can’t pass unless you pay at least something.” This ensures the toll system always collects a fee.&lt;/p&gt;

&lt;p&gt;Why Move’s Safety Features Matters&lt;/p&gt;

&lt;p&gt;Move’s strict rules about integer operations (like aborting on overflow or division by zero) are designed to make smart contracts safer. For example:&lt;br&gt;
If you try to add two huge numbers and the result is too big for a u64, the program stops instead of giving a wrong answer.If you try to subtract a larger number from a smaller one (e.g., 5 - 10), the program stops because negative numbers aren’t allowed in unsigned integers.&lt;br&gt;
If you try to divide by zero, the program stops to avoid crashing the system.However, the left shift (&amp;lt;&amp;lt;) operation is an exception—it doesn’t abort if the result is too big, which can lead to incorrect calculations. For example, shifting a number too far could “lose” bits, producing a wrong value. Developers need to be extra careful with this operation.&lt;/p&gt;

&lt;p&gt;Why This Matters in the Real WorldLet’s &lt;/p&gt;

&lt;p&gt;look at a few real-world scenarios where these issues could cause problems.&lt;/p&gt;

&lt;p&gt;Decentralized Finance (DeFi)&lt;br&gt;
 In a DeFi lending platform, fees are charged on loans. If division rounding causes fees to be zero for small loans, users could borrow tiny amounts repeatedly without paying anything, draining the platform’s revenue.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Aptos Move Tip #6 – Move Abilities</title>
      <dc:creator>Maakai123</dc:creator>
      <pubDate>Fri, 30 May 2025 22:16:49 +0000</pubDate>
      <link>https://dev.to/maakai123/aptos-move-tip-6-move-abilities-3nio</link>
      <guid>https://dev.to/maakai123/aptos-move-tip-6-move-abilities-3nio</guid>
      <description>&lt;p&gt;In Aptos Move, abilities are like permission tags for data structures, controlling what can be done with them (e.g., copying, discarding, or storing). Think of them as security settings on a file—assign the wrong permissions, and you could expose sensitive data or break your smart contract. This tip explains Move’s abilities, their risks, and how to use them securely to build robust dApps on Aptos.&lt;/p&gt;

&lt;p&gt;Why It Matters&lt;/p&gt;

&lt;p&gt;Move’s abilities (copy, drop, store, key) define how data structures behave. Misusing them can lead to serious vulnerabilities:Unauthorized copying of sensitive data (e.g., duplicating tokens).&lt;br&gt;
Resource leaks by not allowing data to be discarded.&lt;br&gt;
Storage mishandling, causing data to be inaccessible or misused.By assigning abilities carefully, you ensure your smart contract is secure, efficient, and behaves as intended.&lt;/p&gt;

&lt;p&gt;Real-World Example&lt;/p&gt;

&lt;p&gt;Imagine a dApp for digital concert tickets on Aptos. Each ticket is a unique Token struct. &lt;br&gt;
If you allow tickets to be copied freely, someone could duplicate their ticket and sell it multiple times, like photocopying a concert pass! Similarly, if a flash loan system lets borrowers “throw away” their debt without repaying, the system collapses. Proper ability management is like setting strict rules for ticket use—only the right actions are allowed.&lt;/p&gt;

&lt;p&gt;Move Abilities Explained&lt;/p&gt;

&lt;p&gt;Here’s what each ability does and why it matters:&lt;br&gt;
Copy: Allows duplicating a value. Useful for simple data like numbers, but dangerous for assets like tokens or NFTs, as it could lead to double-spending.&lt;br&gt;
Drop: Permits discarding a value from memory. Necessary for cleanup, but risky for assets that must persist (e.g., loans).&lt;/p&gt;

&lt;p&gt;Store: Enables saving data in global storage (on-chain). Critical for persistent data, but must be restricted to prevent unauthorized access.&lt;/p&gt;

&lt;p&gt;Key: Allows data to act as a key in global storage, enabling retrieval and manipulation. Essential for resources tied to accounts.&lt;/p&gt;

&lt;p&gt;Insecure Code Example&lt;/p&gt;

&lt;p&gt;This code assigns abilities incorrectly, creating vulnerabilities:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="mi"&gt;0x42&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Token&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;FlashLoan&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Problems:&lt;br&gt;
Token has copy: The copy ability lets anyone duplicate a Token, potentially creating unlimited tokens (like printing fake money). This could inflate the token supply and crash the dApp’s economy.&lt;/p&gt;

&lt;p&gt;FlashLoan has drop: The drop ability allows borrowers to discard a FlashLoan without repaying it, like tearing up an IOU note and walking away debt-free.&lt;/p&gt;

&lt;p&gt;Secure Code Example&lt;br&gt;
Restrict abilities to only what’s needed for the business logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="mi"&gt;0x42&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Token&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;FlashLoan&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why It’s Better:&lt;/p&gt;

&lt;p&gt;Token: Removes copy to prevent duplication, ensuring tokens are unique. Adds key and store to allow secure storage and retrieval in global storage (e.g., tied to a user’s account).&lt;br&gt;
FlashLoan: Removes drop to prevent discarding unpaid loans, ensuring borrowers must repay. Adds key and store for persistent loan tracking.&lt;/p&gt;

&lt;p&gt;Key Takeaways&lt;br&gt;
Assign abilities sparingly: Only give copy, drop, store, or key when the business logic requires it.&lt;br&gt;
Avoid copy for assets: Prevent duplication of tokens, NFTs, or other sensitive resources.&lt;br&gt;
Restrict drop for obligations: Ensure critical data like loans can’t be discarded without proper handling.Use store and key for persistence: Enable secure storage and retrieval of data in global storage.&lt;br&gt;
Impact: Proper ability management prevents vulnerabilities like double-spending or resource leaks, keeping your dApp secure and reliable&lt;/p&gt;

&lt;p&gt;Pro Tips for Developers&lt;/p&gt;

&lt;p&gt;Review each struct’s purpose and assign only the necessary abilities.&lt;br&gt;
Avoid copy for any resource representing value (e.g., tokens, NFTs).&lt;br&gt;
Use drop only for temporary or non-critical data, not obligations like loans.Test your contract with incorrect ability assignments to catch potential exploits.&lt;/p&gt;

</description>
      <category>aptos</category>
      <category>web3</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Aptos Move Tip #5 – Resource Management and Unbounded Execution</title>
      <dc:creator>Maakai123</dc:creator>
      <pubDate>Thu, 29 May 2025 22:52:43 +0000</pubDate>
      <link>https://dev.to/maakai123/aptos-move-tip-5-resource-management-and-unbounded-execution-19kp</link>
      <guid>https://dev.to/maakai123/aptos-move-tip-5-resource-management-and-unbounded-execution-19kp</guid>
      <description>&lt;p&gt;Introduction&lt;/p&gt;

&lt;p&gt;When building smart contracts on Aptos using Move, security and efficiency are paramount. One critical aspect is resource management and preventing unbounded execution.&lt;br&gt;
Poorly designed contracts can allow attackers to clog the system, exhaust gas (the computational cost of transactions), and block functionality. &lt;br&gt;
This tip explains how to manage resources securely and avoid loops that could spiral out of control, with practical examples to make it crystal clear.&lt;/p&gt;

&lt;p&gt;Why It Matters&lt;/p&gt;

&lt;p&gt;In Move, unbounded execution happens when a function loops over a data structure (like a list) that can grow infinitely, consuming excessive gas and potentially causing transactions to fail. Similarly, storing all user data in a single global structure can make your contract vulnerable to attacks and inefficient. &lt;br&gt;
It’s like running a store where every customer’s order is piled into one massive, disorganized box—anyone could overwhelm it with junk orders, slowing everything down or crashing the system!&lt;br&gt;
To  manage resources effectively store user-specific data (like coins or NFTs) in individual user accounts, not a shared global space.Avoid iterating over unbounded structures that anyone can add to without limits.&lt;br&gt;
Use efficient data structures (like SmartTable) to keep operations fast and secure.This ensures your dApp stays scalable, secure, and gas-efficient.&lt;/p&gt;

&lt;p&gt;Real-World Example&lt;/p&gt;

&lt;p&gt;Imagine running an online marketplace on Aptos, like eBay, where users place buy orders for items. All orders are stored in one global list accessible to everyone. An attacker could flood the list with thousands of fake orders, making it slow or impossible to search for a specific order because the system has to check every single one. This could crash the transaction or make the app unusable due to high gas costs.The solution? Store each user’s orders in their own account, like giving every customer their own order folder. This isolates data, prevents tampering, and keeps operations fast by avoiding huge loops.&lt;/p&gt;

&lt;p&gt;Insecure Code Example&lt;/p&gt;

&lt;p&gt;Here’s a problematic code snippet where orders are stored in a single global OrderStore. The get_order_by_id function loops through every order to find a match, which can become slow and costly if the list grows large due to unrestricted additions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="mi"&gt;0x42&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="cm"&gt;/* other fields */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;OrderStore&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;get_order_by_id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;acquires&lt;/span&gt; &lt;span class="n"&gt;OrderStore&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;order_store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;borrow_global_mut&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;OrderStore&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;order_store&lt;/span&gt;&lt;span class="py"&gt;.orders&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;borrow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;order_store&lt;/span&gt;&lt;span class="py"&gt;.orders&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="py"&gt;.id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nn"&gt;option&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nn"&gt;option&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;none&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;create_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buyer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... adds to global order_store */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why It’s Bad:&lt;br&gt;
The orders vector is global and publicly accessible, so anyone can add unlimited orders via create_order.The while loop in get_order_by_id checks every order, making it an O(n) operation (slow for large lists).An attacker could spam the list with fake orders, causing high gas costs or transaction failures, blocking legitimate users.It’s like searching through a giant pile of orders at the post office—slow, costly, and easy to sabotage.&lt;/p&gt;

&lt;p&gt;Secure Code Example&lt;/p&gt;

&lt;p&gt;Instead, store orders in each user’s account using a SmartTable for efficient lookups. This isolates user data and eliminates unbounded loops, ensuring fast and secure operations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="mi"&gt;0x42&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;drop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="cm"&gt;/* other fields */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;OrderStore&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;SmartTable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;get_order_by_id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;acquires&lt;/span&gt; &lt;span class="n"&gt;OrderStore&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;order_store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;borrow_global_mut&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;OrderStore&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;address_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;smart_table&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;order_store&lt;/span&gt;&lt;span class="py"&gt;.orders&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;smart_table&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;borrow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;order_store&lt;/span&gt;&lt;span class="py"&gt;.orders&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nn"&gt;option&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nn"&gt;option&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;none&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why It’s Better:Orders are stored in a SmartTable under the user’s account (signer::address_of(user)), isolating data to prevent tampering.SmartTable enables O(1) lookups (constant time), avoiding costly loops.Only the user can access their own orders, enhancing security and scalability.It’s like giving each customer a personal, indexed filing cabinet—fast, secure, and tamper-proof.Key Takeaways&lt;br&gt;
Avoid unbounded loops: Don’t iterate over structures (like vectors) that anyone can grow infinitely.Isolate user data: Store assets like orders, coins, or NFTs in individual user accounts, not a shared global space.&lt;/p&gt;

&lt;p&gt;Pro Tips for Developers&lt;/p&gt;

&lt;p&gt;Always check if a data structure can grow uncontrollably before looping over it.Use signer::address_of(user) to tie resources to specific accounts.Opt for SmartTable or similar structures for key-value lookups instead of vectors.Test your contract with large datasets to ensure gas efficiency and scalability.By designing with these principles, you’ll build secure, efficient, and scalable smart contracts on Aptos.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Secure Generics Type Checking APTOS MOVE</title>
      <dc:creator>Maakai123</dc:creator>
      <pubDate>Wed, 28 May 2025 17:38:42 +0000</pubDate>
      <link>https://dev.to/maakai123/secure-generics-type-checking-aptos-move-4i6l</link>
      <guid>https://dev.to/maakai123/secure-generics-type-checking-aptos-move-4i6l</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6or0f9ncq1z2z9yfw102.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6or0f9ncq1z2z9yfw102.jpg" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Generics in Move allow functions and structs to work with different data types, like a universal key.  But if you don’t verify the type, attackers could sneak in the wrong key, leading to unauthorized actions or transaction failures. &lt;/p&gt;

&lt;p&gt;Real-World Example&lt;/p&gt;

&lt;p&gt;Picture a dApp offering flash loans on Aptos. Users borrow coins (e.g., USD tokens) and must repay with a fee. Without checking the coin type, someone could borrow USD but repay with worthless “FakeCoin,” cheating the system! 😈Let’s see how this happens and how to fix it.&lt;/p&gt;

&lt;p&gt;Insecure Code &lt;/p&gt;

&lt;p&gt;This flash loan code doesn’t ensure the repaid Coin matches the borrowed type, allowing users to repay with any coin type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="mi"&gt;0x42&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Coin&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Receipt&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;flash_loan&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Coin&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Receipt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fee&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Receipt&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fee&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;repay_flash_loan&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Receipt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Coin&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;Receipt&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;assert!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Problem: The Receipt struct isn’t tied to the coin type T, so users can repay a USD loan with any Coin, like a fake token. It’s like accepting a random gift card to settle a bank loan! &lt;/p&gt;

&lt;p&gt;Secure Code ✅&lt;/p&gt;

&lt;p&gt;Use Move’s type system (e.g., phantom types) to ensure the Receipt and Coin types match, preventing type mismatches.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="mi"&gt;0x42&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Coin&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Receipt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;phantom&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;flash_loan&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Coin&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Receipt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fee&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Receipt&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fee&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;repay_flash_loan&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Receipt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Coin&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;Receipt&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;assert!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fix: Adding phantom T to Receipt ensures the repaid Coin matches the borrowed type. It’s like requiring the exact currency for loan repayment—USD in, USD out! &lt;/p&gt;

</description>
      <category>web3</category>
      <category>aptos</category>
      <category>move</category>
    </item>
    <item>
      <title>What does returns (uint256) in Solidity means?</title>
      <dc:creator>Maakai123</dc:creator>
      <pubDate>Wed, 27 Nov 2024 08:33:04 +0000</pubDate>
      <link>https://dev.to/maakai123/what-does-returns-uint256-in-solidity-means-5fdh</link>
      <guid>https://dev.to/maakai123/what-does-returns-uint256-in-solidity-means-5fdh</guid>
      <description>&lt;p&gt;Have you ever seen something like this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getBorrowingCapacity(address user) external view returns (uint256) {
    // Example calculation: Borrowing capacity is 75% of collateral
    return (collateral[user] * borrowFactor) / 100;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in this function we have external view returns(uint256)&lt;br&gt;
What is (unit256) doing?&lt;/p&gt;

&lt;p&gt;When working with Solidity, the Ethereum blockchain's most widely used smart contract programming language, you’ll often encounter function declarations that specify a returns keyword followed by a data type, such as uint256. If you’ve ever wondered why this is necessary or how it works, this guide will break it all down for you.&lt;/p&gt;



&lt;p&gt;What Does returns (uint256) Mean?&lt;/p&gt;

&lt;p&gt;The returns (uint256) in a Solidity function declaration defines the type of value the function will produce and send back to the caller after it is executed. This lets the caller know exactly what type of data to expect in response.&lt;/p&gt;

&lt;p&gt;Here’s an example of a function declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getBorrowingCapacity(address user) external view returns (uint256).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function, getBorrowingCapacity, is designed to return a single unsigned integer value (a uint256). This return value might represent something like the borrowing capacity of the specified user.&lt;/p&gt;




&lt;p&gt;Why Is uint256 Used?&lt;/p&gt;

&lt;p&gt;In Solidity, uint256 is a widely used data type. Here's why it’s appropriate for functions like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Non-Negative Values: Since uint256 represents unsigned integers, it can only hold non-negative values. This is ideal for cases like borrowing capacity, balances, and other numbers that cannot logically be negative.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Large Range: The uint256 type can store extremely large numbers, from 0 to . This ensures that even the largest possible borrowing capacities can be accurately stored and returned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Standard Practice: Solidity defaults to uint256 for most numerical computations because it is safer and more scalable.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;How returns (uint256) Works in Practice&lt;/p&gt;

&lt;p&gt;Let’s explore a practical example. Imagine you are building a decentralized lending platform where users can borrow tokens based on the value of their deposited collateral. The getBorrowingCapacity function could calculate the maximum amount a user is allowed to borrow.&lt;/p&gt;

&lt;p&gt;Here’s how such a function might be implemented:&lt;/p&gt;

&lt;p&gt;// Mapping to store each user's collateral balance in tokens&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mapping(address =&amp;gt; uint256) public collateral;



// Borrowing factor: users can borrow up to 75% of their collateral value
uint256 public borrowFactor = 75;

function getBorrowingCapacity(address user) external view returns (uint256) {
    // Example calculation: Borrowing capacity is 75% of collateral
    return (collateral[user] * borrowFactor) / 100;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Example Scenario&lt;/p&gt;

&lt;p&gt;Let’s say Alice deposits 1000 tokens as collateral, and the borrowFactor is set to 75%. When the function is called with Alice's address, it calculates her borrowing capacity as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Collateral: 1000 tokens.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Borrowing Factor: 75%.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Calculation: .&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you call the function like this:&lt;/p&gt;

&lt;p&gt;contractInstance.getBorrowingCapacity(alice);&lt;/p&gt;

&lt;p&gt;The function will return:&lt;/p&gt;

&lt;p&gt;750&lt;/p&gt;




&lt;p&gt;Why Use returns in Solidity?&lt;/p&gt;

&lt;p&gt;In Solidity, the returns keyword is essential for creating readable and reusable functions. Here are the main reasons to use it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Data Communication: Functions often need to communicate results (like computed values) back to the caller. Using returns explicitly defines the type of value the function will provide.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Clear Expectations: The return type ensures that developers or external contracts interacting with the function know exactly what type of data they can expect. This reduces ambiguity and makes debugging easier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Chaining and Reuse: Returned values can be stored, reused, or even passed as inputs to other functions. For example:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;uint256 capacity = contractInstance.getBorrowingCapacity(alice);&lt;br&gt;
if (capacity &amp;gt; 500) {&lt;br&gt;
    // Proceed with borrowing&lt;br&gt;
}&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Efficiency: Returning data directly from a function eliminates the need for extra state variables, making the function more efficient and less prone to bugs.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Real-World Use Cases for returns (uint256)&lt;/p&gt;

&lt;p&gt;Here are some common scenarios where uint256 is returned in Solidity:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Token Balances: A function might return the balance of tokens a user holds:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;function balanceOf(address user) external view returns (uint256);&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Borrowing Capacity: As in our example, it might calculate how much a user can borrow:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;function getBorrowingCapacity(address user) external view returns (uint256);&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Interest Rates: It might compute and return interest rates for loans:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;function getInterestRate() external view returns (uint256);&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Auction Bids: It could return the highest bid in an auction:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;function getHighestBid() external view returns (uint256);&lt;/p&gt;




&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;The returns (uint256) in Solidity serves as a powerful tool for defining the outputs of a function. It communicates expectations, ensures clarity, and makes functions reusable and efficient. Whether you’re working with balances, borrowing capacities, or auction bids, understanding how to use return types effectively is key to writing robust smart contracts.&lt;/p&gt;

</description>
      <category>web3</category>
      <category>blockchain</category>
      <category>solidity</category>
      <category>smartcontract</category>
    </item>
    <item>
      <title>WHAT IS EQUALITY IN JAVASCRIPT?</title>
      <dc:creator>Maakai123</dc:creator>
      <pubDate>Sun, 30 Jul 2023 12:01:36 +0000</pubDate>
      <link>https://dev.to/maakai123/what-is-equality-and-boolean-logic-in-javascript-4o8c</link>
      <guid>https://dev.to/maakai123/what-is-equality-and-boolean-logic-in-javascript-4o8c</guid>
      <description>&lt;p&gt;*In this short article, you will understand&lt;br&gt;
1) How to use Equality in JavaScript&lt;br&gt;
2)Understand the basics of Logic operators *&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What is Equality and how to use them in JavaScript?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In JavaScript, equality refers to comparing two values to see if they are the same.(==,===)&lt;br&gt;
Do not be confused, when you see &lt;code&gt;const toDay = saturday&lt;/code&gt;, in JavaScript this means declaration of variable or assignment and not equality.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;JavaScript has two ways of comparing two values&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;1)Loose Equality Operator &lt;code&gt;==&lt;/code&gt;&lt;br&gt;
The Loose equality operators does type coercion, which means converting a string to a number before comparing them.&lt;br&gt;
&lt;code&gt;'18' == 18, ouput: true&lt;/code&gt;&lt;br&gt;
&lt;code&gt;the above means  string == number is true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2) Strict Equality (===):&lt;br&gt;
The best way to compare values in JavaScript.&lt;br&gt;
The strict equality operator compares both the values and their types without type coercion (meaning without converting a string to a number).&lt;br&gt;
It returns true only when both sides are exactly true.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;example '18' === 18 = false&lt;br&gt;
18=== 18 = true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Examples &lt;/p&gt;

&lt;p&gt;&lt;code&gt;const age = '20'&lt;br&gt;
a) if(age === 20) console.log(You can drive)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;b) if(age == 20) console.log(you can drive)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;From the above two if statements (a) is strict so the code will not be executed.&lt;/p&gt;

&lt;p&gt;If statement (b) Is Loose, and it supports type coercion which means string '20' is equal to number 20. The &lt;code&gt;you can drive&lt;/code&gt; will be executed.&lt;/p&gt;

&lt;p&gt;Example 2&lt;br&gt;
a)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const foodNumber = '25'&lt;br&gt;
if(foodNumber == 25) console.log('Number 25 is Rice')&lt;/code&gt;&lt;br&gt;
&lt;code&gt;output:Number 25 is Rice&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above code will be executed because &lt;code&gt;==&lt;/code&gt; converts foodNumber which is a string to a number 25 and immediately compare them.&lt;/p&gt;

&lt;p&gt;b)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const foodNumber = 25&lt;br&gt;
if(foodNumber == 25) console.log('Number 25 is Rice')&lt;/code&gt;&lt;br&gt;
&lt;code&gt;output:Number 25 is Rice&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above code will also be executed because 25 (number) is equal to 25(number).&lt;/p&gt;

&lt;p&gt;Example 3&lt;br&gt;
a)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const foodNumber = '25'&lt;br&gt;
if(foodNumber === 25) console.log('Number 25 is Rice')&lt;/code&gt;&lt;br&gt;
&lt;code&gt;This code will not be executed&lt;/code&gt;&lt;br&gt;
Because foodNumber is a string and === don't support type coercion.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>HOW JavaScript WORKS BEHIND THE SCENES SIMPLE EXPLANATION.</title>
      <dc:creator>Maakai123</dc:creator>
      <pubDate>Sun, 16 Jul 2023 08:50:30 +0000</pubDate>
      <link>https://dev.to/maakai123/how-javascript-works-behind-the-scenes-simple-explanation-4pke</link>
      <guid>https://dev.to/maakai123/how-javascript-works-behind-the-scenes-simple-explanation-4pke</guid>
      <description>&lt;p&gt;Have you ever wondered how JavaScript works behind the scenes?&lt;br&gt;
This will be the simplest explanation ever.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a JavaScript engine or Runtime?&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;JavaScript engine executes JavaScript code.&lt;br&gt;
Example includes V8 (Use in Google Chrome) and other web browsers.&lt;/p&gt;

&lt;p&gt;The JS engine has &lt;br&gt;
1) Call Stack: This is where our code is executed using the execution context&lt;/p&gt;

&lt;p&gt;2)Heap: This stores  Objects&lt;/p&gt;

&lt;p&gt;Javascript codes are compiled or Interpreted or both compiled and interpreted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Compilation and Interpretation?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since every computer processor only understands 0's and 1's, the computer program needs to be converted into a machine code.&lt;/p&gt;

&lt;p&gt;Programs can be converted into machine codes by Compilation or Interpretation&lt;/p&gt;

&lt;p&gt;What is Compilation?&lt;br&gt;
A Compiler converts the entire code/program into a machine(0's and 1's) code at once and writes to a binary file that can be executed by a computer.&lt;/p&gt;

&lt;p&gt;2)Interpretation: &lt;br&gt;
An Interpreter runs through the source code and executes it line by line. This is slow when compared to a compiler.&lt;/p&gt;

&lt;p&gt;Over the years, Javascript has been referred to as an interpreter for reading codes line by line.&lt;br&gt;
Since the introduction of Modern Javascript, Javascript now uses both a compiler and an interpreter called Just in Time compilation.&lt;/p&gt;

&lt;p&gt;Steps Used in JS Engine&lt;/p&gt;

&lt;p&gt;When you write a piece of code inside the Js engine, it passes through the following steps.&lt;/p&gt;

&lt;p&gt;1)Js engine will parse the code and read it.&lt;/p&gt;

&lt;p&gt;2)The code will be parsed into a data structure called the abstract syntax tree(AST)&lt;/p&gt;

&lt;p&gt;3)The AST will split up the codes, check for syntax errors, then generate the machine code (0's and 1's)&lt;/p&gt;

&lt;p&gt;5)The machine code will be compiled and executed.&lt;/p&gt;

&lt;p&gt;Do you enjoy this? Please drop a comment and follow me @maakayjunior&lt;/p&gt;

</description>
    </item>
    <item>
      <title>WHAT IS UNISWAP</title>
      <dc:creator>Maakai123</dc:creator>
      <pubDate>Sun, 26 Feb 2023 18:48:48 +0000</pubDate>
      <link>https://dev.to/maakai123/what-is-uniswap-5bm8</link>
      <guid>https://dev.to/maakai123/what-is-uniswap-5bm8</guid>
      <description>&lt;p&gt;&lt;strong&gt;Uniswap&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Uniswap is a decentralized token exchange protocol built on Ethereum that allows the direct swapping of tokens without the need to use a centralized exchange.&lt;/p&gt;

&lt;p&gt;On Uniswap, you can simply swap your tokens directly from your wallet without depositing tokens to an exchange, place an order on the order book, and then withdraw the swapped tokens as it is in the case of Centralized Exchanges.&lt;/p&gt;

&lt;p&gt;All you need to do is send your tokens from your wallet to Uniswap’s smart contract address, you will receive your desired token in return in your wallet. There is no order book and the token exchange rate is determined algorithmically. All this is achieved via liquidity pools and the automated market maker mechanism.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Liquidity Pools&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Liquidity pools are token reserves that sit on Uniswap’s smart contracts and are available for users to exchange tokens with. For example, using the ETHDAI trading pair with 100 ETH and 20,000 DAI in the liquidity reserves, a user that wants to buy ETH using DAI may send 202.02 DAI to the Uniswap smart contract to get 1 ETH in return. Once the swap has taken place, the liquidity pool is left with 99 ETH and 20,202.02 DAI.&lt;br&gt;
If someone buys ETH using DAI, ETH will be removed from the liquidity pool while DAI will be added into the liquidity pool.&lt;/p&gt;

&lt;p&gt;What is Automated Market Maker Mechanism&lt;/p&gt;

&lt;p&gt;Prices of assets in the pool are algorithmically determined using the Automated Market Maker (AMM) algorithm. AMM works by maintaining a Constant Product based on the amount of liquidity on both sides of the pool.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to get a token added on Uniswap?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Unlike centralized exchanges, Uniswap as a decentralized exchange does not have a team or gatekeepers to evaluate and decide on which tokens to list. Instead, any ERC-20 token can be listed on Uniswap by anyone and be traded as long as liquidity exists for the given pair. All a user needs to do is interact with the platform to register the new token and a new market will be initialized for this token.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Know about Decentralized Exchanges</title>
      <dc:creator>Maakai123</dc:creator>
      <pubDate>Sun, 26 Feb 2023 18:35:21 +0000</pubDate>
      <link>https://dev.to/maakai123/know-about-decentralized-exchanges-1fab</link>
      <guid>https://dev.to/maakai123/know-about-decentralized-exchanges-1fab</guid>
      <description>&lt;p&gt;While Centralized Exchanges (CEXs) have plenty of liquidity and allow large trades to happen, it still carries many risks because users do not hold custody of their assets in exchanges, due to the introduction of Decentralized Finance, Decentralized exchanges were developed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Decentralized Exchange  (Dex)&lt;/strong&gt;: A Dex uses Smart contracts and on-chain transactions to reduce or eliminate the need for an intermediary.&lt;br&gt;
Dex includes, Uniswap, Kyber Network, Curve Finance, dYdX, and SushiSwap.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of DEX&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;There are two types of DEXs: • Order book-based DEXs • Liquidity pool-based DEXs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Order book-based DEXs&lt;/strong&gt;:  These  types  DEXs like dYdX and Deversifi   operate similarly to CEXs where users can set buy and sell orders at either their chosen limit prices or at market prices&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;liquidity pool DEXs:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;liquidity pool DEXs such as Uniswap and Balancer let users become the market makers by providing liquidity to a pair or pool of assets. Users deposit their assets and become liquidity providers, earning a small fee for each swap transaction performed for that particular pool.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Differences between Centralized Exchanges and Decentralized Exchanges&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The main difference between the two is that for CEXs, assets for the trade would be&lt;br&gt;
held on the exchanges’ wallets, whereas for DEXs, assets for the trade would be held in users’ wallets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Limitations of Dex&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lower liquidity&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;The majority of crypto trade still takes place in centralized exchanges. Historically, order books on CEXs have been deeper when compared to DEXs, and traders can thus get better prices when making trades on CEXs.&lt;/p&gt;

&lt;p&gt;Limited features Centralized exchanges include many advanced trading features such as limit orders, stop-loss orders, trailing stops, and so on. Most of these trading features are not available on DEXs.&lt;br&gt;
 Some DEXs now offer limit orders, allowing for a better trading experience. However, a growing number of DEXs are looking to introduce these advanced trading features to compete more effectively against CEXs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Blockchain Interoperability&lt;/strong&gt; Most DEXs today only allow traders to swap tokens within the same blockchain ecosystem. Ethereum-based DEXs, for example, only allow users to trade Ethereum and ERC-20 tokens. It does not permit traders to trade tokens issued on other blockchains like Polkadot or Cosmos. CEXs allows users to trade tokens on various blockchains easily. There are efforts to build cross-chain DEXs, and in the future, trading tokens across multiple blockchains on a DEX will be possible.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>web3</category>
      <category>defi</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>What is DAI in Defi?</title>
      <dc:creator>Maakai123</dc:creator>
      <pubDate>Mon, 13 Feb 2023 19:54:03 +0000</pubDate>
      <link>https://dev.to/maakai123/what-is-dai-in-defi-12if</link>
      <guid>https://dev.to/maakai123/what-is-dai-in-defi-12if</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Dai&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dai is a decentralized stablecoin that is equivalent to 1 dollar, the stablecoin is maintained by Maker, they keep Dail at $1 using a system of collateral and price feeds.&lt;/p&gt;

&lt;p&gt;Due to the high volatility in the crypto market, stablecoins were introduced to help save user funds.&lt;br&gt;
DAI is the world’s first stablecoin that does not require a bank.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How is Dai created?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For a user to create Dai, he/she needs to deposit some Ether in Makers smart contract, it lets users take out loans in Dai.&lt;/p&gt;

&lt;p&gt;A user can create 66 Dai from an ether valued at $100 and a collateralization ratio of 150%.&lt;br&gt;
Each 100 Dai is created by 1.5 ether.&lt;br&gt;
 If the value of ether held as collateral is worth less than the amount of Dai it’s supposed to be backing, then Dai would not be worth one dollar and the system could collapse.&lt;/p&gt;

&lt;p&gt;In a case where the price of ether crashes well below the collateralization ratio, to avoid liquidation, Maker stablecoins come in to salvage the situation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Maker stable coin?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Maker stable coin or maker coin (MKR) is a token on the Ethereum blockchain that has governance rights over Maker smart contracts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does Maker help sustain Dai?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Should the collateral in the system not be enough to cover the amount of Dai in existence, MKR is created and sold onto the open market in order to raise the additional collateral. This provides a strong incentive for MKR holders to responsibly regulate the parameters at which CDPs can create Dai,&lt;/p&gt;

&lt;p&gt;follow me on Twitter &lt;a href="https://twitter.com/cz_binance_HJ4J"&gt;https://twitter.com/cz_binance_HJ4J&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Why stable coins in Defi</title>
      <dc:creator>Maakai123</dc:creator>
      <pubDate>Fri, 10 Feb 2023 13:35:29 +0000</pubDate>
      <link>https://dev.to/maakai123/why-stable-coins-in-defi-2mhh</link>
      <guid>https://dev.to/maakai123/why-stable-coins-in-defi-2mhh</guid>
      <description>&lt;p&gt;Cryptocurrencies are well known for their high volatility, they can change in price easily, in order to protect user funds from this fluctuation in market price, Stablecoins were created.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a Stable Coin?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A stablecoin is a type of cryptocurrency that is pegged to other stable assets like US Dollars.&lt;/p&gt;

&lt;p&gt;Stablecoins maintain this peg through reserves of dollars, other cryptos, or a mix of both kept in U.S-controlled bank accounts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use cases of  Stablecoins.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Apart from being used as a hedge (protection) against crypto market volatility, they are also used for generating passive income through staking or lending.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types  of some popular stablecoins&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1)  Tether (USDT),&lt;br&gt;
2)  USD&lt;br&gt;
3)  USDC&lt;br&gt;
4)  Euro Coin (EUROC)&lt;br&gt;
5)  Binance Dollar(BUSD)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How are Stablecoins maintained?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Stablecoins are maintained by 3 major methods.&lt;/p&gt;

&lt;p&gt;1)  &lt;strong&gt;Fiat or commodity-backed stablecoins.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this method, all stablecoins in circulation are backed by an equivalent of the value in fiat currency or cash equivalents, 1 USD equivalent is held on reserve in U.S bank accounts owned by the issuer, and monthly these reserves are audited.&lt;br&gt;
for example  USDT&lt;/p&gt;

&lt;p&gt;Tether is a centralized, fiat-collateralized stablecoin.&lt;/p&gt;

&lt;p&gt;2) &lt;strong&gt;Cryptocurrency-backed stablecoins.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method is also known as The decentralized method&lt;br&gt;
It involves pegging stablecoins through crypto-collateralization.&lt;br&gt;
Stablecoins are backed by reserves of other cryptocurrencies.&lt;/p&gt;

&lt;p&gt;Due to the fluctuation of the crypto market, stablecoins are usually overcollateralized to help maintain the peg.&lt;br&gt;
for example, Dai which is a decentralized stablecoin is collateralized at 150%, every 1 Dai in circulation is backed by 1.5x its equivalent value in Ethereum.&lt;/p&gt;

&lt;p&gt;3)  &lt;strong&gt;Algorithm-backed statblecoins&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These are stablecoins pegged through smart contracts that automatically execute to manipulate the circulating supply depending on market conditions.&lt;/p&gt;

</description>
      <category>contributorswanted</category>
      <category>announcement</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
