Skip to content

Commit e699a7c

Browse files
authored
Update PiStableCoin.sol
1 parent 6264bc5 commit e699a7c

File tree

1 file changed

+53
-9
lines changed

1 file changed

+53
-9
lines changed

contracts/PiStableCoin.sol

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,31 @@ pragma solidity ^0.8.0;
33

44
import "@openzeppelin/contracts/access/Ownable.sol";
55
import "@openzeppelin/contracts/security/Pausable.sol";
6-
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
7-
import "@openzeppelin/contracts/proxy/utils/Initializable.sol";
86

9-
contract PiStableCoin is Ownable, Pausable, Initializable {
7+
contract PiStableCoin is Ownable, Pausable {
108
uint256 public constant GCV_VALUE = 314159 * 10**18; // $314,159 in wei
119
uint256 public totalSupply;
1210
mapping(address => uint256) public balances;
1311
mapping(address => mapping(address => uint256)) public allowances;
1412

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;
1715

1816
event Mint(address indexed to, uint256 amount);
1917
event Burn(address indexed from, uint256 amount);
2018
event Transfer(address indexed from, address indexed to, uint256 amount);
2119
event FeeUpdated(uint256 newFee);
2220
event FeeRecipientUpdated(address newRecipient);
21+
event SupplyAdjusted(uint256 oldSupply, uint256 newSupply, int256 supplyChange, uint256 marketPrice);
2322

2423
modifier onlyFeeRecipient() {
2524
require(msg.sender == feeRecipient, "Caller is not the fee recipient");
2625
_;
2726
}
2827

29-
function initialize(address _feeRecipient, uint256 _transactionFee) public initializer {
28+
constructor(address _feeRecipient, uint256 _transactionFee) {
3029
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");
3231
feeRecipient = _feeRecipient;
3332
transactionFee = _transactionFee;
3433
}
@@ -43,7 +42,7 @@ contract PiStableCoin is Ownable, Pausable, Initializable {
4342
function mint(address to, uint256 amount) public onlyOwner whenNotPaused {
4443
require(to != address(0), "Cannot mint to the zero address");
4544
require(amount > 0, "Amount must be greater than 0");
46-
45+
4746
totalSupply += amount;
4847
balances[to] += amount;
4948
emit Mint(to, amount);
@@ -74,7 +73,7 @@ contract PiStableCoin is Ownable, Pausable, Initializable {
7473
return true;
7574
}
7675

77-
function approve(address spender, uint256 amount) public returns (bool) {
76+
function approve(address spender, uint256 amount) public whenNotPaused returns (bool) {
7877
allowances[msg.sender][spender] = amount;
7978
return true;
8079
}
@@ -126,4 +125,49 @@ contract PiStableCoin is Ownable, Pausable, Initializable {
126125
function allowance(address owner, address spender) public view returns (uint256) {
127126
return allowances[owner][spender];
128127
}
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+
}
129173
}

0 commit comments

Comments
 (0)