---
title: "如何開始在 Arbitrum 上開發"
description: "學習如何建立 Arbitrum Dapp"
---

# 如何開始在 Arbitrum 上開發

Arbitrum 是一個 Ethereum layer-2 區塊鏈，使用 optimistic rollups，讓開發者在享有 Ethereum layer 1 安全性的同時，比在 Ethereum 基礎層區塊鏈上執行 [apps](https://www.alchemy.com/dapps/top/defi-dapps) 擁有更高的可擴展性與更低的 gas 成本。

如果你有興趣了解如何開發 Arbitrum，這篇文章就是為你準備的！在這份指南中，我們會帶你走過在 Arbitrum 網路上開始寫程式所需的步驟。

## **什麼是 Arbitrum？**

[Arbitrum](https://www.alchemy.com/arbitrum?a=0c128a6d93) 是一個 layer-2 optimistic rollup 區塊鏈，能在維持去信任化安全性的同時協助擴展 Ethereum 主鏈。雖然它是基於 Ethereum's Virtual Machine \(EVM\)，Arbitrum 提供了一種獨特的設計，讓它比 Ethereum 網路更具可擴展性且更易於使用。

此外，Arbitrum 智能合約與 EVM 相容，意味著它們可以搭配現有的 Ethereum 工具、錢包和 apps 使用。Arbitrum 是作為 Ethereum 擴展方案之一而建立的，目的是打造低成本的智能合約，並解決交易時間過長的問題。

Arbitrum Virtual Machine 運行於 ArbOS 之上，在 Arbitrum 鏈上執行智能合約時扮演記錄者、交通指揮和執行者的角色。

## **如何開始在 Arbitrum 上開發**

在你開始於 Arbitrum 上開發之前，了解此區塊鏈的運作方式並設定好一些前置條件是很重要的：

1. Hardhat
1. 一個 Alchemy 帳號
1. 一個 [web3 錢包](https://www.alchemy.com/overviews/web3-wallets)（例如 [MetaMask](https://www.alchemy.com/dapps/metamask)、Apex 等）

### **1. 了解 Arbitrum 的運作方式**

在你開始於 Arbitrum 上開發之前，充分了解此平台的運作方式很重要。你可以閱讀 Arbitrum Whitepaper 來了解更多技術細節。

根據建立並維護 Arbitrum 的團隊 Offchain Labs 所述，Arbitrum 的架構如下：使用者與服務提供者位於左側的鏈下（off-chain），而 Arbitrum 系統則由右側的 Layer 1 與 Layer 2 組成。

Arbitrum 是一個 layer-2 rollup 解決方案（綠色），架構於 Layer-1 Ethereum 鏈（橘色）之上。

<ImageBlock
  src="https://media.alchemy.com/1704205409-how-arbitrum-works.jpg"
  alt="Arbitrum 架構圖，顯示其作為 layer-2 rollup 建構於 Ethereum layer-1 鏈之上"
  width={1600}
  height={742}
  caption="來源：Offchain Labs"
/>

此外，Arbitrum 團隊也建立了多項資源來協助開發者入門，包括平台總覽以及建立你的第一個智能合約的逐步指南。

### **2. 下載 Hardhat（整合開發環境）**

在 Arbitrum 上開發有兩種方式：使用線上 IDE，或建立本機開發環境。無論選哪一種，開始前你都需要選擇並設定一個 [Solidity 開發用的 IDE](https://hardhat.org/hardhat-runner/docs/getting-started#overview)。

本文將使用本機開發環境 [Hardhat](https://hardhat.org/hardhat-runner/docs/getting-started#overview) 進行建置。安裝完 Hardhat 後，[建立一個 Hardhat 專案](https://www.alchemy.com/docs/how-to-deploy-a-smart-contract-to-the-sepolia-testnet)。

### **3. 在 Alchemy 上建立一個 Arbitrum app**

要部署智能合約，你需要一個 [Arbitrum RPC 節點](https://www.alchemy.com/overviews/arbitrum-node)。幸運的是，你不需要自行運行 Arbitrum 節點，只要 [免費註冊 Alchemy](https://dashboard.alchemy.com/?a=0c128a6d93)，並在 Arbitrum-Goerli 測試網上建立一個 app 即可。

註冊完成後，請進行以下步驟：

1. 點擊 Dashboard
1. 點擊「Create App」
1. 為你的 app 取一個名稱
1. 選擇 Arbitrum Nitro Goerli 測試網
1. 然後點擊「Create App」

你的 app 應該就會出現在 Alchemy Dashboard 中。

### **4. 從 Goerli faucet 取得測試用 ETH**

要在 Arbitrum-Goerli 測試網上與 Arbitrum 合約互動，你需要一些測試用 ETH 以及一個 [Arb-Goerli RPC endpoint](https://www.alchemy.com/chain-connect/chain/arbitrum-goerli)。

取得 Goerli ETH 最簡單的方式就是透過 faucet。

1. [將 Arbitrum 加入 MetaMask](https://www.alchemy.com/docs/reference/arbitrum-api-quickstart#arbitrum-tutorials)
1. 前往 [Goerli faucet](https://goerlifaucet.com/)
1. [‍](https://goerlifaucet.com/)輸入你的 Ethereum 地址
1. 幾分鐘內你的 MetaMask 錢包中應該會收到 Goerli ETH

若要確認你是否已從 faucet 收到 ETH，可進入 MetaMask 設定，在 Advanced 底下選擇「Show test networks」。

如果你使用的是與 Arbitrum Nitro Testnet 相容的 Goerli faucet，這裡有[如何將 Goerli tokens 橋接到 Arbitrum Nitro](https://www.alchemy.com/overviews/arbitrum-nitro-testnet) 的說明。

### **5. 撰寫、編譯並部署你的智能合約**

到這一步，你就可以開始撰寫智能合約了。依照[ Alchemy 文件](https://www.alchemy.com/docs/how-to-deploy-a-smart-contract-to-the-sepolia-testnet)中的步驟，建立一個簡單的「Hello World」智能合約。你也可以觀看這支 [Hello World 影片教學](https://www.youtube.com/watch?v=g73EGNKatDw)，學習如何撰寫、編譯並部署「Hello World」智能合約。這個[ github repo](https://github.com/alchemyplatform/hello-world-tutorial) 包含了影片中提到的所有程式碼。

寫完合約後，使用 Hardhat 進行編譯，連接到一個 Ethereum client（Ganache 或 [Geth](https://www.alchemy.com/overviews/what-is-a-geth-node-and-how-to-run-one)），並使用 _hardhat deploy_ 指令來部署你的合約。

如果你想要更多靈感，別忘了看看 [Offchain Labs GitHub monorepo](https://github.com/OffchainLabs/arbitrum-tutorials)，裡面有像 Pet Shop dapp 和 Election dapp 這樣的示範範例。

### **6. 與你的智能合約互動並將其提交到 Etherscan**

建立智能合約後，你還需要學習[如何與智能合約互動](https://www.youtube.com/watch?v=sQJ-XQBzEuc)，這樣它才有用！觀看這支影片教學，了解[如何讀取並與智能合約互動](https://www.youtube.com/watch?v=sQJ-XQBzEuc)。

**這支影片會帶你完成以下步驟：**

- 建立一個 interact.js 檔案
- 更新你的 env 檔案
- 取得你的合約 ABI
- 建立一個 contract instance
- 讀取並更新 _init_ 訊息

完成後，你就可以[將合約發佈到 Etherscan](https://www.alchemy.com/docs/best-practices-for-deploying-a-smart-contract-on-evm-mainnets-1) 了。觀看這支影片，了解[如何在 Etherscan 上驗證你的合約](https://www.youtube.com/watch?v=x1a5lrW-9fo)。

**做法如下：**

- 安裝 hardhat-etherscan 外掛
- 修改 Etherscan 的設定選項
- 在 Etherscan 上驗證智能合約

Arbitrum 上的所有交易都可以在 [Arbitrum block explorer](https://arbiscan.io/) 上查看。

### **7. 將你的智能合約與前端整合**

建立智能合約並在 Etherscan 上看到它被發佈，還不算大功告成！接下來你需要[將智能合約與前端介面整合](https://www.alchemy.com/docs/best-practices-for-deploying-a-smart-contract-on-evm-mainnets-1)。

**此教學將涵蓋以下步驟：**

- 複製 starter files
- 檢視 starter files
- 從智能合約讀取資料
- 設定一個 Ethereum 錢包
- 將 MetaMask 連接到 UI
- 實作 updateMessage 函式
- 打造你自己的 dApp

恭喜！你剛剛在 Arbitrum 上開發出了自己的簡易 dApp！🎉

自從 Arbitrum 主網上線以來，越來越多開發者開始在此區塊鏈上建立 Arbitrum 智能合約，而學習如何在 Arbitrum 上開發，正是[成為全方位 Solidity 開發者](https://www.alchemy.com/overviews/how-to-become-a-solidity-developer)的重要一步。

在將智能合約上線到 Arbitrum 主網之前，記得先在測試網上徹底測試你的 dapp，並考慮聘請一家[智能合約審計公司](https://justjooz.com/best-smart-contract-auditors/)來檢測合約是否存在漏洞。
