Skip to content

aishaa-umar/Digital-Token-Issuance-Contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

Digital Token Issuance Contract

Overview

The Digital Token Issuance Contract is a robust Clarity smart contract that enables the creation and management of collateral-backed digital tokens on the Stacks blockchain. Users can mint digital tokens by locking STX as collateral, with built-in liquidation mechanisms to maintain system solvency.

Key Features

  • Collateralized Token Minting: Create digital tokens backed by STX collateral
  • Multiple Token Support: Issue and manage various digital token types
  • Oracle Price Feeds: Integrate external price data for accurate valuations
  • Liquidation Protection: Automatic liquidation of under-collateralized positions
  • Emergency Controls: Admin functions to pause/resume the system

Core Concepts

Collateral Ratio

  • Minimum Ratio: 150% (users must deposit $1.50 worth of STX for every $1.00 of tokens minted)
  • Liquidation Threshold: 120% (positions below this ratio can be liquidated)
  • Liquidation Penalty: 10% of collateral goes to the liquidator

System Roles

  • Admin: Contract owner who can create tokens, authorize oracles, and manage system state
  • Oracle: Authorized addresses that can update token prices
  • Users: Anyone can open positions, mint tokens, and participate in liquidations

Main Functions

Administrative Functions

register-digital-token

Creates a new digital token with specified name, symbol, and initial price.

(register-digital-token "Bitcoin Synthetic" "sBTC" u50000)

Parameters:

  • name: Token name (max 32 characters)
  • symbol: Token symbol (max 10 characters)
  • initial-price: Starting price in base units

Returns: Token ID (uint)

authorize-price-feed

Authorizes an oracle address to update token prices.

(authorize-price-feed 'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7)

system-freeze / system-resume

Emergency controls to pause or resume all contract operations.

Oracle Functions

modify-price

Updates the price of a specific token (oracle-only).

(modify-price u1 u51000)

Parameters:

  • token-id: ID of the token to update
  • new-price: New price value

User Functions

establish-holding

Opens a new collateralized position and mints digital tokens.

(establish-holding u1 u15000 u100)

Parameters:

  • token-id: ID of the token to mint
  • collateral-amount: Amount of STX to lock as collateral
  • synthetic-amount: Amount of digital tokens to mint

Requirements:

  • Collateral ratio must be ≥ 150%
  • System must not be frozen
  • Sufficient STX balance

terminate-holding

Closes an existing position and returns collateral.

(terminate-holding u1)

Returns: All locked STX collateral to the user

deposit-collateral

Adds additional STX collateral to an existing position.

(deposit-collateral u1 u5000)

Use Case: Increase collateral ratio to avoid liquidation

execute-liquidation

Liquidates an under-collateralized position (anyone can call).

(execute-liquidation 'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7 u1)

Parameters:

  • user: Address of the position owner
  • token-id: ID of the token

Rewards: Liquidator receives 10% of the collateral as a penalty fee

Read-Only Functions

get-token-details

Retrieves information about a specific digital token.

get-account-holding

Returns a user's position details for a given token.

get-user-balance

Gets the digital token balance for a user.

check-liquidation-eligibility

Checks if a position is eligible for liquidation.

get-system-metrics

Returns overall system statistics (total supply, paused status, liquidation penalty).

Error Codes

Code Constant Description
u100 ERROR_NOT_AUTHORIZED Caller lacks required permissions
u101 ERROR_COLLATERAL_TOO_LOW Insufficient collateral for operation
u102 ERROR_HOLDING_NOT_FOUND Position does not exist
u103 ERROR_INVALID_VALUE Invalid amount or parameter
u104 ERROR_LIQUIDATION_LIMIT Position not eligible for liquidation
u105 ERROR_PRICE_FEED_FAILURE Oracle price update failed
u106 ERROR_TOKEN_NOT_FOUND Token ID does not exist
u107 ERROR_INVALID_PARAMETER Invalid input parameter
u108 ERROR_EMPTY_TEXT Empty string provided

Usage Example

Creating and Using a Digital Token

  1. Admin creates a token:
(contract-call? .digital-token-contract register-digital-token "Gold Synthetic" "sGOLD" u200000)
;; Returns: (ok u1)
  1. Admin authorizes an oracle:
(contract-call? .digital-token-contract authorize-price-feed 'SP2ORACLE...)
  1. User opens a position:
(contract-call? .digital-token-contract establish-holding u1 u300000 u1000)
;; Locks 300,000 STX to mint 1,000 sGOLD tokens
;; Collateral ratio: (300,000 / (1,000 * 200,000)) * 100 = 150%
  1. Oracle updates price:
(contract-call? .digital-token-contract modify-price u1 u250000)
;; Gold price increases to 250,000
;; Collateral ratio drops to 120% (liquidation threshold)
  1. Liquidator executes liquidation:
(contract-call? .digital-token-contract execute-liquidation 'SPUSER... u1)
;; Liquidator receives 10% penalty, user gets remaining collateral

Security Considerations

  • Price Oracle Risk: System depends on accurate price feeds from authorized oracles
  • Collateral Volatility: STX price fluctuations can affect position health
  • Front-Running: Liquidations are first-come-first-served and may be front-run
  • Admin Control: Contract owner has significant control over system parameters

Testing Recommendations

Before deploying to mainnet:

  1. Test all functions with various collateral ratios
  2. Simulate liquidation scenarios
  3. Verify oracle price update mechanisms
  4. Test emergency pause/resume functionality
  5. Audit for potential reentrancy or overflow issues

About

This is a sophisticated decentralized finance protocol built on the Stacks blockchain that enables users to create collateral-backed digital tokens. By locking STX cryptocurrency as collateral, users can mint synthetic tokens that represent various assets, creating a flexible and secure system for tokenized asset exposure without direct ownership.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors