Skip to content

Commit

Permalink
chore: add AssetValueCalculator
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Siemssen <[email protected]>
  • Loading branch information
SeanJCasey and fubhy authored Aug 22, 2023
1 parent 828c32e commit 34bc83e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
8 changes: 8 additions & 0 deletions contracts/persistent/off-chain/README.md
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)
78 changes: 78 additions & 0 deletions contracts/release/off-chain/AssetValueCalculator.sol
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;
}
}
8 changes: 8 additions & 0 deletions contracts/release/off-chain/README.md
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)
21 changes: 21 additions & 0 deletions deploy/scripts/release/off-chain/AssetValueCalculator.ts
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;
3 changes: 3 additions & 0 deletions packages/protocol/src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export * from './codegen/AuthUserExecutedSharesRequestorProxy';
export * from './codegen/FundActionsWrapper';
export * from './codegen/FundValueCalculator';

// Off-chain
export * from './codegen/AssetValueCalculator';

// Test contracts
export * from './codegen/TestPeggedDerivativesPriceFeed';
export * from './codegen/TestSinglePeggedDerivativePriceFeed';
Expand Down

0 comments on commit 34bc83e

Please sign in to comment.