Skip to main content

Auction

Git Source

Inherits: Governance, ReentrancyGuard

Author: yearn.fi

General use dutch auction contract for token sales.

State Variables

WAD

uint256 internal constant WAD = 1e18;

MINUTE_HALF_LIFE

Used for the price decay.

uint256 internal constant MINUTE_HALF_LIFE = 0.988514020352896135_356867505 * 1e27;

wantInfo

Struct to hold the info for want.

TokenInfo internal wantInfo;

hook_

Contract to call during write functions.

Hook internal hook_;

startingPrice

The amount to start the auction at.

uint256 public startingPrice;

auctionLength

The time that each auction lasts.

uint256 public auctionLength;

auctionCooldown

The minimum time to wait between auction 'kicks'.

uint256 public auctionCooldown;

auctions

Mapping from an auction ID to its struct.

mapping(bytes32 => AuctionInfo) public auctions;

enabledAuctions

Array of all the enabled auction for this contract.

bytes32[] public enabledAuctions;

Functions

constructor

constructor() Governance(msg.sender);

initialize

Initializes the Auction contract with initial parameters.

function initialize(
address _want,
address _hook,
address _governance,
uint256 _auctionLength,
uint256 _auctionCooldown,
uint256 _startingPrice
) external virtual;

Parameters

NameTypeDescription
_wantaddressAddress this auction is selling to.
_hookaddressAddress of the hook contract (optional).
_governanceaddressAddress of the contract governance.
_auctionLengthuint256Duration of each auction in seconds.
_auctionCooldownuint256Cooldown period between auctions in seconds.
_startingPriceuint256Starting price for each auction.

want

Get the address of this auctions want token.

function want() public view virtual returns (address);

Returns

NameTypeDescription
<none>address. The want token.

hook

Get the address of the hook if any.

function hook() external view virtual returns (address);

Returns

NameTypeDescription
<none>address. The hook.

getHookFlags

Get the current status of which hooks are being used.

function getHookFlags() external view virtual returns (bool, bool, bool, bool);

Returns

NameTypeDescription
<none>bool. If the kickable hook is used.
<none>bool. If the kick hook is used.
<none>bool. If the preTake hook is used.
<none>bool. If the postTake hook is used.

numberOfEnabledAuctions

Get the length of the enabled auctions array.

function numberOfEnabledAuctions() external view virtual returns (uint256);

getAuctionId

Get the unique auction identifier.

function getAuctionId(address _from) public view virtual returns (bytes32);

Parameters

NameTypeDescription
_fromaddressThe address of the token to sell.

Returns

NameTypeDescription
<none>bytes32bytes32 A unique auction identifier.

auctionInfo

Retrieves information about a specific auction.

function auctionInfo(bytes32 _auctionId)
public
view
virtual
returns (address _from, address _to, uint256 _kicked, uint256 _available);

Parameters

NameTypeDescription
_auctionIdbytes32The unique identifier of the auction.

Returns

NameTypeDescription
_fromaddressThe address of the token to sell.
_toaddressThe address of the token to buy.
_kickeduint256The timestamp of the last kick.
_availableuint256The current available amount for the auction.

kickable

Get the pending amount available for the next auction.

Defaults to the auctions balance of the from token if no hook.

function kickable(bytes32 _auctionId) external view virtual returns (uint256);

Parameters

NameTypeDescription
_auctionIdbytes32The unique identifier of the auction.

Returns

NameTypeDescription
<none>uint256uint256 The amount that can be kicked into the auction.

getAmountNeeded

Gets the amount of want needed to buy a specific amount of from.

function getAmountNeeded(bytes32 _auctionId, uint256 _amountToTake) external view virtual returns (uint256);

Parameters

NameTypeDescription
_auctionIdbytes32The unique identifier of the auction.
_amountToTakeuint256The amount of from to take in the auction.

Returns

NameTypeDescription
<none>uint256. The amount of want needed to fulfill the take amount.

getAmountNeeded

Gets the amount of want needed to buy a specific amount of from at a specific timestamp.

function getAmountNeeded(bytes32 _auctionId, uint256 _amountToTake, uint256 _timestamp)
external
view
virtual
returns (uint256);

Parameters

NameTypeDescription
_auctionIdbytes32The unique identifier of the auction.
_amountToTakeuint256The amount from to take in the auction.
_timestampuint256The specific timestamp for calculating the amount needed.

Returns

NameTypeDescription
<none>uint256. The amount of want needed to fulfill the take amount.

_getAmountNeeded

Return the amount of want needed to buy _amountToTake.

function _getAmountNeeded(AuctionInfo memory _auction, uint256 _amountToTake, uint256 _timestamp)
internal
view
virtual
returns (uint256);

price

Gets the price of the auction at the current timestamp.

function price(bytes32 _auctionId) external view virtual returns (uint256);

Parameters

NameTypeDescription
_auctionIdbytes32The unique identifier of the auction.

Returns

NameTypeDescription
<none>uint256. The price of the auction.

price

Gets the price of the auction at a specific timestamp.

function price(bytes32 _auctionId, uint256 _timestamp) public view virtual returns (uint256);

Parameters

NameTypeDescription
_auctionIdbytes32The unique identifier of the auction.
_timestampuint256The specific timestamp for calculating the price.

Returns

NameTypeDescription
<none>uint256. The price of the auction.

_price

Internal function to calculate the scaled price based on auction parameters.

function _price(uint256 _kicked, uint256 _available, uint256 _timestamp) internal view virtual returns (uint256);

Parameters

NameTypeDescription
_kickeduint256The timestamp the auction was kicked.
_availableuint256The initial available amount scaled 1e18.
_timestampuint256The specific timestamp for calculating the price.

Returns

NameTypeDescription
<none>uint256. The calculated price scaled to 1e18.

enable

Enables a new auction.

Uses governance as the receiver.

function enable(address _from) external virtual returns (bytes32);

Parameters

NameTypeDescription
_fromaddressThe address of the token to be auctioned.

Returns

NameTypeDescription
<none>bytes32. The unique identifier of the enabled auction.

enable

Enables a new auction.

function enable(address _from, address _receiver) public virtual onlyGovernance returns (bytes32 _auctionId);

Parameters

NameTypeDescription
_fromaddressThe address of the token to be auctioned.
_receiveraddressThe address that will receive the funds in the auction.

Returns

NameTypeDescription
_auctionIdbytes32The unique identifier of the enabled auction.

disable

Disables an existing auction.

Only callable by governance.

function disable(address _from) external virtual;

Parameters

NameTypeDescription
_fromaddressThe address of the token being sold.

disable

Disables an existing auction.

Only callable by governance.

function disable(address _from, uint256 _index) public virtual onlyGovernance;

Parameters

NameTypeDescription
_fromaddressThe address of the token being sold.
_indexuint256The index the auctionId is at in the array.

setHookFlags

Set the flags to be used with hook.

function setHookFlags(bool _kickable, bool _kick, bool _preTake, bool _postTake) external virtual onlyGovernance;

Parameters

NameTypeDescription
_kickableboolIf the kickable hook should be used.
_kickboolIf the kick hook should be used.
_preTakeboolIf the preTake hook should be used.
_postTakeboolIf the postTake should be used.

kick

Kicks off an auction, updating its status and making funds available for bidding.

function kick(bytes32 _auctionId) external virtual nonReentrant returns (uint256 available);

Parameters

NameTypeDescription
_auctionIdbytes32The unique identifier of the auction.

Returns

NameTypeDescription
availableuint256The available amount for bidding on in the auction.

take

Take the token being sold in a live auction.

Defaults to taking the full amount and sending to the msg sender.

function take(bytes32 _auctionId) external virtual returns (uint256);

Parameters

NameTypeDescription
_auctionIdbytes32The unique identifier of the auction.

Returns

NameTypeDescription
<none>uint256. The amount of fromToken taken in the auction.

take

Take the token being sold in a live auction with a specified maximum amount.

Uses the sender's address as the receiver.

function take(bytes32 _auctionId, uint256 _maxAmount) external virtual returns (uint256);

Parameters

NameTypeDescription
_auctionIdbytes32The unique identifier of the auction.
_maxAmountuint256The maximum amount of fromToken to take in the auction.

Returns

NameTypeDescription
<none>uint256. The amount of fromToken taken in the auction.

take

Take the token being sold in a live auction.

function take(bytes32 _auctionId, uint256 _maxAmount, address _receiver) external virtual returns (uint256);

Parameters

NameTypeDescription
_auctionIdbytes32The unique identifier of the auction.
_maxAmountuint256The maximum amount of fromToken to take in the auction.
_receiveraddressThe address that will receive the fromToken.

Returns

NameTypeDescription
<none>uint256_amountTaken The amount of fromToken taken in the auction.

take

Take the token being sold in a live auction.

function take(bytes32 _auctionId, uint256 _maxAmount, address _receiver, bytes calldata _data)
external
virtual
returns (uint256);

Parameters

NameTypeDescription
_auctionIdbytes32The unique identifier of the auction.
_maxAmountuint256The maximum amount of fromToken to take in the auction.
_receiveraddressThe address that will receive the fromToken.
_databytesThe data signify the callback should be used and sent with it.

Returns

NameTypeDescription
<none>uint256_amountTaken The amount of fromToken taken in the auction.

_take

Implements the take of the auction.

function _take(bytes32 _auctionId, uint256 _maxAmount, address _receiver, bytes memory _data)
internal
virtual
nonReentrant
returns (uint256 _amountTaken);

Events

AuctionEnabled

Emitted when a new auction is enabled

event AuctionEnabled(bytes32 auctionId, address indexed from, address indexed to, address indexed auctionAddress);

AuctionDisabled

Emitted when an auction is disabled.

event AuctionDisabled(bytes32 auctionId, address indexed from, address indexed to, address indexed auctionAddress);

AuctionKicked

Emitted when auction has been kicked.

event AuctionKicked(bytes32 auctionId, uint256 available);

AuctionTaken

Emitted when any amount of an active auction was taken.

event AuctionTaken(bytes32 auctionId, uint256 amountTaken, uint256 amountLeft);

Structs

TokenInfo

Store address and scaler in one slot.

struct TokenInfo {
address tokenAddress;
uint96 scaler;
}

AuctionInfo

Store all the auction specific information.

struct AuctionInfo {
TokenInfo fromInfo;
uint96 kicked;
address receiver;
uint128 initialAvailable;
uint128 currentAvailable;
}

Hook

Store the hook address and each flag in one slot.

struct Hook {
address hook;
bool kickable;
bool kick;
bool preTake;
bool postTake;
}