How to implement Alchemy in your smart contract

How to implement Alchemy in your smart contract

Hey there, creating blockchain nodes is now made easy, using Alchemy!

Today we'd create a smart contract using hardhat, solidity, and alchemy :) This is a tutorial on how to implement Alchemy in your smart contract

Let's get Started!

What is Alchemy?

Screenshot from 2022-10-29 20.21.02.png Alchemy is a well-known blockchain node and API provider. Alchemy offers access to highly scalable, consistent, and reliable nodes. Alchemy also has developer tools used for prototyping, debugging, and testing.

Let's build our smart contract

codemodeon-code.gif

gimme-code-gimme.gif

Installation time

To get started we'll create a new folder. Paste the following command into your terminal

mkdir EtherWallet
cd EtherWallet

Now we'll install Hardhat in our EtherWallet folder:

npm init --yes
npm install --save-dev hardhat

You run:

npx hardhat

Make sure you select Create a Javascript Project and then follow the steps in the terminal to complete your Hardhat setup.

Once your project is set up, start by creating a new file inside the contracts directory called EtherWallet.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract EtherWallet {
    address payable public owner;

    constructor() {
        owner = payable(msg.sender);
    }

    receive() external payable {}

    function withdraw(uint _amount) external {
        require(msg.sender == owner, "caller is not owner");
        payable(msg.sender).transfer(_amount);
    }

    function getBalance() external view returns (uint) {
        return address(this).balance;
    }
}

We will deploy our contract to the goerli network. To do that, we'll create a new file and name it deploy.js or

const { ethers } = require("hardhat");

async function main() {
  const EtherWalletContract = await ethers.getContractFactory("EtherWallet");

  // here we deploy the contract
  const deployedWhitelistContract = await EtherWalletContract.deploy();

  // Wait for it to finish deploying
  await deployedEtherWalletContract.deployed();

  // print the address of the deployed contract
  console.log("Ether Wallet Contract Address:", deployedEtherWalletContract.address);
}

// Call the main function and catch if there is any error
main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

How to implement Alchemy in your smart contract

Now open the hardhat.config.js file, and replace all the lines in the file with the given lines below so that we can deploy our contract to the Goerli network.

require("@nomicfoundation/hardhat-toolbox");
require("@nomiclabs/hardhat-ethers");
require("@nomiclabs/hardhat-waffle");

module.exports = {
  solidity: "0.8.9",
  networks: {
    goerli: {
      url: ALCHEMY_HTTP_URL,
      accounts: [PRIVATE_KEY],
    },
  },
};

Huh? What happened here?

First off, what is the ALCHEMY_HTTP_URL and how can I get it?

  • You go to Alchemy
  • Create an account
  • Go to your dashboard
  • Create an App on the goerli network
  • Now you've created the app, you will be allowed to view key
  • You should paste your HTTP URL in the ALCHEMY_HTTP_URL space

Screenshot from 2022-10-28 21.58.37.png

Next, the Private key.

  • In your metamask, click on the three dots
  • There you will see account details
  • You copy the private key and paste it into the PRIVATE_KEY space

P.S: Do not share your private key with anyone!

Screenshot from 2022-10-28 22.02.40.png

Back to our code!

To compile our code, run this command:

npx hardhat compile

The compilation is done, let's Deploy:

npx hardhat run scripts/deploy.js --network goerli

Congratulations/Thank You

This is a congratulatory conclusion. Congratulations, you know what Alchemy is and you also know how to implement Alchemy in your smart contract. Thanks for using my Favorite tool and also, thanks for READING

200w.gif