A simple project to learn more about developing smart contracts on polygon.

Overview

polygon-books-hardhat

A simple project to learn more about developing smart contracts using Solidity.

Local Development Environment Setup

Install all required dependencies using NPM: npm install

Configure MetaMask to connect to the Polygon Mumbai Testnet

Setting up MetaMask to connect to the Polygon Mumbai Testnet is useful for a few reasons:

  • It makes it easy to interact with faucets, to obtain testnet MATIC and LINK
  • Once the contract is verified on PolygonScan, it will make it easy to invoke functions exposed by your smart contract.

For security purposes, it's highly recommended to setup a fresh MetaMask account. This is because we'll be exporting the private key of your MetaMask wallet once connected to the testnet, this private key will be identical to the private key for your mainnet wallet. If your MetaMask account is holding any cryptocurrency, you don't want to risk leaking your private key, which a bad actor could use to access your funds!

Before creating a brand new MetaMask account, ensure you securely store your seed phrase, which you will need if you ever want to restore your old account.

Navigate here and select Mumbai-Testnet and follow the instructions to add the Mumbai-Testnet to MetaMask.

Follow these instructions to export your account's private key.

Populate your private key in your .env file (PRIVATE_KEY).

Obtain some testnet MATIC and LINK

To deploy to the Polygon testnet you will need some MATIC tokens on the testnet. The smart contract in this project makes use of the Chainlink VRF, making use of the Chainlink VRF will cost LINK, which is why you will need some LINK tokens on the testnet.

Visit this polygon faucet to request LINK and MATIC testnet tokens.

Once the tokens arrive, they should be visible in your MetaMask account.

Obtain a PolygonScan API key

To update token information on PolygonScan, the token contract address must be verified. The process of verification ensures the contract code is exactly what is being deployed to the blockchain, and also allows the public to audit and read the contract.

PolygonScan ensures that, before a contract can be updated with information submitted by the contract owner, the contract must first be verified.

Sign up for an account on PolygonScan.

Once signed up, navigate to your account and obtain an API Key.

Populate your API key in your .env file (POLYGONSCAN_API_KEY).

Once this is done, you'll be easily able to verify your contract using the npx hardhat verify command.

Deploying the smart contract

When deploying our smart contract, we may deploy to our local blockchain, or to the polygon testnet.

Deployment to a local blockchain

To deploy the contract locally, simply execute:

npx hardhat deploy

This will compile and deploy the smart contract to your local hardhat network. The local hardhat network is an in-process local blockchain, an ephemeral ethereum network that is created and destroyed with the process. It has no memory (i.e each time you run a deployment like this, a fresh network is started up) and is destroyed after the deployment. This means we won't be able to interact with our smart contract after this command is done executing. The hardhat network is very quick, and good for running tests and checking if our contracts deploy successfully, but if one wants to actually interact with the deployed contract, its better to deploy to the localhost network (not to be confused with the hardhat network!).

Executing the command to spin up a local node will automatically run the deployment scripts in the deployments folder, so we won't have to execute a npx hardhat deploy.

This spin up a local node, execute:

# optionally supply the `no-deploy` flag to prevent any deployments from executing
npx hardhat node

To manually deploy to this local node, in another terminal, execute:

npx hardhat deploy --network localhost

At this point, if you check in on your node, you'll see some logs indicating that the contract was deployed. You can now interact with your contract using the hardhat console:

npx hardhat console --network localhost

// You can retrieve the address of your contract by inspecting the deployment or node logs.
const contractAddress = '0x5fbdb2315678afecb367f032d93f642f64180aa3';

// retrieve an instance of our deployed contract we can interact with
const contract = await ethers.getContractAt("CryptoBooks", contractAddress);

// Retrieve how many tokens (minted by the contract) belong to the given address
balance = await contract.balanceOf('<address_here>');

TODO: Update this example & ensure it works with the new transparent upgradeable proxy.

TODO: Document mocking of Chainlink VRF Coordinator and Link Token in local environments

Deployment to a remote blockchain (testnet)

Before deploying to the testnet, ensure you have some testnet MATIC; since we are modifying the state of the polygon testnet, we will need to spend some testnet MATIC.

Note that the account deploying this contract will be the one corresponding to your private key stored in your .env file.

To deploy to the testnet execute:

npx hardhat deploy --network mumbai

You're now free to interact with the contract as described in Deployment to a local blockchain by spinning up a hardhat console:

npx hardhat console --network mumbai

Verifying our contract

Note: Not updated yet for verification of proxy/implementation contracts.

If we want to be able to browse the source code of our contract on PolygonScan (and not just view the bytecode) we will need to verify the contract using PolygonScan's API. Doing so will also let us interact with all of our contract's functions via MetaMask.

To verify the contract, execute the following command:

npx hardhat verify --network mumbai <contract address> <... parameters provided to contract constructor at deploy time>

For example, to verify the contract in this repository you would execute:

npx hardhat verify --network mumbai <contract address> 0x8C7382F9D8f56b33781fE506E897a4F1e2d17255 0x326C977E6efc84E512bB9C30f76E30c160eD06FB 0x6e75b569a01ef56d18cab6a8e71e6600d6ce853834d4a5748b720d06f878b3a4

TODO: Update verification instructions to discuss verification of proxy, proxy admin and implementation.

TODO: This verification process should be easily automatable (prevent having to provide parameters manually)

TODO: Document test execution and mock configuration

TODO: Document custom VRFConsumerBase needed for upgrade functionality

TODO: Document custom TransparentUpgradeableProxy constructor, used for compatibility with hardhat deploy

You might also like...

Context-aware smart contracts for blockchain IoT systems

Context-aware smart contracts for blockchain IoT systems It contains 2 directories: contracts: contains the ContextAwareSmartContract.sol contract wit

Jun 17, 2022

This is the main repository for NFT collection dao smart contracts.

Basic Sample Hardhat Project This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, a sample s

Apr 11, 2022

Yet another library for generating NFT artwork, uploading NFT assets and metadata to IPFS, deploying NFT smart contracts, and minting NFT collections

eznft Yet another library for generating NFT artwork, uploading NFT assets and metadata to IPFS, deploying NFT smart contracts, and minting NFT collec

Sep 21, 2022

πŸš€ NFTank (NFT tank for dummies) will allow developers to quickly request NFTs to personal wallets or smart contracts in just a few clicks.

πŸš€ NFTank (NFT tank for dummies) will allow developers to quickly request NFTs to personal wallets or smart contracts in just a few clicks.

πŸ‘·β€β™‚οΈ NFTank πŸš€ NFTank (NFT tank for dummies) will allow developers to quickly request NFTs to personal wallets or smart contracts in just a few click

Nov 8, 2022

Toolkit for development, test and deploy smart-contracts on Waves Enterprise ecosystem.

JS Contract SDK Toolkit for development, test and deploy smart-contracts on Waves Enterprise ecosystem. Quickstart The fastest way to get started with

Dec 15, 2022

Cardinal generator encompasses serverless functions and smart contracts for rendering generative NFTs

Cardinal generator encompasses serverless functions and smart contracts for rendering generative NFTs

Cardinal Generator An open protocol for generative NFTs. Background Cardinal generator encompasses serverless functions and smart contracts for render

Dec 6, 2022

A Typescript Hardhat-based template to develop evm-based smart contracts with all the tooling you need.

EVM-based Smart Contract Scaffold A Typescript Hardhat-based template to develop evm-based smart contracts with all the tooling you need. Features Use

Oct 24, 2022

Elrond blockchain CLI helper tools - interaction with APIs, smart contracts and protocol

Buildo Begins πŸ‘· Meet Buildo. He is here to help you start creating in the Elrond blockchain ecosystem. Here is where everything begins. I'm going on

Dec 30, 2022

πŸ¦ πŸ”¬ Forta agent that detect deployment of smart contracts containing an exploit function

Attack Simulation Bot Description The agent detects deployment of smart contracts containing an exploit function. Using a simulation-based approach, t

Dec 26, 2022
Comments
  • Manually send LINK instead of using `fund-link` task

    Manually send LINK instead of using `fund-link` task

    The fund-link task produces logs that clutter up the logs when we are testing. Removes the usage of fund-link in the tests, and replaces this with a manual interaction with the LinkToken contract.

    Modify the tsconfig.json file so it inspects the types present in typechain-types. This resolves an issue with the ethers function getContractAt, where it was resolving the return type as a generic Contract instead of a concrete contract type.

    opened by LionelB5 0
  • Test contract & mock LINK dependencies

    Test contract & mock LINK dependencies

    Adds some basic tests that utilise mocks to mock out the Chainlink VRF. Also sets up test to environment to contain the LINK contract.

    Some aspects of the contract are yet to be tested.

    The mocking of the Chainlink VRF and creation of the LINK token contract is also done when spinning up local environment. This is useful because in a local environment, we don't have access to an actual Chainlink oracle, nor does the LINK token contract come installed in our local environment.

    To assist in achieving replicable deployments and easier to write as well as speedier tests, switch to using the hardhat-deploy plugin.

    opened by LionelB5 0
Owner
Lionel Bersee
Lionel Bersee
a quick start boilerplate for developing web3 apps and deploying smart contracts.

create-web3 A boilerplate for starting a web3 project. This boilerplate quickly creates a mono repo with 2 environments, a Next JS environment for fro

Eric Roupe 83 Dec 16, 2022
♦ Crowd funding project using Smart Contracts on the Ethereum. Created with Next.js and Tailwind CSS.

Crowdcoin ♦ Crowd funding project using Smart Contracts on the Ethereum. Created with Next.js and Tailwind CSS. Project from "Ethereum and Solidity: T

Luiz Fernando VerΓ­ssimo 2 Dec 14, 2022
This is a CI/CD and version controlling tool for smart contracts which is an award winning project built for ETHPrague Hackaton.

Inspiration Alfred is built-in version control and CI/CD system for your smart contracts. It uses proxy contract and a DAO to upgrade or downgrade you

Mete Karasakal 10 Aug 30, 2022
🌸 A fast and fun way to learn Japanese alphabets: hiragana & katakana. Don't wait - learn now!

Sakurator | Start learning ζ—₯本θͺž here Sakurator (Website publish date: ~4-6 April '22) - a personal trainer for learning Japanese alphabets (hiragana &

Anatoly Frolov 3 Jun 22, 2022
This project aims for Road to web3 Hackathon powered by Polygon

BlogStream A blog site where users directly pay the writers for only what they are reading This is a project created for Road to Web3 hackathon by Web

WebX DAO 11 Sep 12, 2022
A collection of smart contracts for the Stackup platform πŸ€– πŸ“‘

Contracts A collection of smart contracts for the Stackup platform. Dev Blog Deployed Contracts See releases for deployed contracts of previous versio

Stackup 16 Nov 29, 2021
Ethereum Smart Contracts for locking your Ether and ERC20 tokens based on time and price conditions

SmartHold - a simple way to lock and hold your ETH or ERC20 in a smart contract This is a BETA software that has not been audited for security. USE AT

PaweΕ‚ Urbanek 22 May 5, 2022
Cooperative databases using smart contracts.

Vitra β–ˆβ–ˆβ•— β–ˆβ–ˆβ•—β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β•šβ•β•β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β•šβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆ

Paul Frazee 177 Dec 30, 2022
Run CosmWasm smart contracts in Node.js and web browsers

Run CosmWasm smart contracts in Node.js and web browsers

Terran One 31 Nov 25, 2022
A 'to do list' powered by Ethereum smart contracts

A 'to do list' powered by Ethereum smart contracts. built with JavaScript, Ganache, Truffle, Node.js, Metamask, jQuery, and Bootstrap

Christotle Agholor 3 Feb 17, 2022