@@ -3,32 +3,31 @@ pragma solidity ^0.8.0;
3
3
4
4
import "@openzeppelin/contracts/access/Ownable.sol " ;
5
5
import "@openzeppelin/contracts/security/Pausable.sol " ;
6
- import "@openzeppelin/contracts/token/ERC20/IERC20.sol " ;
7
- import "@openzeppelin/contracts/proxy/utils/Initializable.sol " ;
8
6
9
- contract PiStableCoin is Ownable , Pausable , Initializable {
7
+ contract PiStableCoin is Ownable , Pausable {
10
8
uint256 public constant GCV_VALUE = 314159 * 10 ** 18 ; // $314,159 in wei
11
9
uint256 public totalSupply;
12
10
mapping (address => uint256 ) public balances;
13
11
mapping (address => mapping (address => uint256 )) public allowances;
14
12
15
- uint256 public transactionFee; // Fee percentage ( in basis points)
16
- address public feeRecipient; // Address to receive fees
13
+ uint256 public transactionFee; // Fee percentage in basis points (10000 = 100% )
14
+ address public feeRecipient;
17
15
18
16
event Mint (address indexed to , uint256 amount );
19
17
event Burn (address indexed from , uint256 amount );
20
18
event Transfer (address indexed from , address indexed to , uint256 amount );
21
19
event FeeUpdated (uint256 newFee );
22
20
event FeeRecipientUpdated (address newRecipient );
21
+ event SupplyAdjusted (uint256 oldSupply , uint256 newSupply , int256 supplyChange , uint256 marketPrice );
23
22
24
23
modifier onlyFeeRecipient () {
25
24
require (msg .sender == feeRecipient, "Caller is not the fee recipient " );
26
25
_;
27
26
}
28
27
29
- function initialize (address _feeRecipient , uint256 _transactionFee ) public initializer {
28
+ constructor (address _feeRecipient , uint256 _transactionFee ) {
30
29
require (_feeRecipient != address (0 ), "Invalid fee recipient " );
31
- require (_transactionFee <= 10000 , "Fee exceeds maximum limit " ); // 100% in basis points
30
+ require (_transactionFee <= 10000 , "Fee exceeds maximum limit " );
32
31
feeRecipient = _feeRecipient;
33
32
transactionFee = _transactionFee;
34
33
}
@@ -43,7 +42,7 @@ contract PiStableCoin is Ownable, Pausable, Initializable {
43
42
function mint (address to , uint256 amount ) public onlyOwner whenNotPaused {
44
43
require (to != address (0 ), "Cannot mint to the zero address " );
45
44
require (amount > 0 , "Amount must be greater than 0 " );
46
-
45
+
47
46
totalSupply += amount;
48
47
balances[to] += amount;
49
48
emit Mint (to, amount);
@@ -74,7 +73,7 @@ contract PiStableCoin is Ownable, Pausable, Initializable {
74
73
return true ;
75
74
}
76
75
77
- function approve (address spender , uint256 amount ) public returns (bool ) {
76
+ function approve (address spender , uint256 amount ) public whenNotPaused returns (bool ) {
78
77
allowances[msg .sender ][spender] = amount;
79
78
return true ;
80
79
}
@@ -126,4 +125,49 @@ contract PiStableCoin is Ownable, Pausable, Initializable {
126
125
function allowance (address owner , address spender ) public view returns (uint256 ) {
127
126
return allowances[owner][spender];
128
127
}
128
+
129
+ /**
130
+ * @notice Adjust supply based on the market price compared to the GCV_VALUE.
131
+ * If market price is below GCV_VALUE, burn tokens from owner to reduce supply.
132
+ * If market price is above GCV_VALUE, mint new tokens to owner to increase supply.
133
+ * @param marketPrice The current market price in wei
134
+ */
135
+ function adjustSupply (uint256 marketPrice ) public onlyOwner whenNotPaused {
136
+ require (marketPrice > 0 , "Market price must be positive " );
137
+
138
+ uint256 oldSupply = totalSupply;
139
+ int256 supplyChange = 0 ;
140
+
141
+ if (marketPrice < GCV_VALUE) {
142
+ // Calculate difference scaled by token decimals
143
+ uint256 diff = (GCV_VALUE - marketPrice);
144
+ uint256 burnAmount = diff; // Already in 18-decimals wei
145
+
146
+ // Limit burn to owner's balance to prevent underflow
147
+ uint256 ownerBalance = balances[msg .sender ];
148
+ if (burnAmount > ownerBalance) {
149
+ burnAmount = ownerBalance;
150
+ }
151
+
152
+ require (burnAmount > 0 , "No tokens available to burn for supply adjustment " );
153
+
154
+ totalSupply -= burnAmount;
155
+ balances[msg .sender ] -= burnAmount;
156
+ supplyChange = - int256 (burnAmount);
157
+
158
+ emit Burn (msg .sender , burnAmount);
159
+
160
+ } else if (marketPrice > GCV_VALUE) {
161
+ uint256 diff = (marketPrice - GCV_VALUE);
162
+ uint256 mintAmount = diff; // Already in 18-decimals wei
163
+
164
+ totalSupply += mintAmount;
165
+ balances[msg .sender ] += mintAmount;
166
+ supplyChange = int256 (mintAmount);
167
+
168
+ emit Mint (msg .sender , mintAmount);
169
+ }
170
+
171
+ emit SupplyAdjusted (oldSupply, totalSupply, supplyChange, marketPrice);
172
+ }
129
173
}
0 commit comments