ในขณะที่เราเริ่มต้นในส่วนขั้นสูงของหลักสูตรของเรา เราจะใช้เวลาสักครู่เพื่อทบทวนสัญญาสินทรัพย์สังเคราะห์ที่เราพัฒนาในส่วนแรก สัญญานี้ทำหน้าที่เป็นรากฐานที่เราจะสร้างความเข้าใจในการจัดการและอัปเกรดสัญญาอัจฉริยะ
ด้านล่างนี้คือสัญญาสินทรัพย์สังเคราะห์ที่เราดำเนินการก่อนหน้านี้ สัญญานี้อำนวยความสะดวกในการสร้าง การจัดการ และการโต้ตอบกับสินทรัพย์สังเคราะห์บนบล็อกเชน
ความแข็งแกร่ง
// SPDX-License-Identifier: ความแข็งแกร่งของ MIT
pragma ^0.8.0;
สัญญา SyntheticAsset {
uint256 สาธารณะที่อยู่ภายใต้AssetPrice;
uint256 หลักประกันสาธารณะ;
ที่อยู่สาธารณะเจ้าของ;
การทำแผนที่ (ที่อยู่ => uint256) สมดุลสังเคราะห์สาธารณะ;
uint256 สาธารณะ TotalSyntheticSupply;
Constructor() {
owner = msg.sender;
}
ตัวแก้ไข onlyOwner() {
require(msg.sender == owner, "Not the contract owner");
_;
}
ฟังก์ชั่น updatePrice(uint256 _price) public onlyOwner {
underlyingAssetPrice = _price;
}
ฟังก์ชั่นDepositCollateral(uint256 _amount) public {
collateral += _amount;
}
function withdrawCollateral(uint256 _amount) public {
require(collateral >= _amount, "Insufficient collateral");
collateral -= _amount;
}
ฟังก์ชั่น getSyntheticAssetPrice() มุมมองสาธารณะส่งคืน (uint256) {
return underlyingAssetPrice;
}
ฟังก์ชั่น mintSyntheticAsset(uint256 _amount) สาธารณะ {
require(collateral >= _amount, "Insufficient collateral");
syntheticBalance[msg.sender] += _amount;
totalSyntheticSupply += _amount;
collateral -= _amount;
}
ฟังก์ชั่น redSyntheticAsset(uint256 _amount) สาธารณะ {
require(syntheticBalance[msg.sender] >= _amount, "Insufficient synthetic balance");
syntheticBalance[msg.sender] -= _amount;
totalSyntheticSupply -= _amount;
collateral += _amount;
}
}
ตอนนี้ ด้วยความเข้าใจที่สดชื่นเกี่ยวกับสัญญาพื้นฐานของเรา เรามาเจาะลึกแง่มุมต่างๆ ของการจัดการและการอัพเกรดสัญญาสินทรัพย์สังเคราะห์กันดีกว่า
onlyOwner
เพื่อจำกัดการเข้าถึงฟังก์ชันบางอย่างถือเป็นแนวทางปฏิบัติทั่วไปSolidity
Contract Proxy {
กล่าวถึงการใช้งานสาธารณะ;
ฟังก์ชั่น UpgradeImplementation (ที่อยู่ _newImplementation) สาธารณะ onlyOwner {
implementation = _newImplementation;
}
ทางเลือก () เจ้าหนี้ภายนอก {
ที่อยู่ impl = การใช้งาน;
แอสเซมบลี {
ให้ ptr := mload(0x40)
calldatacopy(ptr, 0, calldatasize())
ให้ผลลัพธ์ := delegatecall(gas(), impl, ptr, calldatasize(), 0, 0)
ให้ขนาด := returndatasize()
returndatacopy(ptr, 0, size)
ผลลัพธ์การสลับ
กรณี 0 { revert(ptr, size) }
ค่าเริ่มต้น { return(ptr, size) }
}
}
}
ความแข็งแกร่ง
// SPDX-License-Identifier: ความแข็งแกร่งของ MIT
pragma ^0.8.0; พร็อกซีสัญญา
รายการ {
กล่าวถึงการใช้งานสาธารณะ;
กล่าวถึงเจ้าของสาธารณะ;
ตัวสร้าง () {
owner = msg.sender;
}
ตัวแก้ไข onlyOwner () {
require(msg.sender == owner, "Not the contract owner");
_;
}
ฟังก์ชั่น upgradeImplementation (ที่อยู่ _newImplementation) สาธารณะ onlyOwner {
implementation = _newImplementation;
}
ทางเลือกสำรอง () การชำระภายนอก {
ที่อยู่ impl = การใช้งาน;
แอสเซมบลี {
ให้ ptr := mload(0x40)
calldatacopy(ptr, 0, calldatasize())
ให้ผลลัพธ์ := delegatecall(gas(), impl, ptr, calldatasize(), 0, 0)
ให้ขนาด := returndatasize()
returndatacopy(ptr, 0, size)
ผลลัพธ์การสลับ
กรณี 0 { revert(ptr, size) }
ค่าเริ่มต้น { return(ptr, size) }
}
}
}
สัญญา SyntheticAsset {
uint256 สาธารณะที่อยู่ภายใต้ราคาสินทรัพย์;
uint256 หลักประกันสาธารณะ;
กล่าวถึงเจ้าของสาธารณะ;
การทำแผนที่ (ที่อยู่ => uint256) สมดุลสังเคราะห์สาธารณะ;
uint256 สาธารณะ TotalSyntheticSupply;
ตัวสร้าง() {
owner = msg.sender;
}
ตัวแก้ไข onlyOwner() {
require(msg.sender == owner, "Not the contract owner");
_;
}
ฟังก์ชั่น updatePrice(uint256 _price) สาธารณะ onlyOwner {
underlyingAssetPrice = _price;
}
ฟังก์ชั่นDepositCollateral(uint256 _amount) สาธารณะ {
collateral += _amount;
}
ฟังก์ชั่น withdrawCollateral(uint256 _amount) สาธารณะ {
require(collateral >= _amount, "Insufficient collateral");
collateral -= _amount;
}
ฟังก์ชั่น getSyntheticAssetPrice() ส่งคืนมุมมองสาธารณะ (uint256) {
return underlyingAssetPrice;
}
ฟังก์ชั่น mintSyntheticAsset(uint256 _amount) สาธารณะ {
require(collateral >= _amount, "Insufficient collateral");
syntheticBalance[msg.sender] += _amount;
totalSyntheticSupply += _amount;
collateral -= _amount;
}
ฟังก์ชั่น redSyntheticAsset(uint256 _amount) สาธารณะ {
require(syntheticBalance[msg.sender] >= _amount, "Insufficient synthetic balance");
syntheticBalance[msg.sender] -= _amount;
totalSyntheticSupply -= _amount;
collateral += _amount;
}
}
ในการตั้งค่านี้ สัญญา Proxy
จะทำหน้าที่เป็นเกตเวย์สำหรับสัญญา SyntheticAsset
ช่วยให้สามารถอัปเกรดการใช้งาน (เช่น ตรรกะ) ของ SyntheticAsset
ได้โดยไม่สูญเสียสถานะของสัญญา (เช่น ข้อมูล) ซึ่งสามารถทำได้ผ่านฟังก์ชัน fallback
ใน Proxy
ซึ่งมอบหมายการเรียกไปยังการใช้งาน SyntheticAsset
ในปัจจุบัน และฟังก์ชัน upgradeImplementation
ซึ่งช่วยให้เจ้าของสามารถเปลี่ยนที่อยู่การใช้งานเพื่อชี้ไปยัง SyntheticAsset
เวอร์ชันใหม่
ในบทเรียนนี้ คุณจะได้รับความเชี่ยวชาญที่จำเป็นในการจัดการและอัปเกรดสัญญาสินทรัพย์สังเคราะห์ เพื่อให้มั่นใจว่าสัญญาเหล่านี้ยังคงสามารถปรับให้เข้ากับข้อกำหนดที่เปลี่ยนแปลงไป ในขณะเดียวกันก็รักษาความสมบูรณ์และความปลอดภัยของสัญญา ความรู้พื้นฐานนี้จะเป็นประโยชน์เมื่อเราเจาะลึกเข้าไปในแง่มุมขั้นสูงของการจัดการสินทรัพย์สังเคราะห์ในบทเรียนต่อๆ ไป คอยติดตาม!
ในขณะที่เราเริ่มต้นในส่วนขั้นสูงของหลักสูตรของเรา เราจะใช้เวลาสักครู่เพื่อทบทวนสัญญาสินทรัพย์สังเคราะห์ที่เราพัฒนาในส่วนแรก สัญญานี้ทำหน้าที่เป็นรากฐานที่เราจะสร้างความเข้าใจในการจัดการและอัปเกรดสัญญาอัจฉริยะ
ด้านล่างนี้คือสัญญาสินทรัพย์สังเคราะห์ที่เราดำเนินการก่อนหน้านี้ สัญญานี้อำนวยความสะดวกในการสร้าง การจัดการ และการโต้ตอบกับสินทรัพย์สังเคราะห์บนบล็อกเชน
ความแข็งแกร่ง
// SPDX-License-Identifier: ความแข็งแกร่งของ MIT
pragma ^0.8.0;
สัญญา SyntheticAsset {
uint256 สาธารณะที่อยู่ภายใต้AssetPrice;
uint256 หลักประกันสาธารณะ;
ที่อยู่สาธารณะเจ้าของ;
การทำแผนที่ (ที่อยู่ => uint256) สมดุลสังเคราะห์สาธารณะ;
uint256 สาธารณะ TotalSyntheticSupply;
Constructor() {
owner = msg.sender;
}
ตัวแก้ไข onlyOwner() {
require(msg.sender == owner, "Not the contract owner");
_;
}
ฟังก์ชั่น updatePrice(uint256 _price) public onlyOwner {
underlyingAssetPrice = _price;
}
ฟังก์ชั่นDepositCollateral(uint256 _amount) public {
collateral += _amount;
}
function withdrawCollateral(uint256 _amount) public {
require(collateral >= _amount, "Insufficient collateral");
collateral -= _amount;
}
ฟังก์ชั่น getSyntheticAssetPrice() มุมมองสาธารณะส่งคืน (uint256) {
return underlyingAssetPrice;
}
ฟังก์ชั่น mintSyntheticAsset(uint256 _amount) สาธารณะ {
require(collateral >= _amount, "Insufficient collateral");
syntheticBalance[msg.sender] += _amount;
totalSyntheticSupply += _amount;
collateral -= _amount;
}
ฟังก์ชั่น redSyntheticAsset(uint256 _amount) สาธารณะ {
require(syntheticBalance[msg.sender] >= _amount, "Insufficient synthetic balance");
syntheticBalance[msg.sender] -= _amount;
totalSyntheticSupply -= _amount;
collateral += _amount;
}
}
ตอนนี้ ด้วยความเข้าใจที่สดชื่นเกี่ยวกับสัญญาพื้นฐานของเรา เรามาเจาะลึกแง่มุมต่างๆ ของการจัดการและการอัพเกรดสัญญาสินทรัพย์สังเคราะห์กันดีกว่า
onlyOwner
เพื่อจำกัดการเข้าถึงฟังก์ชันบางอย่างถือเป็นแนวทางปฏิบัติทั่วไปSolidity
Contract Proxy {
กล่าวถึงการใช้งานสาธารณะ;
ฟังก์ชั่น UpgradeImplementation (ที่อยู่ _newImplementation) สาธารณะ onlyOwner {
implementation = _newImplementation;
}
ทางเลือก () เจ้าหนี้ภายนอก {
ที่อยู่ impl = การใช้งาน;
แอสเซมบลี {
ให้ ptr := mload(0x40)
calldatacopy(ptr, 0, calldatasize())
ให้ผลลัพธ์ := delegatecall(gas(), impl, ptr, calldatasize(), 0, 0)
ให้ขนาด := returndatasize()
returndatacopy(ptr, 0, size)
ผลลัพธ์การสลับ
กรณี 0 { revert(ptr, size) }
ค่าเริ่มต้น { return(ptr, size) }
}
}
}
ความแข็งแกร่ง
// SPDX-License-Identifier: ความแข็งแกร่งของ MIT
pragma ^0.8.0; พร็อกซีสัญญา
รายการ {
กล่าวถึงการใช้งานสาธารณะ;
กล่าวถึงเจ้าของสาธารณะ;
ตัวสร้าง () {
owner = msg.sender;
}
ตัวแก้ไข onlyOwner () {
require(msg.sender == owner, "Not the contract owner");
_;
}
ฟังก์ชั่น upgradeImplementation (ที่อยู่ _newImplementation) สาธารณะ onlyOwner {
implementation = _newImplementation;
}
ทางเลือกสำรอง () การชำระภายนอก {
ที่อยู่ impl = การใช้งาน;
แอสเซมบลี {
ให้ ptr := mload(0x40)
calldatacopy(ptr, 0, calldatasize())
ให้ผลลัพธ์ := delegatecall(gas(), impl, ptr, calldatasize(), 0, 0)
ให้ขนาด := returndatasize()
returndatacopy(ptr, 0, size)
ผลลัพธ์การสลับ
กรณี 0 { revert(ptr, size) }
ค่าเริ่มต้น { return(ptr, size) }
}
}
}
สัญญา SyntheticAsset {
uint256 สาธารณะที่อยู่ภายใต้ราคาสินทรัพย์;
uint256 หลักประกันสาธารณะ;
กล่าวถึงเจ้าของสาธารณะ;
การทำแผนที่ (ที่อยู่ => uint256) สมดุลสังเคราะห์สาธารณะ;
uint256 สาธารณะ TotalSyntheticSupply;
ตัวสร้าง() {
owner = msg.sender;
}
ตัวแก้ไข onlyOwner() {
require(msg.sender == owner, "Not the contract owner");
_;
}
ฟังก์ชั่น updatePrice(uint256 _price) สาธารณะ onlyOwner {
underlyingAssetPrice = _price;
}
ฟังก์ชั่นDepositCollateral(uint256 _amount) สาธารณะ {
collateral += _amount;
}
ฟังก์ชั่น withdrawCollateral(uint256 _amount) สาธารณะ {
require(collateral >= _amount, "Insufficient collateral");
collateral -= _amount;
}
ฟังก์ชั่น getSyntheticAssetPrice() ส่งคืนมุมมองสาธารณะ (uint256) {
return underlyingAssetPrice;
}
ฟังก์ชั่น mintSyntheticAsset(uint256 _amount) สาธารณะ {
require(collateral >= _amount, "Insufficient collateral");
syntheticBalance[msg.sender] += _amount;
totalSyntheticSupply += _amount;
collateral -= _amount;
}
ฟังก์ชั่น redSyntheticAsset(uint256 _amount) สาธารณะ {
require(syntheticBalance[msg.sender] >= _amount, "Insufficient synthetic balance");
syntheticBalance[msg.sender] -= _amount;
totalSyntheticSupply -= _amount;
collateral += _amount;
}
}
ในการตั้งค่านี้ สัญญา Proxy
จะทำหน้าที่เป็นเกตเวย์สำหรับสัญญา SyntheticAsset
ช่วยให้สามารถอัปเกรดการใช้งาน (เช่น ตรรกะ) ของ SyntheticAsset
ได้โดยไม่สูญเสียสถานะของสัญญา (เช่น ข้อมูล) ซึ่งสามารถทำได้ผ่านฟังก์ชัน fallback
ใน Proxy
ซึ่งมอบหมายการเรียกไปยังการใช้งาน SyntheticAsset
ในปัจจุบัน และฟังก์ชัน upgradeImplementation
ซึ่งช่วยให้เจ้าของสามารถเปลี่ยนที่อยู่การใช้งานเพื่อชี้ไปยัง SyntheticAsset
เวอร์ชันใหม่
ในบทเรียนนี้ คุณจะได้รับความเชี่ยวชาญที่จำเป็นในการจัดการและอัปเกรดสัญญาสินทรัพย์สังเคราะห์ เพื่อให้มั่นใจว่าสัญญาเหล่านี้ยังคงสามารถปรับให้เข้ากับข้อกำหนดที่เปลี่ยนแปลงไป ในขณะเดียวกันก็รักษาความสมบูรณ์และความปลอดภัยของสัญญา ความรู้พื้นฐานนี้จะเป็นประโยชน์เมื่อเราเจาะลึกเข้าไปในแง่มุมขั้นสูงของการจัดการสินทรัพย์สังเคราะห์ในบทเรียนต่อๆ ไป คอยติดตาม!