-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sebastian Siemssen <[email protected]>
- Loading branch information
1 parent
828c32e
commit 34bc83e
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,8 @@ | ||
# Contracts primarily for off-chain consumption | ||
|
||
This directory contains data aggregation contracts that are primarily intended to be consumed off-chain. | ||
|
||
Prior to using any of these contracts for on-chain purposes, confirm that: | ||
|
||
1. it has been audited | ||
2. the logic is suitable for on-chain use (gas, state changes, etc) |
This file contains 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,78 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
/* | ||
This file is part of the Enzyme Protocol. | ||
(c) Enzyme Council <[email protected]> | ||
For the full license information, please view the LICENSE | ||
file that was distributed with this source code. | ||
*/ | ||
|
||
pragma solidity 0.6.12; | ||
|
||
import "@openzeppelin/contracts/math/SafeMath.sol"; | ||
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
import "../infrastructure/value-interpreter/ValueInterpreter.sol"; | ||
|
||
/// @title AssetValueCalculator Contract | ||
/// @author Enzyme Council <[email protected]> | ||
/// @notice A peripheral contract for calculating asset values | ||
/// @dev These are convenience functions intended for off-chain consumption, | ||
/// some of which involve potentially expensive state transitions | ||
contract AssetValueCalculator { | ||
using SafeMath for uint256; | ||
|
||
address private immutable VALUE_INTERPRETER; | ||
|
||
constructor(address _valueInterpreter) public { | ||
VALUE_INTERPRETER = _valueInterpreter; | ||
} | ||
|
||
// EXTERNAL FUNCTIONS | ||
|
||
/// @notice Calculates the value of a given amount of one asset in terms of another asset | ||
/// @param _baseAsset The asset from which to convert | ||
/// @param _quoteAsset The asset to which to convert | ||
/// @return timestamp_ The current block timestamp | ||
/// @return value_ The equivalent quantity in the _quoteAsset | ||
function calcNormalizedAssetValue(address _baseAsset, address _quoteAsset) | ||
external | ||
returns ( | ||
uint256 timestamp_, | ||
uint256 value_, | ||
bool valueIsValid_ | ||
) | ||
{ | ||
timestamp_ = block.timestamp; | ||
uint256 amount = 10**uint256(ERC20(_baseAsset).decimals()); | ||
|
||
try | ||
ValueInterpreter(getValueInterpreter()).calcCanonicalAssetValue( | ||
_baseAsset, | ||
amount, | ||
_quoteAsset | ||
) | ||
returns (uint256 value, bool isValid) { | ||
if (isValid) { | ||
value_ = value; | ||
valueIsValid_ = true; | ||
} | ||
} catch {} | ||
|
||
uint256 decimals = ERC20(_quoteAsset).decimals(); | ||
value_ = value_.mul(10**18).div(10**decimals); | ||
|
||
return (timestamp_, value_, valueIsValid_); | ||
} | ||
|
||
/////////////////// | ||
// STATE GETTERS // | ||
/////////////////// | ||
|
||
/// @notice Gets the `VALUE_INTERPRETER` variable | ||
/// @return valueInterpreter_ The `VALUE_INTERPRETER` variable value | ||
function getValueInterpreter() public view returns (address valueInterpreter_) { | ||
return VALUE_INTERPRETER; | ||
} | ||
} |
This file contains 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,8 @@ | ||
# Contracts primarily for off-chain consumption | ||
|
||
This directory contains data aggregation contracts that are primarily intended to be consumed off-chain. | ||
|
||
Prior to using any of these contracts for on-chain purposes, confirm that: | ||
|
||
1. it has been audited | ||
2. the logic is suitable for on-chain use (gas, state changes, etc) |
This file contains 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,21 @@ | ||
import { AssetValueCalculatorArgs } from '@enzymefinance/protocol'; | ||
import { DeployFunction } from 'hardhat-deploy/types'; | ||
|
||
const fn: DeployFunction = async function (hre) { | ||
const { deploy, get } = hre.deployments; | ||
const deployer = (await hre.ethers.getSigners())[0]; | ||
|
||
const valueInterpreter = await get('ValueInterpreter'); | ||
|
||
await deploy('AssetValueCalculator', { | ||
args: [valueInterpreter.address] as AssetValueCalculatorArgs, | ||
from: deployer.address, | ||
log: true, | ||
skipIfAlreadyDeployed: true, | ||
}); | ||
}; | ||
|
||
fn.tags = ['Release', 'OffChain', 'AssetValueCalculator']; | ||
fn.dependencies = ['ValueInterpreter']; | ||
|
||
export default fn; |
This file contains 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