Skip to content

Commit

Permalink
refactor: shorten require error strings to error codes & document - W…
Browse files Browse the repository at this point in the history
…stethPricer
  • Loading branch information
CruzMolina committed Aug 18, 2021
1 parent f977fe6 commit 25309b8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
24 changes: 18 additions & 6 deletions contracts/pricers/WstethPricer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ import {WSTETHInterface} from "../interfaces/WSTETHInterface.sol";
import {SafeMath} from "../packages/oz/SafeMath.sol";

/**
* Error Codes
* W1: cannot deploy pricer, wstETH address cannot be 0
* W2: cannot deploy pricer, underlying address cannot be 0
* W3: cannot deploy pricer, oracle address cannot be 0
* W4: cannot retrieve price, underlying price is 0
* W5: cannot set expiry price in oracle, underlying price is 0 and has not been set
* W6: cannot retrieve historical prices, getHistoricalPrice has been deprecated
*/

/**
* @title WstethPricer
* @author Opyn Team
* @notice A Pricer contract for a wstETH token
*/
contract WstethPricer is OpynPricerInterface {
Expand All @@ -31,9 +43,9 @@ contract WstethPricer is OpynPricerInterface {
address _underlying,
address _oracle
) public {
require(_wstETH != address(0), "WstethPricer: wstETH address can not be 0");
require(_underlying != address(0), "WstethPricer: underlying address can not be 0");
require(_oracle != address(0), "WstethPricer: oracle address can not be 0");
require(_wstETH != address(0), "W1");
require(_underlying != address(0), "W2");
require(_oracle != address(0), "W3");

wstETH = WSTETHInterface(_wstETH);
oracle = OracleInterface(_oracle);
Expand All @@ -47,7 +59,7 @@ contract WstethPricer is OpynPricerInterface {
*/
function getPrice() external view override returns (uint256) {
uint256 underlyingPrice = oracle.getPrice(address(underlying));
require(underlyingPrice > 0, "WstethPricer: underlying price is 0");
require(underlyingPrice > 0, "W4");
return _underlyingPriceToWstethPrice(underlyingPrice);
}

Expand All @@ -58,7 +70,7 @@ contract WstethPricer is OpynPricerInterface {
*/
function setExpiryPriceInOracle(uint256 _expiryTimestamp) external {
(uint256 underlyingPriceExpiry, ) = oracle.getExpiryPrice(underlying, _expiryTimestamp);
require(underlyingPriceExpiry > 0, "WstethPricer: underlying price not set yet");
require(underlyingPriceExpiry > 0, "W5");
uint256 wstEthPrice = _underlyingPriceToWstethPrice(underlyingPriceExpiry);
oracle.setExpiryPrice(address(wstETH), _expiryTimestamp, wstEthPrice);
}
Expand All @@ -75,6 +87,6 @@ contract WstethPricer is OpynPricerInterface {
}

function getHistoricalPrice(uint80 _roundId) external view override returns (uint256, uint256) {
revert("WstethPricer: Deprecated");
revert("W6");
}
}
34 changes: 15 additions & 19 deletions test/unit-tests/wstEthPricer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
WstethPricerInstance,
} from '../../build/types/truffle-types'

import { underlyingPriceToYTokenPrice } from '../utils'
import {underlyingPriceToYTokenPrice} from '../utils'

import BigNumber from 'bignumber.js'
import { createScaledNumber } from '../utils'
const { expectRevert, time } = require('@openzeppelin/test-helpers')
import {createScaledNumber} from '../utils'
const {expectRevert, time} = require('@openzeppelin/test-helpers')

const MockPricer = artifacts.require('MockPricer.sol')
const MockOracle = artifacts.require('MockOracle.sol')
Expand All @@ -33,7 +33,7 @@ contract('WstethPricer', ([owner, random]) => {

before('Deployment', async () => {
// deploy mock contracts
oracle = await MockOracle.new({ from: owner })
oracle = await MockOracle.new({from: owner})
weth = await MockERC20.new('WETH', 'WETH', 18)
wstETH = await MockWSTETHToken.new('wstETH', 'wstETH')
// mock underlying pricers
Expand All @@ -50,23 +50,17 @@ contract('WstethPricer', ([owner, random]) => {
assert.equal(await wstethPricer.underlying(), weth.address)
assert.equal(await wstethPricer.oracle(), oracle.address)
})

it('should revert if initializing with wstETH = 0', async () => {
await expectRevert(
WstethPricer.new(ZERO_ADDR, weth.address, oracle.address),
'WstethPricer: wstETH address can not be 0',
)
await expectRevert(WstethPricer.new(ZERO_ADDR, weth.address, oracle.address), 'W1')
})

it('should revert if initializing with underlying = 0 address', async () => {
await expectRevert(
WstethPricer.new(wstETH.address, ZERO_ADDR, oracle.address),
'WstethPricer: underlying address can not be 0',
)
await expectRevert(WstethPricer.new(wstETH.address, ZERO_ADDR, oracle.address), 'W2')
})

it('should revert if initializing with oracle = 0 address', async () => {
await expectRevert(
WstethPricer.new(wstETH.address, weth.address, ZERO_ADDR),
'WstethPricer: oracle address can not be 0',
)
await expectRevert(WstethPricer.new(wstETH.address, weth.address, ZERO_ADDR), 'W3')
})
})

Expand All @@ -87,6 +81,7 @@ contract('WstethPricer', ([owner, random]) => {
// 1 yWETH = 9.4 USD
assert.equal(wstETHprice.toString(), '47435353746')
})

it('should return the new price after resetting answer in underlying pricer', async () => {
const newPrice = createScaledNumber(500)
// await wethPricer.setPrice(newPrice)
Expand All @@ -95,10 +90,11 @@ contract('WstethPricer', ([owner, random]) => {
const expectedResult = await underlyingPriceToYTokenPrice(new BigNumber(newPrice), pricePerShare, weth)
assert.equal(wstETHprice.toString(), expectedResult.toString())
})

it('should revert if price is lower than 0', async () => {
// await wethPricer.setPrice('0')
await oracle.setRealTimePrice(weth.address, '0')
await expectRevert(wstethPricer.getPrice(), 'WstethPricer: underlying price is 0')
await expectRevert(wstethPricer.getPrice(), 'W4')
})
})

Expand All @@ -112,12 +108,12 @@ contract('WstethPricer', ([owner, random]) => {
})

it("should revert if oracle don't have price of underlying yet", async () => {
await expectRevert(wstethPricer.setExpiryPriceInOracle(expiry), 'WstethPricer: underlying price not set yet')
await expectRevert(wstethPricer.setExpiryPriceInOracle(expiry), 'W5')
})

it('should set price successfully by arbitrary address', async () => {
await oracle.setExpiryPrice(weth.address, expiry, ethPrice)
await wstethPricer.setExpiryPriceInOracle(expiry, { from: random })
await wstethPricer.setExpiryPriceInOracle(expiry, {from: random})
const [price] = await oracle.getExpiryPrice(wstETH.address, expiry)
const expectedResult = await underlyingPriceToYTokenPrice(ethPrice, pricePerShare, weth)
assert.equal(price.toString(), expectedResult.toString())
Expand Down

0 comments on commit 25309b8

Please sign in to comment.