2022-11-23大厂面经00

题目分享

  1. 八股文(原型链、类型判断)
  2. useState和useReducer的区别
  3. react中的性能优化有哪些
  4. 发版本的时候 ^和~有什么区别
  5. 包的版本怎么做差异化管理
  6. 自己如何实现一个埋点机制
  7. 如何区分是否为原型属性
  8. props和state的区别,props可以改变吗
  9. 如何减少渲染
  10. 通信的全部方式有哪些,有什么区别
  11. 深拷贝和浅拷贝怎么实现
  12. json.stringfy实现深拷贝的原理是什么
  13. 深拷贝的时候相互依赖,如何解决
  14. useState和setState有啥区别,为什么要使用useState
2022-10-18Web300

Hardhat Setup - Smart Contract Lottery

创建空项目

yarn add hardhat --dev
yarn hardhat 
// 选择empty hardhat.config.js

image.png

安装依赖

yarn add --dev @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers @nomiclabs/hardhat-etherscan @nomiclabs/hardhat-waffle chai ethereum-waffle hardhat hardhat-contract-sizer hardhat-deploy hardhat-gas-reporter prettier prettier-plugin-solidity solhint solidity-coverage dotenv
2022-10-17Web300

tasks

编写的tasks可以很方便的在终端中进行调用

const { task } = require("hardhat/config");

task("block-number", "Prints the current block number.").setAction(
  async (taskArgs, hre) => {
    const blockNumber = await hre.ethers.provider.getBlockNumber();
    console.log(`Current block Number is ${blockNumber}`);
  }
);

以上task实现了查看block-number,第一个参数为task的名称,第二个参数为描述,setAction用来描述真正这个task要实现的功能。

运行tasks

image.png

2022-10-17Web300

使用hardhat-etherscan

链接地址:https://hardhat.org/hardhat-runner/plugins/nomiclabs-hardhat-etherscan

This plugin helps you verify the source code for your Solidity contracts on Etherscan

安装依赖

npm install --save-dev @nomiclabs/hardhat-etherscan

修改hardhat.config.js

require("@nomiclabs/hardhat-etherscan");

const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY

etherscan: {
    apiKey: ETHERSCAN_API_KEY,
},
2022-10-13Web300

依赖和scripts

{
  "name": "hardhat-fund-me-fcc",
  "devDependencies": {
    "@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.13",
    "@nomiclabs/hardhat-etherscan": "^3.0.0",
    "@nomiclabs/hardhat-waffle": "^2.0.2",
    "chai": "^4.3.4",
    "ethereum-waffle": "^3.4.0",
    "ethers": "^5.5.3",
    "hardhat": "^2.8.3",
    "hardhat-deploy": "^0.9.29",
    "hardhat-gas-reporter": "^1.0.7",
    "solidity-coverage": "^0.7.18",
    "@chainlink/contracts": "^0.3.1",
    "dotenv": "^14.2.0",
    "prettier-plugin-solidity": "^1.0.0-beta.19"
  },
  "scripts": {
    "test": "hardhat test",
    "test:staging": "hardhat test --network goerli",
    "lint": "solhint 'contracts/*.sol'",
    "lint:fix": "solhint 'contracts/**/*.sol' --fix",
    "format": "prettier --write .",
    "coverage": "hardhat coverage"
  }
}

编写library库(工具类)PriceConverter

将第三方的.sol引入进来

yarn add @chainlink/contracts
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

library PriceConverter {
  function getPrice(AggregatorV3Interface priceFeed)
    internal
    view
    returns (uint256)
  {
    (, int256 answer, , , ) = priceFeed.latestRoundData();
    // ETH/USD rate in 18 digit
    return uint256(answer * 10000000000);
  }

  // 1000000000
  // call it get fiatConversionRate, since it assumes something about decimals
  // It wouldn't work for every aggregator
  function getConversionRate(uint256 ethAmount, AggregatorV3Interface priceFeed)
    internal
    view
    returns (uint256)
  {
    uint256 ethPrice = getPrice(priceFeed);
    uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
    // the actual ETH/USD conversation rate, after adjusting the extra 0s.
    return ethAmountInUsd;
  }
}