Blockchain Smart Contracts with Solidity · บทเรียน

การสร้างสัญญาออราเคิลแบบกำหนดเอง

เรียนรู้การออกแบบและสร้างรูปแบบออราเคิลของคุณเองใน Solidity รวมถึงกระบวนการร้องขอ-ตอบกลับ ผู้ปรับปรุงข้อมูลที่เชื่อถือได้ และการตรวจสอบความใหม่ของข้อมูลบนเชน

บทเรียน 4 จาก 413 ขั้นตอน

การสร้างสัญญาออราเคิลแบบกำหนดเอง เป็นบทเรียน Blockchain Smart Contracts with Solidity ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Blockchain Smart Contracts with Solidity และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Blockchain Smart Contracts with Solidity มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Beyond Third-Party Oracles

Chainlink and similar networks cover common feeds, but sometimes you need data nobody else provides, like a proprietary API result. In those cases you build a custom oracle: an off-chain updater plus an on-chain contract that stores and serves the data.

The Two-Party Design

A custom oracle has two sides:

  • Off-chain updater: a server that reads the external source and sends transactions.
  • On-chain contract: stores the latest value and exposes a read function for other contracts.

A Minimal Storage Oracle

The simplest oracle just lets a trusted address write a value that anyone can read.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract SimpleOracle {
    address public updater;
    uint256 public value;
    uint256 public lastUpdated;

    constructor() {
        updater = msg.sender;
    }

    function setValue(uint256 newValue) external {
        require(msg.sender == updater, 'not updater');
        value = newValue;
        lastUpdated = block.timestamp;
    }
}

Guarding the Updater

The single point of trust is the updater address. Protect it with access control and allow it to be rotated safely if a key is compromised.

function transferUpdater(address newUpdater) external {
    require(msg.sender == updater, 'not updater');
    require(newUpdater != address(0), 'zero address');
    updater = newUpdater;
}

Checking Data Freshness

Stale data is dangerous. Consumers should reject values older than a threshold using the stored lastUpdated timestamp.

uint256 public constant MAX_AGE = 1 hours;

function getFreshValue() external view returns (uint256) {
    require(block.timestamp - lastUpdated <= MAX_AGE, 'stale data');
    return value;
}

Request-Response Pattern

For on-demand data, a consumer emits a request event. The off-chain updater watches for it and replies with the answer in a callback.

event DataRequested(uint256 indexed requestId, string query);

uint256 private nextId;

function requestData(string calldata query) external returns (uint256) {
    uint256 id = nextId++;
    emit DataRequested(id, query);
    return id;
}

Fulfilling a Request

The updater calls back with the result for a given request id. Store it keyed by id so the consumer can read it later.

mapping(uint256 => uint256) public answers;
mapping(uint256 => bool) public fulfilled;

function fulfill(uint256 requestId, uint256 result) external {
    require(msg.sender == updater, 'not updater');
    answers[requestId] = result;
    fulfilled[requestId] = true;
}

Reducing Single-Source Risk

One updater is one point of failure. To harden the oracle you can require multiple updaters to agree (an aggregation or median of submissions) before a value is accepted.

mapping(address => bool) public isUpdater;
mapping(address => uint256) public submitted;
address[] public updaters;

function submit(uint256 v) external {
    require(isUpdater[msg.sender], 'not updater');
    submitted[msg.sender] = v;
}

Computing a Median

A median resists a single malicious updater better than an average. Collect submissions, sort them off-chain or with a small on-chain helper, and take the middle value.

function median(uint256[] memory data) internal pure returns (uint256) {
    // assume data already sorted
    uint256 n = data.length;
    if (n % 2 == 1) return data[n / 2];
    return (data[n / 2 - 1] + data[n / 2]) / 2;
}

Trust Tradeoffs

Custom oracles trade decentralization for flexibility. Document clearly who runs the updater, what data is sourced, and how consumers should react to staleness. For high-value DeFi, prefer established decentralized oracle networks.

Consumer Integration

A consumer contract simply imports the oracle interface and reads from it, applying its own freshness checks.

interface IOracle {
    function getFreshValue() external view returns (uint256);
}

contract Consumer {
    IOracle public oracle;
    constructor(address o) { oracle = IOracle(o); }
    function price() external view returns (uint256) {
        return oracle.getFreshValue();
    }
}

Quick Check

Test your grasp of custom oracle design.

Recap

You built a custom oracle with an off-chain updater and an on-chain store. Key ideas:

  • Trusted updater with rotatable access control
  • Freshness checks via lastUpdated
  • Request-response flow with events and callbacks
  • Multiple updaters and medians to reduce single-source risk

Use custom oracles for niche data, but prefer decentralized networks for high-stakes feeds.

เริ่มต้นได้ฟรี

เรียนรู้ Blockchain Smart Contracts with Solidity ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การสร้างสัญญาออราเคิลแบบกำหนดเอง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การสร้างสัญญาออราเคิลแบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การสร้างสัญญาออราเคิลแบบกำหนดเอง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Blockchain Smart Contracts with Solidity นี้ได้ไหม

ได้ บทเรียน Blockchain Smart Contracts with Solidity ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. อธิบายปัญหาออราเคิล
  2. การผสานรวมออราเคิล Chainlink
  3. การจัดการการดึงข้อมูลนอกเชน
  4. การสร้างสัญญาออราเคิลแบบกำหนดเอง
← กลับไปที่ Blockchain Smart Contracts with Solidity