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 1
Add two MA crossover trigger #144
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
81 changes: 81 additions & 0 deletions
81
contracts/managers/triggers/TwoMovingAverageCrossoverTrigger.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,81 @@ | ||
/* | ||
Copyright 2020 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.5.7; | ||
pragma experimental "ABIEncoderV2"; | ||
|
||
import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol"; | ||
import { IMetaOracleV2 } from "set-protocol-oracles/contracts/meta-oracles/interfaces/IMetaOracleV2.sol"; | ||
|
||
import { ITrigger } from "./ITrigger.sol"; | ||
|
||
|
||
/** | ||
* @title TwoMovingAverageCrossoverTrigger | ||
* @author Set Protocol | ||
* | ||
* Implementing the ITrigger interface, this contract is queried by a | ||
* RebalancingSetToken Manager to determine if the market is in a bullish | ||
* state by checking if the shorter term moving average is above the longer term | ||
* moving average. | ||
* Note: The MA oracles can be the same or different contracts to allow flexbility of timeframes | ||
* and types of moving averages (EMA, SMA) | ||
*/ | ||
contract TwoMovingAverageCrossoverTrigger is | ||
ITrigger | ||
{ | ||
using SafeMath for uint256; | ||
|
||
/* ============ State Variables ============ */ | ||
IMetaOracleV2 public shortTermMAOracle; | ||
IMetaOracleV2 public longTermMAOracle; | ||
uint256 public shortTermMATimePeriod; | ||
uint256 public longTermMATimePeriod; | ||
|
||
/* | ||
* TwoMovingAverageCrossoverTrigger constructor. | ||
* | ||
* @param _longTermMAOracle The instance of longer term MA oracle | ||
* @param _shortTermMAOracle The instance of shorter term MA oracle | ||
* @param _longTermMATimePeriod The time period in the longer term MA oracle to use in the calculation | ||
* @param _shortTermMATimePeriod The time period in the shorter term MA oracle to use in the calculation | ||
*/ | ||
constructor( | ||
IMetaOracleV2 _longTermMAOracle, | ||
IMetaOracleV2 _shortTermMAOracle, | ||
uint256 _longTermMATimePeriod, | ||
uint256 _shortTermMATimePeriod | ||
) | ||
public | ||
{ | ||
longTermMAOracle = _longTermMAOracle; | ||
shortTermMAOracle = _shortTermMAOracle; | ||
longTermMATimePeriod = _longTermMATimePeriod; | ||
shortTermMATimePeriod = _shortTermMATimePeriod; | ||
} | ||
|
||
/* ============ External ============ */ | ||
|
||
/* | ||
* If shorter term MA is greater than longer term MA return true, else return false | ||
*/ | ||
function isBullish() external view returns (bool) { | ||
uint256 longTermMovingAverage = longTermMAOracle.read(longTermMATimePeriod); | ||
uint256 shortTermMovingAverage = shortTermMAOracle.read(shortTermMATimePeriod); | ||
|
||
return shortTermMovingAverage > longTermMovingAverage; | ||
} | ||
} |
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
255 changes: 255 additions & 0 deletions
255
test/contracts/managers/triggers/twoMovingAverageCrossoverTrigger.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,255 @@ | ||
require('module-alias/register'); | ||
|
||
import * as _ from 'lodash'; | ||
import * as ABIDecoder from 'abi-decoder'; | ||
import * as chai from 'chai'; | ||
import * as setProtocolUtils from 'set-protocol-utils'; | ||
|
||
import { Address } from 'set-protocol-utils'; | ||
import { BigNumber } from 'bignumber.js'; | ||
|
||
import ChaiSetup from '@utils/chaiSetup'; | ||
import { BigNumberSetup } from '@utils/bigNumberSetup'; | ||
import { Blockchain } from 'set-protocol-contracts'; | ||
import { ether } from '@utils/units'; | ||
|
||
import { | ||
LegacyMakerOracleAdapterContract, | ||
LinearizedPriceDataSourceContract, | ||
MedianContract, | ||
MovingAverageOracleV2Contract, | ||
OracleProxyContract, | ||
TimeSeriesFeedContract, | ||
} from 'set-protocol-oracles'; | ||
import { | ||
TwoMovingAverageCrossoverTriggerContract, | ||
} from '@utils/contracts'; | ||
|
||
import { | ||
DEFAULT_GAS, | ||
ONE_DAY_IN_SECONDS | ||
} from '@utils/constants'; | ||
|
||
import { getWeb3 } from '@utils/web3Helper'; | ||
|
||
import { ManagerHelper } from '@utils/helpers/managerHelper'; | ||
import { OracleHelper } from 'set-protocol-oracles'; | ||
import { ProtocolHelper } from '@utils/helpers/protocolHelper'; | ||
|
||
BigNumberSetup.configure(); | ||
ChaiSetup.configure(); | ||
|
||
const TwoMovingAverageCrossoverTrigger = artifacts.require('TwoMovingAverageCrossoverTrigger'); | ||
const web3 = getWeb3(); | ||
const { expect } = chai; | ||
const blockchain = new Blockchain(web3); | ||
const { SetProtocolTestUtils: SetTestUtils } = setProtocolUtils; | ||
|
||
contract('TwoMovingAverageCrossoverTrigger', accounts => { | ||
const [ | ||
deployerAccount, | ||
] = accounts; | ||
|
||
let ethMedianizer: MedianContract; | ||
let legacyMakerOracleAdapter: LegacyMakerOracleAdapterContract; | ||
let oracleProxy: OracleProxyContract; | ||
let linearizedDataSource: LinearizedPriceDataSourceContract; | ||
let longTermTimeSeriesFeed: TimeSeriesFeedContract; | ||
let shortTermTimeSeriesFeed: TimeSeriesFeedContract; | ||
let longTermMAOracle: MovingAverageOracleV2Contract; | ||
let shortTermMAOracle: MovingAverageOracleV2Contract; | ||
|
||
let trigger: TwoMovingAverageCrossoverTriggerContract; | ||
|
||
let initialEthPrice: BigNumber; | ||
|
||
const managerHelper = new ManagerHelper(deployerAccount); | ||
const oracleHelper = new OracleHelper(deployerAccount); | ||
const protocolHelper = new ProtocolHelper(deployerAccount); | ||
|
||
before(async () => { | ||
ABIDecoder.addABI(TwoMovingAverageCrossoverTrigger.abi); | ||
}); | ||
|
||
after(async () => { | ||
ABIDecoder.removeABI(TwoMovingAverageCrossoverTrigger.abi); | ||
}); | ||
|
||
beforeEach(async () => { | ||
blockchain.saveSnapshotAsync(); | ||
|
||
ethMedianizer = await protocolHelper.getDeployedWETHMedianizerAsync(); | ||
await oracleHelper.addPriceFeedOwnerToMedianizer(ethMedianizer, deployerAccount); | ||
|
||
initialEthPrice = ether(150); | ||
await oracleHelper.updateMedianizerPriceAsync( | ||
ethMedianizer, | ||
initialEthPrice, | ||
SetTestUtils.generateTimestamp(1000), | ||
); | ||
|
||
|
||
legacyMakerOracleAdapter = await oracleHelper.deployLegacyMakerOracleAdapterAsync( | ||
ethMedianizer.address, | ||
); | ||
|
||
oracleProxy = await oracleHelper.deployOracleProxyAsync( | ||
legacyMakerOracleAdapter.address, | ||
); | ||
|
||
const interpolationThreshold = ONE_DAY_IN_SECONDS; | ||
linearizedDataSource = await oracleHelper.deployLinearizedPriceDataSourceAsync( | ||
oracleProxy.address, | ||
interpolationThreshold, | ||
); | ||
|
||
await oracleHelper.addAuthorizedAddressesToOracleProxy( | ||
oracleProxy, | ||
[linearizedDataSource.address] | ||
); | ||
|
||
const seededLongTermMATimePeriod = _.map(new Array(20), function(el, i) {return ether(150 + i); }); | ||
longTermTimeSeriesFeed = await oracleHelper.deployTimeSeriesFeedAsync( | ||
linearizedDataSource.address, | ||
seededLongTermMATimePeriod | ||
); | ||
|
||
const dataDescriptionLongTermMA = 'ETHDaily20MA'; | ||
longTermMAOracle = await oracleHelper.deployMovingAverageOracleV2Async( | ||
longTermTimeSeriesFeed.address, | ||
dataDescriptionLongTermMA | ||
); | ||
|
||
const seededShortTermMATimePeriod = _.map(new Array(10), function(el, i) {return ether(200 + i); }); | ||
shortTermTimeSeriesFeed = await oracleHelper.deployTimeSeriesFeedAsync( | ||
linearizedDataSource.address, | ||
seededShortTermMATimePeriod | ||
); | ||
|
||
const dataDescriptionShortTermMA = 'ETHHourly10MA'; | ||
shortTermMAOracle = await oracleHelper.deployMovingAverageOracleV2Async( | ||
shortTermTimeSeriesFeed.address, | ||
dataDescriptionShortTermMA | ||
); | ||
}); | ||
|
||
afterEach(async () => { | ||
blockchain.revertAsync(); | ||
}); | ||
|
||
describe('#constructor', async () => { | ||
let subjectLongTermMAOracle: Address; | ||
let subjectShortTermMAOracle: Address; | ||
let subjectLongTermMATimePeriod: BigNumber; | ||
let subjectShortTermMATimePeriod: BigNumber; | ||
|
||
beforeEach(async () => { | ||
subjectLongTermMAOracle = longTermMAOracle.address; | ||
subjectShortTermMAOracle = shortTermMAOracle.address; | ||
subjectLongTermMATimePeriod = new BigNumber(20); | ||
subjectShortTermMATimePeriod = new BigNumber(10); | ||
}); | ||
|
||
async function subject(): Promise<TwoMovingAverageCrossoverTriggerContract> { | ||
return managerHelper.deployTwoMovingAverageCrossoverTrigger( | ||
subjectLongTermMAOracle, | ||
subjectShortTermMAOracle, | ||
subjectLongTermMATimePeriod, | ||
subjectShortTermMATimePeriod, | ||
); | ||
} | ||
|
||
it('sets the correct long term moving average oracle address', async () => { | ||
trigger = await subject(); | ||
|
||
const actualMovingAveragePriceFeedAddress = await trigger.longTermMAOracle.callAsync(); | ||
|
||
expect(actualMovingAveragePriceFeedAddress).to.equal(subjectLongTermMAOracle); | ||
}); | ||
|
||
it('sets the correct short term moving average oracle address', async () => { | ||
trigger = await subject(); | ||
|
||
const actualMovingAveragePriceFeedAddress = await trigger.shortTermMAOracle.callAsync(); | ||
|
||
expect(actualMovingAveragePriceFeedAddress).to.equal(subjectShortTermMAOracle); | ||
}); | ||
|
||
it('sets the correct long term moving average days', async () => { | ||
trigger = await subject(); | ||
|
||
const actualMovingAverageTimePeriod = await trigger.longTermMATimePeriod.callAsync(); | ||
|
||
expect(actualMovingAverageTimePeriod).to.be.bignumber.equal(subjectLongTermMATimePeriod); | ||
}); | ||
|
||
it('sets the correct short term moving average days', async () => { | ||
trigger = await subject(); | ||
|
||
const actualMovingAverageTimePeriod = await trigger.shortTermMATimePeriod.callAsync(); | ||
|
||
expect(actualMovingAverageTimePeriod).to.be.bignumber.equal(subjectShortTermMATimePeriod); | ||
}); | ||
}); | ||
|
||
describe('#isBullish', async () => { | ||
let subjectCaller: Address; | ||
|
||
let updatedLongTermTimePeriod: BigNumber[]; | ||
|
||
before(async () => { | ||
updatedLongTermTimePeriod = _.map(new Array(20), function(el, i) {return ether(150 + i); }); | ||
}); | ||
|
||
beforeEach(async () => { | ||
const longTermMATimePeriod = new BigNumber(20); | ||
const shortTermMATimePeriod = new BigNumber(10); | ||
|
||
trigger = await managerHelper.deployTwoMovingAverageCrossoverTrigger( | ||
longTermMAOracle.address, | ||
shortTermMAOracle.address, | ||
longTermMATimePeriod, | ||
shortTermMATimePeriod | ||
); | ||
await oracleHelper.addAuthorizedAddressesToOracleProxy( | ||
oracleProxy, | ||
[trigger.address] | ||
); | ||
|
||
await oracleHelper.batchUpdateTimeSeriesFeedAsync( | ||
longTermTimeSeriesFeed, | ||
ethMedianizer, | ||
updatedLongTermTimePeriod.length, | ||
updatedLongTermTimePeriod | ||
); | ||
|
||
subjectCaller = deployerAccount; | ||
}); | ||
|
||
async function subject(): Promise<boolean> { | ||
return trigger.isBullish.callAsync( | ||
{ from: subjectCaller, gas: DEFAULT_GAS} | ||
); | ||
} | ||
|
||
it('returns true', async () => { | ||
const result = await subject(); | ||
expect(result).to.be.true; | ||
}); | ||
|
||
describe('price going from bullish to bearish', async () => { | ||
before(async () => { | ||
updatedLongTermTimePeriod = _.map(new Array(19), function(el, i) {return ether(300 + i); }); | ||
}); | ||
|
||
after(async () => { | ||
updatedLongTermTimePeriod = _.map(new Array(19), function(el, i) {return ether(150 + i); }); | ||
}); | ||
|
||
it('returns false', async () => { | ||
const result = await subject(); | ||
expect(result).to.be.false; | ||
}); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.
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.
Wouldn't these both be the same oracle, just different days passed in?
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.
I guess that's true if we are using the same time frame i.e. 12 day EMA and 26 day EMA
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.
Keeping it flexible in case someone wants to do an hourly MA crossing a daily MA, or a 20 SMA crossing a 26 EMA etc