Skip to content

Commit

Permalink
decimal shift feature (#268)
Browse files Browse the repository at this point in the history
* implementation proposal for decimal shift feature issue #243
  • Loading branch information
branciard authored and akolotov committed Sep 3, 2019
1 parent 1841968 commit 63c11df
Show file tree
Hide file tree
Showing 25 changed files with 1,767 additions and 1,504 deletions.
5 changes: 5 additions & 0 deletions contracts/upgradeable_contracts/BasicBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ contract BasicBridge is Initializable, Validatable, Ownable, Upgradeable, Claima
bytes32 internal constant DAILY_LIMIT = keccak256(abi.encodePacked("dailyLimit"));
bytes32 internal constant EXECUTION_MAX_PER_TX = keccak256(abi.encodePacked("executionMaxPerTx"));
bytes32 internal constant EXECUTION_DAILY_LIMIT = keccak256(abi.encodePacked("executionDailyLimit"));
bytes32 internal constant DECIMAL_SHIFT = keccak256(abi.encodePacked("decimalShift"));

function getBridgeInterfacesVersion() external pure returns (uint64 major, uint64 minor, uint64 patch) {
return (2, 3, 0);
Expand Down Expand Up @@ -127,6 +128,10 @@ contract BasicBridge is Initializable, Validatable, Ownable, Upgradeable, Claima
return executionDailyLimit() >= nextLimit && _amount <= executionMaxPerTx();
}

function decimalShift() public view returns (uint256) {
return uintStorage[DECIMAL_SHIFT];
}

function claimTokens(address _token, address _to) public onlyIfUpgradeabilityOwner validAddress(_to) {
claimValues(_token, _to);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@ contract BasicForeignBridgeErcToErc is BasicForeignBridge {
address _erc20token,
uint256 _requiredBlockConfirmations,
uint256 _gasPrice,
uint256 _maxPerTx,
uint256 _homeDailyLimit,
uint256 _homeMaxPerTx,
address _owner
uint256[] _maxPerTxHomeDailyLimitHomeMaxPerTxArray, // [ 0 = _maxPerTx, 1 = _homeDailyLimit, 2 = _homeMaxPerTx ]
address _owner,
uint256 _decimalShift
) internal {
require(!isInitialized());
require(AddressUtils.isContract(_validatorContract));
require(_requiredBlockConfirmations != 0);
require(_gasPrice > 0);
require(_homeMaxPerTx < _homeDailyLimit);
require(_maxPerTxHomeDailyLimitHomeMaxPerTxArray[2] < _maxPerTxHomeDailyLimitHomeMaxPerTxArray[1]); // _homeMaxPerTx < _homeDailyLimit
require(_owner != address(0));

addressStorage[VALIDATOR_CONTRACT] = _validatorContract;
setErc20token(_erc20token);
uintStorage[DEPLOYED_AT_BLOCK] = block.number;
uintStorage[REQUIRED_BLOCK_CONFIRMATIONS] = _requiredBlockConfirmations;
uintStorage[GAS_PRICE] = _gasPrice;
uintStorage[MAX_PER_TX] = _maxPerTx;
uintStorage[EXECUTION_DAILY_LIMIT] = _homeDailyLimit;
uintStorage[EXECUTION_MAX_PER_TX] = _homeMaxPerTx;
uintStorage[MAX_PER_TX] = _maxPerTxHomeDailyLimitHomeMaxPerTxArray[0];
uintStorage[EXECUTION_DAILY_LIMIT] = _maxPerTxHomeDailyLimitHomeMaxPerTxArray[1];
uintStorage[EXECUTION_MAX_PER_TX] = _maxPerTxHomeDailyLimitHomeMaxPerTxArray[2];
uintStorage[DECIMAL_SHIFT] = _decimalShift;
setOwner(_owner);
setInitialize();

emit RequiredBlockConfirmationChanged(_requiredBlockConfirmations);
emit GasPriceChanged(_gasPrice);
emit ExecutionDailyLimitChanged(_homeDailyLimit);
emit ExecutionDailyLimitChanged(_maxPerTxHomeDailyLimitHomeMaxPerTxArray[1]);
}

function getBridgeMode() external pure returns (bytes4 _data) {
Expand All @@ -52,7 +52,8 @@ contract BasicForeignBridgeErcToErc is BasicForeignBridge {
bytes32 /*_txHash*/
) internal returns (bool) {
setTotalExecutedPerDay(getCurrentDay(), totalExecutedPerDay(getCurrentDay()).add(_amount));
return erc20token().transfer(_recipient, _amount);
uint256 amount = _amount.div(10 ** decimalShift());
return erc20token().transfer(_recipient, amount);
}

function onFailedMessage(address, uint256, bytes32) internal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,34 @@ contract ForeignBridgeErc677ToErc677 is ERC677Bridge, BasicForeignBridgeErcToErc
address _erc20token,
uint256 _requiredBlockConfirmations,
uint256 _gasPrice,
uint256 _dailyLimit,
uint256 _maxPerTx,
uint256 _minPerTx,
uint256 _homeDailyLimit,
uint256 _homeMaxPerTx,
address _owner
uint256[] _dailyLimitMaxPerTxMinPerTxArray, // [ 0 = _dailyLimit, 1 = _maxPerTx, 2 = _minPerTx ]
uint256[] _homeDailyLimitHomeMaxPerTxArray, // [ 0 = _homeDailyLimit, 1 = _homeMaxPerTx ]
address _owner,
uint256 _decimalShift
) external returns (bool) {
require(_minPerTx > 0 && _maxPerTx > _minPerTx && _dailyLimit > _maxPerTx);

require(
_dailyLimitMaxPerTxMinPerTxArray[2] > 0 && // _minPerTx > 0
_dailyLimitMaxPerTxMinPerTxArray[1] > _dailyLimitMaxPerTxMinPerTxArray[2] && // _maxPerTx > _minPerTx
_dailyLimitMaxPerTxMinPerTxArray[0] > _dailyLimitMaxPerTxMinPerTxArray[1] // _dailyLimit > _maxPerTx
);
uint256[] memory _maxPerTxHomeDailyLimitHomeMaxPerTxArray = new uint256[](3);
_maxPerTxHomeDailyLimitHomeMaxPerTxArray[0] = _dailyLimitMaxPerTxMinPerTxArray[1]; // _maxPerTx
_maxPerTxHomeDailyLimitHomeMaxPerTxArray[1] = _homeDailyLimitHomeMaxPerTxArray[0]; // _homeDailyLimit
_maxPerTxHomeDailyLimitHomeMaxPerTxArray[2] = _homeDailyLimitHomeMaxPerTxArray[1]; // _homeMaxPerTx
_initialize(
_validatorContract,
_erc20token,
_requiredBlockConfirmations,
_gasPrice,
_maxPerTx,
_homeDailyLimit,
_homeMaxPerTx,
_owner
_maxPerTxHomeDailyLimitHomeMaxPerTxArray, // [ 0 = _maxPerTx, 1 = _homeDailyLimit, 2 = _homeMaxPerTx ]
_owner,
_decimalShift
);

uintStorage[DAILY_LIMIT] = _dailyLimit;
uintStorage[MIN_PER_TX] = _minPerTx;
uintStorage[DAILY_LIMIT] = _dailyLimitMaxPerTxMinPerTxArray[0];
uintStorage[MIN_PER_TX] = _dailyLimitMaxPerTxMinPerTxArray[2];

emit DailyLimitChanged(_dailyLimit);
emit DailyLimitChanged(_dailyLimitMaxPerTxMinPerTxArray[0]);

return isInitialized();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ contract ForeignBridgeErcToErc is BasicForeignBridgeErcToErc, ERC20Bridge {
address _erc20token,
uint256 _requiredBlockConfirmations,
uint256 _gasPrice,
uint256 _maxPerTx,
uint256 _homeDailyLimit,
uint256 _homeMaxPerTx,
address _owner
uint256[] _maxPerTxHomeDailyLimitHomeMaxPerTxArray, // [ 0 = _maxPerTx, 1 = _homeDailyLimit, 2 = _homeMaxPerTx ]
address _owner,
uint256 _decimalShift
) external returns (bool) {
_initialize(
_validatorContract,
_erc20token,
_requiredBlockConfirmations,
_gasPrice,
_maxPerTx,
_homeDailyLimit,
_homeMaxPerTx,
_owner
_maxPerTxHomeDailyLimitHomeMaxPerTxArray,
_owner,
_decimalShift
);
return isInitialized();
}
Expand Down
102 changes: 45 additions & 57 deletions contracts/upgradeable_contracts/erc20_to_erc20/HomeBridgeErcToErc.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,23 @@ contract HomeBridgeErcToErc is

function initialize(
address _validatorContract,
uint256 _dailyLimit,
uint256 _maxPerTx,
uint256 _minPerTx,
uint256[] _dailyLimitMaxPerTxMinPerTxArray, // [ 0 = _dailyLimit, 1 = _maxPerTx, 2 = _minPerTx ]
uint256 _homeGasPrice,
uint256 _requiredBlockConfirmations,
address _erc677token,
uint256 _foreignDailyLimit,
uint256 _foreignMaxPerTx,
address _owner
uint256[] _foreignDailyLimitForeignMaxPerTxArray, // [ 0 = _foreignDailyLimit, 1 = _foreignMaxPerTx ]
address _owner,
uint256 _decimalShift
) external returns (bool) {
_initialize(
_validatorContract,
_dailyLimit,
_maxPerTx,
_minPerTx,
_dailyLimitMaxPerTxMinPerTxArray,
_homeGasPrice,
_requiredBlockConfirmations,
_erc677token,
_foreignDailyLimit,
_foreignMaxPerTx,
_owner
_foreignDailyLimitForeignMaxPerTxArray,
_owner,
_decimalShift
);
setInitialize();

Expand All @@ -48,33 +44,27 @@ contract HomeBridgeErcToErc is

function rewardableInitialize(
address _validatorContract,
uint256 _dailyLimit,
uint256 _maxPerTx,
uint256 _minPerTx,
uint256[] _dailyLimitMaxPerTxMinPerTxArray, // [ 0 = _dailyLimit, 1 = _maxPerTx, 2 = _minPerTx ]
uint256 _homeGasPrice,
uint256 _requiredBlockConfirmations,
address _erc677token,
uint256 _foreignDailyLimit,
uint256 _foreignMaxPerTx,
uint256[] _foreignDailyLimitForeignMaxPerTxArray, // [ 0 = _foreignDailyLimit, 1 = _foreignMaxPerTx ]
address _owner,
address _feeManager,
uint256 _homeFee,
uint256 _foreignFee
uint256[] _homeFeeForeignFeeArray, // [ 0 = _homeFee, 1 = _foreignFee ]
uint256 _decimalShift
) external returns (bool) {
_rewardableInitialize(
_validatorContract,
_dailyLimit,
_maxPerTx,
_minPerTx,
_dailyLimitMaxPerTxMinPerTxArray,
_homeGasPrice,
_requiredBlockConfirmations,
_erc677token,
_foreignDailyLimit,
_foreignMaxPerTx,
_foreignDailyLimitForeignMaxPerTxArray,
_owner,
_feeManager,
_homeFee,
_foreignFee
_homeFeeForeignFeeArray,
_decimalShift
);
setInitialize();

Expand All @@ -83,71 +73,69 @@ contract HomeBridgeErcToErc is

function _rewardableInitialize(
address _validatorContract,
uint256 _dailyLimit,
uint256 _maxPerTx,
uint256 _minPerTx,
uint256[] _dailyLimitMaxPerTxMinPerTxArray, // [ 0 = _dailyLimit, 1 = _maxPerTx, 2 = _minPerTx ]
uint256 _homeGasPrice,
uint256 _requiredBlockConfirmations,
address _erc677token,
uint256 _foreignDailyLimit,
uint256 _foreignMaxPerTx,
uint256[] _foreignDailyLimitForeignMaxPerTxArray, // [ 0 = _foreignDailyLimit, 1 = _foreignMaxPerTx ]
address _owner,
address _feeManager,
uint256 _homeFee,
uint256 _foreignFee
uint256[] _homeFeeForeignFeeArray, // [ 0 = _homeFee, 1 = _foreignFee ]
uint256 _decimalShift
) internal {
_initialize(
_validatorContract,
_dailyLimit,
_maxPerTx,
_minPerTx,
_dailyLimitMaxPerTxMinPerTxArray,
_homeGasPrice,
_requiredBlockConfirmations,
_erc677token,
_foreignDailyLimit,
_foreignMaxPerTx,
_owner
_foreignDailyLimitForeignMaxPerTxArray,
_owner,
_decimalShift
);
require(AddressUtils.isContract(_feeManager));
addressStorage[FEE_MANAGER_CONTRACT] = _feeManager;
_setFee(_feeManager, _homeFee, HOME_FEE);
_setFee(_feeManager, _foreignFee, FOREIGN_FEE);
_setFee(_feeManager, _homeFeeForeignFeeArray[0], HOME_FEE);
_setFee(_feeManager, _homeFeeForeignFeeArray[1], FOREIGN_FEE);
}

function _initialize(
address _validatorContract,
uint256 _dailyLimit,
uint256 _maxPerTx,
uint256 _minPerTx,
uint256[] _dailyLimitMaxPerTxMinPerTxArray, // [ 0 = _dailyLimit, 1 = _maxPerTx, 2 = _minPerTx ]
uint256 _homeGasPrice,
uint256 _requiredBlockConfirmations,
address _erc677token,
uint256 _foreignDailyLimit,
uint256 _foreignMaxPerTx,
address _owner
uint256[] _foreignDailyLimitForeignMaxPerTxArray, // [ 0 = _foreignDailyLimit, 1 = _foreignMaxPerTx ]
address _owner,
uint256 _decimalShift
) internal {
require(!isInitialized());
require(AddressUtils.isContract(_validatorContract));
require(_requiredBlockConfirmations > 0);
require(_minPerTx > 0 && _maxPerTx > _minPerTx && _dailyLimit > _maxPerTx);
require(_foreignMaxPerTx < _foreignDailyLimit);
require(
_dailyLimitMaxPerTxMinPerTxArray[2] > 0 && // _minPerTx > 0
_dailyLimitMaxPerTxMinPerTxArray[1] > _dailyLimitMaxPerTxMinPerTxArray[2] && // _maxPerTx > _minPerTx
_dailyLimitMaxPerTxMinPerTxArray[0] > _dailyLimitMaxPerTxMinPerTxArray[1] // _dailyLimit > _maxPerTx
);
require(_foreignDailyLimitForeignMaxPerTxArray[1] < _foreignDailyLimitForeignMaxPerTxArray[0]); // _foreignMaxPerTx < _foreignDailyLimit
require(_owner != address(0));
addressStorage[VALIDATOR_CONTRACT] = _validatorContract;
uintStorage[DEPLOYED_AT_BLOCK] = block.number;
uintStorage[DAILY_LIMIT] = _dailyLimit;
uintStorage[MAX_PER_TX] = _maxPerTx;
uintStorage[MIN_PER_TX] = _minPerTx;
uintStorage[DAILY_LIMIT] = _dailyLimitMaxPerTxMinPerTxArray[0];
uintStorage[MAX_PER_TX] = _dailyLimitMaxPerTxMinPerTxArray[1];
uintStorage[MIN_PER_TX] = _dailyLimitMaxPerTxMinPerTxArray[2];
uintStorage[GAS_PRICE] = _homeGasPrice;
uintStorage[REQUIRED_BLOCK_CONFIRMATIONS] = _requiredBlockConfirmations;
uintStorage[EXECUTION_DAILY_LIMIT] = _foreignDailyLimit;
uintStorage[EXECUTION_MAX_PER_TX] = _foreignMaxPerTx;
uintStorage[EXECUTION_DAILY_LIMIT] = _foreignDailyLimitForeignMaxPerTxArray[0];
uintStorage[EXECUTION_MAX_PER_TX] = _foreignDailyLimitForeignMaxPerTxArray[1];
uintStorage[DECIMAL_SHIFT] = _decimalShift;
setOwner(_owner);
setErc677token(_erc677token);

emit RequiredBlockConfirmationChanged(_requiredBlockConfirmations);
emit GasPriceChanged(_homeGasPrice);
emit DailyLimitChanged(_dailyLimit);
emit ExecutionDailyLimitChanged(_foreignDailyLimit);
emit DailyLimitChanged(_dailyLimitMaxPerTxMinPerTxArray[0]);
emit ExecutionDailyLimitChanged(_foreignDailyLimitForeignMaxPerTxArray[0]);
}

function claimTokensFromErc677(address _token, address _to) external onlyIfUpgradeabilityOwner {
Expand All @@ -160,7 +148,7 @@ contract HomeBridgeErcToErc is

function onExecuteAffirmation(address _recipient, uint256 _value, bytes32 txHash) internal returns (bool) {
setTotalExecutedPerDay(getCurrentDay(), totalExecutedPerDay(getCurrentDay()).add(_value));
uint256 valueToMint = _value;
uint256 valueToMint = _value.mul(10 ** decimalShift());
address feeManager = feeManagerContract();
if (feeManager != address(0)) {
uint256 fee = calculateFee(valueToMint, false, feeManager, FOREIGN_FEE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,28 @@ contract HomeBridgeErcToErcPOSDAO is HomeBridgeErcToErc {

function rewardableInitialize(
address _validatorContract,
uint256 _dailyLimit,
uint256 _maxPerTx,
uint256 _minPerTx,
uint256[] _dailyLimitMaxPerTxMinPerTxArray, // [ 0 = _dailyLimit, 1 = _maxPerTx, 2 = _minPerTx ]
uint256 _homeGasPrice,
uint256 _requiredBlockConfirmations,
address _erc677token,
uint256 _foreignDailyLimit,
uint256 _foreignMaxPerTx,
uint256[] _foreignDailyLimitForeignMaxPerTxArray, // [ 0 = _foreignDailyLimit, 1 = _foreignMaxPerTx ]
address _owner,
address _feeManager,
uint256 _homeFee,
uint256 _foreignFee,
address _blockReward
uint256[] _homeFeeForeignFeeArray, // [ 0 = _homeFee, 1 = _foreignFee ]
address _blockReward,
uint256 _decimalShift
) external returns (bool) {
_rewardableInitialize(
_validatorContract,
_dailyLimit,
_maxPerTx,
_minPerTx,
_dailyLimitMaxPerTxMinPerTxArray,
_homeGasPrice,
_requiredBlockConfirmations,
_erc677token,
_foreignDailyLimit,
_foreignMaxPerTx,
_foreignDailyLimitForeignMaxPerTxArray,
_owner,
_feeManager,
_homeFee,
_foreignFee
_homeFeeForeignFeeArray,
_decimalShift
);
_setBlockRewardContract(_feeManager, _blockReward);
setInitialize();
Expand Down
Loading

0 comments on commit 63c11df

Please sign in to comment.