Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from raw ETH to ERC20 #78

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/lib/openzeppelin-contracts
70 changes: 55 additions & 15 deletions contracts/src/ActivePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

pragma solidity 0.8.18;

import './Interfaces/IActivePool.sol';
import "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";

import "./Dependencies/Ownable.sol";
import "./Dependencies/CheckContract.sol";
import './Interfaces/IDefaultPool.sol';
import './Interfaces/IActivePool.sol';

// import "forge-std/console.sol";

/*
* The Active Pool holds the ETH collateral and Bold debt (but not Bold tokens) for all active troves.
Expand All @@ -14,13 +19,16 @@ import "./Dependencies/CheckContract.sol";
*
*/
contract ActivePool is Ownable, CheckContract, IActivePool {
using SafeERC20 for IERC20;

string constant public NAME = "ActivePool";

IERC20 public immutable ETH;
address public borrowerOperationsAddress;
address public troveManagerAddress;
address public stabilityPoolAddress;
address public defaultPoolAddress;
uint256 internal ETH; // deposited ether tracker
uint256 internal ETHBalance; // deposited ether tracker
uint256 internal boldDebt;

// --- Events ---
Expand All @@ -31,7 +39,12 @@ contract ActivePool is Ownable, CheckContract, IActivePool {
event BorrowerOperationsAddressChanged(address _newBorrowerOperationsAddress);
event TroveManagerAddressChanged(address _newTroveManagerAddress);
event ActivePoolBoldDebtUpdated(uint _boldDebt);
event ActivePoolETHBalanceUpdated(uint _ETH);
event ActivePoolETHBalanceUpdated(uint _ETHBalance);

constructor(address _ETHAddress) {
checkContract(_ETHAddress);
ETH = IERC20(_ETHAddress);
}

// --- Contract setters ---

Expand Down Expand Up @@ -59,6 +72,9 @@ contract ActivePool is Ownable, CheckContract, IActivePool {
emit StabilityPoolAddressChanged(_stabilityPoolAddress);
emit DefaultPoolAddressChanged(_defaultPoolAddress);

// Allow funds movements between Liquity contracts
ETH.approve(_defaultPoolAddress, type(uint256).max);

_renounceOwnership();
}

Expand All @@ -69,8 +85,8 @@ contract ActivePool is Ownable, CheckContract, IActivePool {
*
*Not necessarily equal to the the contract's raw ETH balance - ether can be forcibly sent to contracts.
*/
function getETH() external view override returns (uint) {
return ETH;
function getETHBalance() external view override returns (uint) {
return ETHBalance;
}

function getBoldDebt() external view override returns (uint) {
Expand All @@ -81,12 +97,38 @@ contract ActivePool is Ownable, CheckContract, IActivePool {

function sendETH(address _account, uint _amount) external override {
_requireCallerIsBOorTroveMorSP();
ETH = ETH - _amount;
emit ActivePoolETHBalanceUpdated(ETH);

_accountForSendETH(_account, _amount);

ETH.safeTransfer(_account, _amount);
}

function sendETHToDefaultPool(uint _amount) external override {
_requireCallerIsTroveManager();

address defaultPoolAddressCached = defaultPoolAddress;
_accountForSendETH(defaultPoolAddressCached, _amount);

IDefaultPool(defaultPoolAddressCached).receiveETH(_amount);
}

function _accountForSendETH(address _account, uint _amount) internal {
uint256 newETHBalance = ETHBalance - _amount;
ETHBalance = newETHBalance;
emit ActivePoolETHBalanceUpdated(newETHBalance);
emit EtherSent(_account, _amount);
}

function receiveETH(uint256 _amount) external {
_requireCallerIsBorrowerOperationsOrDefaultPool();

(bool success, ) = _account.call{ value: _amount }("");
require(success, "ActivePool: sending ETH failed");
uint256 newETHBalance = ETHBalance + _amount;
ETHBalance = newETHBalance;

// Pull ETH tokens from sender
ETH.safeTransferFrom(msg.sender, address(this), _amount);

emit ActivePoolETHBalanceUpdated(newETHBalance);
}

function increaseBoldDebt(uint _amount) external override {
Expand Down Expand Up @@ -125,11 +167,9 @@ contract ActivePool is Ownable, CheckContract, IActivePool {
"ActivePool: Caller is neither BorrowerOperations nor TroveManager");
}

// --- Fallback function ---

receive() external payable {
_requireCallerIsBorrowerOperationsOrDefaultPool();
ETH = ETH + msg.value;
emit ActivePoolETHBalanceUpdated(ETH);
function _requireCallerIsTroveManager() internal view {
require(
msg.sender == troveManagerAddress,
"ActivePool: Caller is not TroveManager");
}
}
9 changes: 3 additions & 6 deletions contracts/src/BoldToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ contract BoldToken is CheckContract, IBoldToken {
}

// --- EIP 2612 Functionality ---
// TODO: remove and replace by openzeppelin implementation

function domainSeparator() public view override returns (bytes32) {
function DOMAIN_SEPARATOR() public view override returns (bytes32) {
if (_chainID() == _CACHED_CHAIN_ID) {
return _CACHED_DOMAIN_SEPARATOR;
} else {
Expand All @@ -181,7 +182,7 @@ contract BoldToken is CheckContract, IBoldToken {
{
require(deadline >= block.timestamp, 'Bold: expired deadline');
bytes32 digest = keccak256(abi.encodePacked('\x19\x01',
domainSeparator(), keccak256(abi.encode(
DOMAIN_SEPARATOR(), keccak256(abi.encode(
_PERMIT_TYPEHASH, owner, spender, amount,
_nonces[owner]++, deadline))));
address recoveredAddress = ecrecover(digest, v, r, s);
Expand Down Expand Up @@ -297,8 +298,4 @@ contract BoldToken is CheckContract, IBoldToken {
function version() external pure override returns (string memory) {
return _VERSION;
}

function permitTypeHash() external pure override returns (bytes32) {
return _PERMIT_TYPEHASH;
}
}
Loading
Loading