-The protocol mints (adds) or burns (removes) supply from circulation in proportion to the coin's price deviation from the $1 peg. If the coin price > $1, the protocol mints coins. If the coin price < $1, the protocol burns coins.
-Keeps the price of stable coin at targeted price and also checks every 24 hour for the volatility feature to automatically adjust the target price based on market conditions, such as the average price over a certain period or the price of a basket of other stablecoins. This can make the bot more adaptive to changing market dynamics and reduce the need for manual intervention.
Idea Behind The Bot
The basic idea behind it is to adjust the supply of the stablecoin tokens to maintain a constant price in terms of the target asset. If the price of the stablecoin rises above the target price, the protocol will increase the supply of tokens by "minting" new tokens and distributing them to holders of the stablecoin. If the price of the stablecoin falls below the target price, the protocol will decrease the supply of tokens by "burning" existing tokens, effectively removing them from circulation.
The key advantage of using a this mechanism is that it allows the stablecoin to maintain its peg to the target asset without relying on centralized reserves of the target asset. Instead, the supply of the stablecoin is dynamically adjusted based on market conditions, which can help to prevent large fluctuations in price.
Flow
Code Open Source:
// const Web3 = require('web3');constaxios=require('axios');constdotenv=require('dotenv');dotenv.config();constweb3=newWeb3(process.env.INFURA_API_URL); // or any other RPC endpoint// Load the stablecoin contract and define the contract functionsconststablecoinContract=newweb3.eth.Contract(contractABI, contractAddress);constmint=stablecoinContract.methods.mint;constburn=stablecoinContract.methods.burn;// Define the target price and the desired exchange rate with USDTconsttargetPrice=1; // for example, $1 per stablecoinconstexchangeRate=1; // for example, 1 stablecoin = 1 USDT// Create a function to check the current price of your stablecoin by calling an APIasyncfunctioncheckPrice() {try {constresponse=awaitaxios.get('https://api.coingecko.com/api/v3/simple/price?ids=stablecoin&vs_currencies=usd');constcurrentPrice=response.data.stablecoin.usd;return currentPrice; } catch (error) {console.error(error);returnnull; }}// Define a function to update the target price based on market conditionsasyncfunctionupdateTargetPrice() {constpriceHistory=awaitstablecoinContract.getPastEvents('PriceUpdate', { fromBlock:0, toBlock:'latest' });constlatestPrice= priceHistory[priceHistory.length-1].returnValues.price;consttimeThreshold=newDate().getTime() -86400000; // 24 hours agoconstrecentPrices=priceHistory.filter(event =>event.timestamp >= timeThreshold).map(event =>event.returnValues.price);constaveragePrice=recentPrices.reduce((sum, price) => sum + price,0) /recentPrices.length;constvolatility=Math.abs(averagePrice - latestPrice) / averagePrice;consttrend= (latestPrice - averagePrice) / averagePrice;// Update the target price based on the market conditionsif (volatility <0.05) {// If the market is relatively stable, set the target price to the average price targetPrice = averagePrice; } else {// If the market is volatile, adjust the target price based on the trendconstadjustment=Math.min(Math.abs(trend) *0.1,0.05) * averagePrice; targetPrice += trend >0? adjustment :-adjustment; } }// Update the target price periodically using a timer or a cron jobsetInterval(async () => {awaitupdateTargetPrice(); },3600000); // every hour// Use the updated target price to calculate the difference and adjust the supplyconstdifference=calculateDifference(currentPrice, targetPrice);if (Math.abs(difference) >0.01) {awaitadjustSupply(difference); }// Define a function to adjust the stablecoin supply using the mint/burn algorithmconstmaxAdjustment=0.1; // for example, limit to 10% of the total supply// Define a function to adjust the stablecoin supply using the mint/burn algorithmasyncfunctionadjustSupply(difference) {constsupply=awaitstablecoinContract.methods.totalSupply().call();constadjustment=Math.min(Math.abs(difference) * supply * exchangeRate, maxAdjustment * supply);if (difference >0) {// If the price exceeds the target price, call the burn functionawaitburn(adjustment).send({ from: yourAddress });console.log(`Burned ${adjustment} stablecoin`); } elseif (difference <0) {// If the price decreases below the target price, call the mint functionawaitmint(adjustment).send({ from: yourAddress });console.log(`Minted ${adjustment} stablecoin`); }}// Repeat the price check and adjustment process periodically using a timer or a cron jobsetInterval(async () => {constcurrentPrice=awaitcheckPrice();if (currentPrice !==null) {constdifference=calculateDifference(currentPrice, targetPrice);if (Math.abs(difference) >0.01) { // or any other thresholdawaitadjustSupply(difference); } }},60000); // every minute