Lesson 4

在 Remix IDE 中实现合成资产合约

在本章中,我们将添加铸造和兑换合成资产的功能,对SyntheticAsset.sol合约进行扩展。这些功能对于实现合成资产在现实场景中的应用至关重要。我们将对代码逐步分析,了解这些功能是如何实现的。

扩展合约

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 费。

  1. Metamask:确保您的 Metamask 已切换到 Goerli 测试网。

  2. 进入 Goerli faucet,获取GoerliETH。只需粘贴您的 Goerli 测试网以太坊地址,您将很快收到测试 ETH。

配置 Remix 用于 Goerli 测试网部署

  1. 在 Remix 中,进入Deploy & Run Transactions

  2. Environment(环境)中,选择Injected Provider。Remix 将连接到您的 Metamask 当前设置的任何网络(此处为 Goerli 测试网)。

  3. Contract 下拉列表中选择OracleIntegratedContract

  4. Account 下拉列表中,您会看到 Goerli 测试网以太坊地址。

编译和部署扩展合约

准备好更新的功能后,我们将按照第三章中讲到的步骤编译和部署合约。

运行扩展合约

部署好扩展合约后:

  1. 用不同金额调用mintSyntheticAssetredeemSyntheticAsset函数,分别用于创建和赎回合成资产。

  2. 在 Remix IDE 中观察syntheticBalancetotalSyntheticSupplycollateral的变化。

观察合约行为

运行合约并观察状态变量和整体合约状态的变化。手动操作将加深您对合成资产概念和实践的理解。

在我们不断用基本功能丰富合约的时候,我们离一个实际的合成资产合约越来越近。在下一章中,我们将对合约进行测试,确保它在各种场景下都能按预期运行。对合成资产的实际理解将随着我们课程的进行而不断加深。请继续关注后续课程中的更多实践内容!

Disclaimer
* Crypto investment involves significant risks. Please proceed with caution. The course is not intended as investment advice.
* The course is created by the author who has joined Gate Learn. Any opinion shared by the author does not represent Gate Learn.
Catalog
Lesson 4

在 Remix IDE 中实现合成资产合约

在本章中,我们将添加铸造和兑换合成资产的功能,对SyntheticAsset.sol合约进行扩展。这些功能对于实现合成资产在现实场景中的应用至关重要。我们将对代码逐步分析,了解这些功能是如何实现的。

扩展合约

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 费。

  1. Metamask:确保您的 Metamask 已切换到 Goerli 测试网。

  2. 进入 Goerli faucet,获取GoerliETH。只需粘贴您的 Goerli 测试网以太坊地址,您将很快收到测试 ETH。

配置 Remix 用于 Goerli 测试网部署

  1. 在 Remix 中,进入Deploy & Run Transactions

  2. Environment(环境)中,选择Injected Provider。Remix 将连接到您的 Metamask 当前设置的任何网络(此处为 Goerli 测试网)。

  3. Contract 下拉列表中选择OracleIntegratedContract

  4. Account 下拉列表中,您会看到 Goerli 测试网以太坊地址。

编译和部署扩展合约

准备好更新的功能后,我们将按照第三章中讲到的步骤编译和部署合约。

运行扩展合约

部署好扩展合约后:

  1. 用不同金额调用mintSyntheticAssetredeemSyntheticAsset函数,分别用于创建和赎回合成资产。

  2. 在 Remix IDE 中观察syntheticBalancetotalSyntheticSupplycollateral的变化。

观察合约行为

运行合约并观察状态变量和整体合约状态的变化。手动操作将加深您对合成资产概念和实践的理解。

在我们不断用基本功能丰富合约的时候,我们离一个实际的合成资产合约越来越近。在下一章中,我们将对合约进行测试,确保它在各种场景下都能按预期运行。对合成资产的实际理解将随着我们课程的进行而不断加深。请继续关注后续课程中的更多实践内容!

Disclaimer
* Crypto investment involves significant risks. Please proceed with caution. The course is not intended as investment advice.
* The course is created by the author who has joined Gate Learn. Any opinion shared by the author does not represent Gate Learn.