AbhyasKanaujia / hello-smart-contract-alchemy-tutorial

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hello World Alchemy

Following the official tutorial in the Alchemy GitHub. This tutorial is not available on the official docs website but only on GitHub.

Using Alchemy

  1. Alchemy and Metamask setup
  2. Initialize a project
  3. Write a contract in contracts called HelloWorld.sol
  4. Connect Alchemy and Metamask to the project
  5. Compile the contract
    • $ truffle compile
  6. Create a new file in migrations called 2_deploy_contracts.js
  7. Add the contents of the file file here: 2_deploy_contracts.js
  8. Deploy the contract to the blockchain
    • $ truffle migrate --network goerli

Go Back to steps

Now you have a working contract that is deployed on Alchemy. Next you should make a frontend for this. Follow this tutorial on creating a frontend for your smart contract.

Alchemy and Metamask setup

  1. Signup for an acount on Alchemy
  2. Create an app in Alchemy
  3. Switch Metamask to Goerli (or wherever the app is)
  4. Add ether from a faucet
  5. Test connection by checking the balance in Alchemy Composer

Go Back to steps

Initialize a project

  1. Create a folder for the project
  2. Open the project in VS Code
  3. Open ternimal
    • `ctrl + `` (ctrl + backtick)
  4. Initialie a project using
    • $ npm init -y
  5. $ Check if truffle is installed on your system
    • npm list -g
  6. If not installed then install
    • $ npm i -g truffle
  7. Create a new truffle project
    • $ truffle init
  8. Install wallet provider to connect to Alchemy
    • $ npm install @truffle/hdwallet-provider

Go Back to steps

Connect Alchemy and Metamask to the project

  1. IMPORTANT Create a .gitignore and add .env
  2. npm i dotenv
  3. To protect your secret keys add a .env file in the root directory.
  4. Copy the content for .env from the format given below
  5. Go to the Alchemy dashboard
  6. Click on "View Key" for the Driver Registration app
  7. Copy the "HTTPS" link
  8. Replace API_URL in .env
  9. Follow this official tutorial by MetaMask on How to reveal your Secret Recovery Phrase
  10. Copy the 12-word phrase
  11. Replace MNEMONIC in .env
  12. Save .env
  13. Open truffle_config.js
  14. Delete all the contnets of this file
  15. Replace the file with the contnet fo this file: truffle_config.js

Go Back to steps

Files

HelloWorld.sol

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract HelloWorld {
    string public message;

    constructor(string memory _message) {
        message = _message;
    }

    function updateMessasge(string memory _message) public {
        message = _message;
    }
}

Go Back to steps

.env

API_URL = "https://eth-ropsten.alchemyapi.io/v2/your-api-key"
MNEMONIC = "your-metamask-seed-phrase"

Go Back to steps

truffle-config.js

require('dotenv').config()
const HDWalletProvider = require('@truffle/hdwallet-provider')
const { API_URL, MNEMONIC } = process.env

module.exports = {
  networks: {
    development: {
      host: '127.0.0.1',
      port: 7545,
      network_id: '*',
    },
    goerli: {
      provider: function () {
        return new HDWalletProvider(MNEMONIC, API_URL)
      },
      network_id: 5,
      gas: 4000000, //4M is the max
    },
  },
  compilers: {
    solc: {
      version: '0.8.0',
    },
  },
}

Go Back to steps

2_deploy_contracts.js

const HelloWorld = artifacts.require('HelloWorld')
const initMessage = 'Hello world!'

module.exports = function (deployer) {
  deployer.deploy(HelloWorld, initMessage)
}

Go Back to steps

About


Languages

Language:JavaScript 70.3%Language:Solidity 29.7%