-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,251 additions
and
2 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
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
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,37 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity =0.8.13; | ||
|
||
import "./LiquidityAccumulator.sol"; | ||
|
||
import "hardhat/console.sol"; | ||
|
||
abstract contract ValueAndErrorAccumulator is LiquidityAccumulator { | ||
using AddressLibrary for address; | ||
using SafeCast for uint256; | ||
using SafeCastExt for uint256; | ||
|
||
uint112 public constant ERROR_ZERO = 1e18; | ||
|
||
constructor( | ||
IAveragingStrategy averagingStrategy_, | ||
address quoteToken_, | ||
uint256 updateThreshold_, | ||
uint256 minUpdateDelay_, | ||
uint256 maxUpdateDelay_ | ||
) LiquidityAccumulator(averagingStrategy_, quoteToken_, updateThreshold_, minUpdateDelay_, maxUpdateDelay_) {} | ||
|
||
function fetchValue(bytes memory data) internal view virtual returns (uint112 value); | ||
|
||
function fetchTarget(bytes memory data) internal view virtual returns (uint112 target); | ||
|
||
function fetchLiquidity(bytes memory data) internal view virtual override returns (uint112 value, uint112 err) { | ||
value = fetchValue(data); | ||
uint256 target = fetchTarget(data); | ||
|
||
if (target >= value) { | ||
err = (ERROR_ZERO + (target - value)).toUint112(); | ||
} else { | ||
err = (ERROR_ZERO - (value - target)).toUint112(); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
contracts/accumulators/proto/truefi/AlocUtilizationAndErrorAccumulator.sol
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,70 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity =0.8.13; | ||
|
||
import "../../ValueAndErrorAccumulator.sol"; | ||
import "../../../libraries/SafeCastExt.sol"; | ||
|
||
interface IAloc { | ||
function utilization() external view returns (uint256); | ||
|
||
function liquidAssets() external view returns (uint256); | ||
|
||
function BASIS_PRECISION() external view returns (uint256); | ||
} | ||
|
||
contract AlocUtilizationAndErrorAccumulator is ValueAndErrorAccumulator { | ||
using SafeCastExt for uint256; | ||
|
||
uint8 internal immutable _liquidityDecimals; | ||
uint256 internal immutable _decimalFactor; | ||
uint112 internal immutable _target; | ||
|
||
constructor( | ||
uint112 target_, | ||
IAveragingStrategy averagingStrategy_, | ||
uint8 decimals_, | ||
uint256 updateTheshold_, | ||
uint256 minUpdateDelay_, | ||
uint256 maxUpdateDelay_ | ||
) ValueAndErrorAccumulator(averagingStrategy_, address(0), updateTheshold_, minUpdateDelay_, maxUpdateDelay_) { | ||
_liquidityDecimals = decimals_; | ||
_decimalFactor = 10 ** decimals_; | ||
_target = target_; | ||
} | ||
|
||
function getTarget(address token) external view virtual returns (uint112) { | ||
return fetchTarget(abi.encode(token)); | ||
} | ||
|
||
function quoteTokenDecimals() public view virtual override(SimpleQuotationMetadata, IQuoteToken) returns (uint8) { | ||
return _liquidityDecimals; | ||
} | ||
|
||
function liquidityDecimals() public view virtual override returns (uint8) { | ||
return _liquidityDecimals; | ||
} | ||
|
||
function fetchValue(bytes memory data) internal view virtual override returns (uint112) { | ||
address alocAddress = abi.decode(data, (address)); | ||
|
||
uint256 utilization = IAloc(alocAddress).utilization(); | ||
if (utilization == 0 && IAloc(alocAddress).liquidAssets() == 0) { | ||
// Utilization is 0, but the ALOC has no liquidity. Let's instead consider the utilization to be 100%. | ||
// When used in a PID interest rate controller, this will cause the interest rate to rise so as to attract | ||
// more liquidity. | ||
// In other cases, it just makes more sense to consider the utilization to be 100% when there is no | ||
// liquidity. i.e. if there is no liquidity, then all available liquidity is being used. | ||
return _decimalFactor.toUint112(); | ||
} | ||
|
||
// Convert from the ALOC's units to the units used by the accumulator | ||
utilization *= _decimalFactor; | ||
utilization /= IAloc(alocAddress).BASIS_PRECISION(); | ||
|
||
return utilization.toUint112(); | ||
} | ||
|
||
function fetchTarget(bytes memory) internal view virtual override returns (uint112) { | ||
return _target; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
contracts/test/accumulators/AlocUtilizationAndErrorAccumulatorStub.sol
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,52 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity =0.8.13; | ||
|
||
import "../../accumulators/proto/truefi/AlocUtilizationAndErrorAccumulator.sol"; | ||
|
||
contract AlocUtilizationAndErrorAccumulatorStub is AlocUtilizationAndErrorAccumulator { | ||
bool internal targetOverridden; | ||
uint112 internal targetOverride; | ||
|
||
constructor( | ||
uint112 target_, | ||
IAveragingStrategy averagingStrategy_, | ||
uint8 decimals_, | ||
uint256 updateTheshold_, | ||
uint256 minUpdateDelay_, | ||
uint256 maxUpdateDelay_ | ||
) | ||
AlocUtilizationAndErrorAccumulator( | ||
target_, | ||
averagingStrategy_, | ||
decimals_, | ||
updateTheshold_, | ||
minUpdateDelay_, | ||
maxUpdateDelay_ | ||
) | ||
{} | ||
|
||
function stubSetTarget(bool override_, uint112 target_) external { | ||
targetOverridden = override_; | ||
targetOverride = target_; | ||
} | ||
|
||
function stubFetchValue(address token) public view returns (uint112) { | ||
return fetchValue(abi.encode(token)); | ||
} | ||
|
||
function stubFetchTarget(address token) public view returns (uint112) { | ||
return fetchTarget(abi.encode(token)); | ||
} | ||
|
||
function stubFetchLiquidity(address token) public view returns (uint112 value, uint112 err) { | ||
return fetchLiquidity(abi.encode(token)); | ||
} | ||
|
||
function fetchTarget(bytes memory data) internal view virtual override returns (uint112) { | ||
if (targetOverridden) { | ||
return targetOverride; | ||
} | ||
|
||
return super.fetchTarget(data); | ||
} | ||
} |
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,27 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity =0.8.13; | ||
|
||
import {IAloc} from "../../../accumulators/proto/truefi/AlocUtilizationAndErrorAccumulator.sol"; | ||
|
||
contract AlocStub is IAloc { | ||
uint256 public constant BASIS_PRECISION = 1e4; | ||
|
||
uint256 internal _utilization; | ||
uint256 internal _liquidAssets; | ||
|
||
function stubSetUtilization(uint256 utilization_) external { | ||
_utilization = utilization_; | ||
} | ||
|
||
function stubSetLiquidAssets(uint256 liquidAssets_) external { | ||
_liquidAssets = liquidAssets_; | ||
} | ||
|
||
function utilization() external view override returns (uint256) { | ||
return _utilization; | ||
} | ||
|
||
function liquidAssets() external view override returns (uint256) { | ||
return _liquidAssets; | ||
} | ||
} |
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
Oops, something went wrong.