Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitcoin-Backed Stablecoin Smart Contract Implementation #1

Open
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

nicholas-source
Copy link
Owner

Overview

This pull request introduces a comprehensive Bitcoin-backed stablecoin smart contract for the Stacks blockchain, implementing a robust crypto-collateralized stablecoin system with advanced security and governance mechanisms.

Key Features

  • Full SIP-010 token trait compliance
  • Advanced Bitcoin price oracle integration
  • Secure vault management system
  • Comprehensive liquidation and redemption mechanisms
  • Flexible governance controls

Detailed Changes

Contract Architecture

  • Implemented SIP-010 token trait for standard compliance
  • Defined extensive error code constants
  • Created secure vault management system
  • Established governance parameters

Security Enhancements

  • Strict authorization checks
  • Comprehensive input validation
  • Multiple security thresholds
  • Prevent excessive minting and vault creation

Core Functionality

Vault Management

  • create-vault: Generate unique vaults with Bitcoin collateral
  • mint-stablecoin: Mint stablecoins against collateralized vaults
  • redeem-stablecoin: Redeem and burn stablecoins
  • liquidate-vault: Handle undercollateralized vault scenarios

Oracle and Price Management

  • add-btc-price-oracle: Add trusted price oracles
  • update-btc-price: Update Bitcoin price with timestamp
  • get-latest-btc-price: Retrieve most recent price

Governance Functions

  • update-collateralization-ratio: Dynamically adjust system parameters
  • Read-only functions for transparency:
    • get-vault-details
    • get-total-supply

Configuration Parameters

  • Collateralization
    • Minimum ratio: 150%
    • Liquidation threshold: 125%
  • Fees
    • Minting fee: 0.5%
    • Redemption fee: 0.5%
  • Limits
    • Maximum mint limit: 1,000,000 tokens
    • Maximum Bitcoin price: 1,000,000,000,000

Testing

  • Validated vault creation and management
  • Tested price oracle updates
  • Verified liquidation mechanisms
  • Confirmed governance function operations

Notes

  • Requires integration with Bitcoin price oracles
  • Recommended security audit before mainnet deployment

Future Improvements

  • Enhanced oracle redundancy
  • Dynamic fee mechanisms
  • Cross-chain collateral support

Checklist

  • Implement core stablecoin functionality
  • Add comprehensive error handling
  • Create governance mechanisms
  • Conduct thorough security audit
  • Develop comprehensive test suite
  • Create deployment scripts

- Imported SIP-010 token trait for token standard compliance.
- Defined error code constants for various error scenarios:
  - ERR-NOT-AUTHORIZED
  - ERR-INSUFFICIENT-BALANCE
  - ERR-INVALID-COLLATERAL
  - ERR-UNDERCOLLATERALIZED
  - ERR-ORACLE-PRICE-UNAVAILABLE
  - ERR-LIQUIDATION-FAILED
  - ERR-MINT-LIMIT-EXCEEDED
  - ERR-INVALID-PARAMETERS
- Defined CONTRACT-OWNER constant.
- Added stablecoin configuration variables:
  - stablecoin-name
  - stablecoin-symbol
  - total-supply
  - collateralization-ratio
  - liquidation-threshold
- Defined governance parameters:
  - mint-fee-bps (0.5% minting fee)
  - redemption-fee-bps (0.5% redemption fee)
  - max-mint-limit (limit to prevent excessive minting)
- Defined btc-price-oracles map to store authorized price oracles.
- Defined last-btc-price map to store the latest BTC price with timestamp.
- Defined vaults map to store vault details:
  - owner: principal
  - id: uint
  - collateral-amount: uint (BTC collateral)
  - stablecoin-minted: uint (minted stablecoin amount)
  - created-at: uint (timestamp of vault creation)
- Defined vault-counter variable to track the number of vaults.
- Implemented add-btc-price-oracle function to add authorized BTC price oracles:
  - Checks if the sender is the contract owner.
  - Adds the oracle to the btc-price-oracles map.
- Implemented update-btc-price function to update the BTC price:
  - Checks if the sender is an authorized BTC price oracle.
  - Updates the last-btc-price map with the new price and timestamp.
- Implemented get-latest-btc-price function to retrieve the latest BTC price from the last-btc-price map.
- Implemented create-vault function to create a new vault:
  - Generates a new vault ID by incrementing the vault counter.
  - Validates that the collateral amount is greater than zero.
  - Increments the vault counter.
  - Stores the new vault details in the vaults map.
  - Returns the new vault ID.
- Implemented mint-stablecoin function to mint stablecoins against a vault's collateral:
  - Retrieves vault details.
  - Gets the latest BTC price.
  - Calculates the maximum mintable amount based on the collateral.
  - Validates minting conditions to ensure sufficient collateral and adherence to mint limits.
  - Updates the vault with the minted amount.
  - Updates the total supply of stablecoins.
- Implemented liquidate-vault function to handle vault liquidation:
  - Retrieves vault details.
  - Gets the latest BTC price.
  - Calculates the current collateralization ratio.
  - Checks if the vault is below the liquidation threshold.
  - Seizes collateral and burns minted stablecoins.
  - Updates the total supply of stablecoins.
  - Removes the vault from the vaults map.
- Implemented redeem-stablecoin function to redeem stablecoins:
  - Retrieves vault details.
  - Validates the redemption amount against the minted stablecoins.
  - Updates the vault with the redeemed amount.
  - Updates the total supply of stablecoins.
- Implemented update-collateralization-ratio function:
  - Checks if the sender is the contract owner.
  - Validates the new ratio to be within the acceptable range (100% to 300%).
  - Updates the collateralization ratio.
- Implemented get-vault-details function to retrieve details of a specific vault:
  - Takes vault-owner and vault-id as parameters.
  - Returns the vault details from the vaults map.
- Implemented get-total-supply function to retrieve the total supply of stablecoins:
  - Returns the value of the total-supply variable.
… branch

- Added comprehensive overview of the Bitcoin-backed stablecoin system.
- Detailed features including vault management, security, and oracle integration.
- Described key components of the contract architecture.
- Included error handling scenarios.
- Documented key functions: create-vault, mint-stablecoin, redeem-stablecoin, liquidate-vault, and governance functions.
- Listed configuration parameters and security considerations.
- Provided installation and deployment steps.
- Suggested potential improvements.
- Added disclaimer, license, and contributing guidelines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant