Overview
S Balance
0 S
S Value
$0.00More Info
Private Name Tags
ContractCreator
Loading...
Loading
Similar Match Source Code This contract matches the deployed Bytecode of the Source Code for Contract 0x9DBA4411...bD5F21CAf The constructor portion of the code might be different and could alter the actual behaviour of the contract
Contract Name:
BeefyOracleUniswapV2
Compiler Version
v0.8.23+commit.f704f362
Optimization Enabled:
Yes with 200 runs
Other Settings:
paris EvmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { IERC20MetadataUpgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/IERC20MetadataUpgradeable.sol"; import { IUniswapV2Pair } from "../../interfaces/common/IUniswapV2Pair.sol"; import { BeefyOracleHelper, IBeefyOracle, BeefyOracleErrors } from "./BeefyOracleHelper.sol"; /// @title Beefy Oracle for UniswapV2 /// @author Beefy, @kexley /// @notice On-chain oracle using UniswapV2 /// @dev Observations are stored here as UniswapV2 pairs do not store historical observations contract BeefyOracleUniswapV2 { /// @dev Struct of stored price averages and the most recent observation of a pair /// @param priceAverage0 Average price of token0 /// @param priceAverage1 Average price of token1 /// @param observation Cumulative prices of token0 and token1 struct Price { uint256 priceAverage0; uint256 priceAverage1; Observation observation; } /// @dev Struct of the stored latest observation of a pair /// @param price0 Cumulative price of token0 /// @param price1 Cumulative price of token1 /// @param timestamp Timestamp of the observation struct Observation { uint256 price0; uint256 price1; uint256 timestamp; } /// @notice Stored last average prices of tokens in a pair mapping(address => Price) public prices; /// @notice Pair has been updated with average prices /// @param pair Pair address /// @param priceAverage0 Average price of token0 /// @param priceAverage1 Average price of token1 event PairUpdated(address indexed pair, uint256 priceAverage0, uint256 priceAverage1); /// @notice Fetch price from the UniswapV2 pairs using the TWAP observations /// @param _data Payload from the central oracle with the addresses of the token route, pairs /// route and TWAP periods in seconds /// @return price Retrieved price from the chained quotes /// @return success Successful price fetch or not function getPrice(bytes calldata _data) external returns (uint256 price, bool success) { (address[] memory tokens, address[] memory pairs, uint256[] memory twapPeriods) = abi.decode(_data, (address[], address[], uint256[])); uint256 amount = 10 ** IERC20MetadataUpgradeable(tokens[0]).decimals(); uint256 pairLength = pairs.length; for (uint i; i < pairLength;) { address pair = pairs[i]; _updatePair(pair, twapPeriods[i]); amount = _getAmountOut(pair, tokens[i], amount); unchecked { ++i; } } price = BeefyOracleHelper.priceFromBaseToken( msg.sender, tokens[tokens.length - 1], tokens[0], amount ); if (price != 0) success = true; } /// @dev Update the stored price averages and observation for a UniswapV2 pair if outside the TWAP /// period or tracking a new pair. Initial average prices should not be trusted /// @param _pair UniswapV2 pair to update /// @param _twapPeriod TWAP period minimum in seconds function _updatePair(address _pair, uint256 _twapPeriod) private { Observation memory observation = prices[_pair].observation; uint256 timeElapsed = block.timestamp - observation.timestamp; if (timeElapsed > _twapPeriod) { (uint112 reserve0, uint112 reserve1, uint256 lastUpdate) = IUniswapV2Pair(_pair).getReserves(); uint256 price0 = IUniswapV2Pair(_pair).price0CumulativeLast(); uint256 price1 = IUniswapV2Pair(_pair).price1CumulativeLast(); if (block.timestamp > lastUpdate) { uint256 unsyncTime = block.timestamp - lastUpdate; price0 += (2**112 * uint256(reserve1) / reserve0) * unsyncTime; price1 += (2**112 * uint256(reserve0) / reserve1) * unsyncTime; } uint256 priceAverage0; uint256 priceAverage1; if (prices[_pair].observation.timestamp > 0) { priceAverage0 = (price0 - observation.price0) * 1 ether / (timeElapsed * 2**112); priceAverage1 = (price1 - observation.price1) * 1 ether / (timeElapsed * 2**112); } else { priceAverage0 = uint256(reserve1) * 1 ether / reserve0; priceAverage1 = uint256(reserve0) * 1 ether / reserve1; } prices[_pair] = Price(priceAverage0, priceAverage1, Observation(price0, price1, block.timestamp)); emit PairUpdated(_pair, priceAverage0, priceAverage1); } } /// @dev Use the stored price average to get the amount out /// @param _pair UniswapV2 pair /// @param _tokenIn Address of the token being swapped into the pair /// @param _amountIn Amount of the token being swapped in /// @return amountOut Amount of the output token being received from the swap function _getAmountOut( address _pair, address _tokenIn, uint256 _amountIn ) private view returns (uint256 amountOut) { uint256 priceAverage = IUniswapV2Pair(_pair).token0() == _tokenIn ? prices[_pair].priceAverage0 : prices[_pair].priceAverage1; amountOut = priceAverage * _amountIn / 1 ether; } /// @notice Data validation for new oracle data being added to central oracle /// @param _data Encoded addresses of the token route, pair route and TWAP periods function validateData(bytes calldata _data) external view { (address[] memory tokens, address[] memory pairs, uint256[] memory twapPeriods) = abi.decode(_data, (address[], address[], uint256[])); if (tokens.length != pairs.length + 1 || tokens.length != twapPeriods.length + 1) { revert BeefyOracleErrors.ArrayLength(); } uint256 basePrice = IBeefyOracle(msg.sender).getPrice(tokens[0]); if (basePrice == 0) revert BeefyOracleErrors.NoBasePrice(tokens[0]); uint256 pairLength = pairs.length; for (uint i; i < pairLength;) { address fromToken = tokens[i]; address toToken = tokens[i + 1]; address pair = pairs[i]; address token0 = IUniswapV2Pair(pair).token0(); address token1 = IUniswapV2Pair(pair).token1(); if (fromToken != token0 && fromToken != token1) { revert BeefyOracleErrors.TokenNotInPair(fromToken, pair); } if (toToken != token0 && toToken != token1) { revert BeefyOracleErrors.TokenNotInPair(toToken, pair); } unchecked { ++i; } } } }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol) pragma solidity ^0.8.0; import "../IERC20Upgradeable.sol"; /** * @dev Interface for the optional metadata functions from the ERC20 standard. * * _Available since v4.1._ */ interface IERC20MetadataUpgradeable is IERC20Upgradeable { /** * @dev Returns the name of the token. */ function name() external view returns (string memory); /** * @dev Returns the symbol of the token. */ function symbol() external view returns (string memory); /** * @dev Returns the decimals places of the token. */ function decimals() external view returns (uint8); }
// SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol) pragma solidity ^0.8.0; /** * @dev Interface of the ERC20 standard as defined in the EIP. */ interface IERC20Upgradeable { /** * @dev Emitted when `value` tokens are moved from one account (`from`) to * another (`to`). * * Note that `value` may be zero. */ event Transfer(address indexed from, address indexed to, uint256 value); /** * @dev Emitted when the allowance of a `spender` for an `owner` is set by * a call to {approve}. `value` is the new allowance. */ event Approval(address indexed owner, address indexed spender, uint256 value); /** * @dev Returns the amount of tokens in existence. */ function totalSupply() external view returns (uint256); /** * @dev Returns the amount of tokens owned by `account`. */ function balanceOf(address account) external view returns (uint256); /** * @dev Moves `amount` tokens from the caller's account to `to`. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transfer(address to, uint256 amount) external returns (bool); /** * @dev Returns the remaining number of tokens that `spender` will be * allowed to spend on behalf of `owner` through {transferFrom}. This is * zero by default. * * This value changes when {approve} or {transferFrom} are called. */ function allowance(address owner, address spender) external view returns (uint256); /** * @dev Sets `amount` as the allowance of `spender` over the caller's tokens. * * Returns a boolean value indicating whether the operation succeeded. * * IMPORTANT: Beware that changing an allowance with this method brings the risk * that someone may use both the old and the new allowance by unfortunate * transaction ordering. One possible solution to mitigate this race * condition is to first reduce the spender's allowance to 0 and set the * desired value afterwards: * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 * * Emits an {Approval} event. */ function approve(address spender, uint256 amount) external returns (bool); /** * @dev Moves `amount` tokens from `from` to `to` using the * allowance mechanism. `amount` is then deducted from the caller's * allowance. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transferFrom(address from, address to, uint256 amount) external returns (bool); }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; /// @title Beefy Oracle Errors /// @author Beefy, @kexley /// @notice Error list for Beefy Oracles contract BeefyOracleErrors { /// @dev No response from the Chainlink feed error NoAnswer(); /// @dev No price for base token /// @param token Base token error NoBasePrice(address token); /// @dev Token is not present in the pair /// @param token Input token /// @param pair Pair token error TokenNotInPair(address token, address pair); /// @dev Array length is not correct error ArrayLength(); }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { IERC20MetadataUpgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/IERC20MetadataUpgradeable.sol"; import { IBeefyOracle } from "../../interfaces/oracle/IBeefyOracle.sol"; import { BeefyOracleErrors } from "./BeefyOracleErrors.sol"; /// @title Beefy Oracle Helper /// @author Beefy, @kexley /// @notice Helper functions for Beefy oracles library BeefyOracleHelper { /// @dev Calculate the price of the output token in 18 decimals given the base token price /// and the amount out received from swapping 1 unit of the base token /// @param _oracle Central Beefy oracle which stores the base token price /// @param _token Address of token to calculate the price of /// @param _baseToken Address of the base token used in the price chain /// @param _amountOut Amount received from swapping 1 unit of base token into the target token /// @return price Price of the target token in 18 decimals function priceFromBaseToken( address _oracle, address _token, address _baseToken, uint256 _amountOut ) internal returns (uint256 price) { (uint256 basePrice,) = IBeefyOracle(_oracle).getFreshPrice(_baseToken); uint8 decimals = IERC20MetadataUpgradeable(_token).decimals(); _amountOut = scaleAmount(_amountOut, decimals); price = basePrice * 1 ether / _amountOut; } /// @dev Scale an input amount to 18 decimals /// @param _amount Amount to be scaled up or down /// @param _decimals Decimals that the amount is already in /// @return scaledAmount New scaled amount in 18 decimals function scaleAmount( uint256 _amount, uint8 _decimals ) internal pure returns (uint256 scaledAmount) { scaledAmount = _decimals == 18 ? _amount : _amount * 10 ** 18 / 10 ** _decimals; } }
// SPDX-License-Identifier: MIT pragma solidity >=0.6.0 <0.9.0; interface IUniswapV2Pair { function factory() external view returns (address); function token0() external view returns (address); function token1() external view returns (address); function burn(address to) external returns (uint amount0, uint amount1); function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast); function totalSupply() external view returns (uint256); function kLast() external view returns (uint256); function price0CumulativeLast() external view returns (uint256); function price1CumulativeLast() external view returns (uint256); function sync() external; }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; interface IBeefyOracle { function getPrice(address token) external view returns (uint256 price); function getPrice(address[] calldata tokens) external view returns (uint256[] memory prices); function getFreshPrice(address token) external returns (uint256 price, bool success); function getFreshPrice(address[] calldata tokens) external returns (uint256[] memory prices, bool[] memory successes); }
{ "optimizer": { "enabled": true, "runs": 200 }, "evmVersion": "paris", "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "devdoc", "userdoc", "metadata", "abi" ] } }, "libraries": {} }
Contract Security Audit
- No Contract Security Audit Submitted- Submit Audit Here
[{"inputs":[],"name":"ArrayLength","type":"error"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"NoBasePrice","type":"error"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"pair","type":"address"}],"name":"TokenNotInPair","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"pair","type":"address"},{"indexed":false,"internalType":"uint256","name":"priceAverage0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"priceAverage1","type":"uint256"}],"name":"PairUpdated","type":"event"},{"inputs":[{"internalType":"bytes","name":"_data","type":"bytes"}],"name":"getPrice","outputs":[{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"prices","outputs":[{"internalType":"uint256","name":"priceAverage0","type":"uint256"},{"internalType":"uint256","name":"priceAverage1","type":"uint256"},{"components":[{"internalType":"uint256","name":"price0","type":"uint256"},{"internalType":"uint256","name":"price1","type":"uint256"},{"internalType":"uint256","name":"timestamp","type":"uint256"}],"internalType":"struct BeefyOracleUniswapV2.Observation","name":"observation","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"_data","type":"bytes"}],"name":"validateData","outputs":[],"stateMutability":"view","type":"function"}]
Deployed Bytecode
0x608060405234801561001057600080fd5b50600436106100415760003560e01c80636b4dd15814610046578063a6ffa36c14610073578063cfed246b14610088575b600080fd5b610059610054366004610c57565b610108565b604080519283529015156020830152015b60405180910390f35b610086610081366004610c57565b61028f565b005b6100da610096366004610ce1565b600060208181529181526040908190208054600182015483516060810185526002840154815260038401549581019590955260049092015492840192909252909183565b604080519384526020808501939093528151848201529181015160608401520151608082015260a00161006a565b60008080808061011a86880188610de1565b92509250925060008360008151811061013557610135610ebf565b60200260200101516001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa15801561017a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061019e9190610ed5565b6101a990600a610ff4565b835190915060005b818110156102295760008582815181106101cd576101cd610ebf565b602002602001015190506101fa818684815181106101ed576101ed610ebf565b60200260200101516105ff565b61021e8188848151811061021057610210610ebf565b602002602001015186610a28565b9350506001016101b1565b5061027633866001885161023d9190611003565b8151811061024d5761024d610ebf565b60200260200101518760008151811061026857610268610ebf565b602002602001015185610b08565b9650861561028357600195505b50505050509250929050565b6000808061029f84860186610de1565b925092509250815160016102b39190611016565b83511415806102cf575080516102ca906001611016565b835114155b156102ed576040516305c3d17360e11b815260040160405180910390fd5b6000336001600160a01b03166341976e098560008151811061031157610311610ebf565b60200260200101516040518263ffffffff1660e01b815260040161034491906001600160a01b0391909116815260200190565b602060405180830381865afa158015610361573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103859190611029565b9050806000036103dc57836000815181106103a2576103a2610ebf565b602002602001015160405163089bfea760e01b81526004016103d391906001600160a01b0391909116815260200190565b60405180910390fd5b825160005b818110156105f55760008682815181106103fd576103fd610ebf565b602002602001015190506000878360016104179190611016565b8151811061042757610427610ebf565b60200260200101519050600087848151811061044557610445610ebf565b602002602001015190506000816001600160a01b0316630dfe16816040518163ffffffff1660e01b8152600401602060405180830381865afa15801561048f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906104b39190611042565b90506000826001600160a01b031663d21220a76040518163ffffffff1660e01b8152600401602060405180830381865afa1580156104f5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105199190611042565b9050816001600160a01b0316856001600160a01b03161415801561054f5750806001600160a01b0316856001600160a01b031614155b1561058057604051638a9bb81760e01b81526001600160a01b038087166004830152841660248201526044016103d3565b816001600160a01b0316846001600160a01b0316141580156105b45750806001600160a01b0316846001600160a01b031614155b156105e557604051638a9bb81760e01b81526001600160a01b038086166004830152841660248201526044016103d3565b85600101955050505050506103e1565b5050505050505050565b6001600160a01b0382166000908152602081815260408083208151606081018352600282015481526003820154938101939093526004015490820181905290919061064a9042611003565b905082811115610a22576000806000866001600160a01b0316630902f1ac6040518163ffffffff1660e01b8152600401606060405180830381865afa158015610697573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106bb919061107b565b63ffffffff169250925092506000876001600160a01b0316635909c0d56040518163ffffffff1660e01b8152600401602060405180830381865afa158015610707573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061072b9190611029565b90506000886001600160a01b0316635a3d54936040518163ffffffff1660e01b8152600401602060405180830381865afa15801561076d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906107919190611029565b9050824211156108335760006107a78442611003565b905080866001600160701b0316866001600160701b0316600160701b6107cd91906110cb565b6107d791906110e2565b6107e191906110cb565b6107eb9084611016565b925080856001600160701b0316876001600160701b0316600160701b61081191906110cb565b61081b91906110e2565b61082591906110cb565b61082f9083611016565b9150505b6001600160a01b0389166000908152602081905260408120600401548190156108ce5761086488600160701b6110cb565b89516108709086611003565b61088290670de0b6b3a76400006110cb565b61088c91906110e2565b915061089c88600160701b6110cb565b60208a01516108ab9085611003565b6108bd90670de0b6b3a76400006110cb565b6108c791906110e2565b9050610935565b866001600160701b0316866001600160701b0316670de0b6b3a76400006108f591906110cb565b6108ff91906110e2565b9150856001600160701b0316876001600160701b0316670de0b6b3a764000061092891906110cb565b61093291906110e2565b90505b60405180606001604052808381526020018281526020016040518060600160405280878152602001868152602001428152508152506000808d6001600160a01b03166001600160a01b03168152602001908152602001600020600082015181600001556020820151816001015560408201518160020160008201518160000155602082015181600101556040820151816002015550509050508a6001600160a01b03167fe88bc80e80971b2e4816dfe465403ab4ddb1c1572fb5d03d14b5cd0affe399de8383604051610a12929190918252602082015260400190565b60405180910390a2505050505050505b50505050565b600080836001600160a01b0316856001600160a01b0316630dfe16816040518163ffffffff1660e01b8152600401602060405180830381865afa158015610a73573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610a979190611042565b6001600160a01b031614610ac6576001600160a01b038516600090815260208190526040902060010154610ae0565b6001600160a01b0385166000908152602081905260409020545b9050670de0b6b3a7640000610af584836110cb565b610aff91906110e2565b95945050505050565b60405163baeb325b60e01b81526001600160a01b038381166004830152600091829187169063baeb325b9060240160408051808303816000875af1158015610b54573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b789190611104565b5090506000856001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa158015610bbb573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610bdf9190610ed5565b9050610beb8482610c15565b935083610c0083670de0b6b3a76400006110cb565b610c0a91906110e2565b979650505050505050565b60008160ff16601214610c4e57610c2d82600a610ff4565b610c3f84670de0b6b3a76400006110cb565b610c4991906110e2565b610c50565b825b9392505050565b60008060208385031215610c6a57600080fd5b823567ffffffffffffffff80821115610c8257600080fd5b818501915085601f830112610c9657600080fd5b813581811115610ca557600080fd5b866020828501011115610cb757600080fd5b60209290920196919550909350505050565b6001600160a01b0381168114610cde57600080fd5b50565b600060208284031215610cf357600080fd5b8135610c5081610cc9565b634e487b7160e01b600052604160045260246000fd5b604051601f8201601f1916810167ffffffffffffffff81118282101715610d3d57610d3d610cfe565b604052919050565b600067ffffffffffffffff821115610d5f57610d5f610cfe565b5060051b60200190565b600082601f830112610d7a57600080fd5b81356020610d8f610d8a83610d45565b610d14565b8083825260208201915060208460051b870101935086841115610db157600080fd5b602086015b84811015610dd6578035610dc981610cc9565b8352918301918301610db6565b509695505050505050565b600080600060608486031215610df657600080fd5b833567ffffffffffffffff80821115610e0e57600080fd5b610e1a87838801610d69565b9450602091508186013581811115610e3157600080fd5b610e3d88828901610d69565b945050604086013581811115610e5257600080fd5b86019050601f81018713610e6557600080fd5b8035610e73610d8a82610d45565b81815260059190911b82018301908381019089831115610e9257600080fd5b928401925b82841015610eb057833582529284019290840190610e97565b80955050505050509250925092565b634e487b7160e01b600052603260045260246000fd5b600060208284031215610ee757600080fd5b815160ff81168114610c5057600080fd5b634e487b7160e01b600052601160045260246000fd5b600181815b80851115610f49578160001904821115610f2f57610f2f610ef8565b80851615610f3c57918102915b93841c9390800290610f13565b509250929050565b600082610f6057506001610fee565b81610f6d57506000610fee565b8160018114610f835760028114610f8d57610fa9565b6001915050610fee565b60ff841115610f9e57610f9e610ef8565b50506001821b610fee565b5060208310610133831016604e8410600b8410161715610fcc575081810a610fee565b610fd68383610f0e565b8060001904821115610fea57610fea610ef8565b0290505b92915050565b6000610c5060ff841683610f51565b81810381811115610fee57610fee610ef8565b80820180821115610fee57610fee610ef8565b60006020828403121561103b57600080fd5b5051919050565b60006020828403121561105457600080fd5b8151610c5081610cc9565b80516001600160701b038116811461107657600080fd5b919050565b60008060006060848603121561109057600080fd5b6110998461105f565b92506110a76020850161105f565b9150604084015163ffffffff811681146110c057600080fd5b809150509250925092565b8082028115828204841417610fee57610fee610ef8565b6000826110ff57634e487b7160e01b600052601260045260246000fd5b500490565b6000806040838503121561111757600080fd5b825191506020830151801515811461112e57600080fd5b80915050925092905056fea264697066735822122050c05d22e1ab98b66f22a7776abb3a2adbc3b19e1941e1e6fba65ab1b85b703664736f6c63430008170033
Loading...
Loading
Loading...
Loading
Multichain Portfolio | 30 Chains
Chain | Token | Portfolio % | Price | Amount | Value |
---|
A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.