Skip to content

Commit

Permalink
added cbEthRateProvider and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mkflow27 committed Sep 28, 2022
1 parent 701517f commit 359cb88
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
28 changes: 28 additions & 0 deletions contracts/CBETHRateProvider.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

import "./interfaces/IcbETH.sol";
import "./interfaces/IRateProvider.sol";


/**
* @title Coinbase wrapped staked Eth Rate Provider
* @notice Returns value of cbEth in terms of Eth
*/
contract CbEthRateProvider is IRateProvider {
IcbETH public immutable cbETH;
address public origin;

constructor(IcbETH _cbETH) {
cbETH = _cbETH;

}

/**
* @return value of cbETH in terms of Eth scaled by 10**18
*/
function getRate() public view override returns (uint256) {
return cbETH.exchangeRate();
}
}
18 changes: 18 additions & 0 deletions contracts/interfaces/IcbETH.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;



/**
* @title Coinbase Staked ETH interface to return exchangeRate
* @dev
*
*/
interface IcbETH {
/**
* @notice get exchange rate
* @return Returns the current exchange rate scaled by by 10**18
*/
function exchangeRate() external view returns (uint256);
}
18 changes: 18 additions & 0 deletions contracts/test/MockCBEth.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//SPDX-License-Identifier: MIT

pragma solidity^0.8.4;

contract MockCBEth {
uint256 private _exchangeRate = 1;

function exchangeRate() external view returns(uint256){
return _exchangeRate*1e18;
}

// anyone can set the rate in this mock example
function setExchangeRate(uint256 newRate) external {
_exchangeRate = newRate;
}
}


40 changes: 40 additions & 0 deletions test/cbEthRateProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ethers } from 'hardhat';
import { expect } from 'chai';
import { Contract } from 'ethers';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/dist/src/signer-with-address';


describe('Coinbase Eth rate provider', function() {
let mockCBEth: Contract;
let cBEthRateProvider: Contract;
let signer: SignerWithAddress;
let tempAddress;

before('setup eoas', async () => {
[signer, ] = await ethers.getSigners();
})

beforeEach('deploy mock & rate provider', async () =>{
const MockCBEth = await ethers.getContractFactory('MockCBEth');
mockCBEth = await MockCBEth.deploy();
tempAddress = mockCBEth.address;
const CBEthRateProvider = await ethers.getContractFactory('CbEthRateProvider');
cBEthRateProvider = await CBEthRateProvider.deploy(tempAddress);

})

it('returns rate scaled correctly', async () => {
expect(await mockCBEth.exchangeRate()).to.equal(ethers.utils.parseUnits('1', 18));
})
it('can set rate', async () => {
await mockCBEth.setExchangeRate(2)
expect(await mockCBEth.exchangeRate()).to.equal(ethers.utils.parseUnits('2', 18));
})
it('gets rate from rateProvider',async () => {
expect(await cBEthRateProvider.getRate()).to.equal(ethers.utils.parseUnits('1', 18));
})
it('gets correct rate from rateProvider after underlying rate update',async () => {
await mockCBEth.setExchangeRate(2);
expect(await cBEthRateProvider.getRate()).to.equal(ethers.utils.parseUnits('2', 18));
})
})
32 changes: 32 additions & 0 deletions test/chEthRateProviderMainnetFork.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ethers } from 'hardhat';
import { expect, assert } from 'chai';
import { BigNumber, Contract } from 'ethers';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/dist/src/signer-with-address';



describe('Coinbase Eth rate provider', function() {
let cBEthRateProvider: Contract;
let signer: SignerWithAddress;
let cbEthAddres = '0xBe9895146f7AF43049ca1c1AE358B0541Ea49704'; //mainnet

before('setup eoas', async () => {
[signer, ] = await ethers.getSigners();
})

beforeEach('deploy rate provider', async () =>{
const CBEthRateProvider = await ethers.getContractFactory('CbEthRateProvider');
cBEthRateProvider = await CBEthRateProvider.deploy(cbEthAddres);
})
it('checks if proxy = asset in rate provider',async () => {
expect(await cBEthRateProvider.cbETH()).to.equal(cbEthAddres)
})
it('greater than 18 decimals scale - scaled',async () => {
let currentRate = await cBEthRateProvider.getRate();
expect(currentRate.gt(BigNumber.from(10).pow(18))).to.be.true;
})
it('is lower than 19 decimals - scaled',async () => {
let currentRate = await cBEthRateProvider.getRate();
expect(currentRate.lt(BigNumber.from(10).pow(19))).to.be.true;
})
})

1 comment on commit 359cb88

@mkflow27
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.