This repository was archived by the owner on Jan 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Brian/linear auction library #219
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
968c172
First stab at auction libraries.
bweick 5adb017
Linear library finished with tests.
bweick 4107d5a
Added expectedPrice function to rebalancing token wrapper. Added chec…
bweick 12ec6e5
Added and edited comments.
bweick 733d204
Removed .only
bweick 071eb0e
Moved constantPriceAuctionLibrary since it is not tested and thus sho…
bweick be1af07
Renamed contracts. Added auction time increment constant.
bweick 074e119
Removed .only
bweick f83009a
Added awaits to blockchain calls.
bweick f5d5500
Formatting and comments. Removed timeIncrement constant from LinearAu…
bweick d826daa
Edited missed renaming.
bweick 6134966
Remove .only
bweick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
contracts/core/lib/auction-price-libraries/IAuctionPriceCurve.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| 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.24; | ||
|
|
||
| /** | ||
| * @title IAuctionPriceCurve | ||
| * @author Set Protocol | ||
| * | ||
| * The IAuctionPriceCurve interface provides a structured way to interact with any AuctionLibrary | ||
| */ | ||
|
|
||
| interface IAuctionPriceCurve { | ||
|
|
||
| /* | ||
| * Calculate the current priceRatio for an auction given defined price and time parameters | ||
| * | ||
| * @param _auctionStartTime Time of auction start | ||
| * @param _auctionStartPrice The price to start the auction at | ||
| * @param _curveCoefficient The slope (or convexity) of the price curve | ||
| */ | ||
| function getCurrentPrice( | ||
| uint256 _auctionStartTime, | ||
| uint256 _auctionStartPrice, | ||
| uint256 _curveCoefficient | ||
| ) | ||
| external | ||
| view; | ||
| } |
55 changes: 55 additions & 0 deletions
55
contracts/core/lib/auction-price-libraries/LinearAuctionPriceCurve.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| 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.24; | ||
|
|
||
| import { SafeMath } from "zeppelin-solidity/contracts/math/SafeMath.sol"; | ||
|
|
||
|
|
||
| /** | ||
| * @title LinearAuctionPriceCurve | ||
| * @author Set Protocol | ||
| * | ||
| * Contract used in rebalancing auctions to calculate price based off of a linear curve | ||
| */ | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None here |
||
|
|
||
| contract LinearAuctionPriceCurve { | ||
| using SafeMath for uint256; | ||
|
|
||
| /* | ||
| * Calculate the current priceRatio for an auction given defined price and time parameters | ||
| * | ||
| * @param _auctionStartTime Time of auction start | ||
| * @param _auctionStartPrice The price to start the auction at | ||
| * @param _curveCoefficient The slope (or convexity) of the price curve | ||
| */ | ||
| function getCurrentPrice( | ||
| uint256 _auctionStartTime, | ||
| uint256 _auctionStartPrice, | ||
| uint256 _curveCoefficient | ||
| ) | ||
| external | ||
| view | ||
| returns (uint256) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| // Calculate how much time has elapsed since start of auction and divide by | ||
| // timeIncrement of 30 seconds, so price changes every 30 seconds | ||
| uint256 elapsed = block.timestamp.sub(_auctionStartTime).div(30); | ||
|
|
||
| return _curveCoefficient.mul(elapsed).add(_auctionStartPrice); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| 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.24; | ||
|
|
||
|
|
||
| /** | ||
| * @title ConstantAuctionPriceCurve | ||
| * @author Set Protocol | ||
| * | ||
| * Contract used in rebalancing auction testing to return consistent price | ||
| * | ||
| */ | ||
|
|
||
| contract ConstantAuctionPriceCurve { | ||
|
|
||
| uint256 public constantPrice; | ||
|
|
||
| /* | ||
| * Declare price you want this library to return when queried | ||
| * | ||
| * @param _price The price you want this library to always return | ||
| */ | ||
| constructor( | ||
| uint256 _price | ||
| ) | ||
| public | ||
| { | ||
| // Set price to be returned by library | ||
| constantPrice = _price; | ||
| } | ||
|
|
||
|
|
||
| /* | ||
| * Return constant price amount | ||
| * | ||
| * @param -- Unused auction start time to conform to IAuctionPriceCurve -- | ||
| * @param -- Unused auction start price to conform to IAuctionPriceCurve -- | ||
| * @param -- Unused curve coefficient to conform to IAuctionPriceCurve -- | ||
| */ | ||
| function getCurrentPrice( | ||
| uint256, | ||
| uint256, | ||
| uint256 | ||
| ) | ||
| external | ||
| view | ||
| returns (uint256) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| return constantPrice; | ||
| } | ||
| } | ||
91 changes: 91 additions & 0 deletions
91
test/core/lib/auction-price-libraries/linearAuctionPriceCurve.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| require('module-alias/register'); | ||
|
|
||
| import * as chai from 'chai'; | ||
| import * as setProtocolUtils from 'set-protocol-utils'; | ||
| import { Address } from 'set-protocol-utils'; | ||
| import { BigNumber } from 'bignumber.js'; | ||
|
|
||
| import { LinearAuctionPriceCurveContract } from '@utils/contracts'; | ||
| import { Blockchain } from '@utils/blockchain'; | ||
| import { ERC20Wrapper } from '@utils/erc20Wrapper'; | ||
| import { CoreWrapper } from '@utils/coreWrapper'; | ||
| import { RebalancingTokenWrapper } from '@utils/RebalancingTokenWrapper'; | ||
| import { BigNumberSetup } from '@utils/bigNumberSetup'; | ||
| import ChaiSetup from '@utils/chaiSetup'; | ||
| import { DEFAULT_GAS } from '@utils/constants'; | ||
|
|
||
| BigNumberSetup.configure(); | ||
| ChaiSetup.configure(); | ||
| const { SetProtocolUtils: SetUtils } = setProtocolUtils; | ||
| const { expect } = chai; | ||
|
|
||
|
|
||
| contract('LinearAuctionPriceCurve', accounts => { | ||
| const [ | ||
| ownerAccount, | ||
| ] = accounts; | ||
|
|
||
| let auctionCurve: LinearAuctionPriceCurveContract; | ||
|
|
||
| const coreWrapper = new CoreWrapper(ownerAccount, ownerAccount); | ||
| const erc20Wrapper = new ERC20Wrapper(ownerAccount); | ||
| const blockchain = new Blockchain(web3); | ||
| const rebalancingTokenWrapper = new RebalancingTokenWrapper( | ||
| ownerAccount, | ||
| coreWrapper, | ||
| erc20Wrapper, | ||
| blockchain | ||
| ); | ||
|
|
||
| beforeEach(async () => { | ||
| await blockchain.saveSnapshotAsync(); | ||
| auctionCurve = await coreWrapper.deployLinearAuctionPriceCurveAsync(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await blockchain.revertAsync(); | ||
| }); | ||
|
|
||
| describe('#getCurrentPrice', async () => { | ||
| let subjectAuctionStartTime: BigNumber; | ||
| let subjectAuctionStartPrice: BigNumber; | ||
| let subjectCurveCoefficient: BigNumber; | ||
| let subjectCaller: Address; | ||
|
|
||
| beforeEach(async () => { | ||
| subjectAuctionStartPrice = new BigNumber(500); | ||
| subjectCurveCoefficient = new BigNumber (5); | ||
| subjectAuctionStartTime = SetUtils.generateTimestamp(0); | ||
| subjectCaller = ownerAccount; | ||
| }); | ||
|
|
||
| async function subject(): Promise<BigNumber> { | ||
| return auctionCurve.getCurrentPrice.callAsync( | ||
| subjectAuctionStartTime, | ||
| subjectAuctionStartPrice, | ||
| subjectCurveCoefficient, | ||
| { from: subjectCaller, gas: DEFAULT_GAS} | ||
| ); | ||
| } | ||
|
|
||
| it('starts with the correct price', async () => { | ||
| const returnedPrice = await subject(); | ||
|
|
||
| expect(returnedPrice).to.be.bignumber.equal(subjectAuctionStartPrice); | ||
| }); | ||
|
|
||
| it('returns the correct price after one hour', async () => { | ||
| const timeJump = new BigNumber(3600); | ||
| await blockchain.increaseTimeAsync(timeJump); | ||
|
|
||
| const returnedPrice = await subject(); | ||
|
|
||
| const expectedPrice = rebalancingTokenWrapper.getExpectedLinearAuctionPrice( | ||
| timeJump, | ||
| subjectCurveCoefficient, | ||
| subjectAuctionStartPrice | ||
| ); | ||
| expect(returnedPrice).to.be.bignumber.equal(expectedPrice); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the future. Inconsistent spacing. Two lines here