Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/generated/FlareFtsoWords.pointers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pragma solidity ^0.8.25;
// file needs the contract to exist so that it can be compiled.

/// @dev Hash of the known bytecode.
bytes32 constant BYTECODE_HASH = bytes32(0x96e4ec5ff213f69e32f76c5ce42fb5ae8a42af3ad080341ce1d1b74a0061723d);
bytes32 constant BYTECODE_HASH = bytes32(0xa30dc38f88990d4b3d866010bd823ce2da04588e589babd64435f2b4ebbddc0c);

/// @dev The hash of the meta that describes the contract.
bytes32 constant DESCRIBED_BY_META_HASH = bytes32(0x8717d07737e3cedcdddea6cd3337ae762d7089918bf8d818fb0afc5b63e3985a);
Expand Down Expand Up @@ -48,13 +48,13 @@ bytes constant SUB_PARSER_WORD_PARSERS = hex"093509550967";
/// @dev Every two bytes is a function pointer for an operand handler.
/// These positional indexes all map to the same indexes looked up in the parse
/// meta.
bytes constant OPERAND_HANDLER_FUNCTION_POINTERS = hex"0e430e430e43";
bytes constant OPERAND_HANDLER_FUNCTION_POINTERS = hex"0e040e040e04";

/// @dev The function pointers for the integrity check fns.
bytes constant INTEGRITY_FUNCTION_POINTERS = hex"0e200e2c0e38";
bytes constant INTEGRITY_FUNCTION_POINTERS = hex"0de10ded0df9";

/// @dev The function pointers known to the interpreter for dynamic dispatch.
/// By setting these as a constant they can be inlined into the interpreter
/// and loaded at eval time for very low gas (~100) due to the compiler
/// optimising it to a single `codecopy` to build the in memory bytes array.
bytes constant OPCODE_FUNCTION_POINTERS = hex"0a090ac70b1c";
bytes constant OPCODE_FUNCTION_POINTERS = hex"0a090a880add";
10 changes: 2 additions & 8 deletions src/lib/op/LibOpFtsoCurrentPriceUsd.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pragma solidity ^0.8.19;
import {OperandV2, StackItem} from "rain-interpreter-interface-0.1.0/src/interface/IInterpreterV4.sol";
import {LibIntOrAString, IntOrAString} from "rain-intorastring-0.1.0/src/lib/LibIntOrAString.sol";
import {LibFtsoCurrentPriceUsd} from "../price/LibFtsoCurrentPriceUsd.sol";
import {DecimalsTooLarge} from "../../err/ErrFtso.sol";

import {LibDecimalFloat, Float} from "rain-math-float-0.1.1/src/lib/LibDecimalFloat.sol";

Expand Down Expand Up @@ -44,15 +43,10 @@ library LibOpFtsoCurrentPriceUsd {
timeout := mload(add(inputs, 0x40))
}

(uint256 price, uint256 decimals) = LibFtsoCurrentPriceUsd.ftsoCurrentPriceUsd(
(uint256 price, uint8 decimals) = LibFtsoCurrentPriceUsd.ftsoCurrentPriceUsd(
symbol.toStringV3(), LibDecimalFloat.toFixedDecimalLossless(timeout, 0)
);
if (decimals > type(uint8).max) {
revert DecimalsTooLarge(decimals);
}
// Check above ensures safe downcast.
//forge-lint: disable-next-line(unsafe-typecast)
Float priceFloat = LibDecimalFloat.fromFixedDecimalLosslessPacked(price, uint8(decimals));
Float priceFloat = LibDecimalFloat.fromFixedDecimalLosslessPacked(price, decimals);

StackItem[] memory outputs;
assembly ("memory-safe") {
Expand Down
12 changes: 9 additions & 3 deletions src/lib/price/LibFtsoCurrentPriceUsd.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
pragma solidity ^0.8.19;

import {IFtsoRegistry, LibFlareContractRegistry} from "../registry/LibFlareContractRegistry.sol";
import {InactiveFtso, PriceNotFinalized, StalePrice, InconsistentFtso} from "../../err/ErrFtso.sol";
import {InactiveFtso, PriceNotFinalized, StalePrice, InconsistentFtso, DecimalsTooLarge} from "../../err/ErrFtso.sol";
import {IFtso} from "../../vendor/flare-smart-contracts/userInterfaces/IFtso.sol";

library LibFtsoCurrentPriceUsd {
function ftsoCurrentPriceUsd(string memory symbol, uint256 timeout) internal view returns (uint256, uint256) {
function ftsoCurrentPriceUsd(string memory symbol, uint256 timeout) internal view returns (uint256, uint8) {
// Fetch the FTSO from the registry.
IFtsoRegistry ftsoRegistry = LibFlareContractRegistry.getFtsoRegistry();
IFtso ftso = ftsoRegistry.getFtsoBySymbol(symbol);
Expand Down Expand Up @@ -45,12 +45,18 @@ library LibFtsoCurrentPriceUsd {
revert InconsistentFtso();
}

if (decimals > type(uint8).max) {
revert DecimalsTooLarge(decimals);
}

// Handle stale prices.
//slither-disable-next-line timestamp
if (block.timestamp > priceTimestamp + timeout) {
revert StalePrice(priceTimestamp, timeout);
}

return (price, decimals);
// Guard above ensures safe downcast.
//forge-lint: disable-next-line(unsafe-typecast)
return (price, uint8(decimals));
}
}
1 change: 1 addition & 0 deletions test/src/lib/op/LibOpFtsoCurrentPriceUsd.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ contract LibOpFtsoCurrentPriceUsdTest is FtsoTest {
) external {
vm.assume(bytes(symbol).length <= 31);
uint256 intSymbol = IntOrAString.unwrap(LibIntOrAString.fromStringV3(symbol));
currentPrice.decimals = bound(currentPrice.decimals, 0, type(uint8).max);

timeout = bound(timeout, 0, uint256(int256(type(int224).max)));
currentPrice.timestamp = bound(currentPrice.timestamp, 0, type(uint256).max - timeout - 1);
Expand Down
Loading