Prompt Web3
  • 🤓Web3 Refresher Course with GptTutor
  • Creating your own Custom personas
  • Creating The Web3 GptTutor
  • Prompting the course sylibus
  • Prompting new lessons
  • Expanding parts of a lesson
  • Lesson 1
    • Lesson 1: Outline
    • Lesson 1: Assignments & Exercises
      • A1: Smart contracts in the real world
      • E1: HelloWorld.sol
  • Lesson 2
    • Lesson 2: Outline
    • Lesson 2: Assignments & Exercises
      • A1: Ethereum gas costs, data types and control structures - A brief take
      • E1: AssetManagement.sol
    • Lesson 2: Final notes and code repos
  • Lesson 3
    • Lesson 3: Outline
    • Lesson 3: Assignments & Excercises
      • A1 - Notes on View and Pure modifiers in Solidity
      • E1: EthWallet.sol
      • E1: Hardhat Tests
      • E1: NextJs Front End
    • Lesson 3: Final notes & Final project + repo
  • Lesson 4
    • Lesson 4: Outline
    • Lesson 4: Assignments & Exercises
      • A1 - Solidity error handling - notes on require, revert and assert
      • E1: EthWallet.sol - Update
      • E1 - EthWallet.test.js - Update
      • E1: NextJs Front End - Update
    • Lesson 4: Final notes & Final project + repo
  • Lesson 5
    • Lesson 5: Outline
    • Lesson 5: Assignments & Exercises
      • A1 - Using structs and mappings to manage complex data in Solidity
      • E1 - OrderSystem.sol
      • E1 - OrderSystem.test.js
    • Lesson 5: Final notes & Project Repo
  • LESSON 6
    • Lesson 6: Outline
    • Lesson 6: Assignments & Exercises
      • A1 - Solidity Interfaces - Facilitating interaction, modularity and Dapp upgradability
      • E1 - Ranked Choice Voting system
        • E1 - BallotContract.sol
        • E1 - IBallotContract.sol
        • E1 - VotingContract.sol
        • E1 - RCVotingSystem.test.js
      • E1 - Feedback
    • Lesson 6: Final notes & Project Repo
  • Lesson 7
    • Lesson 7: Outline
    • Lesson 7: Assignments & Exercises
      • E1 - Uniswap Flashswap dual Arbitrage Bot
        • E1 - FlashSwapV3.sol
        • E1 - dualArbScan.js
        • E1 - arbQuote.js
      • Lesson 7: Final notes & Project Repo
Powered by GitBook
On this page
  1. Lesson 2

Lesson 2: Final notes and code repos

PreviousE1: AssetManagement.solNextLesson 3: Outline

Last updated 1 year ago

Final notes

Aside from refreshing my mind about data types, control structures and operators, I decided it would be worth getting to know the Hardhat framework again. I wanted to really solidify in my mind how to properly deploy and test a smart contract. It is worth noting that I had delved into these things before on Patrick Collins' youtube channel.

The full repo can be found here -

// deploy.js

const hre = require("hardhat")
const { network } = require("hardhat")
const { developmentChains } = require("../helper-hardhat-config")

async function main() {
    if (developmentChains.includes(network.name)) {
        const AssetManagement =
            await hre.ethers.getContractFactory("AssetManagement")

        console.log("Deploying...")

        const assetManagement = await AssetManagement.deploy()
        console.log(`AssetManagement deployed to: ${assetManagement.target}`)
    }

    if (
        !developmentChains.includes(network.name) &&
        process.env.ETHERSCAN_API_KEY
    ) {
        const AssetManagement =
            await hre.ethers.getContractFactory("AssetManagement")

        console.log("Deploying...")

        const assetManagement = await AssetManagement.deploy()
        console.log(`AssetManagement deployed to: ${assetManagement.target}`)

        const desiredConfirmations = 2
        const receipt = await assetManagement
            .deploymentTransaction()
            .wait(desiredConfirmations)

        console.log(
            `Transaction confirmed. Block number: ${receipt.blockNumber}`,
        )
        await hre.run("verify:etherscan", { address: assetManagement.target })
        console.log("AssetManagement verified!")
        console.log("--------------------------------------------------")
    }
}

main().catch((error) => {
    console.error(error)
    process.exitCode = 1
})
// AssetManagement.test.js

const { assert, expect } = require("chai")
const hre = require("hardhat")
const { developmentChains } = require("../../helper-hardhat-config")

!developmentChains.includes(network.name)
    ? describe.skip
    : describe("AssetManagement", async function () {
          let assetManagement
          let assetDescription
          let assetValue
          let assetId

          beforeEach(async function () {
              ;[owner1, owner2, owner3] = await hre.ethers.getSigners()
              const AssetManagement =
                  await hre.ethers.getContractFactory("AssetManagement")
              assetManagement = await AssetManagement.deploy()
              await assetManagement.waitForDeployment()
          })

          describe("assetRegistration", async function () {
              it("Reverts if asset value is not above zero", async function () {
                  assetValue = 0
                  assetDescription = "asset description"
                  await expect(
                      assetManagement.assetRegistration(
                          assetValue,
                          assetDescription,
                      ),
                  ).to.be.revertedWith("Asset value must be above zero")
              })

              it("Reverts if asset does not have a description", async function () {
                  assetValue = 1
                  assetDescription = ""
                  await expect(
                      assetManagement.assetRegistration(
                          assetValue,
                          assetDescription,
                      ),
                  ).to.be.revertedWith("Asset must have a description")
              })

              it("Adds assets to mapping correctly", async function () {
                  const assetOneValue = 10
                  const assetTwoValue = 20
                  const assetOneDescription = "good description"
                  const assetTwoDescription = "another good description"

                  await assetManagement.assetRegistration(
                      assetOneValue,
                      assetOneDescription,
                  )
                  await assetManagement
                      .connect(owner2)
                      .assetRegistration(assetTwoValue, assetTwoDescription)

                  const assetOne = await assetManagement.getAsset(1)
                  // Must assert each part, because solidity structs do not equate equally to javascript arrays.
                  assert.equal(assetOne[0].toString(), "1")
                  assert.equal(assetOne[1].toString(), "10")
                  assert.equal(assetOne[2], true)
                  assert.equal(assetOne[3], owner1.address)
                  assert.equal(assetOne[4], "good description")

                  const assetTwo = await assetManagement.getAsset(2)

                  assert.equal(assetTwo[0].toString(), "2")
                  assert.equal(assetTwo[1].toString(), "20")
                  assert.equal(assetTwo[2], true)
                  assert.equal(assetTwo[3], owner2.address)
                  assert.equal(assetTwo[4], "another good description")
              })
          })

          describe("updateAsset", async function () {
              beforeEach(async function () {
                  assetValue = 10
                  assetDescription = "good description"
                  await assetManagement.assetRegistration(
                      assetValue,
                      assetDescription,
                  )
              })
              it("Reverts if asset does not exist", async function () {
                  assetId = 2
                  await expect(
                      assetManagement.updateAsset(
                          assetId,
                          assetValue,
                          assetDescription,
                      ),
                  ).to.be.revertedWith("Asset does not exist")
              })
              it("Reverts if caller is not asset owner", async function () {
                  assetId = 1
                  await expect(
                      assetManagement
                          .connect(owner2)
                          .updateAsset(assetId, assetValue, assetDescription),
                  ).to.be.revertedWith("Caller is not asset owner")
              })
              it("Correctly updates assets", async function () {
                  assetId = 1
                  const newAssetValue = 100
                  const newAssetDescription = "new description"
                  await assetManagement.updateAsset(
                      assetId,
                      newAssetValue,
                      newAssetDescription,
                  )

                  const updatedAsset = await assetManagement.getAsset(1)

                  assert.equal(updatedAsset[0].toString(), "1")
                  assert.equal(updatedAsset[1].toString(), "100")
                  assert.equal(updatedAsset[2], true)
                  assert.equal(updatedAsset[3], owner1.address)
                  assert.equal(updatedAsset[4], "new description")
              })
          })
      })
https://github.com/SimSimButDifferent/L2-AssetManagement