<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Apetastic</title>
    <description>The latest articles on DEV Community by Apetastic (@defifofum).</description>
    <link>https://dev.to/defifofum</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%2F653307%2F918c9104-8219-4652-9f06-babd268a9e68.png</url>
      <title>DEV Community: Apetastic</title>
      <link>https://dev.to/defifofum</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/defifofum"/>
    <language>en</language>
    <item>
      <title>Smart Contract Factories</title>
      <dc:creator>Apetastic</dc:creator>
      <pubDate>Thu, 24 Jun 2021 13:38:44 +0000</pubDate>
      <link>https://dev.to/defifofum/smart-contract-factories-aj</link>
      <guid>https://dev.to/defifofum/smart-contract-factories-aj</guid>
      <description>&lt;p&gt;Factories are a wonderful design pattern, which are not only used with smart contracts, but generally in any other coding language to help facilitate better automation. &lt;/p&gt;

&lt;p&gt;Factories are one of my favorite design patterns when dealing with generic smart contracts, because they eliminate the need to open up my IDE and do another command line deployment, when I could be doing a deployment with a simple transaction through &lt;a href="https://metamask.io/"&gt;MetaMask&lt;/a&gt;. Along with the benefits of a factory, they are also simple to create. &lt;/p&gt;

&lt;h2&gt;
  
  
  TLDR; Show Me the Goods!
&lt;/h2&gt;

&lt;p&gt;If you are someone that likes to jump right into the code here it is: &lt;br&gt;
&lt;a href="https://github.com/DeFiFoFum/ApetasticERC20Factory"&gt;ApetasticERC20Factory GitHub Repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The factory is deployed below and you can start making your very own ERC-20 tokens through the &lt;code&gt;Write Contract&lt;/code&gt; tab.&lt;br&gt;
&lt;a href="https://bscscan.com/address/0x8C269841E5a8BC387925325a44c7867DBb3Cd5Cd#code"&gt;ApetasticERC20Factory Deployed&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Full Details: How it Works
&lt;/h2&gt;

&lt;p&gt;Solidity language has a very useful feature for creating new contracts using the &lt;code&gt;new&lt;/code&gt; keyword. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.soliditylang.org/en/v0.8.6/control-structures.html?highlight=new#creating-contracts-via-new"&gt;Solidity Docs - Creating new contracts via new&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; keyword works similarly to javascript in that it creates a new instance. In this case, a new deployed contract instance.&lt;/p&gt;

&lt;p&gt;In the example below, pulled from the &lt;code&gt;ApetasticERC20Factory&lt;/code&gt;, you can see that we first need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Import the contract that we want to deploy. In this case &lt;code&gt;ApetasticERC20.sol&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;new&lt;/code&gt; keyword in a function using the name of the contract to deploy a new instance. &lt;/li&gt;
&lt;li&gt;Pass in arguments which the constructor of the contract needs. &lt;/li&gt;
&lt;li&gt;The return value represents the address of the deployed contract. In the factory it is a good idea to record it so that new contracts can be found later if needed. In this case we are pushing the address to the &lt;code&gt;allTokens&lt;/code&gt; array. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;ApetasticERC20 token&lt;/code&gt; is also more than just an address. It also represents an &lt;code&gt;ApetasticERC20&lt;/code&gt; interface which means &lt;code&gt;uint256 tokenTotalSupply = token.totalSupply()&lt;/code&gt; would also be possible. This allows your factory contract to call methods on the newly deployed contract for configuration or other purposes.&lt;/em&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="cm"&gt;/* from ApetasticERC20Factory contract */&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"./ApetasticERC20.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;/// @notice Create a new ERC-20 contract
/// @dev Tokens created have 18 decimals which means add 18 zeros the integer supply: 000000000000000000 
/// @param name The name of the token
/// @param symbol The symbol of the token
/// @param supply The totalSupply of the token
&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;createToken&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;name&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;symbol&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;supply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&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;address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ApetasticERC20&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;ApetasticERC20&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;symbol&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;supply&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;allTokens&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&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="n"&gt;token&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;TokenCreated&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="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;supply&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;address&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The ApetasticERC20.sol Contract
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;ApetasticERC20&lt;/code&gt; contract is an extension of OpenZeppelin's &lt;a href="https://docs.openzeppelin.com/contracts/erc20"&gt;&lt;code&gt;ERC20&lt;/code&gt; implementation&lt;/a&gt;. This was done so the proper parameters could be set in the &lt;code&gt;constructor&lt;/code&gt; of the token. You could take a similar approach and add a bit more functionality if needed. &lt;/p&gt;

&lt;p&gt;As seen in the contract code below, the constructor is used to mint the initial supply by calling the internal &lt;code&gt;_mint&lt;/code&gt; function and sending it to the &lt;code&gt;mintTo&lt;/code&gt; address. &lt;/p&gt;

&lt;p&gt;Because the factory contract is deploying the contract, &lt;code&gt;msg.sender&lt;/code&gt; would actually send the tokens to the factory which could lock them. To avoid this, the factory passes its &lt;code&gt;msg.sender&lt;/code&gt; (the address that initiated the creation in this case) into &lt;code&gt;ApetasticERC20&lt;/code&gt; so that the tokens are sent to the correct 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="cm"&gt;/* from ApetasticERC20 contract */&lt;/span&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;"@openzeppelin/contracts/token/ERC20/ERC20.sol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;/// @notice Create an ERC20 token with adjustable initial parameters
&lt;/span&gt;&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;ApetasticERC20&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="c1"&gt;/// @param name The name of the token
&lt;/span&gt;    &lt;span class="c1"&gt;/// @param symbol The symbol of the token
&lt;/span&gt;    &lt;span class="c1"&gt;/// @param mintTo The address that the initial supply should be sent to
&lt;/span&gt;    &lt;span class="c1"&gt;/// @param supply The totalSupply of the token
&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;name&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;symbol&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;mintTo&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;supply&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="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;symbol&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;mintTo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;supply&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;
  
  
  Time to Get Dirty!
&lt;/h2&gt;

&lt;p&gt;Sweet, now you can apply this fairly straightforward concept to ever more and more complex smart contracts. Let's build! &lt;/p&gt;

&lt;h2&gt;
  
  
  Challenge: Factory Improvements
&lt;/h2&gt;

&lt;p&gt;This is just an MVP for factories. From here a good improvement is to make the factory contract upgradeable which allows us to add features without changing the contract address. &lt;/p&gt;

&lt;p&gt;Contracts which don't hold value are good candidates for upgradeability.&lt;/p&gt;

&lt;p&gt;Give it a shot &amp;amp; let me know if there are other features you desire! &lt;/p&gt;

</description>
      <category>smartcontracts</category>
      <category>defi</category>
      <category>blockchain</category>
      <category>erc20</category>
    </item>
  </channel>
</rss>
