-
Notifications
You must be signed in to change notification settings - Fork 82
/
FixedPriceStrategy.sol
63 lines (53 loc) · 2.11 KB
/
FixedPriceStrategy.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
import {Math} from '@openzeppelin/contracts/utils/math/Math.sol';
import {IGsmPriceStrategy} from './interfaces/IGsmPriceStrategy.sol';
/**
* @title FixedPriceStrategy
* @author Aave
* @notice Price strategy involving a fixed-rate conversion from an underlying asset to GHO
*/
contract FixedPriceStrategy is IGsmPriceStrategy {
using Math for uint256;
/// @inheritdoc IGsmPriceStrategy
uint256 public constant GHO_DECIMALS = 18;
/// @inheritdoc IGsmPriceStrategy
address public immutable UNDERLYING_ASSET;
/// @inheritdoc IGsmPriceStrategy
uint256 public immutable UNDERLYING_ASSET_DECIMALS;
/// @dev The price ratio from underlying asset to GHO (expressed in WAD), e.g. a ratio of 2e18 means 2 GHO per 1 underlying asset
uint256 public immutable PRICE_RATIO;
/// @dev Underlying asset units represent units for the underlying asset
uint256 internal immutable _underlyingAssetUnits;
/**
* @dev Constructor
* @param priceRatio The price ratio from underlying asset to GHO (expressed in WAD)
* @param underlyingAsset The address of the underlying asset
* @param underlyingAssetDecimals The number of decimals of the underlying asset
*/
constructor(uint256 priceRatio, address underlyingAsset, uint8 underlyingAssetDecimals) {
require(priceRatio > 0, 'INVALID_PRICE_RATIO');
PRICE_RATIO = priceRatio;
UNDERLYING_ASSET = underlyingAsset;
UNDERLYING_ASSET_DECIMALS = underlyingAssetDecimals;
_underlyingAssetUnits = 10 ** underlyingAssetDecimals;
}
/// @inheritdoc IGsmPriceStrategy
function getAssetPriceInGho(uint256 assetAmount, bool roundUp) external view returns (uint256) {
return
assetAmount.mulDiv(
PRICE_RATIO,
_underlyingAssetUnits,
roundUp ? Math.Rounding.Up : Math.Rounding.Down
);
}
/// @inheritdoc IGsmPriceStrategy
function getGhoPriceInAsset(uint256 ghoAmount, bool roundUp) external view returns (uint256) {
return
ghoAmount.mulDiv(
_underlyingAssetUnits,
PRICE_RATIO,
roundUp ? Math.Rounding.Up : Math.Rounding.Down
);
}
}