Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
Use structs to pass auction parameters to price curves, and store par…
Browse files Browse the repository at this point in the history
…ams as struct on rebalancingSetToken. (#309)
  • Loading branch information
bweick committed Dec 1, 2018
1 parent 84a32f4 commit 5253dba
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 112 deletions.
30 changes: 15 additions & 15 deletions contracts/core/RebalancingSetToken.sol
Expand Up @@ -31,6 +31,7 @@ import { ICore } from "./interfaces/ICore.sol";
import { IRebalancingSetFactory } from "./interfaces/IRebalancingSetFactory.sol";
import { ISetToken } from "./interfaces/ISetToken.sol";
import { IVault } from "./interfaces/IVault.sol";
import { RebalancingHelperLibrary } from "./lib/RebalancingHelperLibrary.sol";


/**
Expand Down Expand Up @@ -78,12 +79,9 @@ contract RebalancingSetToken is
uint256 public proposalStartTime;

// State needed for auction/rebalance
uint256 public auctionStartTime;
address public nextSet;
address public auctionLibrary;
uint256 public auctionStartPrice;
uint256 public auctionTimeToPivot;
uint256 public auctionPivotPrice;
RebalancingHelperLibrary.AuctionPriceParameters public auctionParameters;
uint256 public minimumBid;
address[] public combinedTokenArray;
uint256[] public combinedCurrentUnits;
Expand Down Expand Up @@ -245,11 +243,19 @@ contract RebalancingSetToken is
"RebalancingSetToken.propose: Invalid time to pivot, must be less than 3 days"
);

// Set auction parameters
nextSet = _nextSet;
auctionLibrary = _auctionLibrary;
auctionParameters = RebalancingHelperLibrary.AuctionPriceParameters({
auctionTimeToPivot: _auctionTimeToPivot,
auctionStartPrice: _auctionStartPrice,
auctionPivotPrice: _auctionPivotPrice,
auctionStartTime: 0
});

// Check that pivot price is compliant with library restrictions
IAuctionPriceCurve(_auctionLibrary).validateAuctionPriceParameters(
_auctionTimeToPivot,
_auctionStartPrice,
_auctionPivotPrice
auctionParameters
);

// Check that the propoosed set natural unit is a multiple of current set natural unit, or vice versa.
Expand All @@ -265,9 +271,6 @@ contract RebalancingSetToken is
// Set auction parameters
nextSet = _nextSet;
auctionLibrary = _auctionLibrary;
auctionTimeToPivot = _auctionTimeToPivot;
auctionStartPrice = _auctionStartPrice;
auctionPivotPrice = _auctionPivotPrice;

// Update state parameters
proposalStartTime = block.timestamp;
Expand Down Expand Up @@ -306,7 +309,7 @@ contract RebalancingSetToken is
redeemCurrentSet();

// Update state parameters
auctionStartTime = block.timestamp;
auctionParameters.auctionStartTime = block.timestamp;
rebalanceState = State.Rebalance;

emit RebalanceStarted(currentSet, nextSet);
Expand Down Expand Up @@ -447,10 +450,7 @@ contract RebalancingSetToken is

// Get bid conversion price, currently static placeholder for calling auctionlibrary
(uint256 priceNumerator, uint256 priceDivisor) = IAuctionPriceCurve(auctionLibrary).getCurrentPrice(
auctionStartTime,
auctionTimeToPivot,
auctionStartPrice,
auctionPivotPrice
auctionParameters
);

// Normalized quantity amount
Expand Down
40 changes: 40 additions & 0 deletions contracts/core/lib/RebalancingHelperLibrary.sol
@@ -0,0 +1,40 @@
/*
Copyright 2018 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

pragma solidity 0.4.25;
pragma experimental "ABIEncoderV2";


/**
* @title RebalancingHelperLibrary
* @author Set Protocol
*
* The Rebalancing Helper Library contains functions for facilitating the rebalancing process for
* Rebalancing Set Tokens.
*
*/

library RebalancingHelperLibrary {

/* ============ Structs ============ */

struct AuctionPriceParameters {
uint256 auctionStartTime;
uint256 auctionTimeToPivot;
uint256 auctionStartPrice;
uint256 auctionPivotPrice;
}
}
25 changes: 9 additions & 16 deletions contracts/core/lib/auction-price-libraries/IAuctionPriceCurve.sol
Expand Up @@ -15,6 +15,9 @@
*/

pragma solidity 0.4.25;
pragma experimental "ABIEncoderV2";

import { RebalancingHelperLibrary } from "../RebalancingHelperLibrary.sol";


/**
Expand All @@ -36,35 +39,25 @@ interface IAuctionPriceCurve {
/*
* Validate any auction parameters that have library-specific restrictions
*
* @param _auctionTimeToPivot Time until auction reaches pivot point
* @param _auctionStartPrice The price to start the auction at
* @param _auctionPivotPrice The price at which auction curve changes from linear to exponential
* @param _auctionPriceParameters Struct containing relevant auction price parameters
*/
function validateAuctionPriceParameters(
uint256 _auctionTimeToPivot,
uint256 _auctionStartPrice,
uint256 _auctionPivotPrice
RebalancingHelperLibrary.AuctionPriceParameters _auctionParameters
)
external
public
view;

/*
* Calculate the current priceRatio for an auction given defined price and time parameters
*
* @param _auctionStartTime Time of auction start
* @param _auctionTimeToPivot Time until auction reaches pivot point
* @param _auctionStartPrice The price to start the auction at
* @param _auctionPivotPrice The price at which auction curve changes from linear to exponential
* @param _auctionPriceParameters Struct containing relevant auction price parameters
* @return uint256 The auction price numerator
* @return uint256 The auction price denominator
*/
function getCurrentPrice(
uint256 _auctionStartTime,
uint256 _auctionTimeToPivot,
uint256 _auctionStartPrice,
uint256 _auctionPivotPrice
RebalancingHelperLibrary.AuctionPriceParameters _auctionParameters
)
external
public
view
returns (uint256, uint256);
}
Expand Up @@ -15,8 +15,10 @@
*/

pragma solidity 0.4.25;
pragma experimental "ABIEncoderV2";

import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";
import { RebalancingHelperLibrary } from "../RebalancingHelperLibrary.sol";


/**
Expand Down Expand Up @@ -47,57 +49,47 @@ contract LinearAuctionPriceCurve {
/*
* Validate any auction parameters that have library-specific restrictions
*
* @param -- Unused auction time to pivot param to conform to IAuctionPriceCurve --
* @param -- Unused auction start price to conform to IAuctionPriceCurve --
* @param _auctionPivotPrice The price at which auction curve changes from linear to exponential
* @param _auctionPriceParameters Struct containing relevant auction price parameters
*/
function validateAuctionPriceParameters(
uint256,
uint256,
uint256 _auctionPivotPrice
RebalancingHelperLibrary.AuctionPriceParameters _auctionParameters
)
external
public
view
{
// Require pivot price to be greater than 0.5 * price denominator
// Equivalent to oldSet/newSet = 0.5
require(
_auctionPivotPrice > priceDenominator.div(2),
_auctionParameters.auctionPivotPrice > priceDenominator.div(2),
"LinearAuctionPriceCurve.validateAuctionPriceParameters: Pivot price too low"
);
// Require pivot price to be less than 5 * price denominator
// Equivalent to oldSet/newSet = 5
require(
_auctionPivotPrice < priceDenominator.mul(5),
_auctionParameters.auctionPivotPrice < priceDenominator.mul(5),
"LinearAuctionPriceCurve.validateAuctionPriceParameters: Pivot price too high"
);
}

/*
* Calculate the current priceRatio for an auction given defined price and time parameters
*
* @param _auctionStartTime Time of auction start
* @param _auctionTimeToPivot Time until auction reaches pivot point
* @param -- Unused auction start price to conform to IAuctionPriceCurve --
* @param _auctionPivotPrice The price at which auction curve changes from linear to exponential
* @param _auctionPriceParameters Struct containing relevant auction price parameters
* @return uint256 The auction price numerator
* @return uint256 The auction price denominator
*/
function getCurrentPrice(
uint256 _auctionStartTime,
uint256 _auctionTimeToPivot,
uint256,
uint256 _auctionPivotPrice
RebalancingHelperLibrary.AuctionPriceParameters _auctionParameters
)
external
public
view
returns (uint256, uint256)
{
// Calculate how much time has elapsed since start of auction
uint256 elapsed = block.timestamp.sub(_auctionStartTime);
uint256 elapsed = block.timestamp.sub(_auctionParameters.auctionStartTime);

// Initialize numerator and denominator
uint256 priceNumerator = _auctionPivotPrice;
uint256 priceNumerator = _auctionParameters.auctionPivotPrice;
uint256 currentPriceDenominator = priceDenominator;

/*
Expand Down Expand Up @@ -136,12 +128,12 @@ contract LinearAuctionPriceCurve {
*/

// If time hasn't passed to pivot use the user-defined curve
if (elapsed <= _auctionTimeToPivot) {
if (elapsed <= _auctionParameters.auctionTimeToPivot) {
// Calculate the priceNumerator as a linear function of time between 0 and _auctionPivotPrice
priceNumerator = elapsed.mul(_auctionPivotPrice).div(_auctionTimeToPivot);
priceNumerator = elapsed.mul(_auctionParameters.auctionPivotPrice).div(_auctionParameters.auctionTimeToPivot);
} else {
// Calculate how many 30 second increments have passed since pivot was reached
uint256 thirtySecondPeriods = elapsed.sub(_auctionTimeToPivot).div(30);
uint256 thirtySecondPeriods = elapsed.sub(_auctionParameters.auctionTimeToPivot).div(30);

// Because after 1000 thirtySecondPeriods the priceDenominator would be 0 (causes revert)
if (thirtySecondPeriods < 1000) {
Expand All @@ -153,7 +145,9 @@ contract LinearAuctionPriceCurve {
currentPriceDenominator = 1;

// Now priceNumerator just changes linearly, but with slope equal to the pivot price
priceNumerator = _auctionPivotPrice.add(_auctionPivotPrice.mul(thirtySecondPeriods.sub(1000)));
priceNumerator = _auctionParameters.auctionPivotPrice.add(
_auctionParameters.auctionPivotPrice.mul(thirtySecondPeriods.sub(1000))
);
}
}

Expand Down
30 changes: 11 additions & 19 deletions contracts/mocks/core/lib/ConstantAuctionPriceCurve.sol
Expand Up @@ -15,8 +15,10 @@
*/

pragma solidity 0.4.25;
pragma experimental "ABIEncoderV2";

import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";
import { RebalancingHelperLibrary } from "../../../core/lib/RebalancingHelperLibrary.sol";

/**
* @title ConstantAuctionPriceCurve
Expand Down Expand Up @@ -52,49 +54,39 @@ contract ConstantAuctionPriceCurve {
/*
* Validate any auction parameters that have library-specific restrictions
*
* @param -- Unused auction time to pivot param to conform to IAuctionPriceCurve --
* @param -- Unused auction start price to conform to IAuctionPriceCurve --
* @param _auctionPivotPrice The price at which auction curve changes from linear to exponential
* @param _auctionPriceParameters Struct containing relevant auction price parameters
*/
function validateAuctionPriceParameters(
uint256,
uint256,
uint256 _auctionPivotPrice
RebalancingHelperLibrary.AuctionPriceParameters _auctionParameters
)
external
public
view
{
// Require pivot price to be greater than 0.5 * price denominator
// Equivalent to oldSet/newSet = 0.5
require(
_auctionPivotPrice > priceDenominator.div(2),
_auctionParameters.auctionPivotPrice > priceDenominator.div(2),
"LinearAuctionPriceCurve.validateAuctionPriceParameters: Pivot price too low"
);
// Require pivot price to be less than 5 * price denominator
// Equivalent to oldSet/newSet = 5
require(
_auctionPivotPrice < priceDenominator.mul(5),
_auctionParameters.auctionPivotPrice < priceDenominator.mul(5),
"LinearAuctionPriceCurve.validateAuctionPriceParameters: Pivot price too high"
);
}

/*
* Return constant price amount
* Calculate the current priceRatio for an auction given defined price and time parameters
*
* @param -- Unused auction start time to conform to IAuctionPriceCurve --
* @param -- Unused auction time to pivot param to conform to IAuctionPriceCurve --
* @param -- Unused auction start price to conform to IAuctionPriceCurve --
* @param -- Unused auction pivot price to conform to IAuctionPriceCurve --
* @param _auctionPriceParameters Struct containing relevant auction price parameters
* @return uint256 The auction price numerator
* @return uint256 The auction price denominator
*/
function getCurrentPrice(
uint256,
uint256,
uint256,
uint256
RebalancingHelperLibrary.AuctionPriceParameters _auctionParameters
)
external
public
view
returns (uint256, uint256)
{
Expand Down

0 comments on commit 5253dba

Please sign in to comment.