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
的变化。
运行合约并观察状态变量和整体合约状态的变化。手动操作将加深您对合成资产概念和实践的理解。
在我们不断用基本功能丰富合约的时候,我们离一个实际的合成资产合约越来越近。在下一章中,我们将对合约进行测试,确保它在各种场景下都能按预期运行。对合成资产的实际理解将随着我们课程的进行而不断加深。请继续关注后续课程中的更多实践内容!