Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions contracts/managers/lib/Oscillator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2019 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";


/**
* @title Oscillator
* @author Set Protocol
*
* Library of utility functions to deal with oscillator-related functionality.
*/
library Oscillator {

enum State { UPPER, LOWER, NEUTRAL }

// Oscillator bounds typically between 0 and 100
struct Bounds {
uint256 lower;
uint256 upper;
}

/*
* Returns upper of value is greater or equal to upper bound.
* Returns lower if lower than lower bound, and neutral if in between.
*/
function getState(
Bounds storage _bounds,
uint256 _value
)
internal
view
returns(State)
{
return _value >= _bounds.upper ? State.UPPER :
_value < _bounds.lower ? State.LOWER : State.NEUTRAL;
}
}
37 changes: 1 addition & 36 deletions contracts/managers/triggers/ITrigger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,13 @@ pragma experimental "ABIEncoderV2";
* Interface for interacting with PriceTrigger contracts
*/
interface ITrigger {

event TriggerFlipped(
bool _flipTo,
uint256 _triggerFlippedIndex,
uint256 _timestamp
);

/*
* Returns bool indicating whether the current market conditions are bullish.
*
* @return The percentage of base asset to be allocated to
* @return Boolean whether condition is bullish
*/
function isBullish()
external
view
returns (bool);

/*
* For triggers that require confirmation, start the confirmation period.
*/
function initialTrigger()
external;

/*
* Confirm the signal.
*/
function confirmTrigger()
external;

/*
* Check if initialTrigger can be successfully called.
*/
function canInitialTrigger()
external
view
returns (bool);

/*
* Check if confirmTrigger can be successfully called.
*/
function canConfirmTrigger()
external
view
returns (bool);
}
75 changes: 75 additions & 0 deletions contracts/managers/triggers/MovingAverageCrossoverTrigger.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2019 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 { ITrigger } from "./ITrigger.sol";
import { IOracle } from "../../meta-oracles/interfaces/IOracle.sol";
import { IMetaOracleV2 } from "../../meta-oracles/interfaces/IMetaOracleV2.sol";


/**
* @title MovingAverageCrossoverTrigger
* @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 the trading pair price is above or below a moving average.
*/
contract MovingAverageCrossoverTrigger is
ITrigger
{
using SafeMath for uint256;

/* ============ State Variables ============ */
IMetaOracleV2 public movingAveragePriceFeedInstance;
IOracle public assetPairOracleInstance;
uint256 public movingAverageDays;

/*
* MovingAverageCrossoverTrigger constructor.
*
* @param _movingAveragePriceFeedInstance The address of MA price feed
* @param _assetPairOracleInstance The address of risk asset oracle
* @param _movingAverageDays The amount of days to use in moving average calculation
*/
constructor(
IMetaOracleV2 _movingAveragePriceFeedInstance,
IOracle _assetPairOracleInstance,
uint256 _movingAverageDays
)
public
{
movingAveragePriceFeedInstance = _movingAveragePriceFeedInstance;
assetPairOracleInstance = _assetPairOracleInstance;
movingAverageDays = _movingAverageDays;
}

/* ============ External ============ */

/*
* If asset pair price greater than moving average return true, else return false
*/
function isBullish() external view returns (bool) {
uint256 movingAverage = movingAveragePriceFeedInstance.read(movingAverageDays);
uint256 assetPairPrice = assetPairOracleInstance.read();

return assetPairPrice > movingAverage;
}
}
Loading