Developers Forum for XinFin XDC Network

Atul Khekade
Atul Khekade

Posted on

Asking coderun.ai to generate USTY backed yield baring stablecoin

Below is an example of a smart contract for a yield-bearing stablecoin pegged to the price of US Dollars and collateralized by USTY tokens (tokenized US Treasury tokens by Yieldteq). The contract is written in Solidity, designed for deployment on the XinFin XDC Network, which supports EVM-compatible smart contracts.

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

interface IERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    function mint(address to, uint256 amount) external;
    function burn(address from, uint256 amount) external;
}

contract Stablecoin is IERC20 {
    string public constant name = "YieldUSD";
    string public constant symbol = "YUSD";
    uint8 public constant decimals = 18;

    address public admin;
    IERC20 public ustyToken;

    uint256 private _totalSupply;
    mapping(address => uint256) private _balances;
    mapping(address => mapping(address => uint256)) private _allowances;

    constructor(address _ustyAddress) {
        admin = msg.sender;
        ustyToken = IERC20(_ustyAddress);
    }

    function totalSupply() public view override returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address account) public view override returns (uint256) {
        return _balances[account];
    }

    function transfer(address recipient, uint256 amount) public override returns (bool) {
        _transfer(msg.sender, recipient, amount);
        return true;
    }

    function approve(address spender, uint256 amount) public override returns (bool) {
        _approve(msg.sender, spender, amount);
        return true;
    }

    function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, msg.sender, _allowances[sender][msg.sender] - amount);
        return true;
    }

    function mint(uint256 ustAmount) external {
        require(ustyToken.transferFrom(msg.sender, address(this), ustAmount), "USTY transfer failed");
        uint256 mintAmount = ustAmount; // 1:1 peg for simplicity in this example
        _mint(msg.sender, mintAmount);
    }

    function burn(uint256 yusdAmount) external {
        require(_balances[msg.sender] >= yusdAmount, "Insufficient YUSD balance");
        _burn(msg.sender, yusdAmount);
        ustyToken.transfer(msg.sender, yusdAmount); // Redeem collateral
    }

    function _mint(address account, uint256 amount) internal {
        _totalSupply += amount;
        _balances[account] += amount;
        emit Transfer(address(0), account, amount);
    }

    function _burn(address account, uint256 amount) internal {
        _balances[account] -= amount;
        _totalSupply -= amount;
        emit Transfer(account, address(0), amount);
    }

    function _transfer(address sender, address recipient, uint256 amount) internal {
        require(_balances[sender] >= amount, "Insufficient balance");
        _balances[sender] -= amount;
        _balances[recipient] += amount;
        emit Transfer(sender, recipient, amount);
    }

    function _approve(address owner, address spender, uint256 amount) internal {
        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

Enter fullscreen mode Exit fullscreen mode

Explanation:

ERC20/XRC20 Standard: The contract implements the ERC20 interface for fungibility.
Collateral: USTY tokens are used as collateral. Users can mint the stablecoin (YUSD) by depositing USTY tokens into the contract at a 1:1 ratio, reflecting the peg to USD.
Minting and Burning: Users can mint new YUSD by locking USTY tokens in the contract and burn YUSD to redeem their USTY tokens.
Admin Role: The admin address can be used to manage additional functionalities or upgrades, though not shown in this simple example for clarity.

This contract should be further developed with additional features such as handling exchange rates, fees, governance, and more complex interactions with the underlying collateral asset, depending on the specific requirements and regulatory compliance needs.

Discussion (0)