StableXCoin is a robust, decentralized, and overcollateralized stablecoin protocol designed to maintain a stable peg to the US Dollar. Inspired by the resilience and mechanics of systems like MakerDAO, StableXCoin empowers users to mint synthetic USD-pegged tokens (SXC) by depositing approved crypto assets as collateral. This project emphasizes security, transparency, and a clear liquidation mechanism, aiming to provide a reliable on-chain stable asset without the complexities of governance or recurring fees.
- Overcollateralized Stablecoin: Users mint
SXCagainst a higher value of deposited collateral, ensuring robust backing and stability. - Multi-Collateral Support: Currently supports WETH and WBTC as collateral types, with extensibility to include more assets.
- Dynamic Health Factor: Continuously monitors the collateralization ratio of user positions, providing real-time insights into risk levels.
- Automated Liquidation Mechanism: Positions falling below a predefined health factor are subject to liquidation, maintaining protocol solvency and stability.
- Chainlink Price Feeds: Leverages Chainlink decentralized oracle networks for accurate and up-to-date collateral asset pricing, including stale price checks.
- Secure Arithmetic Operations: Integrates a custom
SafeMathlibrary to prevent common Solidity arithmetic vulnerabilities like overflows and underflows. - Reentrancy Guard: Utilizes OpenZeppelin's
ReentrancyGuardto protect critical functions from reentrancy attacks. - Permissioned Minting/Burning:
StableXCoinis governed bySXCEngine, centralizing control overSXCsupply to maintain peg stability.
| Technology | Description | Link |
|---|---|---|
| Solidity | The primary programming language for smart contracts, version 0.8.24, ensuring modern syntax and security features. | Solidity Docs |
| Foundry | A blazing fast, portable, and modular toolkit for Ethereum application development, written in Rust. Used for testing, deployment, and scripting. | Foundry Book |
| Chainlink | Decentralized oracle network that provides real-world data to smart contracts, crucial for collateral price feeds. | Chainlink Docs |
| OpenZeppelin | A library of battle-tested smart contracts for secure development, including ERC20, Ownable, and ReentrancyGuard, enhancing the reliability and security of the protocol. | OpenZeppelin Contracts |
| Git | Distributed version control system used for managing source code history and collaboration, with submodules for external dependencies. | Git Docs |
To get a copy of this project up and running on your local machine, follow these simple steps.
Before you begin, ensure you have the following installed:
- Git: For cloning the repository.
- Foundry: The development toolkit used for this project. Follow the installation instructions here.
-
Clone the Repository: Begin by cloning the project repository to your local machine using Git:
git clone https://github.com/olujimiAdebakin/StableXCoin.git cd StableXCoin -
Install Foundry Dependencies: This project uses Foundry's submodule functionality for external libraries. Initialize and update these submodules:
forge update
This command will fetch
forge-std,openzeppelin-contracts, andchainlinklibraries. -
Build the Project: Compile the smart contracts to ensure everything is set up correctly:
forge build
If the build is successful, you're ready to interact with the contracts!
Interacting with the StableXCoin protocol typically involves deploying the contracts and then executing various functions.
For rapid development and testing, Foundry's local anvil chain is incredibly useful.
-
Start a Local Anvil Node: Open a new terminal and run:
anvil
This will start a local blockchain instance on
http://127.0.0.1:8545. Keep this terminal running. -
Run Tests: In another terminal, you can execute all the project tests:
forge testTo get a more detailed output, including traces, use:
forge test -vvvv
The DeploySXCEngine.s.sol script handles the deployment of StableXCoin and SXCEngine. It uses HelperConfig.s.sol to fetch network-specific addresses (Chainlink price feeds, WETH/WBTC) or deploy mocks on a local development chain.
To deploy locally (e.g., to Anvil):
forge script script/DeploySXCEngine.s.sol --broadcast --rpc-url http://127.0.0.1:8545This command will deploy the StableXCoin and SXCEngine contracts to your local Anvil instance. The script will output the deployed contract addresses.
To deploy to a public testnet (e.g., Sepolia):
First, ensure your PRIVATE_KEY environment variable is set. For example:
export PRIVATE_KEY=<YOUR_PRIVATE_KEY>Then, execute the deployment script, specifying the Sepolia RPC URL:
forge script script/DeploySXCEngine.s.sol --rpc-url https://rpc.sepolia.org --broadcast --verify --etherscan-api-key <YOUR_ETHERSCAN_API_KEY> -vvvvRemember to replace <YOUR_ETHERSCAN_API_KEY> with your actual Etherscan API key for contract verification.
Once deployed, you can interact with the SXCEngine and StableXCoin contracts. Here's a conceptual flow of how a user would interact:
-
Acquire Collateral Tokens (e.g., WETH, WBTC): On a testnet, you'd get testnet WETH/WBTC. On Anvil,
HelperConfigdeploys mock tokens and mints some to the deployer. -
Approve
SXCEngineto Spend Collateral: Before depositing, you must approve theSXCEnginecontract to transfer your collateral tokens.IERC20(WETH_ADDRESS).approve(address(SXCEngine_ADDRESS), AMOUNT_TO_APPROVE);
-
Deposit Collateral and Mint SXC: Users can deposit collateral and simultaneously mint
SXC.SXCEngine_ADDRESS.depositCollateralAndMintSxc(WETH_ADDRESS, AMOUNT_WETH, AMOUNT_SXC_TO_MINT);Alternatively, deposit first:
SXCEngine_ADDRESS.depositCollateral(WETH_ADDRESS, AMOUNT_WETH);Then mint:
SXCEngine_ADDRESS.mintSxc(AMOUNT_SXC_TO_MINT); -
Check Your Health Factor: Monitor the safety of your position. A healthy position has a health factor
>= 1e18(or 1.0).uint256 healthFactor = SXCEngine_ADDRESS.getHealthFactor(YOUR_ADDRESS);
-
Burn SXC to Reduce Debt: To reduce your
SXCdebt, you can burnSXCtokens. This improves your health factor.// Approve SXCEngine to burn your SXC first if not already done SXC_ADDRESS.approve(address(SXCEngine_ADDRESS), AMOUNT_SXC_TO_BURN); SXCEngine_ADDRESS.burnSxc(AMOUNT_SXC_TO_BURN);
-
Redeem Collateral: If your position is overcollateralized or you've burned enough
SXC, you can withdraw deposited collateral.SXCEngine_ADDRESS.redeemCollateral(WETH_ADDRESS, AMOUNT_WETH_TO_REDEEM);Or redeem collateral and burn
SXCatomically:SXCEngine_ADDRESS.redeemCollateralForSxc(WETH_ADDRESS, AMOUNT_WETH_TO_REDEEM, AMOUNT_SXC_TO_BURN); -
Liquidate an Unhealthy Position: If a user's health factor drops below the minimum threshold, anyone can liquidate their position by covering a portion of their debt in
SXCand receiving discounted collateral.// Liquidator must have SXC and approve SXCEngine to spend it SXC_ADDRESS.approve(address(SXCEngine_ADDRESS), DEBT_TO_COVER); SXCEngine_ADDRESS.liquidate(COLLATERAL_ADDRESS_OF_UNHEALTHY_USER, UNHEALTHY_USER_ADDRESS, DEBT_TO_COVER);
Contributions are warmly welcomed! If you're passionate about decentralized finance and robust stablecoin mechanisms, here's how you can help improve StableXCoin:
- ✨ Fork the Repository: Start by forking the
StableXCoinrepository to your GitHub account. - 🌿 Create a New Branch: Create a dedicated branch for your feature or bug fix. Use descriptive names like
feat/add-new-collateralorfix/health-factor-bug. - ✍️ Make Your Changes: Implement your changes, ensuring code quality and adherence to existing patterns.
- 🧪 Write Tests: Add or update tests to cover your new features or bug fixes. Comprehensive testing is crucial for smart contracts.
- ✅ Ensure All Tests Pass: Before submitting, run
forge testto confirm all existing and new tests pass successfully. - 📜 Update Documentation: If your changes affect the protocol's functionality or usage, please update the relevant documentation.
- ✉️ Submit a Pull Request: Push your changes to your fork and open a pull request against the
mainbranch of the original repository. Clearly describe your changes and their purpose.
We appreciate your effort in making StableXCoin more secure and functional for everyone!
This project is licensed under the MIT License. While an explicit LICENSE file is not included in this repository's root, the SPDX license identifier MIT is specified within the source code files. This indicates that the code is free to be used, modified, and distributed, provided the original copyright and license notice are included.
Adebakin Olujimi
- LinkedIn: My LinkedIn Profile
- Twitter: My Twitter Handle
- Portfolio: My Personal Website/Portfolio
Feel free to connect or reach out to me!