go to  ForumEasy.com   
JavaPro  
 
 
   Home  |  MyForum  |  FAQ  |  Archive    You are not logged in. [Login] or [Register]  
Forum Home » Blockchain » Smart Contract --II: Deploy via 3 Ways
Email To Friend  |   Set Alert To This Topic Rewarding Points Availabe: 0 (What's this) New Topic  |   Post Reply
Author Topic: Smart Contract --II: Deploy via 3 Ways
WebSpider
member
offline   
 
posts: 147
joined: 06/29/2006
from: Seattle, WA
  posted on: 06/07/2021 06:59:15 AM    Edit  |   Quote  |   Report 
Smart Contract --II: Deploy via 3 Ways

There are three ways to deploy a smart contract to Ethereum blockchain:

  • Way 1: Via Remix IDE with Built-In/Injected Web3 provider + MetaMask wallet
  • Way 2: Via Truffle Framework with HDWProvider + any wallet
  • Way 3. Via Web3js script with HDWProvider + any wallet

    Here, we use the 3rd way: Via Web3js script with HDWProvider + any wallet

    Prerequisites:
  • Node.js installed
  • Wallet to authenticate to Testnet or Mainnet to make transaction (including deploy)
  • A contract with abi and bytecode to be deployed
  •  Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 06/07/2021 07:01:03 AM    Edit  |   Quote  |   Report 
    Step 1: Install Packages

    npm install web3 --save
    npm install dotenv --save
    npm install truffle-hdwallet-provider --save
    


    The package dotenv is used for storing your credentials like API_KEY or private keys

    The package truffle-hdwallet-provider is used to connect your wallet to blockchain network

     Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 06/07/2021 07:02:17 AM    Edit  |   Quote  |   Report 
    Step 2: Create the .env File
    PROJECT_ID=<Infura project id>
    PROJECT_SECRET=<Infura project secret -- optional>
    PRIVATE_KEY=<Ethereum account's private key>
    


     Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 06/07/2021 07:03:51 AM    Edit  |   Quote  |   Report 
    Step 3: Connect to Ethereum blockchain network
    	const HDWalletProvider = require("truffle-hdwallet-provider");
    	const provider = new HDWalletProvider(process.env.PRIVATE_KEY, 
                            "https://ropdten.infura.io/v3/"+process.env.PROJECT_ID);
    	const web3_conn = new Web3(provider);
    

     Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 06/07/2021 07:05:27 AM    Edit  |   Quote  |   Report 
    Step 4: Prepare the contract for deployment
    	var CONTRACT = require("./CONTRACT");
    	const ABI = CONTRACT.ABI;              // from complied contract's "abi"
    	const BYTECODE = CONTRACT.BYTECODE;    // from complied contract's "data.bytecode.object"
            const CONTRACTOR_ARGS = []; 
    
            let contract = new web3_conn.eth.Contract(ABI);  
        
            contract = contract.deploy({
                data: BYTECODE,
                arguments: CONTRACTOR_ARGS
            });
    

     Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 06/07/2021 07:07:41 AM    Edit  |   Quote  |   Report 
    Step 5. Deploy the contract

        	// account used for connection    
    	let accounts = await web3_conn.eth.getAccounts();
    
    	// deploy
    	const newContractInstance = await contract.send({
                from: accounts[0],
                gas: 1500000,
                gasPrice: '30000000000'
            });
    		
            console.log('Contract deployed at address: ', newContractInstance.options.address);
    

     Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 06/07/2021 07:10:59 AM    Edit  |   Quote  |   Report 
    Step 6. Make sure the contract is deployed on the blockchain

    After successful deployment of your contract, you should see something similar to the following:
    Contract deployed at address:  0x01bf16cfCA22a5CeDa1291DD9512a1BF2A1A44A9
    


    For further verification, you can look up your contract on https://ropsten.etherscan.io/
    ...
    From:                    0x7b6ee9541a2741a9f2631fbed9ffc532746be2de 
    To:                      Contract 0x01bf16cfca22a5ceda1291dd9512a1bf2a1a44a9Created 
    Value:                   0 Ether ($0.00)
    Transaction Fee:         0.00510195 Ether ($0.00)
    Gas Price:               0.00000003 Ether (30 Gwei)
    Gas Limit:               1,500,000
    Gas Used by Transaction: 170,065 (11.34%)
    Nonce Position:          0
    Input Data:              0x608060405234801561001057600... 
    


    It should be noted:
  • The miners sucked out all 30 Gwei for gasPrice set for this transaction, even though the average gasPrice for Ropsten is 2 Gwei at this point.
  • The miners honored how much gas should be burned to execute the transaction, but they won't be mercy to take all of your gas and fail your transaction if your gasLimit is reached in the middle of execution.
  •  Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 06/07/2021 07:14:08 AM    Edit  |   Quote  |   Report 
    Step 7. GasPrice vs GasLimit vs GasUsedStep

  • gasPrice -- It varies around 30 Gwei depending on the demand and supply on the blockchain. It's irrelvent to your transaction's size and complexity. But if you want your transaction to be mined into chain faster, you better set a higher gas price for your transaction.
  • gasLimit -- It is limit which varies from 20,000 to 1,000,000 depending on your transaction's size and complexity. You can estimate a gas and then set a limit to cap the gas to burn. Of course, your transaction could fail due to out of gas, if you under-estimated your gasLimit.
  • gasUsed -- It is the gas actually used by miners to execute your transaction.

    	// estimate gas cost
    	let deploy_estimateGas = await web3_conn.eth.estimateGas( {
    					data: BYTECODE,
    					from: accounts[0]});
            console.log('Deploy estimateGas: ', deploy_estimateGas);
    


    Output:
    Deploy estimateGas:  170065
    


    It should be noted that the estimated value 170065 is the exact number actually used by the blockchain for this case.
  •  Profile | Reply Points Earned: 0

     
    Powered by ForumEasy © 2003-2005, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.