Solidity
// Add these state variables to your contract
mapping(address => uint256) public syntheticBalance;
uint256 public totalSyntheticSupply;
在以上代碼中:
syntheticBalance
是一個映射,用於跟蹤每個地址的合成資産餘額。totalSyntheticSupply
是一個變量,用於跟蹤流通中的合成資産總供應量。Solidity
// Update the mintSyntheticAsset function
function mintSyntheticAsset(uint256 _amount) public {
require(collateral >= _amount, "Insufficient collateral");
syntheticBalance[msg.sender] += _amount;
totalSyntheticSupply += _amount;
collateral -= _amount;
}
在mintSyntheticAsset
函數中:
require
語句檢查是否有足夠的抵押品來鑄造所要求的合成資産。msg.sender
)的合成資産餘額增加了_amount
。_amount
。collateral
(抵押品)由於用於支持新鑄造的合成資産,因此減少了_amount
。Solidity
// Update the redeemSyntheticAsset function
function redeemSyntheticAsset(uint256 _amount) public {
require(syntheticBalance[msg.sender] >= _amount, "Insufficient synthetic balance");
syntheticBalance[msg.sender] -= _amount;
totalSyntheticSupply -= _amount;
collateral += _amount;
}
在redeemSyntheticAsset
函數中:
require
語句檢查調用者是否有足夠的合成資産餘額來贖回。_amount
。_amount
。_amount
,因爲抵押品在合成資産贖回時會釋放回來。TypeScript
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SyntheticAsset {
uint256 public underlyingAssetPrice;
uint256 public collateral;
address public owner;
mapping(address => uint256) public syntheticBalance;
uint256 public totalSyntheticSupply;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Not the contract owner");
_;
}
function updatePrice(uint256 _price) public onlyOwner {
underlyingAssetPrice = _price;
}
function depositCollateral(uint256 _amount) public {
collateral += _amount;
}
function withdrawCollateral(uint256 _amount) public {
require(collateral >= _amount, "Insufficient collateral");
collateral -= _amount;
}
function getSyntheticAssetPrice() public view returns (uint256) {
return underlyingAssetPrice;
}
function mintSyntheticAsset(uint256 _amount) public {
require(collateral >= _amount, "Insufficient collateral");
syntheticBalance[msg.sender] += _amount;
totalSyntheticSupply += _amount;
collateral -= _amount;
}
function redeemSyntheticAsset(uint256 _amount) public {
require(syntheticBalance[msg.sender] >= _amount, "Insufficient synthetic balance");
syntheticBalance[msg.sender] -= _amount;
totalSyntheticSupply -= _amount;
collateral += _amount;
}
}
要在 Goerli 測試網上部署合約,我們需要 Goerli ETH(GoerliETH)。雖然它沒有任何實際價值,但我們需要用它支付測試網上的 gas 費。
Metamask:確保您的 Metamask 已切換到 Goerli 測試網。
進入 Goerli faucet,穫取GoerliETH。隻需粘貼您的 Goerli 測試網以太坊地址,您將很快收到測試 ETH。
在 Remix 中,進入Deploy & Run Transactions
。
在Environment
(環境)中,選擇Injected Provider
。Remix 將連接到您的 Metamask 當前設置的任何網絡(此處爲 Goerli 測試網)。
在Contract
下拉列錶中選擇OracleIntegratedContract
。
在Account
下拉列錶中,您會看到 Goerli 測試網以太坊地址。
準備好更新的功能後,我們將按照第三章中講到的步驟編譯和部署合約。
部署好擴展合約後:
用不衕金額調用mintSyntheticAsset
和redeemSyntheticAsset
函數,分別用於創建和贖回合成資産。
在 Remix IDE 中觀察syntheticBalance
、totalSyntheticSupply
和collateral
的變化。
運行合約併觀察狀態變量和整體合約狀態的變化。手動操作將加深您對合成資産概念和實踐的理解。
在我們不斷用基本功能豐富合約的時候,我們離一個實際的合成資産合約越來越近。在下一章中,我們將對合約進行測試,確保它在各種場景下都能按預期運行。對合成資産的實際理解將隨著我們課程的進行而不斷加深。請繼續關註後續課程中的更多實踐內容!
Solidity
// Add these state variables to your contract
mapping(address => uint256) public syntheticBalance;
uint256 public totalSyntheticSupply;
在以上代碼中:
syntheticBalance
是一個映射,用於跟蹤每個地址的合成資産餘額。totalSyntheticSupply
是一個變量,用於跟蹤流通中的合成資産總供應量。Solidity
// Update the mintSyntheticAsset function
function mintSyntheticAsset(uint256 _amount) public {
require(collateral >= _amount, "Insufficient collateral");
syntheticBalance[msg.sender] += _amount;
totalSyntheticSupply += _amount;
collateral -= _amount;
}
在mintSyntheticAsset
函數中:
require
語句檢查是否有足夠的抵押品來鑄造所要求的合成資産。msg.sender
)的合成資産餘額增加了_amount
。_amount
。collateral
(抵押品)由於用於支持新鑄造的合成資産,因此減少了_amount
。Solidity
// Update the redeemSyntheticAsset function
function redeemSyntheticAsset(uint256 _amount) public {
require(syntheticBalance[msg.sender] >= _amount, "Insufficient synthetic balance");
syntheticBalance[msg.sender] -= _amount;
totalSyntheticSupply -= _amount;
collateral += _amount;
}
在redeemSyntheticAsset
函數中:
require
語句檢查調用者是否有足夠的合成資産餘額來贖回。_amount
。_amount
。_amount
,因爲抵押品在合成資産贖回時會釋放回來。TypeScript
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SyntheticAsset {
uint256 public underlyingAssetPrice;
uint256 public collateral;
address public owner;
mapping(address => uint256) public syntheticBalance;
uint256 public totalSyntheticSupply;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Not the contract owner");
_;
}
function updatePrice(uint256 _price) public onlyOwner {
underlyingAssetPrice = _price;
}
function depositCollateral(uint256 _amount) public {
collateral += _amount;
}
function withdrawCollateral(uint256 _amount) public {
require(collateral >= _amount, "Insufficient collateral");
collateral -= _amount;
}
function getSyntheticAssetPrice() public view returns (uint256) {
return underlyingAssetPrice;
}
function mintSyntheticAsset(uint256 _amount) public {
require(collateral >= _amount, "Insufficient collateral");
syntheticBalance[msg.sender] += _amount;
totalSyntheticSupply += _amount;
collateral -= _amount;
}
function redeemSyntheticAsset(uint256 _amount) public {
require(syntheticBalance[msg.sender] >= _amount, "Insufficient synthetic balance");
syntheticBalance[msg.sender] -= _amount;
totalSyntheticSupply -= _amount;
collateral += _amount;
}
}
要在 Goerli 測試網上部署合約,我們需要 Goerli ETH(GoerliETH)。雖然它沒有任何實際價值,但我們需要用它支付測試網上的 gas 費。
Metamask:確保您的 Metamask 已切換到 Goerli 測試網。
進入 Goerli faucet,穫取GoerliETH。隻需粘貼您的 Goerli 測試網以太坊地址,您將很快收到測試 ETH。
在 Remix 中,進入Deploy & Run Transactions
。
在Environment
(環境)中,選擇Injected Provider
。Remix 將連接到您的 Metamask 當前設置的任何網絡(此處爲 Goerli 測試網)。
在Contract
下拉列錶中選擇OracleIntegratedContract
。
在Account
下拉列錶中,您會看到 Goerli 測試網以太坊地址。
準備好更新的功能後,我們將按照第三章中講到的步驟編譯和部署合約。
部署好擴展合約後:
用不衕金額調用mintSyntheticAsset
和redeemSyntheticAsset
函數,分別用於創建和贖回合成資産。
在 Remix IDE 中觀察syntheticBalance
、totalSyntheticSupply
和collateral
的變化。
運行合約併觀察狀態變量和整體合約狀態的變化。手動操作將加深您對合成資産概念和實踐的理解。
在我們不斷用基本功能豐富合約的時候,我們離一個實際的合成資産合約越來越近。在下一章中,我們將對合約進行測試,確保它在各種場景下都能按預期運行。對合成資産的實際理解將隨著我們課程的進行而不斷加深。請繼續關註後續課程中的更多實踐內容!