部署到测试网与主网
了解将您的 DApp 部署到公共测试网进行最终测试,并最终部署到以太坊主网的流程
部署到测试网与主网 是 CoddyKit 上的免费 NestJS Enterprise Backend APIs 课时。 这是第 6 节课,共 6 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 NestJS Enterprise Backend APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 NestJS Enterprise Backend APIs 课程共包含 6 节课。
本课时的部分内容尚未翻译,以英文显示。
What is DApp Deployment?
Welcome! Today, we'll learn about deploying Decentralized Applications (DApps). Deployment is the process of publishing your smart contracts and associated front-end code to a blockchain network.
It's a crucial step that makes your DApp accessible to users worldwide. Without deployment, your DApp remains just code on your computer.
Why Deploy to Testnets First?
Before launching your DApp to the world, it's vital to test it thoroughly. This is where testnets come in!
- Testnets are separate blockchain networks that mimic the main Ethereum network.
- They use 'test Ether' (fake currency) for transactions, so you can test without risking real funds.
- This allows developers to find and fix bugs, ensuring the DApp works as expected.
Key Ethereum Testnets
Ethereum has several public testnets. The most prominent one currently is Sepolia.
- Sepolia: This is the recommended testnet for new development. It's a proof-of-stake network, like the Ethereum mainnet.
- Historically, there were others like Goerli, but Sepolia is now the primary choice for testing smart contracts and DApps.
Connecting to a Testnet
To deploy to a testnet, your development environment (like Hardhat or Truffle) needs to connect to it. This is usually done via an RPC URL provided by a service like Infura or Alchemy.
These services act as a gateway, allowing your tools to communicate with the blockchain without running a full node yourself. You'll need an API key for access.
Getting Test Ether (Faucets)
On a testnet, every transaction (including contract deployment) requires gas, paid in test Ether. You can get free test Ether from a faucet.
A faucet is a web service that sends small amounts of test Ether to your wallet address. Simply visit a Sepolia faucet website, enter your address, and request funds.
Deployment Scripts
Deploying smart contracts manually can be tedious. That's why we use deployment scripts, typically written in JavaScript.
These scripts automate the process of compiling your contract, signing the transaction with your private key, and sending it to the chosen blockchain network (testnet or mainnet).
Understanding Gas for Deployment
Every operation on Ethereum, including contract deployment, consumes gas. Gas is a unit that measures the computational effort required.
- Gas Limit: The maximum gas you're willing to spend.
- Gas Price: How much Ether you're willing to pay per unit of gas.
- Total Cost: Gas Limit × Gas Price.
Deployment is often one of the most gas-intensive transactions.
Connecting to a Network Example
Here's a simple JavaScript snippet using Ethers.js to connect to an Ethereum network and fetch its current block number. This is a foundational step for any deployment script.
Remember to replace YOUR_INFURA_API_KEY with your actual key.
const { ethers } = require("ethers");
async function main() {
// Connect to a Sepolia testnet RPC (Infura example)
const provider = new ethers.JsonRpcProvider("https://sepolia.infura.io/v3/YOUR_INFURA_API_KEY");
try {
const blockNumber = await provider.getBlockNumber();
console.log("Connected to network. Current block number:", blockNumber);
} catch (error) {
console.error("Failed to connect or get block number:", error.message);
}
}
main();Deploying to Mainnet
Once your DApp is thoroughly tested on testnets, you might consider deploying to the Ethereum Mainnet. This is the live, public blockchain where real assets and value exist.
- Real Funds: Mainnet deployments cost real Ether.
- Security: Ensure extensive audits and testing have been completed.
- Immutability: Once deployed, smart contracts are generally unchangeable.
Always proceed with extreme caution and ensure all configurations are correct!
Deployment Knowledge Check
You've learned about deploying DApps. Let's test your understanding!
Deployment Journey Recap
Well done! You've learned the essential steps for deploying DApps.
- We started with testnets for safe, free testing.
- We explored how to connect to networks and get test Ether.
- You now understand the role of deployment scripts and gas costs.
- Finally, we discussed the critical considerations for deploying to the mainnet.
This knowledge is fundamental for bringing your decentralized applications to life!
常见问题解答
「部署到测试网与主网」课时是免费的吗?
是的 — 「部署到测试网与主网」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 NestJS Enterprise Backend APIs 课程的其余内容,请升级到 CoddyKit PRO。 NestJS Enterprise Backend APIs 课程共包含 6 节课。
「部署到测试网与主网」这节课中我会学到什么?
了解将您的 DApp 部署到公共测试网进行最终测试,并最终部署到以太坊主网的流程 你通过在浏览器中直接运行的动手代码来练习 NestJS Enterprise Backend APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 NestJS Enterprise Backend APIs 需要有经验吗?
无需任何先前经验。CoddyKit 上的 NestJS Enterprise Backend APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 6 节课,共 6 节。
「部署到测试网与主网」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 NestJS Enterprise Backend APIs 课中编写并运行代码吗?
能。每节 NestJS Enterprise Backend APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。