bump RDA impl

This commit is contained in:
gozzy 2023-03-20 18:57:48 +00:00
parent 85cc752b91
commit 228831255e
11 changed files with 286 additions and 269 deletions

View File

@ -1,8 +1,8 @@
pragma solidity 0.8.0;
import "./interfaces/IERC20.sol";
import "./interfaces/IStaking.sol";
import "./interfaces/IGovernance.sol";
import "@openzeppelin/token/ERC20/IERC20.sol";
import "@interfaces/IStaking.sol";
import "@interfaces/IGovernance.sol";
contract DelegatedInstance {

View File

@ -1,10 +1,7 @@
pragma solidity 0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
// https://docs.uniswap.org/contracts/v2/reference/smart-contracts/router-02
// https://github.com/Uniswap/v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@openzeppelin/token/ERC20/IERC20.sol";
import "@interfaces/IUniswapV2Router02.sol";
contract LiquidityManagement {
@ -21,7 +18,7 @@ contract LiquidityManagement {
IERC20 UNIV2_ETH_TORN;
IUniswapV2Router02 UNIV2_ROUTER;
constructor() {
constructor() public {
IERC20 DAI = IERC20(DAI_ADDRESS);
IERC20 TORN = IERC20(TORN_ADDRESS);
IERC20 UNIV2_DAI_TORN = IERC20(DAI_TORN_ADDRESS);
@ -52,7 +49,6 @@ contract LiquidityManagement {
uint256 minimumAmountETH = amountETH - slippageETH;
uint256 minimumAmountTORN = amountSeedTORN - slippageTORN;
// DAI/TORN
DAI.approve(UNIV2_ROUTER02_ADDRESS, amountDAI);
UNIV2_ROUTER.addLiquidity(

View File

@ -1,7 +1,7 @@
pragma solidity 0.8.0;
import "@root/interfaces/IERC20.sol";
import "@root/interfaces/IRollingDutchAuction.sol";
import "@openzeppelin/token/ERC20/IERC20.sol";
import "@interfaces/IRDA.sol";
contract Proposal {
@ -19,7 +19,7 @@ contract Proposal {
address auctionAddress;
address vestingAddress;
IRollingDutchAuction(auctionAddress).createAuction(
IRDA(auctionAddress).createAuction(
vestingAddress,
governanceAddress,
tokenAddress,

View File

@ -1,18 +1,20 @@
pragma solidity 0.8.13;
import { UD60x18 } from "@prb/math/UD60x18.sol";
import { IERC20 } from "@root/interfaces/IERC20.sol";
import { IVesting } from "@root/interfaces/IVesting.sol";
import { inv, add, sub, mul, exp, ln, wrap, unwrap, gte, mod, div } from "@prb/math/UD60x18.sol";
import { IRDA } from "@interfaces/IRDA.sol";
import { IVesting } from "@interfaces/IVesting.sol";
import { IERC20 } from "@openzeppelin/token/ERC20/IERC20.sol";
import { add, sub, mul, wrap, unwrap, gt, mod, div } from "@prb/math/UD60x18.sol";
/*
* @title Rolling Dutch Auction
* @author Samuel JJ Gosling
* @description A dutch auction derivative with composite logarithimic decay
* @title Rolling Dutch Auction (RDA)
* @author Samuel JJ Gosling (@deomaius)
* @description A dutch auction derivative with composite decay
*/
contract RollingDutchAuction {
contract RDA is IRDA {
/* @dev Address mapping for an auction's redeemable balances */
mapping(address => mapping(bytes => bytes)) public _claims;
@ -27,7 +29,6 @@ contract RollingDutchAuction {
mapping(bytes => uint256) public _windows;
struct Auction {
address vestingAddress;
uint256 windowDuration; /* @dev Unix time window duration */
uint256 windowTimestamp; /* @dev Unix timestamp for window start */
uint256 startTimestamp; /* @dev Unix auction start timestamp */
@ -126,7 +127,7 @@ contract RollingDutchAuction {
uint256 startTimestamp,
uint256 endTimestamp,
uint256 windowDuration
) public returns (bytes memory) {
) external returns (bytes memory) {
bytes memory auctionId = abi.encode(
operatorAddress,
reserveToken,
@ -137,18 +138,20 @@ contract RollingDutchAuction {
Auction storage state = _auctions[auctionId];
require(state.price == 0, "AUCTION EXISTS");
if (state.price != 0) {
revert AuctionExists();
}
IERC20(reserveToken).transferFrom(msg.sender, address(this), reserveAmount);
state.vestingAddress = vestingAddress;
state.duration = endTimestamp - startTimestamp;
state.windowDuration = windowDuration;
state.windowTimestamp = startTimestamp;
state.startTimestamp = startTimestamp;
state.endTimestamp = endTimestamp;
state.price = startingOriginPrice;
state.reserves = reserveAmount;
state.price = startingOriginPrice;
state.vesting = vestingAddress;
emit NewAuction(auctionId, reserveToken, reserveAmount, startingOriginPrice, endTimestamp);
@ -163,46 +166,40 @@ contract RollingDutchAuction {
(,,, minimumAmount,) = abi.decode(auctionId, (address, address, address, uint256, bytes));
}
/*
* @dev Helper to view an auction's maximum order reserve amount
* @param a͟u͟c͟t͟i͟o͟n͟I͟d͟ Encoded auction parameter identifier
*/
function maximumPurchase(bytes memory auctionId) public view returns (uint256) {
return unwrap(inv(scalarPrice(auctionId)));
}
/*
* @dev Helper to view an auction's active scalar price formatted to uint256
* @param a͟u͟c͟t͟i͟o͟n͟I͟d͟ Encoded auction parameter identifier
*/
function scalarPriceUint(bytes memory auctionId) public view returns (uint256) {
function scalarPriceUint(bytes calldata auctionId) external returns (uint256) {
return unwrap(scalarPrice(auctionId));
}
/*
* @dev Active price decay following time delta (x) between the current
* timestamp and the window's start timestamp or if the window is expired; time
* delta between the window's expiration. Which is the applied as the exponent
* of Euler's number and subject to the natural logarithim. Finally applied as
* as a product to the origin price (y) and substracted from itself
* @dev Active price decay proportional to time delta (t) between the current
* timestamp and the window's start timestamp or if the window is expired;
* the window's expiration. Time remaining (t_r) since the predefined
* timestamp until the auctions conclusion, is subtracted from t and applied
* as modulo to t subject to addition of itself. The resultant is divided by t_r
* to compute elapsed progress (x) from the last timestamp, x is multipled by
* the origin price (y) and subtracted by y to result the decayed price
* @param a͟u͟c͟t͟i͟o͟n͟I͟d͟ Encoded auction parameter identifier
*/
function scalarPrice(bytes memory auctionId) public view returns (UD60x18) {
Auction storage state = _auctions[auctionId];
Window storage w = _window[auctionId][_windows[auctionId]];
Window storage window = _window[auctionId][_windows[auctionId]];
bool isInitialised = w.expiry != 0;
bool isExpired = w.expiry < block.timestamp && isInitialised;
bool isInitialised = window.expiry != 0;
bool isExpired = window.expiry < block.timestamp && isInitialised;
uint256 timestamp = isExpired ? w.expiry : state.windowTimestamp;
uint256 timestamp = isExpired ? window.expiry : state.windowTimestamp;
UD60x18 t = wrap(block.timestamp - timestamp);
UD60x18 t_r = wrap(state.endTimestamp - timestamp);
UD60x18 t_r = wrap(state.duration - elapsedTime(auctionId, timestamp));
UD60x18 x = div(add(t, mod(t, sub(t_r, t))), t_r);
UD60x18 y = !isInitialised ? wrap(state.price) : wrap(w.price);
UD60x18 y = !isInitialised ? wrap(state.price) : wrap(window.price);
return sub(y, mul(ln(exp(x)), y));
return sub(y, mul(y, x));
}
/*
@ -213,33 +210,40 @@ contract RollingDutchAuction {
*/
function commitBid(bytes memory auctionId, uint256 price, uint256 volume)
activeAuction(auctionId)
public returns (bytes memory) {
Window storage w = _window[auctionId][_windows[auctionId]];
external returns (bytes memory) {
Window storage window = _window[auctionId][_windows[auctionId]];
require(minimumPurchase(auctionId) <= volume, "INSUFFICIENT VOLUME");
if (volume < minimumPurchase(auctionId)) {
revert InvalidPurchaseVolume();
}
bool hasExpired;
if (w.expiry != 0) {
if (window.expiry != 0) {
if (remainingWindowTime(auctionId) > 0) {
if (w.price < price) {
require(w.volume <= volume, "INSUFFICIENT WINDOW VOLUME");
if (window.price < price) {
if (volume < window.volume) {
revert InvalidWindowVolume();
}
} else {
require(w.price < price, "INVALID WINDOW PRICE");
revert InvalidWindowPrice();
}
} else {
hasExpired = true;
}
}
if (w.price == 0 || hasExpired) {
require(gte(wrap(price), scalarPrice(auctionId)), "INVALID CURVE PRICE");
if (window.price == 0 || hasExpired) {
if (gt(scalarPrice(auctionId), wrap(price))) {
revert InvalidScalarPrice();
}
}
IERC20(purchaseToken(auctionId)).transferFrom(msg.sender, address(this), volume);
require(_auctions[auctionId].reserves >= (volume / price), "INSUFFICIENT RESERVES");
require(maximumPurchase(auctionId) >= (volume / price), "INVALID VOLUME");
if (_auctions[auctionId].reserves < (volume / price)) {
revert InsufficientReserves();
}
bytes memory bidId = abi.encode(auctionId, msg.sender, price, volume);
@ -248,17 +252,17 @@ contract RollingDutchAuction {
_claims[msg.sender][auctionId] = abi.encode(refund + volume, claim);
if (hasExpired) {
w = _window[auctionId][windowExpiration(auctionId)];
window = _window[auctionId][windowExpiration(auctionId)];
}
_auctions[auctionId].windowTimestamp = block.timestamp;
w.expiry = block.timestamp + _auctions[auctionId].windowDuration;
w.volume = volume;
w.price = price;
w.bidId = bidId;
window.expiry = block.timestamp + _auctions[auctionId].windowDuration;
window.volume = volume;
window.price = price;
window.bidId = bidId;
emit Offer(auctionId, msg.sender, w.bidId, w.expiry);
emit Offer(auctionId, msg.sender, window.bidId, window.expiry);
return bidId;
}
@ -272,8 +276,6 @@ contract RollingDutchAuction {
uint256 auctionElapsedTime = elapsedTime(auctionId, block.timestamp);
uint256 auctionRemainingTime = _auctions[auctionId].duration - auctionElapsedTime;
bytes memory winningBidId = _window[auctionId][windowIndex].bidId;
_auctions[auctionId].endTimestamp = block.timestamp + auctionRemainingTime;
_auctions[auctionId].price = _window[auctionId][windowIndex].price;
@ -281,7 +283,7 @@ contract RollingDutchAuction {
fulfillWindow(auctionId, windowIndex);
emit Expiration(auctionId, winningBidId, windowIndex);
emit Expiration(auctionId, _window[auctionId][windowIndex].bidId, windowIndex);
return windowIndex + 1;
}
@ -291,24 +293,28 @@ contract RollingDutchAuction {
* @param a͟u͟c͟t͟i͟o͟n͟I͟d͟ Encoded auction parameter identifier
*/
function fulfillWindow(bytes memory auctionId, uint256 windowId) public {
Window storage w = _window[auctionId][windowId];
Window storage window = _window[auctionId][windowId];
require(w.expiry < block.timestamp, "WINDOW UNEXPIRED");
require(!w.processed, "WINDOW ALREADY FUFILLED");
if (window.expiry > block.timestamp) {
revert WindowUnexpired();
}
if (window.processed) {
revert WindowFulfilled();
}
(, address bidder, uint256 price, uint256 volume) = abi.decode(w.bidId, (bytes, address, uint256, uint256));
(, address bidder, uint256 price, uint256 volume) = abi.decode(window.bidId, (bytes, address, uint256, uint256));
(uint256 refund, uint256 claim) = balancesOf(_claims[bidder][auctionId]);
delete _claims[bidder][auctionId];
w.processed = true;
window.processed = true;
_auctions[auctionId].reserves -= volume / price;
_auctions[auctionId].proceeds += volume;
_claims[bidder][auctionId] = abi.encode(refund - volume, claim + (volume / price));
emit Fufillment(auctionId, w.bidId, windowId);
emit Fulfillment(auctionId, window.bidId, windowId);
}
/*
@ -332,10 +338,10 @@ contract RollingDutchAuction {
function remainingWindowTime(bytes memory auctionId) public view returns (uint256) {
uint256 expiryTimestamp = _window[auctionId][_windows[auctionId]].expiry;
if (expiryTimestamp == 0 || block.timestamp > expiryTimestamp) {
return 0;
} else {
if (expiryTimestamp > 0 && block.timestamp < expiryTimestamp) {
return expiryTimestamp - block.timestamp;
} else {
return 0;
}
}
@ -345,9 +351,14 @@ contract RollingDutchAuction {
*/
function elapsedTime(bytes memory auctionId, uint256 timestamp) public view returns (uint256) {
uint256 windowIndex = _windows[auctionId] + 1;
uint256 auctionElapsedTime = timestamp - _auctions[auctionId].startTimestamp;
uint256 windowElapsedTime = _auctions[auctionId].windowDuration * windowIndex;
return timestamp - _auctions[auctionId].startTimestamp - windowElapsedTime;
if (auctionElapsedTime > windowElapsedTime) {
return auctionElapsedTime - windowElapsedTime;
} else {
return auctionElapsedTime;
}
}
/*
@ -356,7 +367,7 @@ contract RollingDutchAuction {
*/
function withdraw(bytes memory auctionId)
inactiveAuction(auctionId)
public {
external {
uint256 proceeds = _auctions[auctionId].proceeds;
uint256 reserves = _auctions[auctionId].reserves;
@ -379,8 +390,9 @@ contract RollingDutchAuction {
*/
function redeem(address bidder, bytes memory auctionId)
inactiveAuction(auctionId)
public {
external {
bytes memory claimHash = _claims[bidder][auctionId];
address vestingAddress = _auctions[auctionId].vesting;
(uint256 refund, uint256 claim) = balancesOf(claimHash);
@ -390,23 +402,10 @@ contract RollingDutchAuction {
IERC20(purchaseToken(auctionId)).transfer(bidder, refund);
}
if (claim > 0) {
IVesting(_auctions[auctionId].vestingAddress).makeCommitment(bidder, claim);
IVesting(vestingAddress).makeCommitment(bidder, claim);
}
emit Claim(auctionId, claimHash);
}
event NewAuction(
bytes indexed auctionId, address reserveToken, uint256 reserves, uint256 price, uint256 endTimestamp
);
event Offer(bytes indexed auctionId, address indexed owner, bytes indexed bidId, uint256 expiry);
event Fufillment(bytes indexed auctionId, bytes indexed bidId, uint256 windowId);
event Expiration(bytes indexed auctionId, bytes indexed bidId, uint256 windowId);
event Claim(bytes indexed auctionId, bytes indexed bidId);
event Withdraw(bytes indexed auctionId);
}

View File

@ -2,14 +2,7 @@ pragma solidity 0.8.0;
interface IGovernance {
function lock(
address owner,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
function lock(address owner, uint256 amount, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external;
function delegate(address to) external;

51
src/interfaces/IRDA.sol Normal file
View File

@ -0,0 +1,51 @@
pragma solidity 0.8.13;
interface IRDA {
error InvalidPurchaseVolume();
error InvalidWindowVolume();
error InvalidWindowPrice();
error InsufficientReserves();
error InvalidScalarPrice();
error WindowUnexpired();
error WindowFulfilled();
error AuctionExists();
function createAuction(
address operatorAddress,
address reserveToken,
address purchaseToken,
uint256 reserveAmount,
uint256 minimumPurchaseAmount,
uint256 startingOriginPrice,
uint256 startTimestamp,
uint256 endTimestamp,
uint256 windowDuration
) external returns (bytes memory);
function withdraw(bytes memory auctionId) external;
function redeem(address bidder, bytes memory auctionId) external;
event NewAuction(
bytes indexed auctionId, address reserveToken, uint256 reserves, uint256 price, uint256 endTimestamp
);
event Offer(bytes indexed auctionId, address indexed owner, bytes indexed bidId, uint256 expiry);
event Fulfillment(bytes indexed auctionId, bytes indexed bidId, uint256 windowId);
event Expiration(bytes indexed auctionId, bytes indexed bidId, uint256 windowId);
event Claim(bytes indexed auctionId, bytes indexed bidId);
event Withdraw(bytes indexed auctionId);
}

View File

@ -1,22 +0,0 @@
pragma solidity 0.8.0;
interface IRollingDutchAuction {
function createAuction(
address vestingAddress,
address operatorAddress,
address reserveToken,
address purchaseToken,
uint256 reserveAmount,
uint256 minimumPurchaseAmount,
uint256 startingOriginPrice,
uint256 startTimestamp,
uint256 endTimestamp,
uint256 windowDuration
) external returns (bytes memory);
function withdraw(bytes memory auctionId) external;
function redeem(address bidder, bytes memory auctionId external);
}