0Pricing
Web3 & DApp Development Fundamentals · 课时

部署与交互

练习将编译后的智能合约部署到模拟区块链环境中,并通过 Remix 用户界面与其函数交互。

部署与交互 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 3 节课。

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

What is Smart Contract Deployment?

You've learned to write and compile smart contracts in Remix. The next step is to deploy them.

Deploying a contract means publishing its compiled code onto a blockchain. Once deployed, your contract becomes an immutable, self-executing program accessible to anyone on that network.

Remix's Deploy & Run Panel

Remix IDE has a dedicated panel for deployment and interaction. Look for the 'Deploy & Run transactions' plugin, usually represented by an Ethereum logo.

This panel allows you to:

  • Select a blockchain environment.
  • Choose an account for deployment.
  • Deploy your compiled contracts.
  • Interact with deployed contracts.

Choosing a Blockchain Environment

Before deploying, you need to tell Remix which blockchain to target. In the 'Environment' dropdown, you'll see options:

  • JavaScript VM: A local, in-browser simulated blockchain. Perfect for testing!
  • Injected Provider: Connects to an external wallet like MetaMask.
  • Web3 Provider: Connects to a custom RPC endpoint (e.g., a local Hardhat node).

For this lesson, we'll use the JavaScript VM.

Test Accounts in JavaScript VM

When you select 'JavaScript VM', Remix automatically provides several test accounts. These accounts are pre-funded with fake Ether (ETH).

You'll use one of these accounts to deploy your contract. The deployment transaction will deduct 'gas' (transaction fees) from the chosen account's balance, just like on a real blockchain.

Gas Limit and Value

In the 'Deploy & Run' panel, you'll also see fields for 'Gas limit' and 'Value'.

  • Gas limit: The maximum amount of gas (computational effort) you're willing to spend on a transaction.
  • Value: The amount of Ether (or other native token) you want to send along with the transaction (e.g., for payable functions or sending funds to a contract).

For most simple deployments, the default gas limit is sufficient. We'll leave 'Value' at 0 for now.

Our Simple Counter Contract

Let's use a basic Counter contract to practice deployment and interaction. This contract simply stores an unsigned integer and allows us to increment or decrement it.

Copy and paste this code into a new file named Counter.sol in Remix, then compile it.

pragma solidity ^0.8.0;

contract Counter {
    uint public count;

    constructor() {
        count = 0;
    }

    function increment() public {
        count++;
    }

    function decrement() public {
        count--;
    }
}

Deploying the Counter Contract

With Counter.sol compiled and 'JavaScript VM' selected:

  1. Ensure Counter is selected in the 'CONTRACT' dropdown.
  2. Click the orange 'Deploy' button.

You'll see a transaction recorded in the Remix terminal, and your deployed contract will appear under 'Deployed Contracts'.

Interacting with Deployed Contracts

Once deployed, your contract will show up under the 'Deployed Contracts' section in the 'Deploy & Run' panel.

You'll see buttons for each of the contract's functions and public state variables. These buttons are color-coded:

  • Blue: View/pure functions (read-only, no transaction cost).
  • Orange: Non-payable functions (modify state, require transaction).
  • Red: Payable functions (modify state and accept Ether).

Reading Contract State: 'count'

Our Counter contract has a public state variable called count. When a state variable is declared public, Solidity automatically creates a getter function for it.

Click the blue 'count' button in the deployed contract interface. You should see its initial value, 0, displayed below the button.

Modifying Contract State: 'increment()'

Now, let's change the contract's state. Click the orange 'increment' button.

This will trigger a transaction. You'll see a new transaction in the terminal. After it's mined (almost instantly in JS VM), click the blue 'count' button again. The value should now be 1!

Try clicking 'decrement' and observing the change too.

Quick Check: Remix Deployment

Which of the following best describes the primary purpose of the 'JavaScript VM' environment in Remix?

Recap: Deploying & Interacting

Congratulations! You've learned the essential steps to deploy and interact with smart contracts using Remix IDE:

  • Used the 'Deploy & Run transactions' panel.
  • Selected the 'JavaScript VM' for a simulated environment.
  • Deployed a simple Counter contract.
  • Interacted with its public variable and functions to read and modify its state.

This hands-on experience is crucial for understanding how smart contracts live and operate on the blockchain!

常见问题解答

「部署与交互」课时是免费的吗?

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

「部署与交互」这节课中我会学到什么?

练习将编译后的智能合约部署到模拟区块链环境中,并通过 Remix 用户界面与其函数交互。 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web3 & DApp Development Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。

「部署与交互」课时需要多长时间?

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

我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?

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

此课程中的所有课时

  1. Remix IDE 概览
  2. 编译智能合约
  3. 部署与交互
← 返回 Web3 & DApp Development Fundamentals