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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
[submodule "user-rating"]
path = user-rating
url = https://github.com/backstop-protocol/user-rating.git
[submodule "open-oracle"]
path = open-oracle
url = https://github.com/compound-finance/open-oracle.git
2 changes: 1 addition & 1 deletion contracts/bprotocol/info/LiquidatorInfo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ contract LiquidatorInfo {
if(registry.cEther() == cTokens[i])
info.debtTokens[i] = ETH;
else
info.debtTokens[i] = address(CTokenInterface(info.debtTokens[i]).underlying());
info.debtTokens[i] = address(CTokenInterface(cTokens[i]).underlying());

address bToken = bComptroller.c2b(cTokens[i]);
info.debtAmounts[i] = IBToken(bToken).borrowBalanceCurrent(user);
Expand Down
2 changes: 2 additions & 0 deletions contracts/bprotocol/info/UserInfo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ contract UserInfo {
string memory name = ERC20Like(ctoken).name();
if(keccak256(abi.encodePacked(name)) == keccak256(abi.encodePacked("Compound ETH"))) return true;
if(keccak256(abi.encodePacked(name)) == keccak256(abi.encodePacked("Compound Ether"))) return true;
if(keccak256(abi.encodePacked(name)) == keccak256(abi.encodePacked("cETH"))) return true; // for testenv

return false;
}
Expand All @@ -139,6 +140,7 @@ contract UserInfo {
}

function getTokenInfo(address comptroller, address bComptroller) public returns(TokenInfo memory info) {

address[] memory markets = ComptrollerLike(comptroller).getAllMarkets();
uint numMarkets = markets.length;
info.btoken = new address[](numMarkets);
Expand Down
81 changes: 81 additions & 0 deletions contracts/mock/MockUniswapAnchoredView.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
pragma solidity 0.5.16;
pragma experimental ABIEncoderV2;

import "hardhat/console.sol";

contract MockUniswapAnchoredView {
// cToken => uint
mapping(address => uint) public prices;
// symbol => cToken
mapping(string => address) public symbolToCTokens;
// cToken => symbol
mapping(address => string) public cTokenToSymbol;
// cToken => decimals
mapping(address => uint) public cTokenDecimals;

constructor(string[] memory symbols, address[] memory cTokens, uint[] memory decimals) public {

for (uint i = 0; i < symbols.length; i++) {
string memory symbol = symbols[i];
address cToken = cTokens[i];
uint decimal = decimals[i];
symbolToCTokens[symbol] = cToken;
cTokenToSymbol[cToken] = symbol;
cTokenDecimals[cToken] = decimal;
}

}

function postPrices(bytes[] calldata messages, bytes[] calldata signatures, string[] calldata symbols) external {

for (uint i = 0; i < messages.length; i++) {
(address source, uint64 timestamp, string memory key, uint64 value) = decodeMessage(messages[i], signatures[i]);

console.log("source", source);
console.log("timestamp", timestamp);
console.log("key", key);
console.log("value", value);
address cToken = symbolToCTokens[key];
// signed `value` is in USD upto 6 decimal places
// hence, `value` already have 6 decimals
// For example: USDT which has 6 decimal tokens:
// oracle price will be `decimalToIncrease` = (36 - `value`-decimals - tokenDecimals)
// `decimalToIncrease` = 36 - 6 - 6
// `decimalToIncrease` = 30 - 6 = 24 decimals to add more in value
// fixed 30 which is derived from 36 - `value`-decimals = 36 - 6 = 30
uint decimalToIncrease = 1 * 10**(30 - cTokenDecimals[cToken]); // 1e(30-decimals)
uint convertedValue = value * decimalToIncrease;
console.log("convertedValue", convertedValue);
setPrice(cToken, convertedValue);
}

// custom code below
//require(source != address(0), "invalid signature");
//prices[source][key] = value;
}

// Directly set the prices
function setPrice(address cToken, uint newPrice) public {
prices[cToken] = newPrice;
}

function getUnderlyingPrice(address cToken) external view returns (uint) {
return prices[cToken];
}

function decodeMessage(bytes memory message, bytes memory signature) internal pure returns (address, uint64, string memory, uint64) {
// Recover the source address
address source = source(message, signature);

// Decode the message and check the kind
(string memory kind, uint64 timestamp, string memory key, uint64 value) = abi.decode(message, (string, uint64, string, uint64));
require(keccak256(abi.encodePacked(kind)) == keccak256(abi.encodePacked("prices")), "Kind of data must be 'prices'");
return (source, timestamp, key, value);
}

function source(bytes memory message, bytes memory signature) public pure returns (address) {
(bytes32 r, bytes32 s, uint8 v) = abi.decode(signature, (bytes32, bytes32, uint8));
bytes32 hash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", keccak256(message)));
return ecrecover(hash, v, r, s);
}
}
1 change: 1 addition & 0 deletions open-oracle
Submodule open-oracle added at 046b32
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@types/mocha": "^8.2.0",
"@types/node": "^14.14.14",
"@types/web3": "^1.2.2",
"axios": "^0.21.1",
"chai": "^4.2.0",
"chai-bn": "^0.2.1",
"eslint": "^7.16.0",
Expand Down Expand Up @@ -48,7 +49,7 @@
},
"scripts": {
"deploy-compound": "cd compound-protocol && PROVIDER='http://localhost:8545/' yarn run repl -s script/scen/scriptFlywheel.scen",
"postinstall": "git submodule update --init --recursive && cd compound-protocol && yarn && cd scenario && yarn",
"postinstall": "git submodule update --init --recursive && cd compound-protocol && yarn && cd scenario && yarn && cd ../../open-oracle/sdk/javascript && yarn",
"compile": "hardhat compile",
"ganache": "ganache-cli --gasLimit 20000000 --gasPrice 20000 --defaultBalanceEther 1000000000 --allowUnlimitedContractSize true",
"test": "hardhat test",
Expand All @@ -62,7 +63,8 @@
"exec-coverage": "node --max-old-space-size=4096 ./node_modules/.bin/truffle run coverage --network coverage",
"coverage": "FILE=coverage npm run clean-snapshot && npm run unzip-snapshot && npm run copy-coverage-files && npm run exec-coverage",
"load-snapshot": "echo $FILE && npm run clean-snapshot && npm run unzip-snapshot && npm run start-ganache",
"start-ganache": "echo $FILE && npx ganache-cli -l 1250000000000 --allowUnlimitedContractSize -a 20 -e 100000000000000 -m 'bonus patient judge normal delay supreme sentence confirm fox desk cool estate' --db './snapshot/'$FILE"
"start-ganache": "echo $FILE && npx ganache-cli -l 0xfffffffffff --allowUnlimitedContractSize -a 20 -e 100000000000000 -m 'bonus patient judge normal delay supreme sentence confirm fox desk cool estate' --db './snapshot/'$FILE",
"start-price-reporter": "cd open-oracle/sdk/javascript && yarn run start --private_key 0x177ee777e72b8c042e05ef41d1db0f17f1fcb0e8150b37cfad6993e4373bdf10 --script ../../../scripts/FakeOraclePrice.js"
},
"repository": {
"type": "git",
Expand Down
7 changes: 6 additions & 1 deletion playground/bcompound.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
"Migrate": "0xeC991E2686be779cFaaA8D0C723464435F17bD9f",
"Pool": "0x86F99Aa3ff0D9B53eeF319E20bA3a8F5e54990B4",
"Registry": "0x066a5D381D02F435190527Ac7c0D2370Ed56B18F",
"BScore": "0x178743c6477774C9e80a5B7644c35f9eC49F3071"
"BScore": "0x178743c6477774C9e80a5B7644c35f9eC49F3071",

"MEMBER_1": "0xfc046d71fc76629bEc2Cb72E6324A8036BBf8ffF",
"MEMBER_2": "0xa6082e72eE9ed626Af46c5A20B2C8eA72283A695",
"MEMBER_3": "0xdd8dD6A5555e15066ecCbdd185828b9C389Cc881",
"MEMBER_4": "0xE7dE50F74A3533f044eB6edAE3C425b9ca6f2909"
}

}
5 changes: 5 additions & 0 deletions scripts/FakeOraclePrice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = async function fetchPrices(now) {
// mainnet prices taken
// ZRX price is actually = 1.605584, but increased 10% (0.1605584) = 1.605584 + 0.1605584 = 1.7661424
return [now, { eth: 1617.45, zrx: 1.7661424, bat: 0.4, usdt: 1.0, wbtc: 39202.8 }];
};
43 changes: 43 additions & 0 deletions test/bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

## Testing with Bot

Testing of the Bot requires four different terminal sessions.

### Load BCompound snapshot

Load BCompound contracts snapshot in first CLI session:

`$ FILE=bcompound npm run load-snapshot`

Ensure that ganache started before starting the following command.

### Start open-oracle-reporter

Open second CLI and run the below command to start the open-oracle-reporter. The prices this command use it configured in `scripts/FakeOraclePrice.js`. You can modify this file to change the token prices that you want bot to post to the oracle contract.

`$ npm run start-price-reporter`

### Run bot

Open third CLI session and run the bot using the below command:

#### Set coinbase API keys as environment variables
You need to set the coinbase API keys to fetch the mainnet prices. You can configure these as environment variables.

`$ export COINBASE_SECRET="<coinbase_secret>"`

`$ export COINBASE_APIKEY="<coinbase_apikey>"`

`$ export COINBASE_PHRASE="<coinbase_phrase>"`

Command to start the bot:

`$ npx truffle exec test/bot/bot.js`

Ensure that after running the bot you must wait for the output `listening for blocks...` in the CLI then only start the bot-test (given below).

### Run bot test

Open fourth CLI session and run the below command to start the bot test:

`npx hardhat test test/bot/testBot.ts`
Loading