<?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: Independence DEV</title>
    <description>The latest articles on DEV Community by Independence DEV (@independencedev).</description>
    <link>https://dev.to/independencedev</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%2F874342%2Fd98564bf-1f54-4c97-9c2f-0a6f6dd44de3.png</url>
      <title>DEV Community: Independence DEV</title>
      <link>https://dev.to/independencedev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/independencedev"/>
    <language>en</language>
    <item>
      <title>Hardhat : Deployment and Upgradable Contract 👷</title>
      <dc:creator>Independence DEV</dc:creator>
      <pubDate>Sun, 24 Jul 2022 07:50:00 +0000</pubDate>
      <link>https://dev.to/independencedev/hardhat-deployment-and-upgradable-contract-3aij</link>
      <guid>https://dev.to/independencedev/hardhat-deployment-and-upgradable-contract-3aij</guid>
      <description>&lt;p&gt;Deploying your smart contracts in a professional way is important to maintain them properly. Hardhat is the perfect solution for that!&lt;/p&gt;

&lt;h1&gt;
  
  
  Installation &amp;amp; configuration
&lt;/h1&gt;

&lt;p&gt;To install and initialize a HardHat project, use these commands :&lt;br&gt;
&lt;code&gt;npm install hardhat&lt;br&gt;
npx hardhat&lt;/code&gt;&lt;br&gt;
You will have a file "Greeter.sol" in the "contracts" folder.&lt;br&gt;
You can compile this Smart Contract with :&lt;br&gt;
&lt;code&gt;npx hardhat compile&lt;/code&gt;&lt;br&gt;
In the "hardhat.config.js" file we will set the path of the artfifacts (Smart contracts compiled) and the BNB Smart chain testnet network for the future deployments.&lt;br&gt;
&lt;code&gt;paths: {&lt;br&gt;
    artifacts: './src/artifacts',&lt;br&gt;
  },&lt;br&gt;
  networks: {&lt;br&gt;
    bsctestnet: &lt;br&gt;
    {&lt;br&gt;
      url: "https://speedy-nodes-nyc.moralis.io/c96c432c75339d23af5d7364/bsc/testnet",&lt;br&gt;
      chainId: 97,&lt;br&gt;
        accounts: [secret.key]&lt;br&gt;
    }&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
In the "secret.json" file I put the private key of my wallet.&lt;br&gt;
&lt;code&gt;{&lt;br&gt;
    "key": "myPrivateKey"&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Deployment of a smart contract
&lt;/h1&gt;

&lt;p&gt;To deploy the Smart Contract, we will have to create a script in the "scripts" folder. We have the "sample-script.js".&lt;br&gt;
&lt;code&gt;const hre = require("hardhat");&lt;br&gt;
async function main() {&lt;br&gt;
  const Greeter = await hre.ethers.getContractFactory("Greeter");&lt;br&gt;
  const greeter = await Greeter.deploy("Hello, Hardhat!");&lt;br&gt;
  await greeter.deployed();&lt;br&gt;
  console.log("Greeter deployed to:", greeter.address);&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
Use this command to launch it : &lt;br&gt;
&lt;code&gt;npx hardhat run scripts/sample-script.js --network bsctestnet&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Interaction with the smart contract
&lt;/h2&gt;

&lt;p&gt;Now we deployed the Smart Contract and have the Address on the Blockchain, so we can interact with it.&lt;br&gt;
Example of script to set new value :&lt;br&gt;
&lt;code&gt;const hre = require("hardhat");&lt;br&gt;
async function main() {&lt;br&gt;
    const GreeterContract = await ethers.getContractFactory("Greeter");&lt;br&gt;
    const Greeter = await GreeterContract.attach("0xC9f9f144f58d590B136D526d43E9eDE8aa24Cd91");&lt;br&gt;
    const greet = await Greeter.setGreeting("Nouvelle valeur");&lt;br&gt;
    const newValue = await Greeter.greet();&lt;br&gt;
    console.log(newValue);&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Upgradable Smart Contract
&lt;/h1&gt;

&lt;p&gt;A Smart Contract is immutable but we can use proxy to solve this problem. HardHat provide all the tools to do it easily.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deployment with proxy
&lt;/h2&gt;

&lt;p&gt;Script to deploy a Smart Contract with a proxy.&lt;br&gt;
&lt;code&gt;const { ethers, upgrades } = require("hardhat");&lt;br&gt;
async function main() {&lt;br&gt;
  const Greeter = await ethers.getContractFactory("Greeter");&lt;br&gt;
  const greeter = await upgrades.deployProxy(Greeter, ["Hello, Hardhat!"]);&lt;br&gt;
  await greeter.deployed();&lt;br&gt;
  console.log("Greeter deployed to:", greeter.address);&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Upgrade with proxy
&lt;/h2&gt;

&lt;p&gt;Script to change the implementation of the Smart Contract behind the proxy. (Need the proxy address)&lt;br&gt;
&lt;code&gt;const { ethers, upgrades } = require("hardhat");&lt;br&gt;
async function main() {&lt;br&gt;
  const Greeter = await ethers.getContractFactory("Greeter");&lt;br&gt;
  const greeter = await upgrades.upgradeProxy("0xC9f9f144f58d590B136D526d43E9eDE8aa24Cd91", Greeter);&lt;br&gt;
  console.log("Greeter upgraded");&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Verify Smart Contract
&lt;/h1&gt;

&lt;p&gt;To verify a Smart Contract on the explorer (bscscan.com) we need to install the package "@nomiclabs/hardhat-etherscan" and get an API key from BscScan.&lt;br&gt;
Add this code in the "hardhat.config.js"  :&lt;br&gt;
&lt;code&gt;etherscan: {&lt;br&gt;
    apiKey: {&lt;br&gt;
      bsctestnet: "yourApiKey"&lt;br&gt;
    }&lt;br&gt;
  }&lt;/code&gt;&lt;br&gt;
And you just have to launch this command :&lt;br&gt;
&lt;code&gt;npx hardhat verify --network bsctestnet&lt;/code&gt;&lt;/p&gt;

</description>
      <category>web3</category>
      <category>ethereum</category>
      <category>solidity</category>
      <category>hardhat</category>
    </item>
    <item>
      <title>Create connect wallet button with React + Web3.js + Bootstrap</title>
      <dc:creator>Independence DEV</dc:creator>
      <pubDate>Thu, 09 Jun 2022 12:30:04 +0000</pubDate>
      <link>https://dev.to/independencedev/create-connect-wallet-button-with-react-web3js-bootstrap-d40</link>
      <guid>https://dev.to/independencedev/create-connect-wallet-button-with-react-web3js-bootstrap-d40</guid>
      <description>&lt;p&gt;I will teach you how to create a button that will interact with MetaMask to let the user connect their wallet on your app.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-react-app wallet-connect&lt;br&gt;
cd wallet-connect&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Check the react-scripts version in package.json, the version need to be 4.0.3 to be compatible with web3.js&lt;/p&gt;

&lt;p&gt;Run this command line to install everything we need :&lt;br&gt;
&lt;code&gt;yarn add web3 react-bootstrap bootstrap react-bootstrap-icons&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;add this line in index.js :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'bootstrap/dist/css/bootstrap.css';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Final App.js file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import Web3 from "web3";
import { Button } from 'react-bootstrap';
import { Wallet } from 'react-bootstrap-icons';
import './App.css';

class App extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      web3: null,
      currentAccount: null,
      ethBalance: 0,
      networkId: null
    };
  }

  componentDidMount = async () =&amp;gt; {
    this.connectWalletHandler();
  };

  connectWalletHandler = async () =&amp;gt; {
    try {
      const web3 = new Web3(Web3.givenProvider);
      this.setState({ web3: web3});
      const accounts = await web3.eth.requestAccounts();
      if (accounts.length !== 0) {
        const networkId = await web3.eth.net.getId();
        const ethBalance = await web3.eth.getBalance(accounts[0]);
        this.setState({currentAccount: accounts[0], ethBalance: Web3.utils.fromWei(ethBalance), networkId: networkId});
      } 
    } catch (error) {
      console.error(error);
    }
  }

  connectedWallet = () =&amp;gt; {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;p&amp;gt;{parseFloat(this.state.ethBalance).toFixed(4)} ETH&amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;{this.state.currentAccount.slice(0,5)+'...'+this.state.currentAccount.slice(38,42)}&amp;lt;br /&amp;gt;WALLET CONNECTED&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    )
  }

  connectWalletButton = () =&amp;gt; {
    return (
      &amp;lt;Button variant="outline-primary" onClick={() =&amp;gt; this.connectWalletHandler()}&amp;gt;&amp;lt;Wallet /&amp;gt;&amp;amp;ensp;Connect wallet&amp;lt;/Button&amp;gt;
    )
  }

  render() {
    return (
      &amp;lt;div className="App"&amp;gt;
         {this.state.currentAccount ? this.connectedWallet() : this.connectWalletButton()}
      &amp;lt;/div&amp;gt;
    );
  }
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>web3</category>
      <category>bootstrap</category>
      <category>metamask</category>
    </item>
  </channel>
</rss>
