Skip to content

Commit 36fceb0

Browse files
authored
Add KyberFprReserveV2 supporting WETH as quote (KyberNetwork#1059)
* Add KyberReserve supporting WETH as quote, where weth is in external wallet. * Add max gas price feature for reserve. reserve manager can give 0 rate reply when gas price above this value. * Change name to KyberFprReserveV2 * Add max gas price tests * Add trade tests for using weth * Add withdraw tests * Add set token wallet tests * Add set contracts tests * Add tests for getBalance, fix getBalance of weth * Add more dev comments * Add get conversion rate tests * Add test trade recipient is not receiving eth * Add try catch for getting rate from conversion rate * Fix solhint error * Fix full regression tests * Add reserve setup, fix review comments * Add rate validation in reserve trade * Fix travis build errors * Add some gas usage tests * Simplify reserve tests with mock conversion rates * Update reviews * Add gas consumption report for old conversion rate * Add doRateValidation as config * Fix solhint * Add spdx license * Change to use local variable for config data * Update js files to 2 spaces * Add missing set do rate validation in sell tests * Fix missing set validation in tests
1 parent ad1122b commit 36fceb0

9 files changed

+4343
-0
lines changed

contracts/sol4/mock/MockConversionRate.sol

+13
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,17 @@ contract MockConversionRate is ConversionRates {
3232
function mockIsTokenTradeEnabled(address token) public view returns (bool) {
3333
return tokenData[token].enabled;
3434
}
35+
36+
function getInitImbalance(ERC20 token) public view returns(int totalImbalance) {
37+
// check if trade is enabled
38+
if (!tokenData[token].enabled) return 0;
39+
if (tokenControlInfo[token].minimalRecordResolution == 0) return 0; // token control info not set
40+
41+
// get rate update block
42+
bytes32 compactData = tokenRatesCompactData[tokenData[token].compactDataArrayIndex];
43+
44+
uint updateRateBlock = getLast4Bytes(compactData);
45+
// check imbalance
46+
(totalImbalance, ) = getImbalance(token, updateRateBlock, block.number);
47+
}
3548
}

contracts/sol4/mock/MockEnhancedStepFunctions.sol

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ contract MockEnhancedStepFunctions is ConversionRateEnhancedSteps {
99

1010
}
1111

12+
function getInitImbalance(ERC20 token) public view returns(int totalImbalance) {
13+
// check if trade is enabled
14+
if (!tokenData[token].enabled) return 0;
15+
if (tokenControlInfo[token].minimalRecordResolution == 0) return 0; // token control info not set
16+
17+
// get rate update block
18+
bytes32 compactData = tokenRatesCompactData[tokenData[token].compactDataArrayIndex];
19+
20+
uint updateRateBlock = getLast4Bytes(compactData);
21+
// check imbalance
22+
(totalImbalance, ) = getImbalance(token, updateRateBlock, block.number);
23+
}
24+
1225
function mockGetMaxTotalImbalance(ERC20 token) public view returns(uint) {
1326
return getMaxTotalImbalance(token);
1427
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
pragma solidity 0.6.6;
2+
3+
import "../IERC20.sol";
4+
5+
6+
contract MockConversionRates {
7+
8+
mapping (address => uint256) public buyRates;
9+
mapping (address => uint256) public sellRates;
10+
mapping (address => uint256) public imbalances;
11+
address public reserve;
12+
13+
function setBaseRates(IERC20 token, uint256 buyRate, uint256 sellRate) external {
14+
buyRates[address(token)] = buyRate;
15+
sellRates[address(token)] = sellRate;
16+
}
17+
18+
function recordImbalance(
19+
IERC20 token,
20+
int buyAmount,
21+
uint256 rateUpdateBlock,
22+
uint256 currentBlock
23+
) external {}
24+
25+
function setReserveAddress(address _reserve) external {
26+
reserve = _reserve;
27+
}
28+
29+
function getRate(
30+
IERC20 token,
31+
uint256 currentBlockNumber,
32+
bool buy,
33+
uint256 qty
34+
) external view returns(uint256) {
35+
currentBlockNumber;
36+
qty;
37+
if (buy) {
38+
return buyRates[address(token)];
39+
}
40+
return sellRates[address(token)];
41+
}
42+
43+
function getInitImbalance(IERC20 token) external view returns(uint256) {
44+
return imbalances[address(token)];
45+
}
46+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pragma solidity 0.6.6;
2+
3+
import "../IKyberSanity.sol";
4+
5+
6+
contract MockSanityRates is IKyberSanity {
7+
uint256 public sanityRateValue;
8+
9+
function setSanityRateValue(uint256 _value) external {
10+
sanityRateValue = _value;
11+
}
12+
13+
function getSanityRate(IERC20 src, IERC20 dest) external override view returns (uint256 rate) {
14+
src;
15+
dest;
16+
rate = sanityRateValue;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pragma solidity 0.6.6;
2+
3+
4+
import "../IERC20.sol";
5+
6+
7+
interface IConversionRates {
8+
9+
function recordImbalance(
10+
IERC20 token,
11+
int buyAmount,
12+
uint256 rateUpdateBlock,
13+
uint256 currentBlock
14+
) external;
15+
16+
function getRate(
17+
IERC20 token,
18+
uint256 currentBlockNumber,
19+
bool buy,
20+
uint256 qty
21+
) external view returns(uint256);
22+
}

contracts/sol6/reserves/IWeth.sol

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pragma solidity 0.6.6;
2+
3+
import "../IERC20.sol";
4+
5+
6+
interface IWeth is IERC20 {
7+
function deposit() external payable;
8+
function withdraw(uint256 wad) external;
9+
}

0 commit comments

Comments
 (0)