0Pricing
Blockchain Smart Contracts with Solidity · 课时

编译与部署合约

学习将 Solidity 代码编译为字节码,并将合约部署到本地开发网络。

编译与部署合约 是 CoddyKit 上的免费 Blockchain Smart Contracts with Solidity 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Blockchain Smart Contracts with Solidity 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Intro to Compile & Deploy

Great job setting up your environment! Now let's bring your Solidity code to life. This lesson covers two essential steps: compiling and deploying your smart contracts.

Think of it like building an app: first, you write the code, then you turn it into something a computer can run (compile), and finally, you put it somewhere for people to use (deploy).

What is Compilation?

Compilation is the process of converting your human-readable Solidity code into machine-readable bytecode. The Ethereum Virtual Machine (EVM) understands this bytecode.

  • Solidity is a high-level language.
  • The EVM runs low-level bytecode.
  • The compiler acts as a translator between them.

Compiling with Hardhat

Hardhat makes compiling simple. Once your Solidity files are in the contracts/ folder, you just need one command in your terminal.

This command tells Hardhat to find all .sol files and compile them into bytecode.

npx hardhat compile

Understanding Artifacts

After compiling, Hardhat creates an artifacts/ folder. Inside, you'll find JSON files for each contract. These files contain vital information:

  • Bytecode: The EVM-executable code.
  • ABI (Application Binary Interface): Describes how to interact with your contract.
  • Source Map: Helps with debugging by linking bytecode to source code.

What is Deployment?

Deployment is the act of sending your contract's bytecode to a blockchain network. Once deployed, your contract lives on that network and can be interacted with by anyone.

Each deployment costs gas (a transaction fee) and results in a unique contract address on the blockchain.

Local Dev Networks

Before deploying to a public network like Ethereum Mainnet, it's crucial to test your contracts on a local development network.

Hardhat comes with its own built-in network, which is perfect for fast, free, and isolated testing. It resets every time you restart it.

Our Example Contract

Before deploying, we need a contract! Let's use a simple Greeter.sol contract. This contract stores a greeting and lets you change it.

Save this in your contracts/ folder:

/* contracts/Greeter.sol */
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Greeter {
    string private greeting;

    constructor(string memory _greeting) {
        greeting = _greeting;
    }

    function greet() public view returns (string memory) {
        return greeting;
    }

    function setGreeting(string memory _greeting) public {
        greeting = _greeting;
    }
}

The Deployment Script

This JavaScript script, saved as scripts/deploy.js, will tell Hardhat how to deploy our Greeter contract to the network.

It uses Hardhat's built-in ethers.js integration to handle contract deployment. Try running this example:

// scripts/deploy.js
async function main() {
  const Greeter = await ethers.getContractFactory("Greeter");
  const greeter = await Greeter.deploy("Hello, CoddyKit!");

  await greeter.deployed();

  console.log("Greeter deployed to:", greeter.address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Running the Deploy Script

With your contract compiled and your deployment script ready, it's time to deploy! Open your terminal and run this command:

Hardhat will automatically start its local network, deploy your contract, and then shut down (unless you run npx hardhat node first).

npx hardhat run scripts/deploy.js

Test Your Knowledge

Let's check your understanding of the compilation and deployment process in Hardhat.

Your Contracts Are Live!

You've successfully learned how to compile your Solidity code into bytecode and deploy it to a local development network!

  • Compile: Transforms Solidity into EVM bytecode and creates ABI.
  • Deploy: Puts your contract's bytecode on a blockchain.
  • Hardhat Network: Ideal for local testing.

Next, you'll learn how to interact with these deployed contracts!

常见问题解答

「编译与部署合约」课时是免费的吗?

是的 — 「编译与部署合约」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Blockchain Smart Contracts with Solidity 课程的其余内容,请升级到 CoddyKit PRO。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。

「编译与部署合约」这节课中我会学到什么?

学习将 Solidity 代码编译为字节码,并将合约部署到本地开发网络。 你通过在浏览器中直接运行的动手代码来练习 Blockchain Smart Contracts with Solidity,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Blockchain Smart Contracts with Solidity 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Blockchain Smart Contracts with Solidity 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「编译与部署合约」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Blockchain Smart Contracts with Solidity 课中编写并运行代码吗?

能。每节 Blockchain Smart Contracts with Solidity 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 设置 Hardhat 或 Truffle
  2. 编译与部署合约
  3. 与已部署的合约交互
  4. 编写与运行合约测试
← 返回 Blockchain Smart Contracts with Solidity