Skip to content
This repository was archived by the owner on Oct 26, 2022. It is now read-only.

SingleInteractionTWAP #24

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Downgraded Solidity version on TWAP and added imports
  • Loading branch information
Clearwood committed May 28, 2021
commit 2da236f4b03cda45d25523e367d032915daaf62e
24 changes: 13 additions & 11 deletions contracts/oracles/SimpleSLPTWAP0Oracle.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Using the same Copyleft License as in the original Repository
pragma solidity 0.6.12;
pragma solidity 0.6.8;
pragma experimental ABIEncoderV2;
import "../interfaces/IOracle.sol";
import "@boringcrypto/boring-solidity/contracts/libraries/BoringMath.sol";
import "@sushiswap/core/contracts/uniswapv2/interfaces/IUniswapV2Factory.sol";
import "@sushiswap/core/contracts/uniswapv2/interfaces/IUniswapV2Pair.sol";
import '@keydonix/uniswap-oracle-contracts/source/IUniswapV2Pair.sol';
import "../libraries/FixedPoint.sol";

// solhint-disable not-rely-on-time


library BoringMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
require(b == 0 || (c = a * b) / b == a, "BoringMath: Mul Overflow");
}

}

// adapted from https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/examples/ExampleSlidingWindowOracle.sol

contract SimpleSLPTWAP0Oracle is IOracle {
Expand Down Expand Up @@ -43,7 +50,6 @@ contract SimpleSLPTWAP0Oracle is IOracle {
}

// Get the latest exchange rate, if no valid (recent) rate is available, return false
/// @inheritdoc IOracle
function get(bytes calldata data) external override returns (bool, uint256) {
IUniswapV2Pair pair = abi.decode(data, (IUniswapV2Pair));
uint32 blockTimestamp = uint32(block.timestamp);
Expand All @@ -69,8 +75,7 @@ contract SimpleSLPTWAP0Oracle is IOracle {
}

// Check the last exchange rate without any state changes
/// @inheritdoc IOracle
function peek(bytes calldata data) public view override returns (bool, uint256) {
function peek(bytes memory data) public view override returns (bool, uint256) {
IUniswapV2Pair pair = abi.decode(data, (IUniswapV2Pair));
uint32 blockTimestamp = uint32(block.timestamp);
if (pairs[pair].blockTimestampLast == 0) {
Expand All @@ -89,20 +94,17 @@ contract SimpleSLPTWAP0Oracle is IOracle {
}

// Check the current spot exchange rate without any state changes
/// @inheritdoc IOracle
function peekSpot(bytes calldata data) external view override returns (uint256 rate) {
IUniswapV2Pair pair = abi.decode(data, (IUniswapV2Pair));
(uint256 reserve0, uint256 reserve1, ) = pair.getReserves();
rate = reserve1.mul(1e18) / reserve0;
}

/// @inheritdoc IOracle
function name(bytes calldata) public view override returns (string memory) {
function name(bytes memory) public view override returns (string memory) {
return "SushiSwap TWAP";
}

/// @inheritdoc IOracle
function symbol(bytes calldata) public view override returns (string memory) {
function symbol(bytes memory) public view override returns (string memory) {
return "S";
}
}
21 changes: 11 additions & 10 deletions contracts/oracles/SimpleSLPTWAP1Oracle.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Using the same Copyleft License as in the original Repository
pragma solidity 0.6.12;
pragma solidity 0.6.8;
pragma experimental ABIEncoderV2;
import "../interfaces/IOracle.sol";
import "@boringcrypto/boring-solidity/contracts/libraries/BoringMath.sol";
import "@sushiswap/core/contracts/uniswapv2/interfaces/IUniswapV2Factory.sol";
import "@sushiswap/core/contracts/uniswapv2/interfaces/IUniswapV2Pair.sol";
import "../libraries/FixedPoint.sol";

// solhint-disable not-rely-on-time

library BoringMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
require(b == 0 || (c = a * b) / b == a, "BoringMath: Mul Overflow");
}

}

// adapted from https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/examples/ExampleSlidingWindowOracle.sol

contract SimpleSLPTWAP1Oracle is IOracle {
Expand Down Expand Up @@ -43,7 +49,6 @@ contract SimpleSLPTWAP1Oracle is IOracle {
}

// Get the latest exchange rate, if no valid (recent) rate is available, return false
/// @inheritdoc IOracle
function get(bytes calldata data) external override returns (bool, uint256) {
IUniswapV2Pair pair = abi.decode(data, (IUniswapV2Pair));
uint32 blockTimestamp = uint32(block.timestamp);
Expand All @@ -69,8 +74,7 @@ contract SimpleSLPTWAP1Oracle is IOracle {
}

// Check the last exchange rate without any state changes
/// @inheritdoc IOracle
function peek(bytes calldata data) public view override returns (bool, uint256) {
function peek(bytes memory data) public view override returns (bool, uint256) {
IUniswapV2Pair pair = abi.decode(data, (IUniswapV2Pair));
uint32 blockTimestamp = uint32(block.timestamp);
if (pairs[pair].blockTimestampLast == 0) {
Expand All @@ -89,20 +93,17 @@ contract SimpleSLPTWAP1Oracle is IOracle {
}

// Check the current spot exchange rate without any state changes
/// @inheritdoc IOracle
function peekSpot(bytes calldata data) external view override returns (uint256 rate) {
IUniswapV2Pair pair = abi.decode(data, (IUniswapV2Pair));
(uint256 reserve0, uint256 reserve1, ) = pair.getReserves();
rate = reserve0.mul(1e18) / reserve1;
}

/// @inheritdoc IOracle
function name(bytes calldata) public view override returns (string memory) {
function name(bytes memory) public view override returns (string memory) {
return "SushiSwap TWAP";
}

/// @inheritdoc IOracle
function symbol(bytes calldata) public view override returns (string memory) {
function symbol(bytes memory) public view override returns (string memory) {
return "S";
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"@boringcrypto/boring-solidity": "boringcrypto/BoringSolidity#371a01c11465eb1c881193c60a53c0bf15ee8a19",
"@keydonix/uniswap-oracle-contracts": "^1.0.6",
"@sushiswap/bentobox-sdk": "sushiswap/bentobox-sdk#e3f809870b86afbd57e0930662221d2362a160db",
"@sushiswap/core": "^1.4.2",
"hardhat-deploy-ethers": "^0.3.0-beta.7"
Expand Down
38 changes: 38 additions & 0 deletions settings.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
module.exports = {
hardhat: {
solidity: {
compilers: [
{
version: "0.6.12",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
{
version: "0.6.8",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
}
],
overrides: {
"contracts/oracles/SimpleSLPTWAP1Oracle.sol": {
version: "0.6.8",
settings: {
optimizer: {
enabled: true,
runs: 400,
},
},
},
"contracts/oracles/SimpleSLPTWAP0Oracle.sol": {
version: "0.6.8",
settings: {
optimizer: {
enabled: true,
runs: 400,
},
},
},
"contracts/KashiPair.sol": {
version: "0.6.12",
settings: {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@
"@ethersproject/properties" "^5.0.7"
"@ethersproject/strings" "^5.0.8"

"@keydonix/uniswap-oracle-contracts@^1.0.6":
version "1.0.6"
resolved "https://registry.yarnpkg.com/@keydonix/uniswap-oracle-contracts/-/uniswap-oracle-contracts-1.0.6.tgz#5fd371fad999c15d0ae687e28f37d8f396d6eb89"
integrity sha512-EJxtF9Jm9awWIfoTAqLLfOlv9JQ5rKSvQlJjGobUmYbcbl8a8c4/BsiWMmDLWcj2Be/eXipLd2bOQXLlJVTBAw==

"@nodelib/[email protected]":
version "2.1.4"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.4.tgz#d4b3549a5db5de2683e0c1071ab4f140904bbf69"
Expand Down