Skip to content

Commit

Permalink
feat: prefix BaseAuctioneer members with auction
Browse files Browse the repository at this point in the history
  • Loading branch information
fp-crypto committed Apr 24, 2024
1 parent 9d3b69e commit 377a4e5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 44 deletions.
75 changes: 39 additions & 36 deletions src/Bases/Auctioneer/BaseAuctioneer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
uint256 internal constant MINUTE_HALF_LIFE =
0.988514020352896135_356867505 * 1e27; // 0.5^(1/60)

/// @notice Struct to hold the info for `want`.
TokenInfo internal wantInfo;
/// @notice Struct to hold the info for `auctionWant`.
TokenInfo internal auctionWantInfo;

/// @notice Mapping from an auction ID to its struct.
mapping(bytes32 => AuctionInfo) public auctions;
Expand All @@ -72,7 +72,7 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
bytes32[] public enabledAuctions;

/// @notice The amount to start the auction at.
uint256 public startingPrice;
uint256 public auctionStartingPrice;

/// @notice The time that each auction lasts.
uint32 public auctionLength;
Expand All @@ -82,38 +82,38 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {

/**
* @notice Initializes the Auction contract with initial parameters.
* @param _want Address this auction is selling to.
* @param _auctionWant Address this auction is selling to.
* @param _auctionLength Duration of each auction in seconds.
* @param _auctionCooldown Cooldown period between auctions in seconds.
* @param _startingPrice Starting price for each auction.
* @param _auctionStartingPrice Starting price for each auction.
*/
constructor(
address _asset,
string memory _name,
address _want,
address _auctionWant,
uint32 _auctionLength,
uint32 _auctionCooldown,
uint256 _startingPrice
uint256 _auctionStartingPrice
) BaseHealthCheck(_asset, _name) {
require(auctionLength == 0, "initialized");
require(_want != address(0), "ZERO ADDRESS");
require(_auctionWant != address(0), "ZERO ADDRESS");
require(_auctionLength != 0, "length");
require(_auctionLength < _auctionCooldown, "cooldown");
require(_startingPrice != 0, "starting price");
require(_auctionStartingPrice != 0, "starting price");

// Cannot have more than 18 decimals.
uint256 decimals = ERC20(_want).decimals();
uint256 decimals = ERC20(_auctionWant).decimals();
require(decimals <= 18, "unsupported decimals");

// Set variables
wantInfo = TokenInfo({
tokenAddress: _want,
auctionWantInfo = TokenInfo({
tokenAddress: _auctionWant,
scaler: uint96(WAD / 10 ** decimals)
});

auctionLength = _auctionLength;
auctionCooldown = _auctionCooldown;
startingPrice = _startingPrice;
auctionStartingPrice = _auctionStartingPrice;
}

/*//////////////////////////////////////////////////////////////
Expand All @@ -124,8 +124,8 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
* @notice Get the address of this auctions want token.
* @return . The want token.
*/
function want() public view virtual returns (address) {
return wantInfo.tokenAddress;
function auctionWant() public view virtual returns (address) {
return auctionWantInfo.tokenAddress;
}

/**
Expand All @@ -141,7 +141,7 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
* @return bytes32 A unique auction identifier.
*/
function getAuctionId(address _from) public view virtual returns (bytes32) {
return keccak256(abi.encodePacked(_from, want(), address(this)));
return keccak256(abi.encodePacked(_from, auctionWant(), address(this)));
}

/**
Expand Down Expand Up @@ -169,7 +169,7 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {

return (
auction.fromInfo.tokenAddress,
want(),
auctionWant(),
auction.kicked,
auction.kicked + uint256(auctionLength) > block.timestamp
? auction.currentAvailable
Expand Down Expand Up @@ -198,10 +198,10 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
}

/**
* @notice Gets the amount of `want` needed to buy a specific amount of `from`.
* @notice Gets the amount of `auctionWant` needed to buy a specific amount of `from`.
* @param _auctionId The unique identifier of the auction.
* @param _amountToTake The amount of `from` to take in the auction.
* @return . The amount of `want` needed to fulfill the take amount.
* @return . The amount of `auctionWant` needed to fulfill the take amount.
*/
function getAmountNeeded(
bytes32 _auctionId,
Expand All @@ -216,11 +216,11 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
}

/**
* @notice Gets the amount of `want` needed to buy a specific amount of `from` at a specific timestamp.
* @notice Gets the amount of `auctionWant` needed to buy a specific amount of `from` at a specific timestamp.
* @param _auctionId The unique identifier of the auction.
* @param _amountToTake The amount `from` to take in the auction.
* @param _timestamp The specific timestamp for calculating the amount needed.
* @return . The amount of `want` needed to fulfill the take amount.
* @return . The amount of `auctionWant` needed to fulfill the take amount.
*/
function getAmountNeeded(
bytes32 _auctionId,
Expand All @@ -232,7 +232,7 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
}

/**
* @dev Return the amount of `want` needed to buy `_amountToTake`.
* @dev Return the amount of `auctionWant` needed to buy `_amountToTake`.
*/
function _getAmountNeeded(
AuctionInfo memory _auction,
Expand All @@ -250,8 +250,8 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
_timestamp
)) /
1e18 /
// Scale back down to want.
wantInfo.scaler;
// Scale back down to auctionWant.
auctionWantInfo.scaler;
}

/**
Expand Down Expand Up @@ -280,7 +280,7 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
auctions[_auctionId].initialAvailable *
auctions[_auctionId].fromInfo.scaler,
_timestamp
) / wantInfo.scaler;
) / auctionWantInfo.scaler;
}

/**
Expand All @@ -307,7 +307,10 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
MINUTE_HALF_LIFE,
(secondsElapsed % 3600) / 60
);
uint256 initialPrice = Maths.wdiv(startingPrice * 1e18, _available);
uint256 initialPrice = Maths.wdiv(
auctionStartingPrice * 1e18,
_available
);

return
(initialPrice * Maths.rmul(hoursComponent, minutesComponent)) /
Expand All @@ -326,8 +329,8 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
function enableAuction(
address _from
) public virtual onlyManagement returns (bytes32 _auctionId) {
address _want = want();
require(_from != address(0) && _from != _want, "ZERO ADDRESS");
address _auctionWant = auctionWant();
require(_from != address(0) && _from != _auctionWant, "ZERO ADDRESS");
// Cannot have more than 18 decimals.
uint256 decimals = ERC20(_from).decimals();
require(decimals <= 18, "unsupported decimals");
Expand All @@ -349,7 +352,7 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
// Add to the array.
enabledAuctions.push(_auctionId);

emit AuctionEnabled(_auctionId, _from, _want, address(this));
emit AuctionEnabled(_auctionId, _from, _auctionWant, address(this));
}

/**
Expand Down Expand Up @@ -406,7 +409,7 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
// Pop the id off the array.
enabledAuctions.pop();

emit AuctionDisabled(_auctionId, _from, want(), address(this));
emit AuctionDisabled(_auctionId, _from, auctionWant(), address(this));
}

/*//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -552,13 +555,13 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
);
}

// Cache the want address.
address _want = want();
// Cache the auctionWant address.
address _auctionWant = auctionWant();

// Pull `want`.
ERC20(_want).safeTransferFrom(msg.sender, address(this), needed);
// Pull `auctionWant`.
ERC20(_auctionWant).safeTransferFrom(msg.sender, address(this), needed);

_postTake(_want, _amountTaken, needed);
_postTake(_auctionWant, _amountTaken, needed);

emit AuctionTaken(_auctionId, _amountTaken, left);
}
Expand Down Expand Up @@ -589,7 +592,7 @@ abstract contract BaseAuctioneer is BaseHealthCheck, ReentrancyGuard {
* occurs.
* @param _token Address of the token being taken.
* @param _amountToTake Amount of `_token` needed.
* @param _amountToPay Amount of `want` that will be payed.
* @param _amountToPay Amount of `auctionWant` that will be payed.
*/
function _preTake(
address _token,
Expand Down
6 changes: 4 additions & 2 deletions src/Bases/Auctioneer/IBaseAuctioneer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ interface IBaseAuctioneer is IBaseHealthCheck {
uint96 scaler;
}

function startingPrice() external view returns (uint256);
function auctionStartingPrice() external view returns (uint256);

function auctionLength() external view returns (uint32);

function cooldownLength() external view returns (uint32);
function auctionCooldown() external view returns (uint32);

function auctions(
bytes32
Expand All @@ -29,6 +29,8 @@ interface IBaseAuctioneer is IBaseHealthCheck {

function enabledAuctions() external view returns (bytes32[] memory);

function auctionWant() external view returns (address);

function numberOfEnabledAuctions() external view returns (uint256);

function getAuctionId(address _from) external view returns (bytes32);
Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/Solidly/ISolidly.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ interface ISolidly {
}

function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
uint256 amountIn,
uint256 amountOutMin,
route[] calldata routes,
address to,
uint deadline
uint256 deadline
) external returns (uint256[] memory amounts);

function getAmountsOut(
uint amountIn,
uint256 amountIn,
route[] memory routes
) external view returns (uint256[] memory amounts);
}
6 changes: 4 additions & 2 deletions src/test/BaseAuctioneer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,13 @@ contract BaseAuctioneerTest is Setup {
uint256 available = auctioneer.kick(id);

assertEq(ERC20(from).balanceOf(address(auctioneer)), _amount);
assertEq(ERC20(from).balanceOf(address(auctioneer)), available);

assertEq(auctioneer.kickable(id), 0);
(, , _kicked, _available) = auctioneer.auctionInfo(id);
assertEq(_kicked, block.timestamp);
assertEq(_available, _amount);
uint256 startingPrice = ((auctioneer.startingPrice() *
uint256 startingPrice = ((auctioneer.auctionStartingPrice() *
(WAD / wantScaler)) * 1e18) /
_amount /
fromScaler;
Expand Down Expand Up @@ -338,12 +339,13 @@ contract BaseAuctioneerTest is Setup {
uint256 available = auctioneer.kick(id);

assertEq(ERC20(from).balanceOf(address(auctioneer)), _amount);
assertEq(kickable, available);

assertEq(auctioneer.kickable(id), 0);
(, , _kicked, _available) = auctioneer.auctionInfo(id);
assertEq(_kicked, block.timestamp);
assertEq(_available, kickable);
uint256 startingPrice = ((auctioneer.startingPrice() *
uint256 startingPrice = ((auctioneer.auctionStartingPrice() *
(WAD / wantScaler)) * 1e18) /
kickable /
fromScaler;
Expand Down

0 comments on commit 377a4e5

Please sign in to comment.