การคอมไพล์และนำสัญญาไปใช้งาน
เรียนรู้การคอมไพล์โค้ด Solidity เป็นไบต์โค้ด และนำสัญญาไปใช้งานบนเครือข่ายการพัฒนาในเครื่อง
การคอมไพล์และนำสัญญาไปใช้งาน เป็นบทเรียน Blockchain Smart Contracts with Solidity ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 compileUnderstanding 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.jsTest 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Blockchain Smart Contracts with Solidity ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การคอมไพล์และนำสัญญาไปใช้งาน”
เรียนรู้การคอมไพล์โค้ด Solidity เป็นไบต์โค้ด และนำสัญญาไปใช้งานบนเครือข่ายการพัฒนาในเครื่อง คุณปฏิบัติ Blockchain Smart Contracts with Solidity ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Blockchain Smart Contracts with Solidity หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Blockchain Smart Contracts with Solidity บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การคอมไพล์และนำสัญญาไปใช้งาน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Blockchain Smart Contracts with Solidity นี้ได้ไหม
ได้ บทเรียน Blockchain Smart Contracts with Solidity ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตั้งค่า Hardhat หรือ Truffle
- การคอมไพล์และนำสัญญาไปใช้งาน
- การโต้ตอบกับสัญญาที่นำไปใช้งานแล้ว
- การเขียนและเรียกใช้การทดสอบสัญญา