Skip to content
Open
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
26 changes: 11 additions & 15 deletions src/types/ConditionalOrdersUtilsLib.sol
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

/**
* @title ConditionalOrdersUtilsLib - Utility functions for standardising conditional orders.
* @author mfw78 <mfw78@rndlabs.xyz>
*/
/// @title ConditionalOrdersUtilsLib - Utility functions for standardising conditional orders
/// @author mfw78 <mfw78@nxm.rs>
library ConditionalOrdersUtilsLib {
uint256 constant MAX_BPS = 10000;

/**
* Given the width of the validity bucket, return the timestamp of the *end* of the bucket.
* @param validity The width of the validity bucket in seconds.
*/
/// @notice Given the width of the validity bucket, return the timestamp of the *end* of the bucket.
/// @param validity The width of the validity bucket in seconds.
function validToBucket(uint32 validity) internal view returns (uint32 validTo) {
validTo = ((uint32(block.timestamp) / validity) * validity) + validity;
// Calculate which bucket we're in, then return the end of that bucket
uint32 currentBucket = uint32(block.timestamp) / validity;
validTo = (currentBucket + 1) * validity;
}

/**
* Given a price returned by a chainlink-like oracle, scale it to the desired amount of decimals
* @param oraclePrice return by a chainlink-like oracle
* @param fromDecimals the decimals the oracle returned (e.g. 8 for USDC)
* @param toDecimals the amount of decimals the price should be scaled to
*/
/// @notice Scale a price from one decimal precision to another
/// @param oraclePrice Price returned by a chainlink-like oracle
/// @param fromDecimals The decimals the oracle returned (e.g. 8 for USDC)
/// @param toDecimals The amount of decimals the price should be scaled to
function scalePrice(int256 oraclePrice, uint8 fromDecimals, uint8 toDecimals) internal pure returns (int256) {
if (fromDecimals < toDecimals) {
return oraclePrice * int256(10 ** uint256(toDecimals - fromDecimals));
Expand Down