Skip to content

Create and deploy your own smart contract to mint a video NFT with Livepeer.

Notifications You must be signed in to change notification settings

camiinthisthang/livepeer-smart-contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

livepeer-smart-contract

Create and deploy your own smart contract to mint a video NFT with Livepeer.

Deploy Your Own ERC721 Contract - Housekeeping

If you don't want to use Livepeer's contract, you can deploy your own contract. One thing to note is that you must deploy your contract to the Polygon network in order to use the UI to mint your NFT.

We'll be walking through deploying an ERC721 Contract with Alchemy, an RPC provider which runs the node to give you access to write to the blockchain.

  1. Sign up for a free account with Alchemy here.
  2. Once signed in, create an app in the dashboard. For network, select Polygon Mumbai net so that you don't have to spend real money deploying your smart contract.

Once you're confident in your contract, you can repeat these steps and set it to Polygon mainnet.

Screen Shot 2022-03-10 at 8 29 49 PM

Screen Shot 2022-03-10 at 8 30 52 PM

3. If you havn't already, connect your Metamask wallet to the Mumbai Testnet. To do that, open Metamask and click on the dropdown of networks at the top of the screen. Go down to ```Add Network``` and fill in the following information:

Metamask Network Parameters

Network Name: Mumbai Testnet

New RPC URL: https://rpc-mumbai.maticvigil.com

Chain ID: 80001

Currency Symbol: MATIC

Block Explorer URL: https://polygonscan.com/

Screen Shot 2022-03-10 at 9 00 15 PM

Double check that the network was added to your wallet:

Screen Shot 2022-03-10 at 9 00 35 PM

  1. Get some fake MATIC from a facuet. In order to deploy your contract to the Polygon blockchain, your wallet address needs to have some MATIC to make the transaction. Because we're on the Mumbai Testnet, we'll use fake MATIC which you can get from a faucet like this one.
  • Copy your wallet address from Metamask

Screen Shot 2022-03-10 at 9 09 34 PM

- Select Mumbai Network, MATIC Token, and paste in your wallet address

Screen Shot 2022-03-10 at 9 09 21 PM

- Wait 1-2 mins for the MATIC to show up in your wallet

Screen Shot 2022-03-10 at 9 09 50 PM

Deploy Your Own ERC721 Contract - The Fun Part!

Now that we're all set up, we're ready to start coding our smart contract.

  1. In your terminal, navigate to your Desktop (or whatever directory you want to use to house this project's code.)
  2. Create a new folder and then move into that new directory by running the following commands, one after the other: mkdir project-name-here cd project-name-here
  3. Initalize your project: npm init
  4. Enter the information requested, but it doesn't really matter how you answer these. Approve the package.json at the end and then you're good to go!
  5. Download Hardhat, a devleopment envirtonment to compile and deploy your smart contracts by running this command in the terminal in your project directory: npm install --save-dev hardhat
  6. Create a Hardhat project by running the following command in your project directory: npx hardhat
  7. Select Create an empty hardhat.config.js

Screen Shot 2022-03-10 at 9 43 24 PM

8. Create a contracts and scripts folder in your project. This is where we will create the smart contract file as well as the scripts to test and deploy the smart contract. You can do this by running the following commands in your project directory, one after the other: ```mkdir contracts mkdir scripts ``` 9. Open this project in an IDE like VSCode and navigate to the contracts folder. Crate a new file, we'll call it videoNFT.sol
  1. Our smart contract code will be based on the OpenZepplin ERC721 implementation. To get access to the classe from OpenZepplin contracts, run the following command in the terminal in your project directory: npm install @openzeppelin/contracts@3.1.0-solc-0.7

  2. NEED TO FIGURE OUT HOW TO GET RID OF THIS ERROR, WHICH PACKAGE DO I NEED TO INSTALL TO GET ACCESS TO THESE CLASSES<

Screen Shot 2022-03-10 at 9 48 17 PM

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";

contract VideoNFT is ERC721URIStorage {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() ERC721("Video NFT", "VIDEO") {}

    event Mint(
        address indexed sender,
        address indexed owner,
        string tokenURI,
        uint256 tokenId
    );

    function mint(address owner, string memory tokenURI)
        public
        returns (uint256)
    {
        require(
            owner == msg.sender,
            "Can only mint NFT for yourself on default contract"
        );
        _tokenIds.increment();

        uint256 newItemId = _tokenIds.current();
        _mint(owner, newItemId);
        _setTokenURI(newItemId, tokenURI);

        emit Mint(msg.sender, owner, tokenURI, newItemId);
        return newItemId;
    }

    function uri(uint256 tokenId) public view returns (string memory) {
        return tokenURI(tokenId);
    }
}

Deploying Your Contract

Our contract is all done, now we have to deploy it. In order to perform any transaction on the blockchain, you need to sign the signature using your private key. Because we want to sign a tranaction from our smart contract, we need to give our program access to this key. You don't want to expose this private key to anyone when you push your project up to Github. We can give our app access to this key while protecting it by using something like Dotenv which loads a module that loads environemnt variables from a .env file.

  1. Install the dotenv package into your project by running the following command in the terminal: npm install dotenv --save
  2. Copy your app's HTTP URL API from Alchemy.

Screen Shot 2022-03-10 at 8 36 06 PM

  1. Export your private key from Metamask. Instructions for how to do that can be found here.
  2. Go to your newly created .env file in your project in your code editor and update the file to look like this:
API_URL = "https://polygon-mumbai.g.alchemy.com/v2/your-http-here"
PRIVATE_KEY = "your-metamask-private-key-here"
  1. Instal Ether.js, a library for interacting with the blockchain: npm install --save-dev @nomiclabs/hardhat-ethers "ethers@^5.0.0"
  2. Update your hardhat.config.js file to tell your hardhat environment how to find your private keys, as well as update our dependecies.
/**
* @type import('hardhat/config').HardhatUserConfig
*/
require('dotenv').config();
require("@nomiclabs/hardhat-ethers");
const { API_URL, PRIVATE_KEY } = process.env;
module.exports = {
   solidity: "0.7.3",
   defaultNetwork: "matic",
   networks: {
      hardhat: {},
      matic: {
         url: API_URL,
         accounts: [`0x${PRIVATE_KEY}`]
      }
   },
}
  1. Now that everything is configured, compile your contract by running this command in the terminal: npx hardhat compile
  2. Write the script to deploy your smart contract to the network. You can do this by creating afile inside the scripts folder called deploy.js
async function main() {
   // Grab the contract factory 
   const MyNFT = await ethers.getContractFactory("MyNFT");

   // Start deployment, returning a promise that resolves to a contract object
   const myNFT = await MyNFT.deploy(); // Instance of the contract 
   console.log("Contract deployed to address:", myNFT.address);
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });
  1. You're finally ready to execute this script and deploy your contract. You can do that by running the following command in your terminal: npx hardhat run scripts/deploy.js --network matic
  2. Once your contract is deployed, you'll get the address of where your contract lives. Copy this address and paste it in the Livepeer UI we walked through earlier.

About

Create and deploy your own smart contract to mint a video NFT with Livepeer.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published