Basic Sample Hardhat Project – 基本示例安全帽项目区块链毕设代写

区块链毕设代写本文提供国外最新区块链项目源码下载,包括solidity,eth,fabric等blockchain区块链,Basic Sample Hardhat Project – 基本示例安全帽项目区块链毕设代写 是一篇很好的国外资料

Basic Sample Hardhat Project

This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, a sample script that deploys that contract, and an example of a task implementation, which simply lists the available accounts.

Try running some of the following tasks:

npx hardhat accounts npx hardhat compile npx hardhat clean npx hardhat test npx hardhat node node scripts/sample-script.js npx hardhat help

Buildspace: Build a Web3 App with Solidity + Ethereum Smart Contracts

Part 1

Setup your env to start working w/ the blockchain

Before anything, we’ll need to get our local Ethereum network working. This is how we can compile and test our smart contract code! You know how you need to spin up a local environment to work on it? Same deal here!

For now, all you need to know is that a smart contract is a piece of code that lives on the blockchain. The blockchain is a public place where anyone can securely read and write data for a fee. Think of it sorta like AWS or Heroku, except no one actually owns it!

So in this case, we want people to 👋 at us. The bigger picture here is:

  1. We’re going to write a smart contract. That contract has all the logic around how the 👋 s are handled. This is like your server code.

  2. Our smart contract will be deployed to the blockchain. This way, anyone in the world will be able to access and run our smart contract (if we give them permission to do so). So, pretty much like a server :).

  3. We’re going to build a client website that will let people easily interact with our smart contract on the blockchain. I’ll explain certain things in-depth as needed (ex. how mining working, how smart contracts are compiled and ran, etc) but for now lets just focus on getting stuff running.

The magic of Hardhat

  1. We’re going to be using a tool called Hardhat a lot. This will let us easily spin up a local Ethereum network and give us fake test Ethereum and fake test accounts to work with. Remember, it’s just like a local server, except the “server” is the blockchain

  2. Quickly compile smart contracts and test them on our local blockchain

First you’ll need to get node/npm. This is a package manager for JavaScript. It’s a tool that lets us install and manage packages for our JavaScript projects.

Next, lets head to the terminal. Go ahead and cd to the directory you want to work in. Once you’re there run these commands:

mkdir my-wave-portal cd my-wave-portal npm init -y npm install --save-dev hardhat

👏 Get sample project going

Cool, now we should have hardhat. Lets get a sample project going.

Run:

npx hardhat

Choose the option to create a sample project. Say yes to everything.

The sample project will ask you to install hardhat-waffle and hardhat-ethers. These are other goodies we’ll use later :).

Go ahead and install these other dependencies just in case it didn’t do it automatically.

npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers

Finally, run npx hardhat accounts and this should print out a bunch of strings that looks like this: 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720. These are Ethereum addresses that Hardhat generates for us to simulate real users on the blockchain. This is going to help us a ton later in the project when we want to simulate users 👋-ing at us!

🌟 Run it

To make sure everything is working, run:

npx hardhat compile 

Then run:

npx hardhat test 

You should see something like this:

Basic Sample Hardhat Project - 基本示例安全帽项目

Lets do a little clean-up.

Go ahead and open the code for the project now in your favorite code editor. I like VSCode best! We want to delete all the lame starter code generated for us. We don’t need any of that. We’re pros ;)!

Go ahead and delete the file sample-test.js under test. Also, delete sample-script.js under scripts. Then, delete Greeter.sol under contracts. Don’t delete the actual folders!

Part 2

👩‍💻 Let’s write a contract

Awesome, we made it.

We’re just going to hop right into our project.

Lets build a smart contract that lets us send a 👋 to our contract and keep track of the total # of waves. This is going to be useful because on your site, you might want to keep track of this #! Feel free to change this to fit your use case.

Create a file named WavePortal.sol under the contracts directory. File structure is super important when using Hardhat, so, be careful here!

We’re going to start out with the structure every contract starts out with.

Basic Sample Hardhat Project - 基本示例安全帽项目

Note: You may want to download the VS Code Solidity extension for easy syntax highlighting here.

// SPDX-License-Identifier: UNLICENSED

Just a fancy comment. It’s called a “SPDX license identifier”, feel free to Google what it is :).

pragma solidity ^0.8.0;

This is the version of the Solidity compiler we want our contract to use. It basically says “when running this, I only want to use version 0.8.0 of the Solidity compiler, nothing lower. Bonus: Can you find where set our Solidity compiler? Hint: It’s in a config file :).

import "hardhat/console.sol";

Some magic given to us by Hardhat to do some console logs in our contract. It’s actually challenging to debug smart contracts but this is one of the goodies Hardhat gives us to make life easier.

contract WavePortal { 		constructor() { 		console.log("Yo yo, I am a contract and I am smart"); 	} }

So, smart contracts kinda look like a class in other languages, if you’ve ever seen those! Once we initialize this contract for the first time, that constructor will run and print out that line. Please make that line say whatever you want :)! Call the variable whatever you want.

Let’s run this and see what we get!

Part 3

🔥 Imitating the blockchain environment to test

You’ve done it. You written a smart contract. You’re a champ!

Now we need to actually

  1. Compile it.
  2. Deploy it to our local blockchain.
  3. Once it’s there, that console.log will run :).

We need to do this because in the real world, smart contracts live on the blockchain. And, we want our website and smart contract to be used by real people so they can 👋 at us or do whatever you want them to do!

So, even when we’re working locally we want to imitate that environment. Technically, we could just compile and run Solidity code, but, what makes Solidity magical is how it can interact with the blockchain and Ethereum wallets (which we’ll see more of in next lesson). So, better to just knock this out right now.

We’re just going to write a custom script that handles those 3 steps for us.

Let’s do it!

📝 Build a script to run our contract

Go into the scripts directory and make a file named run.js.

So, to test a smart contract we’ve got to do a bunch of stuff right. Like: compile, deploy, then execute.

Our script will make it really easy to iterate on our contract really fast :).

So, this is what run.js is going to have:

Basic Sample Hardhat Project - 基本示例安全帽项目

Awesome.

🤔 How’s it work? Again going line by line here.

const waveContractFactory = await hre.ethers.getContractFactory("WavePortal"); 

This will actually compile our contract and generate the necessary files we need to work with our contract under the artifacts directory. Go check it out after you run this :).

const waveContract = await waveContractFactory.deploy(); 

This is pretty fancy :).

What’s happening here is Hardhat will create a local Ethereum network for us, but just for this contract. Then, after the script completes it’ll destroy that local network. So, every time you run the contract, it’ll be a fresh blockchain. Whats the point? It’s kinda like refreshing your local server every time so you always start from a clean slate which makes it easy to debug errors.

await waveContract.deployed(); 

We’ll wait until our contract is officially deployed to our local blockchain! Our constructor runs when we actually deploy.

console.log(“Contract deployed to:”, waveContract.address); Finally, once it’s deployed waveContract.address will basically give us the address of the deployed contract. This address is how can actually find our contract on the blockchain. There are millions of contracts on the actual blockchain. So, this address gives us easy access to the contract we’re interested in working with! This will be more important a bit later once we deploy to a real Ethereum network.

Lets run it!

npx hardhat run scripts/run.js 

You should see your console.log run from within the contract and then you should also see the contract address print out!!! Here’s what I get:

Basic Sample Hardhat Project - 基本示例安全帽项目

config files


基本示例安全帽项目

这个项目演示了一个基本的安全帽用例。它附带了一个示例契约、该契约的测试、一个部署该契约的示例脚本,以及一个简单列出可用帐户的任务实现示例

尝试运行以下一些任务:

npx hardhat accounts npx hardhat compile npx hardhat clean npx hardhat test npx hardhat node node scripts/sample-script.js npx hardhat help

构建空间:构建Web具有Solidity+以太坊eth智能合约的3款应用程序第1部分✅ 设置您的环境以开始与区块链blockchain一起工作✨ 安全帽的魔力👏 让示例项目继续进行

🌟 运行它第2部分👩‍💻 让我们写一份合同第三部分🔥 模拟区块链blockchain环境进行测试📝 构建一个脚本来运行我们的契约

  • 我们将要编写一个智能契约。该契约包含所有关于👋 处理。这就像您的服务器代码。
  • 我们的智能合约将部署到区块链blockchain。这样,世界上的任何人都可以访问和运行我们的智能合约(如果我们允许他们这样做)。因此,非常像服务器:)。
  • 我们将建立一个客户端网站,让人们可以轻松地在区块链blockchain上与我们的智能合约交互。我将根据需要深入解释某些事情(例如,挖掘如何工作,智能合约如何编译和运行,等等)但现在让我们把重点放在运行东西上。
  • 我们将经常使用一种叫做Hardhat的工具。这将使我们能够轻松地启动本地以太坊eth网络,并为我们提供假测试以太坊eth和假测试帐户。记住,它就像本地服务器,除了“服务器”之外区块链blockchain是否能快速编译智能合约并在本地区块链blockchain上测试?编译它。
  • 将它部署到本地区块链blockchain
  • 一旦它出现,console.log将运行:)
  • 🌟 运行它第2部分👩‍💻 让我们写一份合同第三部分🔥 模拟区块链blockchain环境进行测试📝 构建一个脚本来运行我们的契约

    Setup your env to start working w/ the blockchain

    在执行任何操作之前,我们需要让本地以太坊eth网络正常工作。这就是我们如何编译和测试我们的智能合约代码!你知道你需要如何建立一个本地环境来处理它吗?这里也一样

    现在,你需要知道的是,智能合约是存在于区块链blockchain上的一段代码。区块链blockchain是一个公共场所,任何人都可以免费安全地读写数据。想想它有点像AWS或Heroku,除非没有人真正拥有它

    所以在这种情况下,我们希望人们👋 看着我们。这里的大局是:

    1. 我们将要编写一个智能契约。该契约包含所有关于👋 处理。这就像您的服务器代码。
    2. 我们的智能合约将部署到区块链blockchain。这样,世界上的任何人都可以访问和运行我们的智能合约(如果我们允许他们这样做)。因此,非常像服务器:)。
    3. 我们将建立一个客户端网站,让人们可以轻松地在区块链blockchain上与我们的智能合约交互。我将根据需要深入解释某些事情(例如,挖掘如何工作,智能合约如何编译和运行,等等)但现在让我们把重点放在运行东西上。

    The magic of Hardhat

    1. 我们将经常使用一种叫做Hardhat的工具。这将使我们能够轻松地启动本地以太坊eth网络,并为我们提供假测试以太坊eth和假测试帐户。记住,它就像本地服务器,除了“服务器”之外区块链blockchain是否能快速编译智能合约并在本地区块链blockchain上测试?编译它。
    2. 将它部署到本地区块链blockchain

    First you’ll need to get node/npm. This is a package manager for JavaScript. It’s a tool that lets us install and manage packages for our JavaScript projects.

    接下来,让我们前往终端。继续,cd到您想使用的目录。一旦你在那里运行这些命令:

    mkdir my-wave-portal cd my-wave-portal npm init -y npm install --save-dev hardhat

    👏 Get sample project going

    酷,现在我们应该有安全帽了。让我们开始一个示例项目

    运行:

    npx hardhat

    选择创建示例项目的选项。对一切都说是

    示例项目将要求您安装hardhat华夫饼干和hardhat以太。这些是我们稍后将使用的其他好东西:)

    继续安装这些其他依赖项,以防它不能自动完成

    npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers

    最后,运行npx hardhat accounts,它将打印出一组如下所示的字符串:0xa0Ee7A142d267C1f36714E4a8F75612F20a79720。这些是Hardhat为我们模拟区块链blockchain上的真实用户而生成的以太坊eth地址。这将有助于我们以后在项目中模拟用户👋-你在嘲笑我们

    🌟 Run it

    要确保一切正常,请运行:

    npx hardhat compile 

    然后运行:

    npx hardhat test 

    您应该会看到如下内容:

    让我们做一点清理

    现在,在您喜爱的代码编辑器中打开项目的代码。我最喜欢VSCode!我们想删除为我们生成的所有跛脚初学者代码。我们不需要这些。我们是职业选手

    继续并删除正在测试的sample-test.js文件。另外,删除脚本下的sample-script.js。然后,删除合同下的Greeter.sol。不要删除实际的文件夹

    我们马上就要开始我们的项目了

    Part 2

    👩‍💻 Let’s write a contract

    让我们建立一个智能合约,让我们发送👋 与我们的合同保持一致,并跟踪波浪总数。这将非常有用,因为在您的站点上,您可能希望跟踪此#!您可以随意更改它以适合您的用例

    在contracts目录下创建一个名为WavePortal.sol的文件。使用Hardhat时,文件结构非常重要,因此,在这里要小心

    我们将从每个合同开始的结构开始

    Basic Sample Hardhat Project - 基本示例安全帽项目

    注意:您可能需要下载VS代码Solidity扩展,以便在此处轻松突出显示语法

    只是一个花哨的评论。它被称为“SPDX许可证标识符”,请随意在谷歌上搜索它是什么:)

    这是我们希望合同使用的Solidity编译器的版本。它基本上说“当运行此程序时,我只想使用Solidity编译器的0.8.0版,而不是更低的版本。奖励:你能找到我们的Solidity编译器的设置位置吗?提示:它在一个配置文件中:)。

    // SPDX-License-Identifier: UNLICENSED

    Hardhat给了我们一些魔法,让我们可以在合同中做一些控制台日志。调试智能合同确实很有挑战性,但这是H的优点之一。”这让我们的生活更轻松。

    pragma solidity ^0.8.0;

    所以,智能合约看起来有点像其他语言中的一个类,如果你见过的话!一旦我们第一次初始化这个合约,那个构造函数将运行并打印出那一行。请让那一行说你想说什么:)!调用变量,不管你想调用什么。

    import "hardhat/console.sol";

    让我们运行吧看看我们得到了什么!

    contract WavePortal { 		constructor() { 		console.log("Yo yo, I am a contract and I am smart"); 	} }

    你做到了。你写了一份智能合同。你是一名冠军!

    现在我们需要真正做到这一点,因为在现实世界中,智能合同存在于区块链blockchain上。而且,我们希望我们的网站和智能合同被真正的人使用,这样他们就可以👋 所以,即使我们在本地工作,我们也希望模仿这种环境。从技术上讲,我们可以编译和运行Solidity代码,但是,Solidity的神奇之处在于它如何与区块链blockchain以太坊eth钱包交互(我们将在下一课中看到更多)所以,最好现在就把它删除。

    Part 3

    🔥 Imitating the blockchain environment to test

    我们将编写一个自定义脚本,为我们处理这三个步骤。

    让我们开始吧!

    1. 一旦它出现,console.log将运行:)
    2. Deploy it to our local blockchain.
    3. Once it’s there, that console.log will run :).

    进入脚本目录,创建一个名为run.js的文件。

    所以,要测试智能合约,我们必须做一些正确的事情。比如:编译、部署,然后执行。

    我们的脚本将使我们很容易快速地迭代合同:)。

    因此,run.js将具备以下功能:

    📝 Build a script to run our contract

    棒极了。

    🤔 它是如何工作的?再次在这里逐行进行。

    这实际上会编译我们的合同,并在artifacts目录下生成我们处理合同所需的必要文件。运行此命令后,请查看它:)。

    这非常奇妙:)。

    这里发生的是Hardhat将创建一个本地以太坊eth网络对我们来说,但只是为了这份合同。然后,脚本完成后,它会破坏本地网络。所以,每次运行合同时,它都是一个新的区块链blockchain。这有什么意义?这有点像每次都刷新本地服务器,所以你总是从一个干净的状态开始,这样可以很容易地调试错误。

    我们等待你直到我们的合同正式部署到本地区块链blockchain!我们的构造函数在实际部署时运行。

    console.log(“合同部署到:”,waveContract.address);最后,一旦部署waveContract.address将基本上为我们提供已部署合同的地址。该地址是如何在区块链blockchain上找到我们的合同的。在实际区块链blockchain上有数百万份合同。因此,该地址使我们能够轻松访问我们感兴趣的合同!这将更加重要信息,稍后我们部署到真正的以太坊eth网络。

    const waveContractFactory = await hre.ethers.getContractFactory("WavePortal"); 

    让我们运行它吧!

    const waveContract = await waveContractFactory.deploy(); 

    您应该看到您的console.log在合同中运行,然后您还应该看到合同地址打印出来!!!以下是我得到的信息:

    基本示例安全帽项目

    构建空间:构建Web具有Solidity+以太坊eth智能合约的3款应用程序第1部分✅ 设置您的环境以开始与区块链blockchain一起工作✨ 安全帽的魔力👏 让示例项目继续进行

    🌟 运行它第2部分👩‍💻 让我们写一份合同第三部分🔥 模拟区块链blockchain环境进行测试📝 构建一个脚本来运行我们的契约

  • 我们将要编写一个智能契约。该契约包含所有关于👋 处理。这就像您的服务器代码。
  • 我们的智能合约将部署到区块链blockchain。这样,世界上的任何人都可以访问和运行我们的智能合约(如果我们允许他们这样做)。因此,非常像服务器:)。
  • 我们将建立一个客户端网站,让人们可以轻松地在区块链blockchain上与我们的智能合约交互。我将根据需要深入解释某些事情(例如,挖掘如何工作,智能合约如何编译和运行,等等)但现在让我们把重点放在运行东西上。
  • 我们将经常使用一种叫做Hardhat的工具。这将使我们能够轻松地启动本地以太坊eth网络,并为我们提供假测试以太坊eth和假测试帐户。记住,它就像本地服务器,除了“服务器”之外区块链blockchain是否能快速编译智能合约并在本地区块链blockchain上测试?编译它。
  • 将它部署到本地区块链blockchain
  • 一旦它出现,console.log将运行:)
  • await waveContract.deployed(); 

    We’ll wait until our contract is officially deployed to our local blockchain! Our constructor runs when we actually deploy.

    console.log(“Contract deployed to:”, waveContract.address); Finally, once it’s deployed waveContract.address will basically give us the address of the deployed contract. This address is how can actually find our contract on the blockchain. There are millions of contracts on the actual blockchain. So, this address gives us easy access to the contract we’re interested in working with! This will be more important a bit later once we deploy to a real Ethereum network.

    Lets run it!

    npx hardhat run scripts/run.js 

    You should see your console.log run from within the contract and then you should also see the contract address print out!!! Here’s what I get:

    Basic Sample Hardhat Project - 基本示例安全帽项目

    config files

    部分转自网络,侵权联系删除区块链源码网

    www.interchains.cc

    https://www.interchains.cc/24422.html

    区块链毕设网(www.interchains.cc)全网最靠谱的原创区块链毕设代做网站 部分资料来自网络,侵权联系删除! 最全最大的区块链源码站 ! QQ3039046426
    区块链知识分享网, 以太坊dapp资源网, 区块链教程, fabric教程下载, 区块链书籍下载, 区块链资料下载, 区块链视频教程下载, 区块链基础教程, 区块链入门教程, 区块链资源 » Basic Sample Hardhat Project – 基本示例安全帽项目区块链毕设代写

    提供最优质的资源集合

    立即查看 了解详情