Skip to content

Latest commit

 

History

History
217 lines (140 loc) · 9.41 KB

File metadata and controls

217 lines (140 loc) · 9.41 KB

⛓ Deploying Smart Contracts

⏱ Agenda {docsify-ignore}

[02m] 🏆 Objectives

  1. Describe the process required to obtain test Ether in order to deploy a Smart Contract on a test network.
  2. Identify the different test networks and the use cases for each.
  3. Deploy a Smart Contract to a test network.

[03m] 🤔 Why You Should Know This

Alongside automated test cases, it’s important to run your ÐApp on a test network before deploying it to Mainnet: Mainnet deployment costs real money; it’s better to test on a low-stakes, "fake" network first.

[20m] ☀️ Warm Up: Set Up Testnet Access

Note

What is Infura? Infura provides tools and infrastructure that make it quick, easy, and cost-effective for developers to connect to Ethereum and IPFS and start building awesome decentralized applications. No syncing required. No complex set-ups. Just your decentralized app, live and functioning, right now.

Instructions

  1. Sign up for Infura.

  2. Create a new project (name it anything you want --- you can delete it later).

  3. Find the project ID and write it down in your notes for use later on in today's activities.

  4. Use the video below if you get stuck:

    <iframe width="560" src="https://www.youtube.com/embed/z-lRuKBimW8" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

[30m] 📖 Overview: Test Networks

Testnets are copies of the Ethereum blockchain almost identical in every way to Mainnet --- except in the fact that their Ether is worthless.

In addition to mainnet, there are public testnets. These are networks used by protocol developers or smart contract developers to test both protocol upgrades as well as potential smart contracts in a production-like environment before deployment to mainnet. Think of this as an analog to production versus staging servers.

It’s generally important to test any contract code you write on a testnet before deploying to the mainnet. If you're building a dapp that integrates with existing smart contracts, most projects have copies deployed to testnets that you can interact with.

Most testnets use a proof-of-authority consensus mechanism. This means a small number of nodes are chosen to validate transactions and create new blocks – staking their identity in the process. It's hard to incentivize mining on a proof-of-work testnet which can leave it vulnerable.

Existing Test Networks

Mainnet

Mainnet is the primary public Ethereum production blockchain, where actual-value transactions occur on the distributed ledger.

When people and exchanges discuss ETH prices, they're talking about mainnet ETH.

Ropsten

  • Last public proof-of-work testnet
  • Last testnet that is available across different client implementations
  • Specifications closest to the Ethereum main network
  • Kept alive to test contracts and DApps before they are deployed to mainnet

Rinkeby

  • Second proof-of-authority network
  • Also launched in response to the Ropsten spam attacks
  • However, instead of Aura, it's using Geth's Clique proof-of-authority engine which differs in its specification.

Görli

[20m] 💻 Activity: Get Gas Money

This Ether faucet is running on the Rinkeby network. To prevent malicious actors from exhausting all available funds or accumulating enough Ether to mount long running spam attacks, requests are tied to common 3rd party social network accounts. Anyone having a Twitter or Facebook account may request funds within the permitted limits.

Instructions

  • First, copy your Ethereum address from MetaMask.
  • To request funds via Twitter:
    • Make a tweet with your MetaMask Ethereum address pasted into the contents (surrounding text doesn't matter).
    • Copy-paste the tweet URL into the input box at the top and press Give Me Ether.
  • To request funds via Facebook:
    • Publish a new public post with your MetaMask Ethereum address embedded into the content (surrounding text doesn't matter).
    • Copy-paste the post URL into the input box at the top and press Give Me Ether.

[15m] 🌴 BREAK

[25m] 💻 Activity: Import Account to Brownie

Import Instructions

  1. Copy your private key in MetaMask:

    • Click Triple Dots in header > Click Account Details in dropdown > Click Export Private Key button.
    • Copy private key to clipboard.
    • Open your Terminal.
  2. Run the following command to import your MetaMask account:

    $ brownie accounts new metamask
    Brownie v1.11.3 - Python development framework for Ethereum
    
    Enter the private key you wish to add: PASTE METAMASK PRIVATE KEY HERE
    Enter the password to encrypt this account with: PASTE METAMASK PASSWORD HERE
    SUCCESS: A new account '0xe4233b38fEa3B8c27ea9F54d5A90ec27cEe7F42C' has been generated with the id 'metamask'
  3. You will be asked to input the private key --- paste it and hit .

  4. You will be asked to input a password --- paste the same password you use for MetaMask, and hit .

  5. The account will then be available as metamask.

Discussion

Why do you think we imported an existing account instead of creating a new one?

[30m] 📖 Overview: Deployment

  1. If missing inside the contracts folder, add Token.sol to the project again: code here.

  2. Select a testnet. In class, we'll use Rinkeby --- where we just received funds from the faucet.

  3. Export WEB3_INFURA_PROJECT_ID for use by Brownie. Replace YOUR_PROJECT_ID with the one you copied during today's warmup:

    $ export WEB3_INFURA_PROJECT_ID=YOUR_PROJECT_ID
  4. Test the console using the Rinkeby network:

    $ brownie console --network rinkeby

[35m] Activity: Deployment Challenges

  1. Using the Brownie console, find your account and print out the balance.

    🤔 HINT: Use Accounts.load()!

  2. Using the code you just wrote, change the deployment script in deploy/token.py: to load your account, then use it to deploy the contract.

  3. Run brownie run token --network rinkeby to deploy and see if your modifications worked!

    🟢 If you receive output like this, it worked!

    $ Brownie v1.11.3 - Python development framework for Ethereum
    
    TokenProject is the active project.
    Enter the password to unlock this account:
    
    Running 'scripts/token.py::main'...
    Transaction sent: 0xea1ff5050a8ab83069d643a1c147753578f5be1267130dc1eab81e84c085cccf
     Gas price: 1.0 gwei   Gas limit: 568012
    Waiting for confirmation...
     Token.constructor confirmed - Block: 7243486   Gas used: 516375 (90.91%)
     Token deployed at: 0x7E1c0953A72659DD64d05f98dA302D93491E3601

    🔴 If you receive an error like below, keep working on the code. You haven't loaded an account successfully yet!

        File "brownie/network/account.py", line 101, in __getitem__
          return self._accounts[key]
      IndexError: list index out of range

📚 Resources & Credits

Infura

Test Networks

Brownie Docs