<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://c4crypto.in</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1675081730080/z5Y58kJrI.png</url><title>Untitled Publication</title><link>https://c4crypto.in</link></image><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Nov 2026 01:04:34 GMT</lastBuildDate><atom:link href="https://c4crypto.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Writing an NFT Contract with inbuilt Push Notifications Support ~ Push Protocol]]></title><description><![CDATA[Smart Contracts typically operate independently, meaning if you deploy an NFT contract and users mint NFTs, you might not have a straightforward way to…]]></description><link>https://c4crypto.in/writing-an-nft-contract-with-inbuilt-push-notifications-support-push-protocol</link><guid isPermaLink="true">https://c4crypto.in/writing-an-nft-contract-with-inbuilt-push-notifications-support-push-protocol</guid><category><![CDATA[push notifications]]></category><category><![CDATA[NFT]]></category><category><![CDATA[Solidity]]></category><category><![CDATA[Goerli]]></category><category><![CDATA[pushprotocol]]></category><dc:creator><![CDATA[Siddesh]]></dc:creator><pubDate>Thu, 26 Oct 2023 15:16:45 GMT</pubDate><content:encoded><![CDATA[<p>Smart Contracts typically operate independently, meaning if you deploy an NFT contract and users mint NFTs, you might not have a straightforward way to monitor these actions. While you can create custom event listeners or services to send email notifications to stakeholders, it can be a complex and resource-intensive process.</p>
<p>However, in this guide, I'll demonstrate how, in just 5 minutes, you can effortlessly enable push notifications within your smart contracts for any function call, utilizing the Push Protocol Notification Service, without the need for additional server setups or unnecessary complexities.</p>
<h2 id="heading-goal">Goal</h2>
<p>To write an ERC-721 NFT smart contract and give it the ability to trigger Notifications directly to wallet addresses using Push Protocol's Notification Service.</p>
<p><strong><em>TLDR; The NFT Contract Owner will receive a Notification to his wallet whenever someone mints his NFT.</em></strong></p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Before we start writing our NFT contract, there are a few things we need to setup,</p>
<ol>
<li><p><a target="_blank" href="https://metamask.io/">Metamask Wallet</a> or any other Ethereum Wallet.</p>
</li>
<li><p>Some Goerli ETH tokens since we will be deploying our NFT in Goerli Testnet. You can acquire gETH from this <a target="_blank" href="https://goerli-faucet.pk910.de/">PoW Faucet</a> or <a target="_blank" href="https://discord.gg/pushprotocol">Push Protocol Discord</a>.  </p>
<p> %[https://youtu.be/OtKLAKYjWh0] </p>
</li>
<li><p>So now we have acquired the gETH tokens, we should create a Channel on Push Staging network to use for sending notifications in Push Network from your smart contract.</p>
</li>
</ol>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://youtu.be/CFkzk6NyQhc">https://youtu.be/CFkzk6NyQhc</a></div>
<p> </p>
<p>I will be using the ERC-721 NFT contract code from OpenZeppelin Library which you can copy from <a target="_blank" href="https://www.openzeppelin.com/contracts">Openzeppelin</a> website or Just copy from the code below.</p>
<pre><code class="lang-solidity"><span class="hljs-comment">//SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.20;</span>

<span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts@5.0.0/token/ERC721/ERC721.sol"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts@5.0.0/token/ERC721/extensions/ERC721URIStorage.sol"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts@5.0.0/access/Ownable.sol"</span>;

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">MyTokenb</span> <span class="hljs-keyword">is</span> <span class="hljs-title">ERC721</span>, <span class="hljs-title">ERC721URIStorage</span>, <span class="hljs-title">Ownable</span> </span>{
    <span class="hljs-keyword">uint256</span> <span class="hljs-keyword">private</span> _nextTokenId;

    <span class="hljs-function"><span class="hljs-keyword">constructor</span>(<span class="hljs-params"></span>)
        <span class="hljs-title">ERC721</span>(<span class="hljs-params"><span class="hljs-string">"MyToken"</span>, <span class="hljs-string">"MTK"</span></span>)
        <span class="hljs-title">Ownable</span>(<span class="hljs-params"><span class="hljs-built_in">msg</span>.sender</span>)
    </span>{}

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">safeMint</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
        <span class="hljs-keyword">uint256</span> tokenId <span class="hljs-operator">=</span> _nextTokenId<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
        _safeMint(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>, tokenId);
        _setTokenURI(tokenId, <span class="hljs-string">"https://bafybeiezmpzzg2wfapt5cjaxttfsgmi3q5hi5oypqxwfbh6fikclrwgtdu.ipfs.w3s.link/metadata.json"</span>);
    }
    <span class="hljs-comment">// The following functions are overrides required by Solidity.</span>

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">tokenURI</span>(<span class="hljs-params"><span class="hljs-keyword">uint256</span> tokenId</span>)
        <span class="hljs-title"><span class="hljs-keyword">public</span></span>
        <span class="hljs-title"><span class="hljs-keyword">view</span></span>
        <span class="hljs-title"><span class="hljs-keyword">override</span></span>(<span class="hljs-params">ERC721, ERC721URIStorage</span>)
        <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span></span>)
    </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>.tokenURI(tokenId);
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">supportsInterface</span>(<span class="hljs-params"><span class="hljs-keyword">bytes4</span> interfaceId</span>)
        <span class="hljs-title"><span class="hljs-keyword">public</span></span>
        <span class="hljs-title"><span class="hljs-keyword">view</span></span>
        <span class="hljs-title"><span class="hljs-keyword">override</span></span>(<span class="hljs-params">ERC721, ERC721URIStorage</span>)
        <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">bool</span></span>)
    </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>.supportsInterface(interfaceId);
    }

 }
</code></pre>
<p>Boom, We already have a fully functional NFT contract with metadata defined, here I've passed an IPFS file in Line number 19 where you can pass your own NFT Metadata if you wish.</p>
<p>Now, All we need to do is add a block of code within the <code>safeMint</code> function to trigger Notifications whenever someone mints your NFT.</p>
<pre><code class="lang-solidity">    IPUSHCommInterface(EPNS_COMM_ADDRESS).sendNotification(
            CHANNEL_ADDRESS, <span class="hljs-comment">// from channel</span>
            TO_ADDRESS, <span class="hljs-comment">// to recipient, put address(this) in case you want Broadcast or Subset. For Targetted put the address to which you want to send</span>
            <span class="hljs-keyword">bytes</span>(
                <span class="hljs-keyword">string</span>(
                    <span class="hljs-comment">// We are passing identity here: https://docs.epns.io/developers/developer-guides/sending-notifications/advanced/notification-payload-types/identity/payload-identity-implementations</span>
                    <span class="hljs-built_in">abi</span>.<span class="hljs-built_in">encodePacked</span>(
                        <span class="hljs-string">"0"</span>, <span class="hljs-comment">// this is notification identity: https://docs.epns.io/developers/developer-guides/sending-notifications/advanced/notification-payload-types/identity/payload-identity-implementations</span>
                        <span class="hljs-string">"+"</span>, <span class="hljs-comment">// segregator</span>
                        <span class="hljs-string">"3"</span>, <span class="hljs-comment">// this is payload type: https://docs.epns.io/developers/developer-guides/sending-notifications/advanced/notification-payload-types/payload (1, 3 or 4) = (Broadcast, targetted or subset)</span>
                        <span class="hljs-string">"+"</span>, <span class="hljs-comment">// segregator</span>
                        <span class="hljs-string">"Woohoo, We're making moooneiz!"</span>, <span class="hljs-comment">// this is notificaiton title</span>
                        <span class="hljs-string">"+"</span>, <span class="hljs-comment">// segregator       </span>
                        addressToString(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>), <span class="hljs-comment">// notification body</span>
                        <span class="hljs-string">"just minted an NFT"</span>
                    )
                )
            )
        );
</code></pre>
<p>In the above code snippet, You will need to replace <code>EPNS_COMM_ADDRESS</code>, <code>CHANNEL_ADDRESS</code>, <code>TO_ADDRESS</code> with corresponding values.</p>
<ol>
<li><p><code>EPNS_COMM_ADDRESS</code> - <code>0xb3971BCef2D791bc4027BbfedFb47319A4AAaaAa</code></p>
<p> <em>Refer Push Contract Addresses Page to find</em> <code>EPNS_COMM_ADDRESS</code> <em>for the chain that you're using.</em></p>
</li>
<li><p><code>CHANNEL_ADDRESS</code> - Wallet address that you used for creating a channel in Push Staging Site.</p>
</li>
<li><p><code>TO_ADDRESS</code> - Recipient Address(es)</p>
</li>
</ol>
<p>Apart from this, You can edit the title and body text of your notification.</p>
<p>The above code is all you need to trigger the notification, paste this block of code inside <code>safeMint</code> function. The above code uses one helper function called <code>addressToString</code> and also uses <code>IPUSHCommInterface</code> interface which we should define in our contract.</p>
<h3 id="heading-addresstostring"><code>addressToString</code></h3>
<p>Helper function to convert an Ethereum address to a string.</p>
<pre><code class="lang-solidity">      <span class="hljs-comment">// Helper function to convert address to string</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addressToString</span>(<span class="hljs-params">
        <span class="hljs-keyword">address</span> _address
    </span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span></span>) </span>{
        <span class="hljs-keyword">bytes32</span> _bytes <span class="hljs-operator">=</span> <span class="hljs-keyword">bytes32</span>(<span class="hljs-keyword">uint256</span>(<span class="hljs-keyword">uint160</span>(_address)));
        <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> HEX <span class="hljs-operator">=</span> <span class="hljs-string">"0123456789abcdef"</span>;
        <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> _string <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">bytes</span>(<span class="hljs-number">42</span>);
        _string[<span class="hljs-number">0</span>] <span class="hljs-operator">=</span> <span class="hljs-string">"0"</span>;
        _string[<span class="hljs-number">1</span>] <span class="hljs-operator">=</span> <span class="hljs-string">"x"</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&lt;</span> <span class="hljs-number">20</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
            _string[<span class="hljs-number">2</span> <span class="hljs-operator">+</span> i <span class="hljs-operator">*</span> <span class="hljs-number">2</span>] <span class="hljs-operator">=</span> HEX[<span class="hljs-keyword">uint8</span>(_bytes[i <span class="hljs-operator">+</span> <span class="hljs-number">12</span>] <span class="hljs-operator">&gt;</span><span class="hljs-operator">&gt;</span> <span class="hljs-number">4</span>)];
            _string[<span class="hljs-number">3</span> <span class="hljs-operator">+</span> i <span class="hljs-operator">*</span> <span class="hljs-number">2</span>] <span class="hljs-operator">=</span> HEX[<span class="hljs-keyword">uint8</span>(_bytes[i <span class="hljs-operator">+</span> <span class="hljs-number">12</span>] <span class="hljs-operator">&amp;</span> <span class="hljs-number">0x0f</span>)];
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">string</span>(_string);
    }
</code></pre>
<h3 id="heading-ipushcomminterface"><code>IPUSHCommInterface</code></h3>
<pre><code class="lang-solidity"><span class="hljs-comment">// PUSH Comm Contract Interface</span>
<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">IPUSHCommInterface</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sendNotification</span>(<span class="hljs-params">
        <span class="hljs-keyword">address</span> _channel,
        <span class="hljs-keyword">address</span> _recipient,
        <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">calldata</span> _identity
    </span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span></span>;
}
</code></pre>
<p>We now paste this interface code above the <code>contract</code> keyword in our solidity file.</p>
<p>Hooray! This is what my final contract code looks like👀</p>
<pre><code class="lang-solidity"><span class="hljs-comment">//SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.20;</span>

<span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts@5.0.0/token/ERC721/ERC721.sol"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts@5.0.0/token/ERC721/extensions/ERC721URIStorage.sol"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts@5.0.0/access/Ownable.sol"</span>;
<span class="hljs-comment">// PUSH Comm Contract Interface</span>
<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">IPUSHCommInterface</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sendNotification</span>(<span class="hljs-params">
        <span class="hljs-keyword">address</span> _channel,
        <span class="hljs-keyword">address</span> _recipient,
        <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">calldata</span> _identity
    </span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span></span>;
}

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">MyTokenb</span> <span class="hljs-keyword">is</span> <span class="hljs-title">ERC721</span>, <span class="hljs-title">ERC721URIStorage</span>, <span class="hljs-title">Ownable</span> </span>{
    <span class="hljs-keyword">uint256</span> <span class="hljs-keyword">private</span> _nextTokenId;

    <span class="hljs-function"><span class="hljs-keyword">constructor</span>(<span class="hljs-params"></span>)
        <span class="hljs-title">ERC721</span>(<span class="hljs-params"><span class="hljs-string">"MyToken"</span>, <span class="hljs-string">"MTK"</span></span>)
        <span class="hljs-title">Ownable</span>(<span class="hljs-params"><span class="hljs-built_in">msg</span>.sender</span>)
    </span>{}

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">safeMint</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
        <span class="hljs-keyword">uint256</span> tokenId <span class="hljs-operator">=</span> _nextTokenId<span class="hljs-operator">+</span><span class="hljs-operator">+</span>;
        _safeMint(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>, tokenId);
        _setTokenURI(tokenId, <span class="hljs-string">"https://bafybeiezmpzzg2wfapt5cjaxttfsgmi3q5hi5oypqxwfbh6fikclrwgtdu.ipfs.w3s.link/metadata.json"</span>);
          IPUSHCommInterface(<span class="hljs-number">0xb3971BCef2D791bc4027BbfedFb47319A4AAaaAa</span>).sendNotification(
            <span class="hljs-number">0xb3fE9B4b68Eff6aB07D349296a9a80a0F02B55f1</span>, <span class="hljs-comment">// from channel</span>
            <span class="hljs-number">0xb3fE9B4b68Eff6aB07D349296a9a80a0F02B55f1</span>, <span class="hljs-comment">// to recipient, put address(this) in case you want Broadcast or Subset. For Targetted put the address to which you want to send</span>
            <span class="hljs-keyword">bytes</span>(
                <span class="hljs-keyword">string</span>(
                    <span class="hljs-comment">// We are passing identity here: https://docs.epns.io/developers/developer-guides/sending-notifications/advanced/notification-payload-types/identity/payload-identity-implementations</span>
                    <span class="hljs-built_in">abi</span>.<span class="hljs-built_in">encodePacked</span>(
                        <span class="hljs-string">"0"</span>, <span class="hljs-comment">// this is notification identity: https://docs.epns.io/developers/developer-guides/sending-notifications/advanced/notification-payload-types/identity/payload-identity-implementations</span>
                        <span class="hljs-string">"+"</span>, <span class="hljs-comment">// segregator</span>
                        <span class="hljs-string">"3"</span>, <span class="hljs-comment">// this is payload type: https://docs.epns.io/developers/developer-guides/sending-notifications/advanced/notification-payload-types/payload (1, 3 or 4) = (Broadcast, targetted or subset)</span>
                        <span class="hljs-string">"+"</span>, <span class="hljs-comment">// segregator</span>
                        <span class="hljs-string">"Woohoo, We're making moooneiz!"</span>, <span class="hljs-comment">// this is notificaiton title</span>
                        <span class="hljs-string">"+"</span>, <span class="hljs-comment">// segregator       </span>
                        addressToString(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>), <span class="hljs-comment">// notification body</span>
                        <span class="hljs-string">"just minted an NFT"</span>
                    )
                )
            )
        );
    }

    <span class="hljs-comment">// The following functions are overrides required by Solidity.</span>

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">tokenURI</span>(<span class="hljs-params"><span class="hljs-keyword">uint256</span> tokenId</span>)
        <span class="hljs-title"><span class="hljs-keyword">public</span></span>
        <span class="hljs-title"><span class="hljs-keyword">view</span></span>
        <span class="hljs-title"><span class="hljs-keyword">override</span></span>(<span class="hljs-params">ERC721, ERC721URIStorage</span>)
        <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span></span>)
    </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>.tokenURI(tokenId);
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">supportsInterface</span>(<span class="hljs-params"><span class="hljs-keyword">bytes4</span> interfaceId</span>)
        <span class="hljs-title"><span class="hljs-keyword">public</span></span>
        <span class="hljs-title"><span class="hljs-keyword">view</span></span>
        <span class="hljs-title"><span class="hljs-keyword">override</span></span>(<span class="hljs-params">ERC721, ERC721URIStorage</span>)
        <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">bool</span></span>)
    </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>.supportsInterface(interfaceId);
    }
        <span class="hljs-comment">// Helper function to convert address to string</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addressToString</span>(<span class="hljs-params">
        <span class="hljs-keyword">address</span> _address
    </span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span></span>) </span>{
        <span class="hljs-keyword">bytes32</span> _bytes <span class="hljs-operator">=</span> <span class="hljs-keyword">bytes32</span>(<span class="hljs-keyword">uint256</span>(<span class="hljs-keyword">uint160</span>(_address)));
        <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> HEX <span class="hljs-operator">=</span> <span class="hljs-string">"0123456789abcdef"</span>;
        <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> _string <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">bytes</span>(<span class="hljs-number">42</span>);
        _string[<span class="hljs-number">0</span>] <span class="hljs-operator">=</span> <span class="hljs-string">"0"</span>;
        _string[<span class="hljs-number">1</span>] <span class="hljs-operator">=</span> <span class="hljs-string">"x"</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">uint</span> i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&lt;</span> <span class="hljs-number">20</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>) {
            _string[<span class="hljs-number">2</span> <span class="hljs-operator">+</span> i <span class="hljs-operator">*</span> <span class="hljs-number">2</span>] <span class="hljs-operator">=</span> HEX[<span class="hljs-keyword">uint8</span>(_bytes[i <span class="hljs-operator">+</span> <span class="hljs-number">12</span>] <span class="hljs-operator">&gt;</span><span class="hljs-operator">&gt;</span> <span class="hljs-number">4</span>)];
            _string[<span class="hljs-number">3</span> <span class="hljs-operator">+</span> i <span class="hljs-operator">*</span> <span class="hljs-number">2</span>] <span class="hljs-operator">=</span> HEX[<span class="hljs-keyword">uint8</span>(_bytes[i <span class="hljs-operator">+</span> <span class="hljs-number">12</span>] <span class="hljs-operator">&amp;</span> <span class="hljs-number">0x0f</span>)];
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">string</span>(_string);
    }
}
</code></pre>
<p>Now it's time to Deployyy our contract and test out our notifications, <a target="_blank" href="https://vulehuan.com/en/blog/2023/05/deploy-your-blockchain-contract-on-remix-with-metamask-and-goerli-64680f9664ac40ae37cdf47a.html#gsc.tab=0">Follow this guide if you are not aware of how to deploy your contract to goerli network from Remix IDE</a></p>
<p>Now that We have deployed our Contract to Goerli Network before our contract can start sending notifications, we should add our contract as a delegate in our Push Channel.</p>
<h2 id="heading-adding-a-delegate">Adding a delegate</h2>
<p>Navigate to <a target="_blank" href="https://staging.push.org/dashboard">Push Channel Dashboard</a> and click add a delegate, paste your NFT contract address. Then Sign the transaction on your Metamask/Wallet.</p>
<h2 id="heading-testing">Testing</h2>
<p>The best part about Remix, is you can test by calling function in your contract right from that interface.</p>
<p>Let's call <code>safeMint</code> function and mint our cool NFT......</p>
<p>Woooho, I got a notification from my contract as soon as someone called <code>safeMint</code> function.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698332769990/c10d22d8-e0b6-4daf-9c79-f92ca97492c4.png" alt class="image--center mx-auto" /></p>
<p>Woooohoo, Now my boss will get an notification when someone mints his NFT, How cool is that?</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>While this is a sample use case of how quickly you can add push notifications to any contract, you can come up with any creative use cases and add Push Notification in a few lines powered by Push Protocol.</p>
]]></content:encoded></item><item><title><![CDATA[Deploying rewarder contract for communities on Mantle Testnet]]></title><description><![CDATA[DAOs and Rewards go hand in hand. In this tutorial, we will learn how to deploy a contract that will reward 100 tokens to users who hold an NFT on Mantle…]]></description><link>https://c4crypto.in/contract-on-mantle</link><guid isPermaLink="true">https://c4crypto.in/contract-on-mantle</guid><category><![CDATA[Smart Contracts]]></category><category><![CDATA[Solidity]]></category><category><![CDATA[layer2]]></category><category><![CDATA[Mantle ]]></category><category><![CDATA[EVM]]></category><dc:creator><![CDATA[Siddesh]]></dc:creator><pubDate>Sat, 03 Jun 2023 15:34:44 GMT</pubDate><content:encoded><![CDATA[<p>DAOs and Rewards go hand in hand. In this tutorial, we will learn how to <mark>deploy a contract that will reward 100 tokens to users who hold an NFT on Mantle Testnet.</mark><br />This is a great way to get started in writing smart contracts.</p>
<p>Before we get started with development, let me explain what the Mantle network is.</p>
<p>Mantle Network is a layer 2 solution built on top of the Ethereum blockchain which aims to offer super scalability by implementing a modular approach. This also means Mantle will offer low gas fees and increased security.</p>
<p>Before we proceed, <mark>We must make sure to have Metamask with Mantle Network added to your wallet</mark>, to do so visit <a target="_blank" href="https://chainlist.org/?search=mantle&amp;testnets=true">Chainlist</a>.</p>
<p>1. Connect your wallet to Chainlist and click <em>Add to Metamask</em></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685803099712/79ba7c15-9873-4097-9f55-1244822e1fc2.png" alt class="image--center mx-auto" /></p>
<p>Now let's <mark>claim some testnet </mark> <strong><mark>$BIT</mark></strong> <mark>tokens</mark> which will be used to pay for gas fees.</p>
<ol>
<li>Visit <a target="_blank" href="https://faucet.testnet.mantle.xyz/">https://faucet.testnet.mantle.xyz/</a> and connect your metamask with Goerli Network where $BIT tokens will be minted and sent to your wallet.</li>
</ol>
<p>Then head over to <a target="_blank" href="https://bridge.testnet.mantle.xyz/">https://bridge.testnet.mantle.xyz/</a> and connect your wallet with Mantle test network. You can now bridge $BIT tokens from Goerli to Mantle Testnet.</p>
<p>Okay, so we have test $BIT, let's get started.</p>
<p>We will use Remix IDE for writing, compiling and deploying our contracts onto Mantle testnet. <mark>Remix IDE is the simplest and best IDE to get started</mark>.</p>
<p>Visit <a target="_blank" href="https://remix.ethereum.org/">https://remix.ethereum.org/</a> to start writing your first contract.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685803733715/7905ed7a-ada5-4052-8719-2f276a365648.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-game-plan"><strong>Game plan</strong></h2>
<p>We will first create an <mark>ERC-20 token contract</mark> and then an <mark>ERC-721 NFT contract</mark> which will be used in our <mark>Rewarder contract.</mark></p>
<p><mark>If someone holds the NFT from our contract then the rewarder contract will give 100 ERC-20 tokens that we are creating.</mark></p>
<p><strong>1. ERC-20 Contract</strong></p>
<ul>
<li><p>Create a new file called <code>erc20.sol</code> under <code>contracts</code> folder in Remix IDE.</p>
</li>
<li><p>Then copy-paste the code below, haha ik we all copy paste 😅</p>
</li>
<li><p>Just press <mark>ctrl+s,</mark> Remix will compile the contract.</p>
</li>
<li><p>Click the Ethereum-like icon and at the top, Click the dropdown that says <mark>REMIX VM (Shanghai), select Injected Provider to connect to our Metamask network.</mark></p>
</li>
<li><p>Then click <strong>deploy</strong> button.</p>
<pre><code class="lang-solidity">  <span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
  <span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.9;</span>

  <span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts/token/ERC20/ERC20.sol"</span>;
  <span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol"</span>;
  <span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts/access/Ownable.sol"</span>;

  <span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">MantleTutorialToken</span> <span class="hljs-keyword">is</span> <span class="hljs-title">ERC20</span>, <span class="hljs-title">ERC20Burnable</span>, <span class="hljs-title">Ownable</span> </span>{
      <span class="hljs-keyword">mapping</span>(<span class="hljs-keyword">address</span> <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> <span class="hljs-keyword">bool</span>) <span class="hljs-keyword">private</span> owners;

      <span class="hljs-function"><span class="hljs-keyword">constructor</span>(<span class="hljs-params"></span>) <span class="hljs-title">ERC20</span>(<span class="hljs-params"><span class="hljs-string">"Mantle Tutorial"</span>, <span class="hljs-string">"MT"</span></span>) </span>{
          _mint(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>, <span class="hljs-number">10</span> <span class="hljs-operator">*</span> <span class="hljs-number">10</span> <span class="hljs-operator">*</span><span class="hljs-operator">*</span> decimals());
          owners[<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>] <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>; <span class="hljs-comment">// Add the deployer as an owner</span>
      }

      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">mint</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> to, <span class="hljs-keyword">uint256</span> amount</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title">ownerOnly</span> </span>{
          _mint(to, amount);
      }

      <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addOwner</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> newOwner</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title">ownerOnly</span> </span>{
          owners[newOwner] <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>;
      }

      <span class="hljs-function"><span class="hljs-keyword">modifier</span> <span class="hljs-title">ownerOnly</span>(<span class="hljs-params"></span>) </span>{
          <span class="hljs-built_in">require</span>(owners[<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>], <span class="hljs-string">"Caller is not the owner"</span>);
          <span class="hljs-keyword">_</span>;
      }
  }
</code></pre>
<h3 id="heading-congrats-we-have-deployed-our-first-contract"><em><mark>Congrats we have deployed our first contract!</mark></em></h3>
</li>
</ul>
<ol>
<li><strong>NFT Contract</strong></li>
</ol>
<ul>
<li><p>Create a new file called <code>erc721.sol</code> under <code>contracts</code> folder in Remix IDE.</p>
</li>
<li><p>Then copy-paste the code below again.</p>
</li>
<li><p>Just like before press <mark>ctrl+s,</mark> Remix will compile the contract.</p>
</li>
<li><p>Click the Ethereum-like icon, since we already selected injected provider, we can directly click <strong>deploy</strong> button</p>
</li>
</ul>
<pre><code class="lang-solidity"><span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.9;</span>

<span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts/token/ERC721/ERC721.sol"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts/access/Ownable.sol"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts/utils/Counters.sol"</span>;

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">MantleTutorialContract</span> <span class="hljs-keyword">is</span> <span class="hljs-title">ERC721</span>, <span class="hljs-title">Ownable</span> </span>{
    <span class="hljs-keyword">using</span> <span class="hljs-title">Counters</span> <span class="hljs-title"><span class="hljs-keyword">for</span></span> <span class="hljs-title">Counters</span>.<span class="hljs-title">Counter</span>;

    Counters.Counter <span class="hljs-keyword">private</span> _tokenIdCounter;

    <span class="hljs-function"><span class="hljs-keyword">constructor</span>(<span class="hljs-params"></span>) <span class="hljs-title">ERC721</span>(<span class="hljs-params"><span class="hljs-string">"Mantle Tutorial"</span>, <span class="hljs-string">"MT"</span></span>) </span>{}
    <span class="hljs-comment">// dummy metadata URI for placeholder, you can replace with your own IPFS hash</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_baseURI</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> <span class="hljs-title"><span class="hljs-keyword">pure</span></span> <span class="hljs-title"><span class="hljs-keyword">override</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-keyword">memory</span></span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-string">"https://bafyreihz6dpge5g7e6o7sjn6x7yyukbhygr5uttv5mnuppasqqhut222ny.ipfs.dweb.link/metadata.json"</span>;
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">safeMint</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> to</span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> <span class="hljs-title">onlyOwner</span> </span>{
        <span class="hljs-keyword">uint256</span> tokenId <span class="hljs-operator">=</span> _tokenIdCounter.current();
        _tokenIdCounter.increment();
        _safeMint(to, tokenId);
    }
}
</code></pre>
<p><em><mark>tada, we have deployed our NFT contract!</mark></em></p>
<p><strong>Main Rewarder Contract</strong></p>
<ul>
<li>Follow the steps you did previously to compile and deploy.</li>
</ul>
<pre><code class="lang-solidity"><span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.0;</span>


<span class="hljs-keyword">import</span> <span class="hljs-string">"@openzeppelin/contracts/token/ERC721/ERC721.sol"</span>;
<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">ERC20</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">mint</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> to, <span class="hljs-keyword">uint256</span> amount</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span></span>;
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">balanceOf</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> account</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> <span class="hljs-title"><span class="hljs-keyword">view</span></span> <span class="hljs-title"><span class="hljs-keyword">returns</span></span> (<span class="hljs-params"><span class="hljs-keyword">uint256</span></span>)</span>;
}

<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">MantleContract</span> </span>{
    ERC721 <span class="hljs-keyword">public</span> nftContract;
    ERC20 <span class="hljs-keyword">public</span> tokenContract;
    <span class="hljs-keyword">uint256</span> <span class="hljs-keyword">public</span> tokenAmount <span class="hljs-operator">=</span> <span class="hljs-number">100</span>;

    <span class="hljs-function"><span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">address</span> _nftContract, <span class="hljs-keyword">address</span> _tokenContract</span>) </span>{
        nftContract <span class="hljs-operator">=</span> ERC721(_nftContract);
        tokenContract <span class="hljs-operator">=</span> ERC20(_tokenContract);
    }

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">claimTokens</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> </span>{
        <span class="hljs-built_in">require</span>(nftContract.balanceOf(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>) <span class="hljs-operator">&gt;</span> <span class="hljs-number">0</span>, <span class="hljs-string">"Caller does not own the required NFT"</span>);
         tokenContract.mint(<span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>, <span class="hljs-number">100</span> <span class="hljs-operator">*</span><span class="hljs-number">1e18</span>);
    }
}
</code></pre>
<p><mark>Now its time to get some rewardssss, Now lets first mint one NFT from our NFT contract by calling </mark> <code>safeMint</code> <mark> function within Remix IDE</mark></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685805922663/783a3df6-6139-41e6-9f47-6282a38f13c0.png" alt class="image--center mx-auto" /></p>
<p>After that tx is done, let's go to our <mark>tokenContract </mark> and call <code>addOwner</code> function with input address as the address of our rewarded contract to allow our Rewarded contract to mint erc-20 tokens that we created.</p>
<p>We are ready to claim our rewards, simply call <code>claimTokens</code> function in our rewarded contract to enjoy some free rewards!</p>
<p>Here is a tx that I did, <a target="_blank" href="https://explorer.testnet.mantle.xyz/tx/0x96f45c58a17b60712d748f652c0db4aef51bd6d368f9273c283f0eac819f355f">https://explorer.testnet.mantle.xyz/tx/0x96f45c58a17b60712d748f652c0db4aef51bd6d368f9273c283f0eac819f355f</a></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685806087180/23ec8b0e-af51-4418-94d4-ff2c1a2d00eb.png" alt class="image--center mx-auto" /></p>
<p><em><mark>Congrats, you're now a smart contract developer!</mark></em></p>
<p><strong><em>Conclusion</em></strong><br />Mantle network is an L2 chain with faster finalty and cheaper gas fees and is governed by $BIT DAO. To learn more about Mantle network, visit<a target="_blank" href="https://www.mantle.xyz/">https://www.mantle.xyz/</a></p>
]]></content:encoded></item></channel></rss>