<?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>Forem: Majid Kareem</title>
    <description>The latest articles on Forem by Majid Kareem (@emkay860).</description>
    <link>https://forem.com/emkay860</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%2F783180%2Fbd12ceb4-2084-4189-b1fe-be8b3319f885.jpeg</url>
      <title>Forem: Majid Kareem</title>
      <link>https://forem.com/emkay860</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/emkay860"/>
    <language>en</language>
    <item>
      <title>A Beginner-Friendly Roadmap to Learning AI: From Zero to Expert</title>
      <dc:creator>Majid Kareem</dc:creator>
      <pubDate>Thu, 26 Feb 2026 10:10:21 +0000</pubDate>
      <link>https://forem.com/emkay860/a-beginner-friendly-roadmap-to-learning-ai-from-zero-to-expert-3n5o</link>
      <guid>https://forem.com/emkay860/a-beginner-friendly-roadmap-to-learning-ai-from-zero-to-expert-3n5o</guid>
      <description>&lt;p&gt;Artificial Intelligence can feel overwhelming when you’re just getting started. There are countless courses, tools, and buzzwords: LLMs, agents, MLOps, RAG and it’s not always clear what to learn first or how everything fits together.&lt;/p&gt;

&lt;p&gt;If you’re looking for a &lt;strong&gt;clear, structured learning path&lt;/strong&gt;, Ed Donner’s Udemy courses form a practical roadmap that takes you from &lt;strong&gt;AI beginner to advanced practitioner&lt;/strong&gt;, with a strong focus on real-world applications.&lt;/p&gt;

&lt;p&gt;Below is a &lt;strong&gt;simple, progressive guide&lt;/strong&gt;, starting with non-technical foundations and ending with production-ready AI systems.&lt;/p&gt;

&lt;p&gt;Note: All recommendations are paid courses.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. AI Strategy &amp;amp; Foundations (Beginner / Non-Technical)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;a href="https://www.udemy.com/course/executive-briefing-generative-ai-and-large-language-models-llm/" rel="noopener noreferrer"&gt;Executive Briefing: Generative AI &amp;amp; Large Language Models&lt;/a&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This course is ideal if you’re completely new to AI or coming from a &lt;strong&gt;business, product, or leadership background&lt;/strong&gt;. It explains what Generative AI and LLMs are, why they matter, and how organizations can use them without requiring any coding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You’ll learn:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What Generative AI and LLMs actually do&lt;/li&gt;
&lt;li&gt;Real-world business use cases&lt;/li&gt;
&lt;li&gt;The difference between hype and practical value&lt;/li&gt;
&lt;li&gt;How AI is changing workflows and decision-making&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best for:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Beginners, founders, managers, consultants, and anyone who wants context before diving into code.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Core LLM Engineering (Beginner → Intermediate)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;a href="https://www.udemy.com/course/llm-engineering-master-ai-large-language-models-agents/" rel="noopener noreferrer"&gt;LLM Engineering: Master AI, Large Language Models &amp;amp; Agents&lt;/a&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the &lt;strong&gt;core technical foundation&lt;/strong&gt; of Ed Donner’s AI curriculum. Even if you’re relatively new to programming, the course walks you step-by-step through building real applications with Large Language Models.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You’ll learn:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How LLMs work in practice&lt;/li&gt;
&lt;li&gt;Prompt engineering and structured outputs&lt;/li&gt;
&lt;li&gt;Retrieval-Augmented Generation (RAG)&lt;/li&gt;
&lt;li&gt;Building chatbots and AI assistants&lt;/li&gt;
&lt;li&gt;Using modern AI frameworks and tools&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why this course matters:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
It bridges the gap between &lt;em&gt;“I understand AI conceptually”&lt;/em&gt; and &lt;em&gt;“I can actually build something with it.”&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Agentic AI Systems (Intermediate)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;a href="https://www.udemy.com/course/the-complete-agentic-ai-engineering-course/" rel="noopener noreferrer"&gt;The Complete Agentic AI &amp;amp; MCP Course&lt;/a&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Once you understand LLM fundamentals, the next step is &lt;strong&gt;agentic AI&lt;/strong&gt; systems that can reason, plan, and take actions autonomously.&lt;/p&gt;

&lt;p&gt;This course focuses on building &lt;strong&gt;AI agents&lt;/strong&gt; that go beyond simple chat interfaces and start behaving like intelligent collaborators.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You’ll learn:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What agentic AI is and why it matters&lt;/li&gt;
&lt;li&gt;Designing multi-step reasoning systems&lt;/li&gt;
&lt;li&gt;Tool-calling and action-taking agents&lt;/li&gt;
&lt;li&gt;Coordinating multiple agents together&lt;/li&gt;
&lt;li&gt;Real-world agent use cases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best for:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Developers and builders who want to create more autonomous, powerful AI applications.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Production-Ready AI &amp;amp; MLOps (Advanced / Expert)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;a href="https://www.udemy.com/course/generative-and-agentic-ai-in-production/" rel="noopener noreferrer"&gt;AI Engineering &amp;amp; MLOps: Deploy AI Systems to Production&lt;/a&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the &lt;strong&gt;expert-level stage&lt;/strong&gt; of the journey. Many people can build AI demos, but far fewer know how to deploy, scale, and maintain AI systems in real environments.&lt;/p&gt;

&lt;p&gt;This course focuses on taking AI from prototype to production.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You’ll learn:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deploying AI systems in the cloud&lt;/li&gt;
&lt;li&gt;Scaling LLM applications reliably&lt;/li&gt;
&lt;li&gt;Monitoring, logging, and performance optimization&lt;/li&gt;
&lt;li&gt;MLOps best practices for Generative AI&lt;/li&gt;
&lt;li&gt;Building production-grade AI pipelines&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why this matters:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
If you want to work professionally with AI or deploy AI products for users , this is the missing piece most courses never cover.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Follow This Learning Path
&lt;/h2&gt;

&lt;p&gt;A simple way to approach these courses:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Start with AI strategy&lt;/strong&gt; if you’re new or non-technical
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build strong LLM fundamentals&lt;/strong&gt; with hands-on projects
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Advance into agentic systems&lt;/strong&gt; for more intelligent applications
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Finish with MLOps&lt;/strong&gt; to deploy and scale AI in the real world
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You don’t need to rush, each step builds naturally on the previous one.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Ed Donner’s Udemy courses stand out because they focus on &lt;strong&gt;practical, modern AI&lt;/strong&gt;, not outdated theory. Whether your goal is to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transition into AI engineering
&lt;/li&gt;
&lt;li&gt;Build AI-powered products
&lt;/li&gt;
&lt;li&gt;Understand AI as a business leader
&lt;/li&gt;
&lt;li&gt;Or deploy real systems at scale
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This roadmap gives you a clear, beginner-friendly path from &lt;strong&gt;first concepts to expert-level execution&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to comment and I'll add your recommendations to the list.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>What is EIP2612 and How to Implement EIP2612 in NextJS?</title>
      <dc:creator>Majid Kareem</dc:creator>
      <pubDate>Wed, 09 Oct 2024 09:58:33 +0000</pubDate>
      <link>https://forem.com/emkay860/what-is-eip2612-and-how-to-implement-eip2612-in-nextjs-4f0j</link>
      <guid>https://forem.com/emkay860/what-is-eip2612-and-how-to-implement-eip2612-in-nextjs-4f0j</guid>
      <description>&lt;p&gt;To implement EIP-2612 in a Next.js application, you need to handle both the off-chain signature creation and the on-chain execution of the &lt;code&gt;permit&lt;/code&gt; function followed by the &lt;code&gt;transferFrom&lt;/code&gt;. Below is a detailed step-by-step guide on how to achieve this using &lt;code&gt;ethers.js&lt;/code&gt; in your Next.js app.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 1: Set Up the Next.js Project&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Ensure your Next.js project is set up. If not, you can create one using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx create-next-app my-eip2612-app
&lt;span class="nb"&gt;cd &lt;/span&gt;my-eip2612-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Step 2: Install Required Dependencies&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Install &lt;code&gt;ethers.js&lt;/code&gt; to interact with Ethereum blockchain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;ethers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Step 3: Create a Permit Component&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Create a Next.js component that will handle both the off-chain signing of the &lt;code&gt;permit&lt;/code&gt; and the on-chain execution of &lt;code&gt;permit&lt;/code&gt; and &lt;code&gt;transferFrom&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Component Structure&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Sign the Permit&lt;/strong&gt;: This part is done off-chain by the token owner using their private key via a connected wallet like MetaMask.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execute Permit and Transfer&lt;/strong&gt;: This is done on-chain by the spender, which involves calling the &lt;code&gt;permit&lt;/code&gt; function followed by the &lt;code&gt;transferFrom&lt;/code&gt; function.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;PermitComponent.js&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Create a file named &lt;code&gt;PermitComponent.js&lt;/code&gt; inside your &lt;code&gt;components&lt;/code&gt; folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// components/PermitComponent.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ethers&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ethers&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Replace with the ABI of your ERC20 token with permit functionality&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ERC20_ABI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;function transferFrom(address sender, address recipient, uint256 amount) external returns (bool)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;function nonces(address owner) external view returns (uint256)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;function DOMAIN_SEPARATOR() external view returns (bytes32)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;PermitComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setProvider&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSigner&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setOwner&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;spender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSpender&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setDeadline&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1 hour from now&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tokenAddress&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTokenAddress&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0xYourTokenAddressHere&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Replace with your token address&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Initialize provider and signer when the component mounts&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initializeProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ethereum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;web3Provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ethers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Web3Provider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ethereum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;web3Provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;eth_requestAccounts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt; &lt;span class="c1"&gt;// Request access to user accounts&lt;/span&gt;
        &lt;span class="nf"&gt;setProvider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;web3Provider&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;web3Signer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;web3Provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getSigner&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nf"&gt;setSigner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;web3Signer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;accounts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;web3Provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listAccounts&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nf"&gt;setOwner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accounts&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="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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Please install MetaMask!&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;initializeProvider&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handlePermitAndTransfer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;signer&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;tokenAddress&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Provider, signer, or token address not set&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tokenContract&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ethers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Contract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tokenAddress&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ERC20_ABI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tokenContract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nonces&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;domainSeparator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tokenContract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DOMAIN_SEPARATOR&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

      &lt;span class="c1"&gt;// EIP712 Domain and Permit Type Definitions&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;domain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;My Token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Replace with your token's name&lt;/span&gt;
        &lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;chainId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getNetwork&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nx"&gt;chainId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;verifyingContract&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;tokenAddress&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;types&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;Permit&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;owner&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;address&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;spender&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;address&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;uint256&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nonce&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;uint256&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;deadline&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;uint256&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;spender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ethers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parseUnits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="na"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toNumber&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="nx"&gt;deadline&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;

      &lt;span class="c1"&gt;// Sign the typed data&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;signature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;signer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_signTypedData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;domain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;types&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ethers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splitSignature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;signature&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="c1"&gt;// 1. Call permit function with the signed data&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;permitTx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tokenContract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;permit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;spender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;permitTx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Wait for the permit transaction to be mined&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Permit transaction confirmed:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;permitTx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="c1"&gt;// 2. Now call transferFrom to move the tokens&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;transferTx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tokenContract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transferFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;spender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;transferTx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Wait for the transfer transaction to be mined&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Transfer transaction confirmed:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;transferTx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tokens successfully transferred!&lt;/span&gt;&lt;span class="dl"&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;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error executing permit and transfer:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;EIP&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2612&lt;/span&gt; &lt;span class="nx"&gt;Permit&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;Transfer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
        &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Spender Address&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;spender&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setSpender&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
      &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
        &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Amount&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
      &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handlePermitAndTransfer&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Execute&lt;/span&gt; &lt;span class="nx"&gt;Permit&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;Transfer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;h3&gt;
  
  
  &lt;strong&gt;Step 4: Use the Permit Component in Your Application&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You can now import and use the &lt;code&gt;PermitComponent&lt;/code&gt; in your Next.js pages.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Example Usage in a Next.js Page&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Create a file named &lt;code&gt;index.js&lt;/code&gt; inside your &lt;code&gt;pages&lt;/code&gt; folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pages/index.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;PermitComponent&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../components/PermitComponent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Home&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="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt; &lt;span class="nx"&gt;EIP&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2612&lt;/span&gt; &lt;span class="nx"&gt;Implementation&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PermitComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;h3&gt;
  
  
  &lt;strong&gt;Step 5: Run the Application&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Run your Next.js application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Explanation of Key Steps&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Provider and Signer Initialization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Web3 provider and signer are set up to interact with the blockchain using MetaMask.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Signature Creation (Off-Chain)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;signer._signTypedData()&lt;/code&gt; method creates an EIP-712 compliant signature without spending gas, allowing the token owner to authorize the transfer off-chain.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Execution of &lt;code&gt;permit&lt;/code&gt; and &lt;code&gt;transferFrom&lt;/code&gt; (On-Chain)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;permit&lt;/code&gt; transaction is submitted to the blockchain, granting approval.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;transferFrom&lt;/code&gt; function is called immediately after, using the permit to transfer the tokens.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Considerations&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Gas Costs&lt;/strong&gt;: The spender (or anyone calling &lt;code&gt;permit&lt;/code&gt;) will pay gas for the on-chain transactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling&lt;/strong&gt;: Ensure proper error handling to manage failed transactions or signature errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt;: Ensure the signer correctly signs the &lt;code&gt;permit&lt;/code&gt; and that domain data is accurate to avoid replay attacks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This setup provides a practical way to leverage EIP-2612 in a Next.js application, enhancing user experience by reducing redundant on-chain transactions.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
      <category>web3</category>
      <category>solidity</category>
    </item>
    <item>
      <title>10 Youtube channels to enhance your skills as a frontend developer</title>
      <dc:creator>Majid Kareem</dc:creator>
      <pubDate>Mon, 11 Dec 2023 06:08:44 +0000</pubDate>
      <link>https://forem.com/emkay860/10-youtube-channels-to-enhance-your-skills-as-a-frontend-developer-546i</link>
      <guid>https://forem.com/emkay860/10-youtube-channels-to-enhance-your-skills-as-a-frontend-developer-546i</guid>
      <description>&lt;p&gt;If you are a frontend developer or aspiring to become one, you might be looking for some resources to improve your skills and learn new techniques. YouTube is a great platform to find tutorials, tips, and inspiration from experts and peers in the field. Here are 10 YouTube channels that can help you enhance your frontend development skills.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Traversy Media: This channel offers a variety of web development and programming tutorials, ranging from beginner to advanced levels. You can learn about HTML, CSS, JavaScript, React, Angular, Vue, Bootstrap, and more. The videos are clear, concise, and engaging.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dev Ed: Dev Ed is a fun and creative channel that teaches web development and design with humor and personality. You can learn how to create websites, apps, games, animations, and more using HTML, CSS, JavaScript, React, Node.js, etc. The channel also features some challenges and reviews of other developers' projects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Net Ninja: The Net Ninja is a channel that provides high-quality tutorials on web development and other related topics. You can learn about HTML, CSS, JavaScript, React, Vue, Firebase, MongoDB, and more. The channel also has some playlists on web design, UI/UX, and accessibility.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Web Dev Simplified: Web Dev Simplified is a channel that aims to simplify web development concepts and make them easy to understand. You can learn about HTML, CSS, JavaScript, React, TypeScript, algorithms, data structures, and more. The channel also has some videos on web development best practices and common mistakes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DesignCourse: DesignCourse is a channel that focuses on web design and UI/UX. You can learn how to design beautiful and user-friendly websites and apps using tools like Adobe XD, Figma, Photoshop, etc. The channel also has some videos on web development using HTML, CSS, JavaScript, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Coding Tech: Coding Tech is a channel that curates the best talks from tech conferences and events around the world. You can watch presentations from experts and leaders in the web development industry on topics like HTML, CSS, JavaScript, React, Vue, Angular, GraphQL, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Academind: Academind is a channel that offers in-depth and comprehensive tutorials on web development and programming. You can learn about HTML, CSS, JavaScript, React, Angular, Vue, Node.js, MongoDB, Flutter, etc. The channel also has some courses on web development fundamentals and advanced topics.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;freeCodeCamp.org: freeCodeCamp.org is a channel that provides free courses and certifications on web development and other programming topics. You can learn about HTML, CSS, JavaScript, React, D3.js, Python, etc. The channel also has some videos on web development projects and interviews.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Kevin Powell: Kevin Powell is a channel that teaches web development and design with a focus on CSS. You can learn how to style websites and apps using CSS grid, flexbox, animations, transitions, variables, etc. The channel also has some videos on HTML, JavaScript, Sass, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Codevolution: Codevolution is a channel that teaches web development with a focus on React. You can learn how to use React hooks, custom hooks, context API, Redux, Next.js, Gatsby, etc. The channel also has some videos on JavaScript, TypeScript, GraphQL, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you have more suggestions, please drop them in the comment section&lt;/p&gt;

&lt;p&gt;Additional channels as suggested in the comments&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Programmingwithmosh: Programming with Mosh provides comprehensive tutorials on various programming topics, with a specialization in web development. The channel covers in-depth lessons on React, including topics such as React hooks, custom hooks, context API, Redux, Next.js, Gatsby, and more. Additionally, you'll find valuable content on JavaScript, TypeScript, GraphQL, and other essential programming concepts.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>productivity</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Coding and testing as related to Blockchain security</title>
      <dc:creator>Majid Kareem</dc:creator>
      <pubDate>Mon, 11 Dec 2023 05:46:58 +0000</pubDate>
      <link>https://forem.com/emkay860/coding-and-testing-as-related-to-blockchain-security-1af2</link>
      <guid>https://forem.com/emkay860/coding-and-testing-as-related-to-blockchain-security-1af2</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Coding and testing are essential aspects of blockchain security, as they ensure the reliability and robustness of smart contracts and decentralized applications. In this blog post, we will explore some of the challenges and best practices of coding and testing for blockchain security, and introduce some of the tools that can help developers achieve this goal.&lt;/p&gt;

&lt;p&gt;Blockchain security is a complex and multidimensional topic, as it involves not only the technical aspects of cryptography, consensus algorithms, and network protocols, but also the economic and social aspects of incentives, governance, and user behavior. Moreover, blockchain security is not static, but dynamic and evolving, as new threats and vulnerabilities emerge over time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges of coding and testing
&lt;/h2&gt;

&lt;p&gt;One of the main challenges of coding and testing for blockchain security is the high cost of failure. Unlike traditional software development, where bugs can be fixed with patches and updates, blockchain code is often immutable and irreversible, meaning that any mistake or flaw can have permanent and catastrophic consequences. For example, in 2016, a hacker exploited a vulnerability in the DAO smart contract, a decentralized autonomous organization built on Ethereum, and drained more than $50 million worth of ether from its funds. The incident led to a controversial hard fork of Ethereum to restore the stolen funds, which resulted in a split of the community and the creation of Ethereum Classic.&lt;/p&gt;

&lt;p&gt;Another challenge of coding and testing for blockchain security is the diversity and complexity of the attack vectors. Blockchain code can be vulnerable to various types of attacks, such as reentrancy, front-running, overflow/underflow, denial-of-service, phishing, Sybil, eclipse, and more. Some of these attacks are specific to blockchain technology, while others are common to any software system. Moreover, some of these attacks can be executed by malicious actors within the system, such as miners, validators, or users, while others can be executed by external adversaries, such as hackers or regulators.&lt;/p&gt;

&lt;p&gt;To address these challenges, developers need to adopt a rigorous and systematic approach to coding and testing for blockchain security. This involves following some of the best practices of secure software development, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Applying the principle of least privilege, which means granting the minimum amount of access and permissions necessary for each function or user.&lt;/li&gt;
&lt;li&gt;Applying the principle of defense in depth, which means implementing multiple layers of security measures to protect against different types of threats.&lt;/li&gt;
&lt;li&gt;Applying the principle of simplicity, which means avoiding unnecessary complexity and keeping the code as clear and concise as possible.&lt;/li&gt;
&lt;li&gt;Applying the principle of transparency, which means making the code open-source and verifiable by anyone.&lt;/li&gt;
&lt;li&gt;Applying the principle of auditability, which means subjecting the code to regular and independent reviews by experts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition to these general principles, developers can also leverage some of the tools that are designed specifically for coding and testing for blockchain security. These tools can help developers detect and prevent common vulnerabilities, optimize performance and gas efficiency, and verify the correctness and compliance of their code. Some examples of these tools are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MythX: MythX is a security analysis platform for Ethereum smart contracts. It integrates with various development frameworks and tools, such as Truffle, Remix, Hardhat, Brownie, and VS Code. It performs static analysis, dynamic analysis, symbolic execution, fuzzing, and property-based testing to identify potential security issues in smart contract code. It also provides detailed reports with severity levels, descriptions, locations, and remediation suggestions for each issue.&lt;/li&gt;
&lt;li&gt;Slither: Slither is a static analysis framework for Solidity smart contracts. It detects vulnerabilities such as reentrancy, uninitialized storage pointers, shadowing state variables, incorrect inheritance order, dangerous delegate calls, and more. It also provides metrics such as cyclomatic complexity, number of variables per function, number of dependencies per contract, etc. Slither can be used as a command-line tool or as a library for custom analysis.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;if you have other useful tools that can be used to ensure the security of blockchain codes, feel free to drop them in the comment section.&lt;/p&gt;

</description>
      <category>security</category>
      <category>blockchain</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Intro to Blockchain Security</title>
      <dc:creator>Majid Kareem</dc:creator>
      <pubDate>Mon, 11 Dec 2023 05:41:06 +0000</pubDate>
      <link>https://forem.com/emkay860/intro-to-blockchain-security-15ha</link>
      <guid>https://forem.com/emkay860/intro-to-blockchain-security-15ha</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Blockchain security is a crucial aspect of ensuring the reliability, trustworthiness, and resilience of blockchain networks and transactions. Blockchain security involves applying various risk management techniques, cybersecurity frameworks, assurance services, and best practices to protect the blockchain system from attacks and fraud.&lt;/p&gt;

&lt;p&gt;Blockchain networks are based on distributed ledger technology (DLT), which allows multiple nodes to share and validate data without relying on a central authority. This decentralized and transparent nature of blockchain offers many benefits, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enhanced data integrity: Blockchain data is immutable, meaning that once it is recorded, it cannot be altered or deleted without leaving a trace. This ensures the authenticity and verifiability of the data and prevents tampering or corruption.&lt;/li&gt;
&lt;li&gt;Improved efficiency: Blockchain transactions are faster, cheaper and more scalable than traditional transactions, as they eliminate intermediaries and reduce transaction costs and delays.&lt;/li&gt;
&lt;li&gt;Increased trust: Blockchain transactions are secured by cryptographic mechanisms, such as digital signatures and hash functions, which ensure that only authorized parties can access and modify the data. Moreover, blockchain transactions are visible to all nodes in the network, creating a high level of transparency and accountability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, blockchain security is not without challenges. Blockchain networks are vulnerable to various types of attacks and threats, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;51% attack: This occurs when a malicious actor gains control of more than 50% of the network's computing power, allowing them to manipulate the consensus mechanism and reverse or double-spend transactions.&lt;/li&gt;
&lt;li&gt;Sybil attack: This occurs when a malicious actor creates multiple fake identities or nodes in the network, allowing them to influence the network's behavior or disrupt its functionality.&lt;/li&gt;
&lt;li&gt;Smart contract vulnerabilities: Smart contracts are self-executing agreements that run on blockchain platforms, such as Ethereum. However, smart contracts may contain coding errors or bugs that can be exploited by hackers or malicious users to manipulate or steal funds or data.&lt;/li&gt;
&lt;li&gt;Data breaches: Blockchain data is encrypted and distributed across multiple nodes, making it difficult to access or compromise. However, blockchain data may still be exposed or leaked if the nodes or endpoints are not properly secured or the encryption keys are stolen or lost.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To address these challenges, blockchain security requires a holistic and proactive approach that covers various aspects of the blockchain system, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Network governance: This involves establishing clear rules and protocols for the operation and maintenance of the blockchain network, such as node selection, consensus mechanism, transaction validation, and dispute resolution.&lt;/li&gt;
&lt;li&gt;Coding and testing: This involves ensuring that the blockchain code and smart contracts are well-designed, error-free, and secure, using tools such as code analysis, debugging, and auditing.&lt;/li&gt;
&lt;li&gt;Data security: This involves protecting the blockchain data from unauthorized access or modification, using techniques such as encryption, authentication, authorization, and backup.&lt;/li&gt;
&lt;li&gt;Monitoring and detection: This involves tracking and analyzing the blockchain network's performance and activity, using tools such as metrics, logs, and alerts.&lt;/li&gt;
&lt;li&gt;Response and recovery: This involves responding to and mitigating potential incidents or attacks on the blockchain network, using tools such as incident management, forensics, and backup.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Blockchain security is an evolving and dynamic field that requires constant research and innovation. Blockchain security experts need to keep up with the latest developments and trends in blockchain technology and cybersecurity, as well as collaborate with other stakeholders in the blockchain ecosystem. By doing so, they can ensure that blockchain networks are secure, reliable, and trustworthy for various applications and use cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  More on Blockchain security
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/emkay860/coding-and-testing-as-related-to-blockchain-security-1af2"&gt;Coding and testing as related to Blockchain security&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  References:
&lt;/h3&gt;

&lt;p&gt;[1] What is Blockchain Security? | IBM&lt;br&gt;
[2] What is Blockchain Security? - GeeksforGeeks&lt;br&gt;
[3] How Effective Is Blockchain in Cybersecurity? - ISACA&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>security</category>
    </item>
    <item>
      <title>Essential AI Tools to Boost Your Productivity as a Frontend developer</title>
      <dc:creator>Majid Kareem</dc:creator>
      <pubDate>Sun, 12 Nov 2023 11:15:46 +0000</pubDate>
      <link>https://forem.com/emkay860/essential-ai-tools-to-boost-your-productivity-as-a-frontend-developer-325p</link>
      <guid>https://forem.com/emkay860/essential-ai-tools-to-boost-your-productivity-as-a-frontend-developer-325p</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As a front-end developer, you know how challenging and time-consuming it can be to create engaging and responsive web applications. You have to deal with complex layouts, animations, user interactions, accessibility, performance, and more. But what if you could use some AI tools to make your life easier and boost your productivity? In this blog post, we will introduce you to some essential AI tools that can help you with various aspects of frontend development, such as design, testing, optimization, and deployment. Let's get started!&lt;/p&gt;

&lt;h3&gt;
  
  
  Design
&lt;/h3&gt;

&lt;p&gt;One of the most important and creative parts of frontend development is design. You want to create a beautiful and intuitive user interface that matches your client's vision and user's needs. But sometimes, you may struggle with finding the right colors, fonts, icons, images, or layouts for your web pages. That's where AI tools can come in handy. Here are some examples of AI tools that can help you with design:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Wix ADI&lt;/strong&gt;: This is an AI-powered website builder that can create a stunning and personalized website for you in minutes. You just have to answer a few questions about your business or project, and Wix ADI will generate a website that suits your style and needs. You can then customize it further with your own content and features.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adobe Sensei&lt;/strong&gt;: This is a collection of AI and machine learning features that are integrated into various Adobe products, such as Photoshop, Illustrator, XD, and Spark. Adobe Sensei can help you with tasks such as image editing, vector graphics, prototyping, animation, and more. For example, you can use Photoshop's Content-Aware Fill to remove unwanted objects from your images, or use Illustrator's Image Trace to convert raster images into vector graphics.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tailor Brands&lt;/strong&gt;: This is an AI-powered logo maker that can create a unique and professional logo for your brand in minutes. You just have to enter your brand name and industry, and Tailor Brands will generate a logo that reflects your identity and personality. You can then customize it further with different fonts, colors, icons, and shapes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Testing
&lt;/h3&gt;

&lt;p&gt;Another crucial part of frontend development is testing. You want to make sure that your web application works well on different browsers, devices, screen sizes, and network conditions. You also want to ensure that your web application is bug-free, secure, and user-friendly. But testing can be tedious and time-consuming, especially if you have to do it manually. That's why AI tools can be very helpful for testing. Here are some examples of AI tools that can help you with testing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Applitools&lt;/strong&gt;: This is an AI-powered visual testing platform that can help you detect and fix UI bugs across browsers and devices. Applitools uses computer vision to compare screenshots of your web pages and identify any visual differences or anomalies. You can then review the results and approve or reject the changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testim&lt;/strong&gt;: This is an AI-powered end-to-end testing platform that can help you create and execute automated tests for your web applications. Testim uses machine learning to learn from your actions and generate stable and reusable test cases. You can then run the tests on different browsers and devices using Testim's cloud infrastructure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mabl&lt;/strong&gt;: This is an AI-powered testing service that can help you test the functionality and performance of your web applications. Mabl uses natural language processing to understand your user journeys and generate automated tests based on them. You can then run the tests on different browsers and devices using Mabl's cloud infrastructure.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Optimization
&lt;/h3&gt;

&lt;p&gt;One more important aspect of frontend development is optimization. You want to make sure that your web application loads fast, consumes less bandwidth, and delivers a smooth user experience. But optimization can be tricky and complex, especially if you have to deal with large files, images, videos, or animations. That's why AI tools can be very useful for optimization. Here are some examples of AI tools that can help you with optimization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cloudinary&lt;/strong&gt;: This is an AI-powered media management platform that can help you optimize the delivery of your images and videos on the web. Cloudinary uses machine learning to analyze your media files and apply the best compression, format, quality, resolution, and cropping for each device and browser. You can also use Cloudinary's transformations to apply effects, filters, overlays, watermarks, and more to your media files.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lighthouse&lt;/strong&gt;: This is an AI-powered performance analysis tool that can help you measure and improve the quality of your web pages. Lighthouse uses machine learning to audit your web pages based on various metrics, such as performance, accessibility, best practices, SEO, and PWA. You can then use Lighthouse's recommendations to optimize your web pages for speed, usability, and user satisfaction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unscreen&lt;/strong&gt;: This is an AI-powered background removal tool that can help you create transparent videos for your web applications. Unscreen uses machine learning to detect the foreground and background of your videos and remove the background automatically. You can then download the transparent videos or use Unscreen's integrations to add them to your web pages.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Deployment
&lt;/h3&gt;

&lt;p&gt;The final step of frontend development is deployment. You want to make sure that your web application is accessible and available to your users on the web. But deployment can be challenging and costly, especially if you have to deal with hosting, servers, domains, SSL, CDN, and more. That's why AI tools can be very beneficial for deployment. Here are some examples of AI tools that can help you with deployment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Netlify&lt;/strong&gt;: This is an AI-powered web development platform that can help you deploy your web applications with ease. Netlify uses machine learning to automate the deployment process and provide you with features such as continuous integration, continuous delivery, serverless functions, forms, identity, analytics, and more. You can also use Netlify's integrations to connect your web applications with various services and tools.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vercel&lt;/strong&gt;: This is an AI-powered web development platform that can help you deploy your web applications with speed. Vercel uses machine learning to optimize the deployment process and provide you with features such as edge network, serverless functions, preview deployments, custom domains, SSL, CDN, and more. You can also use Vercel's integrations to connect your web applications with various frameworks and technologies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Firebase&lt;/strong&gt;: This is an AI-powered web development platform that can help you deploy your web applications with scale. Firebase uses machine learning to enhance the deployment process and provide you with features such as hosting, authentication, database, storage, functions, analytics, messaging, and more. You can also use Firebase's integrations to connect your web applications with various services and tools.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;As you can see, there are many AI tools that can help you boost your productivity as a frontend developer. These tools can help you with various aspects of frontend development, such as design, testing, optimization, and deployment. By using these tools, you can save time, money, and effort, and focus on creating amazing web applications for your users. So why not give them a try and see for yourself how they can improve your frontend development workflow?&lt;/p&gt;

&lt;p&gt;Your insights are invaluable! Share your suggestions and any new tools you've discovered in the comments section.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>productivity</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Common mistakes to avoid when using React Hooks</title>
      <dc:creator>Majid Kareem</dc:creator>
      <pubDate>Sun, 12 Nov 2023 10:46:38 +0000</pubDate>
      <link>https://forem.com/emkay860/common-mistakes-to-avoid-when-using-react-hooks-53gn</link>
      <guid>https://forem.com/emkay860/common-mistakes-to-avoid-when-using-react-hooks-53gn</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;React Hooks are a powerful feature of React that allow you to use state and other React features without writing a class component. They can simplify your code, make it more reusable, and enable better performance. However, they also come with some challenges and pitfalls that you need to be aware of. In this blog post, I will show you how to use React Hooks effectively and avoid some common mistakes.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are React Hooks?
&lt;/h2&gt;

&lt;p&gt;React Hooks are functions that let you "hook into" React state and lifecycle features from function components. They also let you create your own custom hooks to share reusable stateful logic between components.&lt;/p&gt;

&lt;p&gt;There are two main types of hooks: built-in hooks and custom hooks. Built-in hooks are provided by React and include useState, useEffect, useRef, useContext, useReducer, useMemo, useCallback, and useImperativeHandle. Custom hooks are user-defined functions that start with "use" and can call other hooks inside them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use React Hooks?
&lt;/h2&gt;

&lt;p&gt;React Hooks have several benefits over class components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They reduce the amount of boilerplate code you need to write, such as constructors, binding methods, and lifecycle methods.&lt;/li&gt;
&lt;li&gt;They make it easier to reuse stateful logic between components, without relying on higher-order components or render props.&lt;/li&gt;
&lt;li&gt;They allow you to split your code into smaller functions based on related functionality, rather than forcing a split based on lifecycle methods.&lt;/li&gt;
&lt;li&gt;They enable better performance optimizations, such as memoizing values and avoiding unnecessary re-renders.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to use React Hooks effectively?
&lt;/h2&gt;

&lt;p&gt;To use React Hooks effectively, you need to follow some rules and best practices. Here are some of them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only call hooks at the top level of your function component. Don't call them inside loops, conditions, or nested functions. This ensures that the hooks are called in the same order every time your component renders.&lt;/li&gt;
&lt;li&gt;Only call hooks from React function components or custom hooks. Don't call them from regular JavaScript functions or class methods. This ensures that the hooks have access to the correct React context and state.&lt;/li&gt;
&lt;li&gt;Use the array argument of useEffect to specify the dependencies of your effect. This tells React when to run your effect and when to skip it. If you omit the array argument, your effect will run after every render. If you pass an empty array, your effect will run only once after the initial render.&lt;/li&gt;
&lt;li&gt;Use custom hooks to extract and reuse stateful logic between components. A custom hook should start with "use" and return a value or an object with values. You can call other hooks inside your custom hook, but don't call your custom hook inside another hook.&lt;/li&gt;
&lt;li&gt;Use useCallback to memoize functions that depend on props or state. This prevents unnecessary re-creations of the same function on every render, which can cause performance issues and bugs.&lt;/li&gt;
&lt;li&gt;Use useMemo to memoize expensive calculations that depend on props or state. This prevents unnecessary re-computations of the same value on every render, which can also cause performance issues and bugs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to avoid common pitfalls with React Hooks?
&lt;/h2&gt;

&lt;p&gt;React Hooks can also introduce some pitfalls and challenges that you need to be careful about. Here are some of them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't use setState inside useEffect without a dependency array. This can create an infinite loop of updates and re-renders, which can crash your app or cause memory leaks.&lt;/li&gt;
&lt;li&gt;Don't use stale props or state inside useEffect or useCallback. This can lead to inconsistent or incorrect behavior of your component. To avoid this, use useRef to store the latest props or state values, or use functional updates to access the current state value.&lt;/li&gt;
&lt;li&gt;Don't forget to clean up your effects. If your effect creates any subscriptions, listeners, timers, or other resources that need to be disposed of when the component unmounts, you need to return a cleanup function from your effect that does that. Otherwise, you may cause memory leaks or errors in your app.&lt;/li&gt;
&lt;li&gt;Don't mutate state directly. Always use setState or useReducer to update your state in a functional way. Mutating state directly can cause bugs and unexpected behavior in your component.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;React Hooks are a great way to use state and other React features in function components. They can make your code simpler, more reusable, and more performant. However, they also require some discipline and attention to avoid common pitfalls and mistakes. By following the rules and best practices outlined in this blog post, you can use React Hooks effectively and confidently in your React projects.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Promises, Async/await in JavaScript</title>
      <dc:creator>Majid Kareem</dc:creator>
      <pubDate>Sat, 19 Aug 2023 11:31:45 +0000</pubDate>
      <link>https://forem.com/emkay860/promises-asyncawait-in-javascript-4gk5</link>
      <guid>https://forem.com/emkay860/promises-asyncawait-in-javascript-4gk5</guid>
      <description>&lt;p&gt;Promises and async/await are important concepts in JavaScript for managing asynchronous operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are Promises?&lt;/strong&gt;&lt;br&gt;
Promises are a way to handle asynchronous operations in a more organized manner. They represent a value that might be available now, or in the future, or never. A Promise has three states: pending, fulfilled, or rejected. It's a container for an operation that may complete in the future and can be used to handle both successful and failed outcomes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Async/await?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Async/await is a more modern and readable way to work with asynchronous code. The async keyword is used to define a function that returns a Promise. Inside an async function, you can use the await keyword to pause execution until the Promise is fulfilled or rejected. This allows you to write asynchronous code in a more synchronous style, making it easier to understand and maintain.&lt;/p&gt;

&lt;p&gt;Here's a simple example of using Promises and async/await in JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Using Promises&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchUserData&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="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;example&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="c1"&gt;// Simulating a successful fetch&lt;/span&gt;
      &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="c1"&gt;// Simulating an error&lt;/span&gt;
      &lt;span class="c1"&gt;// reject(new Error('Failed to fetch'));&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&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="nx"&gt;fetchUserData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User data:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// Using async/await&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetchUserData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User data:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&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="nx"&gt;getUserData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, both fetchUserData and getUserData deal with asynchronous operations. The Promises version uses then and catch to handle results and errors, while the async/await version makes the code flow more naturally, similar to synchronous code.Overall, Promises and async/await are powerful tools in JavaScript that make managing asynchronous code more intuitive and maintainable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros of using Async/await?&lt;/strong&gt;&lt;br&gt;
Async/await offers several advantages when it comes to writing asynchronous code in JavaScript:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Readability and Maintainability:&lt;/strong&gt; Async/await makes asynchronous code look and feel more like synchronous code. This makes the code easier to read, understand, and maintain, as it follows a linear flow of execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling:&lt;/strong&gt; Error handling with async/await is more straightforward. You can use try-catch blocks to handle both synchronous and asynchronous errors in a single location, which improves code organization and makes it easier to manage errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sequencing:&lt;/strong&gt; Async/await allows you to write code that executes in a sequential manner, even when dealing with asynchronous operations. This can help avoid the "callback hell" problem that can arise when nesting multiple callbacks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Variable Scope:&lt;/strong&gt; With async/await, variables declared within the function scope are accessible within the asynchronous code block. This eliminates the need for closures or additional context management that might be required with Promises.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Stacks:&lt;/strong&gt; Async/await maintains better error stacks. When an error occurs, the stack trace provides more accurate information about where the error originated, making debugging easier.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parallelism:&lt;/strong&gt; You can use async/await to run multiple asynchronous operations in parallel and then wait for all of them to complete using Promise.all.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Native Support:&lt;/strong&gt; Async/await is now natively supported by modern JavaScript environments and widely adopted, making it a standard and reliable way to handle asynchronous operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compatibility with Other Constructs:&lt;/strong&gt; Async/await can be used alongside other modern JavaScript constructs like destructuring, spread/rest operators, and template literals, improving the overall coding experience.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While async/await offers many benefits, it's important to note that it might not be suitable for every situation. For example, if you need to perform complex control flow or manipulate multiple asynchronous operations at once, you might still need to work with Promises or other asynchronous patterns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cons of using Async/await&lt;/strong&gt;&lt;br&gt;
While async/await is a powerful and widely-used feature in JavaScript, there are a few potential drawbacks and considerations to keep in mind:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Loss of Parallelism in Loops:&lt;/strong&gt; If you use async/await within loops, the asynchronous operations will run sequentially due to the nature of the event loop. This can impact performance when you actually want to run them in parallel.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unhandled Promise Rejections:&lt;/strong&gt; If you forget to use try-catch or .catch() with an async function, unhandled Promise rejections might occur, causing your application to terminate unexpectedly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Async-Only Functions:&lt;/strong&gt; While async/await can simplify most asynchronous scenarios, it might not be suitable for functions that need to return multiple values or perform complex control flow, which are better handled using traditional callback patterns or Promises.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limited Granularity:&lt;/strong&gt; Async/await operates at the function level, so if you need to await multiple asynchronous operations within a single function, you can't fine-tune the order of their execution as you can with individual Promises.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Overall, while async/await offers significant benefits, it's important to carefully consider its usage within the context of your project and be aware of its potential limitations. Depending on the specific requirements of your application, other asynchronous patterns might be more appropriate.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>Understanding Asynchronous programming in JavaScript.</title>
      <dc:creator>Majid Kareem</dc:creator>
      <pubDate>Thu, 27 Jul 2023 10:35:14 +0000</pubDate>
      <link>https://forem.com/emkay860/understanding-asynchronous-programming-in-javascript-163n</link>
      <guid>https://forem.com/emkay860/understanding-asynchronous-programming-in-javascript-163n</guid>
      <description>&lt;h2&gt;
  
  
  What is Asynchronous programming?
&lt;/h2&gt;

&lt;p&gt;According to MDN official docs, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Asynchronous programming is a technique that enables your program to start a potentially long-running task and still be able to be responsive to other events while that task runs, rather than having to wait until that task has finished. Once that task has finished, your program is presented with the result. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Various functions provided by browsers can potentially take a long time, and therefore, are asynchronous. For example: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Making HTTP requests using &lt;strong&gt;&lt;em&gt;fetch()&lt;/em&gt;&lt;/strong&gt; API &lt;/li&gt;
&lt;li&gt;Accessing a user's camera or microphone using &lt;strong&gt;&lt;em&gt;getUserMedia()&lt;/em&gt;&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;Asking a user to select files using &lt;strong&gt;_showOpenFilePicker() _&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So even though you may not need to implement your own asynchronous functions very often, you are very likely to need to use other asynchronous functions correctly. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why are asynchronous functions important?
&lt;/h2&gt;

&lt;p&gt;Lets have a scenario where a function generates large number if prime numbers (&lt;code&gt;generatePrime()&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Even if the &lt;code&gt;generatePrime()&lt;/code&gt; is written to be very efficient, it will still take a bit of time to complete (long-running function). During this period when the function is running, the program/app will become totally unresponsive: you won't be able to click anything, type anything, or do anything else. &lt;/p&gt;

&lt;p&gt;This is the basic problem with long-running synchronous functions. What we need is a way for our program to: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start a long-running operation by calling a function. &lt;/li&gt;
&lt;li&gt;Have that function start the operation and return immediately, so our program can still respond to other events. &lt;/li&gt;
&lt;li&gt;Notify us of the result of the operation when it eventually completes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are precisely what asynchronous functions can do and that is why they are so important. &lt;/p&gt;

&lt;h2&gt;
  
  
  Typical Examples of Asynchronous programming
&lt;/h2&gt;

&lt;p&gt;An example of asynchronous programming is &lt;strong&gt;Event handlers&lt;/strong&gt;: you provide a function (the event handler) that will be called, not right away, but whenever the event happens. &lt;/p&gt;

&lt;p&gt;An Event handler is a type of &lt;strong&gt;callback&lt;/strong&gt; function. &lt;br&gt;
A callback is just a function that's passed into another function, with the expectation that the callback will be called at the appropriate time. &lt;/p&gt;

&lt;p&gt;Callbacks used to be the main way asynchronous functions were implemented in Javascript. &lt;br&gt;
However, Callbacks have been seen to have a bunch of downsides, for instance : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Callback-based code can become hard to understand when the callback itself has to call functions that accept a callback.&lt;/li&gt;
&lt;li&gt;The code becomes harder to read as the nesting increase. This is sometimes called "callback hell" or the "pyramid of doom". &lt;/li&gt;
&lt;li&gt;it can also get very hard to handle errors as you often have to handle errors at each level of the "pyramid"/ nesting, instead of having error handling only once at the top level.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For these reasons, most modern asynchronous APIs don't use callbacks anymore. Instead, the foundation of asynchronous programming in JavaScript is the &lt;strong&gt;Promise&lt;/strong&gt;, &lt;/p&gt;

&lt;p&gt;In the next post, I'll be discussing &lt;strong&gt;&lt;a href="https://dev.to/emkay860/promises-asyncawait-in-javascript-4gk5"&gt;Promises&lt;/a&gt;&lt;/strong&gt;, the popular &lt;strong&gt;&lt;a href="https://dev.to/emkay860/promises-asyncawait-in-javascript-4gk5"&gt;async/await&lt;/a&gt;&lt;/strong&gt;, and how to work with them. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;u&gt;References:&lt;/u&gt;&lt;/em&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing"&gt;MDN official Documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>typescript</category>
      <category>react</category>
    </item>
    <item>
      <title>Introduction to Smart Contracts (Solidity) Part 1</title>
      <dc:creator>Majid Kareem</dc:creator>
      <pubDate>Sat, 02 Jul 2022 05:07:44 +0000</pubDate>
      <link>https://forem.com/emkay860/introduction-to-smart-contracts-solidity-part-1-2mk9</link>
      <guid>https://forem.com/emkay860/introduction-to-smart-contracts-solidity-part-1-2mk9</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is the first in a possibly long list of web3 tutorials to come. It was written to serve as an entry point for beginners to Solidity programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Expectations
&lt;/h2&gt;

&lt;p&gt;It assumes no previous knowledge of solidity although some knowledge of other programming languages like python or JavaScript will make the journey a lot easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are smart contracts
&lt;/h2&gt;

&lt;p&gt;A smart contract is a program that runs at an address on Ethereum. They're made up of data and functions that can execute upon receiving a transaction.&lt;/p&gt;

&lt;h2&gt;
  
  
  What makes up a smart contract
&lt;/h2&gt;

&lt;p&gt;Smart contracts comprise different parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Environment variables&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Functions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Events and Logs&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;we will be having a brief explanation of each part below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data
&lt;/h2&gt;

&lt;p&gt;Any contract data must be assigned to a location: either to &lt;code&gt;storage&lt;/code&gt; or &lt;code&gt;memory&lt;/code&gt;. It's costly to modify storage in a smart contract so you need to consider where your data should live.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Storage&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Persistent data is referred to as &lt;code&gt;storage&lt;/code&gt; and is represented by state variables. These values get stored permanently on the blockchain. You need to declare the &lt;code&gt;type&lt;/code&gt; of a variable so that the contract can keep track of how much storage it needs when it compiles.&lt;/p&gt;

&lt;p&gt;It is comparatively costly to read, and even more to initialize and modify storage. Because of this cost, you should minimize what you store in persistent storage to what the contract needs to run. Store data like derived calculations, caching, and aggregates outside of the contract. A contract can neither read nor write to any storage apart from its own.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Solidity example
&lt;/span&gt;&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Storage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="n"&gt;savedData&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// State variable
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll likely be familiar with most types if you've already programmed in object-oriented languages. However, the &lt;code&gt;address&lt;/code&gt; type should be new to you if you're new to Ethereum development.&lt;/p&gt;

&lt;p&gt;An &lt;code&gt;address&lt;/code&gt; type can hold an Ethereum address which equates to 20 bytes or 160 bits. It returns a hexadecimal notation with a leading 0x.&lt;/p&gt;

&lt;p&gt;Other types include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;boolean&lt;/li&gt;
&lt;li&gt;integer&lt;/li&gt;
&lt;li&gt;fixed point numbers&lt;/li&gt;
&lt;li&gt;fixed-size byte arrays&lt;/li&gt;
&lt;li&gt;dynamically-sized byte arrays&lt;/li&gt;
&lt;li&gt;Rational and integer literals&lt;/li&gt;
&lt;li&gt;String literals&lt;/li&gt;
&lt;li&gt;Hexadecimal literals&lt;/li&gt;
&lt;li&gt;Enums&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For detailed explanations on each type, visit the official documentation for &lt;a href="https://docs.soliditylang.org/en/latest/types.html#value-types"&gt;Solidity types&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Memory&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Values that are only stored for the lifetime of a contract function's execution are called memory variables. Since these are not stored permanently on the blockchain, they are much cheaper to use.&lt;/p&gt;

&lt;p&gt;To learn more about the EVM stores data, check out the &lt;a href="https://docs.soliditylang.org/en/latest/introduction-to-smart-contracts.html?highlight=memory#storage-memory-and-the-stack"&gt;docs on Solidity&lt;/a&gt; &lt;/p&gt;
&lt;h2&gt;
  
  
  Environment variables
&lt;/h2&gt;

&lt;p&gt;In addition to the variables you define in your contract, there are some special global variables. They are primarily used to provide information about the blockchain, the current transaction, or are general-use utility functions.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;block.chainid (uint)&lt;/code&gt;: the current chain id&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;block.timestamp (uint)&lt;/code&gt;: the current block timestamp as seconds since unix epoch&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;msg.value (uint)&lt;/code&gt;: number of wei/Ether sent with the message&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;msg.sender (address)&lt;/code&gt;: sender of the message (current call)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;blockhash(uint blockNumber)&lt;/code&gt; returns (bytes32): hash of the given block when blocknumber is one of the 256 most recent blocks; otherwise returns zero&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;p&gt;Functions are the executable units of code. In the most simplistic terms, functions can get or set information in response to incoming transactions on the blockchain.&lt;/p&gt;

&lt;p&gt;Function Calls can happen internally or externally and have different levels of visibility towards other contracts. Functions accept parameters and return variables to pass parameters and values between them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Function visibility&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Solidity knows two kinds of function calls: external ones that do create an actual EVM message call and internal ones that do not. Furthermore, internal functions can be made inaccessible to derived contracts. This gives rise to four types of visibility for functions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;external&lt;/li&gt;
&lt;li&gt;public&lt;/li&gt;
&lt;li&gt;internal&lt;/li&gt;
&lt;li&gt;private&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For detailed explanations on each visibility type, visit the solidity docs on &lt;a href="https://docs.soliditylang.org/en/latest/contracts.html#function-visibility"&gt;function visibility&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Solidity example to show different parts of a function
&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;setUsername&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;userName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&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;ul&gt;
&lt;li&gt;The parameter value of type &lt;code&gt;string&lt;/code&gt; is passed into the function: &lt;code&gt;setUsername&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It's declared &lt;code&gt;public&lt;/code&gt;, meaning anyone can access it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Events and Logs
&lt;/h2&gt;

&lt;p&gt;Events let you communicate with your smart contract from other subscribing applications. When a transaction is mined, smart contracts can emit events and write logs to the blockchain that other applications can then process.&lt;/p&gt;

&lt;p&gt;Solidity events give an abstraction on top of the EVM’s logging functionality. Applications can subscribe and listen to these events through the RPC interface of an Ethereum client.&lt;/p&gt;

&lt;p&gt;For more explanations check out &lt;a href="https://docs.soliditylang.org/en/latest/contracts.html#events"&gt;Solidity Events&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far we've learned the basic parts of a smart contract written in Solidity. These various parts are what when put together form a group of code that executes certain commands on the blockchain.&lt;/p&gt;

&lt;p&gt;I know this is a pretty long (and possibly boring) tutorial, but since it's a tutorial on smart contracts, it won't be complete if we don't write any smart contracts.&lt;br&gt;
Below is an example of a very simple smart contract written in solidity. It was lifted directly (with a little update to use the latest version of solidity) from the &lt;a href="https://ethereum.org/en/developers/docs/smart-contracts/anatomy/#hello-world"&gt;Ethereum documentation&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;//SPDX-License-Identifier: Unlicense
// Specifies the version of Solidity, using semantic versioning.
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/layout-of-source-files.html#pragma
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Defines a contract named `HelloWorld`.
// A contract is a collection of functions and data (its state).
// Once deployed, a contract resides at a specific address on the Ethereum blockchain.
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/structure-of-a-contract.html
&lt;/span&gt;&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;HelloWorld&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Declares a state variable `message` of type `string`.
&lt;/span&gt;    &lt;span class="c1"&gt;// State variables are variables whose values are permanently stored in contract storage.
&lt;/span&gt;    &lt;span class="c1"&gt;// The keyword `public` makes variables accessible from outside a contract
&lt;/span&gt;    &lt;span class="c1"&gt;// and creates a function that other contracts or clients can call to access the value.
&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Similar to many class-based object-oriented languages, a constructor is
&lt;/span&gt;    &lt;span class="c1"&gt;// a special function that is only executed upon contract creation.
&lt;/span&gt;    &lt;span class="c1"&gt;// Constructors are used to initialize the contract's data.
&lt;/span&gt;    &lt;span class="c1"&gt;// Learn more: https://solidity.readthedocs.io/en/v0.5.10/contracts.html#constructors
&lt;/span&gt;    &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;initMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Accepts a string argument `initMessage` and sets the value
&lt;/span&gt;        &lt;span class="c1"&gt;// into the contract's `message` storage variable).
&lt;/span&gt;        &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;initMessage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// A public function that accepts a string argument
&lt;/span&gt;    &lt;span class="c1"&gt;// and updates the `message` storage variable.
&lt;/span&gt;    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;newMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newMessage&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;Here is a list of references and useful links for further reading&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ethereum.org/en/developers/docs/smart-contracts/"&gt;Ethereum official doc on smart contracts&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.soliditylang.org/en/latest/index.html"&gt;Official Solidity docs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://remix.ethereum.org/"&gt;Remix an IDE for writing smart contracts&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a link to &lt;a href="https://www.codementor.io/@majidk"&gt;My Public profile&lt;/a&gt; if you feel the tutorial is not enough and you need a 1 on 1 session 😉.&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>blockchain</category>
      <category>ethereum</category>
      <category>evm</category>
    </item>
    <item>
      <title>Create a Charity/Donation Platform on the Blockchain (part 1)</title>
      <dc:creator>Majid Kareem</dc:creator>
      <pubDate>Fri, 27 May 2022 19:21:47 +0000</pubDate>
      <link>https://forem.com/emkay860/create-a-charitydonation-platform-on-the-blockchain-part-1-6nb</link>
      <guid>https://forem.com/emkay860/create-a-charitydonation-platform-on-the-blockchain-part-1-6nb</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;We will be creating the smart contract for a charity donation platform in this post. Yes, something like &lt;strong&gt;Gofundme&lt;/strong&gt; but on the blockchain.&lt;br&gt;
and users can donate to individual campaigns with &lt;strong&gt;&lt;em&gt;Ether&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Expectations
&lt;/h2&gt;

&lt;p&gt;This post assumes you are already familiar with the structure of a smart contract and basic types in solidity.&lt;/p&gt;
&lt;h2&gt;
  
  
  Required tools
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Remix (a browser-based IDE)&lt;/li&gt;
&lt;li&gt;Any browser (preferably Chrome)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;Our platform will allow anyone to create a charity/donation campaign and set a time limit or deadline for it, After creating the campaign, a user can donate to any campaign they choose to support and the creator of the campaign can withdraw all the funds donated to the campaign after the deadline is exceeded.&lt;/p&gt;

&lt;p&gt;Enough talk, let's get started with the code&lt;/p&gt;

&lt;p&gt;First, we'll create our contract file and name it &lt;code&gt;Charity.sol&lt;/code&gt;. The we populate it with the code below;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"@openzeppelin/contracts/utils/Counters.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Charity&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;The first and second line in the code above defines the license and the solidity version for compiling the code. Then we import a library from openzeppelin called &lt;a href="https://docs.openzeppelin.com/contracts/3.x/api/utils#Counters"&gt;&lt;code&gt;Counters.sol&lt;/code&gt;&lt;/a&gt; which provides us with counters that can only be incremented or decremented. &lt;br&gt;
Next, we define a new contract called &lt;em&gt;Charity&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To give us a sense of direction, we will create all the functions skeleton we need before implementing them one by one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;
&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Charity&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;Counters&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Counters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// These are events to be emitted when specific actions are completed
&lt;/span&gt;    &lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;CampaignStarted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;WithdrawFunds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;FundsDonated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;donor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// defines a variable to keep track of total number of campaigns created
&lt;/span&gt;    &lt;span class="n"&gt;Counters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Counter&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;_campaignCount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="c1"&gt;// Campaign details to be saved
&lt;/span&gt;    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Campaign&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;imgUrl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;fundsRaised&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isLive&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="nb"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// allows us to keep track of the campaigns created and it's details using a unique ID
&lt;/span&gt;    &lt;span class="k"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;Campaign&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;_campaigns&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// allows us to keep track of the who donates to a campaign and the amount they donated
&lt;/span&gt;    &lt;span class="k"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;userCampaignDonations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// this function generated a unique ID for a campaign
//from it's title, descrition and creator address
&lt;/span&gt;    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;generateCampaignId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;pure&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&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="c1"&gt;// this function will be called by a user to create a new campaign
&lt;/span&gt;    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;startCampaign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;imgUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;deadline&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// calling this function allows users to donate to a charity campaign of their choice
&lt;/span&gt;    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;donateToCampaign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// returns the details of a campaign given the campaignId
&lt;/span&gt;    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getCampaign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Campaign&lt;/span&gt; &lt;span class="k"&gt;memory&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="c1"&gt;// this function allows the creator of the campaign to withdraw all the funds donated to the campaign 
&lt;/span&gt;    &lt;span class="c1"&gt;// after the campaign has ended
&lt;/span&gt;    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;withdrawCampaignFunds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&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;Now let's take the functions one after the other and flesh them out. First is the &lt;code&gt;generateCampaignId&lt;/code&gt; function, this function will create a unique hash from the campaign title, description, and initiator address&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;generateCampaignId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;pure&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;keccak256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;abi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;encodePacked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;initiator&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;campaignId&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;Next, we create the function &lt;code&gt;startCampaign&lt;/code&gt; which allows a user to actually create a campaign,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;startCampaign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;imgUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;deadline&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// first, we generate a campaignID 
&lt;/span&gt;        &lt;span class="c1"&gt;// using the title, description and the address of the initiator
&lt;/span&gt;        &lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;generateCampaignId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// get a reference to the campaign with the generated Id
&lt;/span&gt;        &lt;span class="n"&gt;Campaign&lt;/span&gt; &lt;span class="k"&gt;storage&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_campaigns&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="c1"&gt;// require that the campaign is not live yet.
&lt;/span&gt;        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isLive&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Campaign exists"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// require the current time to be less than the campaign deadline
&lt;/span&gt;        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Campaign ended"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initiator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;imgUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;imgUrl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deadline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isLive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// increment the total number of charity campaigns created
&lt;/span&gt;        &lt;span class="n"&gt;_campaignCount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// emit an event to the blockchain
&lt;/span&gt;        &lt;span class="k"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;CampaignStarted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&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;After creating the campaign, we need a way to allow users donate to a live campaign. So let's create the &lt;code&gt;donateToCampaign&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// allows users to donate to a charity campaign of their choice
&lt;/span&gt;    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;donateToCampaign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// get campaign details with the given campaign
&lt;/span&gt;        &lt;span class="n"&gt;Campaign&lt;/span&gt; &lt;span class="k"&gt;storage&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_campaigns&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="c1"&gt;// end the campaign if the deadline is exceeded
&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;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isLive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// require the campaign has not ended
&lt;/span&gt;        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Campaign has ended"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amountToDonate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&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;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amountToDonate&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="s"&gt;"Wrong ETH value"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// increase the campaign balance by the amount donated;
&lt;/span&gt;        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fundsRaised&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;amountToDonate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;amountToDonate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// keep track of users donation history
&lt;/span&gt;        &lt;span class="n"&gt;userCampaignDonations&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;amountToDonate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// emit FundsDonated event
&lt;/span&gt;        &lt;span class="k"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;FundsDonated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountToDonate&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;Okay, users can now donate to our cause via the above function. But we still need a way for the creator of the campaign to withdraw the Ether donated to them. So let's complete the &lt;code&gt;withdrawCampaignFunds&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;withdrawCampaignFunds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Campaign&lt;/span&gt; &lt;span class="k"&gt;storage&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_campaigns&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="c1"&gt;// require the msg.sender is the creator of the campaign
&lt;/span&gt;        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Not campaign initiator"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// require the campaign has ended
&lt;/span&gt;        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isLive&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"campaign is still active"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Campaign is still active"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// require the campaign has funds to be withdrawn
&lt;/span&gt;        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;balance&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="s"&gt;"No funds to withdraw"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amountToWithdraw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// zero the campaign balance
&lt;/span&gt;        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;balance&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="c1"&gt;// transfer the balance to the initiator address;
&lt;/span&gt;        &lt;span class="k"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nb"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amountToWithdraw&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// emit an event to the blockchain
&lt;/span&gt;        &lt;span class="k"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;WithdrawFunds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountToWithdraw&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;Cool, we now have complete flow for the donation process.&lt;br&gt;
Below is the full code for this tutorial.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"@openzeppelin/contracts/utils/Counters.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;Charity&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;Counters&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Counters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;CampaignStarted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;WithdrawFunds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;FundsDonated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;donor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint256&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;Counters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Counter&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;_campaignCount&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;Campaign&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;imgUrl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;fundsRaised&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isLive&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="nb"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;Campaign&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;_campaigns&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;userCampaignDonations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;generateCampaignId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt; &lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;pure&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;keccak256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;abi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;encodePacked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;initiator&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;campaignId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;startCampaign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;calldata&lt;/span&gt; &lt;span class="n"&gt;imgUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;deadline&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// generate a campaignID 
&lt;/span&gt;        &lt;span class="c1"&gt;// using the title, description and the address of the initiator
&lt;/span&gt;        &lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;generateCampaignId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// get a reference to the campaign with the generated Id
&lt;/span&gt;        &lt;span class="n"&gt;Campaign&lt;/span&gt; &lt;span class="k"&gt;storage&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_campaigns&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="c1"&gt;// require that the campaign is not live yet.
&lt;/span&gt;        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isLive&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Campaign exists"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// require the current time to be less than the campaign deadline
&lt;/span&gt;        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Campaign ended"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initiator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;imgUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;imgUrl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deadline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isLive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// increment the total number of charity campaigns created
&lt;/span&gt;        &lt;span class="n"&gt;_campaignCount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// emit an event to the blockchain
&lt;/span&gt;        &lt;span class="k"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;CampaignStarted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;endCampaign&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// allows users to donate to a charity campaign of their choice
&lt;/span&gt;    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;donateToCampaign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// get campaign details with the given campaign
&lt;/span&gt;        &lt;span class="n"&gt;Campaign&lt;/span&gt; &lt;span class="k"&gt;storage&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_campaigns&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="c1"&gt;// end the campaign if the deadline is exceeded
&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;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isLive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// require the campaign has not ended
&lt;/span&gt;        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Campaign has ended"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amountToDonate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&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;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amountToDonate&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="s"&gt;"Wrong ETH value"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// increase the campaign balance by the amount donated;
&lt;/span&gt;        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fundsRaised&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;amountToDonate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;amountToDonate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// keep track of users donation history
&lt;/span&gt;        &lt;span class="n"&gt;userCampaignDonations&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;amountToDonate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// emit FundsDonated event
&lt;/span&gt;        &lt;span class="k"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;FundsDonated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountToDonate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// returns the details of a campaign given the campaignId
&lt;/span&gt;    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getCampaign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;view&lt;/span&gt; &lt;span class="k"&gt;returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Campaign&lt;/span&gt; &lt;span class="k"&gt;memory&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="n"&gt;_campaigns&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;withdrawCampaignFunds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bytes32&lt;/span&gt; &lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Campaign&lt;/span&gt; &lt;span class="k"&gt;storage&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_campaigns&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="c1"&gt;// require the msg.sender is the creator of the campaign
&lt;/span&gt;        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Not campaign initiator"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// require the campaign has ended
&lt;/span&gt;        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isLive&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"campaign is still active"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deadline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Campaign is still active"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// require the campaign has funds to be withdrawn
&lt;/span&gt;        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;balance&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="s"&gt;"No funds to withdraw"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amountToWithdraw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// zero the campaign balance
&lt;/span&gt;        &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;balance&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="c1"&gt;// transfer the balance to the initiator address;
&lt;/span&gt;        &lt;span class="k"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nb"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amountToWithdraw&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// emit an event to the blockchain
&lt;/span&gt;        &lt;span class="k"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;WithdrawFunds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;campaignId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;campaign&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initiator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountToWithdraw&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;Well done!!! 🎉🎉🎉&lt;br&gt;
If you have been able to get here, I hope you have been able to learn more about creating smart contracts. &lt;br&gt;
In the next part of this series, I'll be creating a UI for our smart contract using React or Next.js&lt;/p&gt;

&lt;p&gt;Feel free to reach out to me on &lt;a href="https://www.codementor.io/@majidk"&gt;codementor&lt;/a&gt; If you have any suggestions or questions or if you just wanna say hi.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>smartcontract</category>
      <category>solidity</category>
      <category>gofundme</category>
    </item>
    <item>
      <title>Create a decentralized exchange (DEX) for ERC-20 token </title>
      <dc:creator>Majid Kareem</dc:creator>
      <pubDate>Wed, 09 Mar 2022 00:06:18 +0000</pubDate>
      <link>https://forem.com/emkay860/create-a-decentralized-exchange-dex-for-erc-20-token-kpm</link>
      <guid>https://forem.com/emkay860/create-a-decentralized-exchange-dex-for-erc-20-token-kpm</guid>
      <description>&lt;p&gt;This is the first part in a probably very long series of posts.&lt;br&gt;
In this post, I'll be explaining how to create a decentralized exchange for an ERC-20 token.&lt;/p&gt;
&lt;h2&gt;
  
  
  Expectations
&lt;/h2&gt;

&lt;p&gt;This post assumes basic knowledge of solidity, the ERC standard, and creating a basic ERC20 token &lt;/p&gt;
&lt;h2&gt;
  
  
  Creating the ERC20 token
&lt;/h2&gt;

&lt;p&gt;We'll create a basic ERC20 token by importing the &lt;code&gt;ERC20.sol&lt;/code&gt; from Openzeppelin. Our token will be named &lt;code&gt;TSTtoken&lt;/code&gt; and it's gonna have a total supply of 1 million.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// TSTtoken.sol
// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&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="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;TSTtoken&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;ERC20&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;initialSupply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000000e18&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;ERC20&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"TST"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_mint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;initialSupply&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;Next we will create the smart contract for our DEX in &lt;code&gt;DEX.sol&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;//SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&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="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'./TSTtoken.sol'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;DEX&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;IERC20&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;Bought&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;Sold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;TSTtoken&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;buy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// TODO
&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;sell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// TODO
&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;Our DEX contract has two functions &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;buy: The user can send ether and receive TSTtoken in exchange&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;sell: The user can send TSTtoken and get back ether&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Buy Function
&lt;/h2&gt;

&lt;p&gt;To code the buy function, we'll have to check the amount ether being sent to the contract in the message (i.e msg.value) and verify that the contract owns enough TSTtoken. If the contract has enough TSTtoken, it sends it to the user and emits the &lt;code&gt;Bought&lt;/code&gt; event.&lt;/p&gt;

&lt;h2&gt;
  
  
  Note:
&lt;/h2&gt;

&lt;p&gt;For simplicity, we'll exchange 1 TSTtoken for 1 Wei&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;buy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amountTobuy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&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="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;dexBalance&lt;/span&gt; &lt;span class="o"&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;balanceOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;this&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amountTobuy&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="s"&gt;"You need to send some ether"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amountTobuy&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;dexBalance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Not enough tokens in the reserve"&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="nb"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountTobuy&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;Bought&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amountTobuy&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;I guess the buy function is pretty easy&lt;/p&gt;

&lt;h2&gt;
  
  
  The Sell Function
&lt;/h2&gt;

&lt;p&gt;This function will require the user to have approved the sell amount by calling the &lt;code&gt;ERC-20 approve&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;When the sell function is called, we'll call the &lt;code&gt;ERC-20 transferFrom&lt;/code&gt; function to transfer the tokens from the caller address to the contract address. If the transfer is successful, we'll send the equivalent amount of Eth back to the caller address.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;  &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;sell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;require&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;&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="s"&gt;"You need to sell at least some tokens"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;allowance&lt;/span&gt; &lt;span class="o"&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;allowance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;this&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;allowance&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="s"&gt;"Check the token allowance"&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;transferFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;this&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="k"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nb"&gt;transfer&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="k"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;Sold&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool, we've just written a smart contract for a very basic DEX. Now anyone can buy and sell our token with Eth. Here is the complete code for the &lt;code&gt;DEX.sol&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;//SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&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="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'./TSTtoken.sol'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;DEX&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;IERC20&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;Bought&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;event&lt;/span&gt; &lt;span class="n"&gt;Sold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;TSTtoken&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;buy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;payable&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amountTobuy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&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="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;dexBalance&lt;/span&gt; &lt;span class="o"&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;balanceOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;this&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amountTobuy&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="s"&gt;"You need to send some ether"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amountTobuy&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;dexBalance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Not enough tokens in the reserve"&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="nb"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountTobuy&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;Bought&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amountTobuy&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;sell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;require&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;&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="s"&gt;"You need to sell at least some tokens"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;allowance&lt;/span&gt; &lt;span class="o"&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;allowance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;this&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="nb"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;allowance&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="s"&gt;"Check the token allowance"&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;transferFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;this&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="k"&gt;payable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nb"&gt;transfer&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="k"&gt;emit&lt;/span&gt; &lt;span class="n"&gt;Sold&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="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Ways to improve the DEX
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Implement an exchange of TSTtoken to another ERC20 token e.g DAI, UNI etc&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add a fee on Buy and Sell of the token&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add the liquidity for TSTtoken with transaction fees generated to uniswap by implementing the &lt;a href="https://docs.uniswap.org/protocol/V2/reference/smart-contracts/router-02#addliquidity"&gt;addLiquidity/addLiquidityETH function&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>web3</category>
      <category>blockchain</category>
      <category>ethereum</category>
    </item>
  </channel>
</rss>
