# 编程日记:优化项目结构(2022-08-24) **Published by:** [Corror](https://paragraph.com/@corror/) **Published on:** 2022-08-25 **URL:** https://paragraph.com/@corror/2022-08-24 ## Content 编程学习做了什么?成功在 Stack Overflow 提出了第一个有大佬 (37k reputation) 认真回答的问题,也是这段时间编程以来,第二次在老外的帮助下解决了编程上的难题。成功在视频解释 dotenv 包之前,通过自行阅读 npm 文档,成功尝试出了包的安装和使用。成功在两个MetaMask账户之间实现切换,保证拥有真钱和NFT的钱包,与开发用的钱包完全分离。成功了解了 README.md 的书写格式,并将格式安装自己的想法,重新写在了项目当中作为模板。有什么收获?JS BigNumber 与 String问题: Title: When JS passes numeric values as arguments to Solidity function, why is it better to wrap them in quotes? Content: I defined a function in Solidity:function store(uint256 _favoriteNumber) public {} Then call the function with argument 7 in JavaScript:contract.store(7); But, according to the Patrick Collins JS video, it's a better form to wrap number 7 with quotes like this:contract.sotre("7"); I don't understand why it's better to wrap number with quotes and why ehter.js can automatically convert string to number? 以上问题已在 Stack Overflow 当中提问,详见此问题。 得到答复后,我的理解是: The smart contract libraries don't accept integers as numeric values, but native JS string or an instance of BigNumber. 即,由于智能合约所能处理的数值范围在0~2^256之间,而JS所能处理的数值范围在0~2^53-1的范围之间,所以智能合约所能接收的数值大于JS所能传递的数值。 因此,为了让JS能够传递超出2^53-1大小的数值,比如“1 Ether”即“10^18 Wei”, 数值将会以字符串或者 BigNumber 的实例这两种方式传递,这样JS将不会因为数值大小溢出而报错 ehter.js。Transaction Receipt 语法有两种获得交易收据的方法:一种是在部署合约的时候,获得initcode transaction的交易收据的方法:await contract.deployTransaction.wait();另一种是在利用合约发起交易时,获得普通transaction的交易收据的方法:await transaction.wait();.env file.env 文件可以用来存储环境变量,它有两个特点:只存储在本地,无法被分享;构成程序的运行环境,使得任何本地程序都可以读取的变量。环境变量可以自定义名称,但是所有命名都需要采用大写的形式。每一个环境变量的名称都需要对应其意义,比如:PRIVATE_KEY 就是用来存储私钥的环境变量,PATH 就是用来存储路径的环境变量。dotenv packageIn .env file:PRIVATE_KEY=0x4e35151e33632cf03d3a4631dc2960f95884a678221cbfbb64ee94ecb3d1d89b To use the PRIVATE_KEY in JS file, we can use the dotenv package:require("dotenv").config(); console.log(process.env.PRIVATE_KEY); README.md 模板来源:Best-README-Template README.md 的格式大概分为以下几个部分:引入:项目Logo, 名称,描述。项目介绍 (About the project): 用一句话描述项目。技术使用 (Build With): 自己用到的技术及其Logo, 比如:ethers.js 等入门指南 (Getting Started)预备工具 (Prerequisite)安装 (Installation)Get a free API at https://example.comClone the repoInstall NPM packageEnter your API in config.js项目用途 (Usage): 截图说明自己的项目有哪些作用。经过重构后,在 README.md 中所使用的模板如下## 引入 介绍项目的Logo, Title, 以及 Description. ## 一、项目介绍 (About the project) 用一句话描述项目。 ## 二、项目安装 (Getting Startted) 介绍如何使用该项目。 ### 预备工具 (Prerequisite) - Node.js ### 安装步骤 (Installation) 1. 克隆仓库 2. 安装库,详见 `package.json` 文件。 ## 三、项目用途 (Usage) 截图说明该项目有哪些典型的用法。 ## Publication Information - [Corror](https://paragraph.com/@corror/): Publication homepage - [All Posts](https://paragraph.com/@corror/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@corror): Subscribe to updates