Skip to content

Commit

Permalink
Add POSDAOHomeBridgeErcToErc
Browse files Browse the repository at this point in the history
  • Loading branch information
patitonar committed Apr 17, 2019
1 parent 63fbd12 commit 8ab76b9
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,20 @@ contract FeeManagerErcToErcPOSDAO is BlockRewardFeeManager {
function getFeeManagerMode() public pure returns(bytes4) {
return bytes4(keccak256(abi.encodePacked("manages-both-directions")));
}

function blockRewardContract() public view returns(address) {
return _blockRewardContract();
}

function setBlockRewardContract(address _blockReward) external {
require(_blockReward != address(0) && isContract(_blockReward) && (IBlockReward(_blockReward).bridgesAllowedLength() != 0));
addressStorage[keccak256(abi.encodePacked("blockRewardContract"))] = _blockReward;
}

function isContract(address _addr) internal view returns (bool)
{
uint length;
assembly { length := extcodesize(_addr) }
return length > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,48 @@ contract HomeBridgeErcToErc is ERC677Receiver, EternalStorage, BasicBridge, Basi
uint256 _foreignDailyLimit,
uint256 _foreignMaxPerTx,
address _owner,
address _blockReward,
address _feeManager,
uint256 _homeFee,
uint256 _foreignFee
) public
returns(bool)
{
_rewardableInitialize (
_validatorContract,
_dailyLimit,
_maxPerTx,
_minPerTx,
_homeGasPrice,
_requiredBlockConfirmations,
_erc677token,
_foreignDailyLimit,
_foreignMaxPerTx,
_owner,
_feeManager,
_homeFee,
_foreignFee
);
setInitialize(true);

return isInitialized();
}

function _rewardableInitialize (
address _validatorContract,
uint256 _dailyLimit,
uint256 _maxPerTx,
uint256 _minPerTx,
uint256 _homeGasPrice,
uint256 _requiredBlockConfirmations,
address _erc677token,
uint256 _foreignDailyLimit,
uint256 _foreignMaxPerTx,
address _owner,
address _feeManager,
uint256 _homeFee,
uint256 _foreignFee
) internal
returns(bool)
{
_initialize (
_validatorContract,
Expand All @@ -80,14 +116,9 @@ contract HomeBridgeErcToErc is ERC677Receiver, EternalStorage, BasicBridge, Basi
_owner
);
require(isContract(_feeManager));
require(_blockReward == address(0) || isContract(_blockReward));
addressStorage[keccak256(abi.encodePacked("feeManagerContract"))] = _feeManager;
_setFee(_feeManager, _homeFee, HOME_FEE);
_setFee(_feeManager, _foreignFee, FOREIGN_FEE);
addressStorage[keccak256(abi.encodePacked("blockRewardContract"))] = _blockReward;
setInitialize(true);

return isInitialized();
}

function _initialize (
Expand Down Expand Up @@ -132,15 +163,6 @@ contract HomeBridgeErcToErc is ERC677Receiver, EternalStorage, BasicBridge, Basi
revert();
}

function blockRewardContract() public view returns(IBlockReward) {
return IBlockReward(addressStorage[keccak256(abi.encodePacked("blockRewardContract"))]);
}

function setBlockRewardContract(address _blockReward) public onlyOwner {
require(_blockReward != address(0) && isContract(_blockReward) && (IBlockReward(_blockReward).bridgesAllowedLength() != 0));
addressStorage[keccak256(abi.encodePacked("blockRewardContract"))] = _blockReward;
}

function onExecuteAffirmation(address _recipient, uint256 _value, bytes32 txHash) internal returns(bool) {
setTotalExecutedPerDay(getCurrentDay(), totalExecutedPerDay(getCurrentDay()).add(_value));
uint256 valueToMint = _value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
pragma solidity 0.4.24;

import "./HomeBridgeErcToErc.sol";

contract POSDAOHomeBridgeErcToErc is HomeBridgeErcToErc {

function rewardableInitialize (
address _validatorContract,
uint256 _dailyLimit,
uint256 _maxPerTx,
uint256 _minPerTx,
uint256 _homeGasPrice,
uint256 _requiredBlockConfirmations,
address _erc677token,
uint256 _foreignDailyLimit,
uint256 _foreignMaxPerTx,
address _owner,
address _feeManager,
uint256 _homeFee,
uint256 _foreignFee,
address _blockReward
) public
returns(bool)
{
_rewardableInitialize (
_validatorContract,
_dailyLimit,
_maxPerTx,
_minPerTx,
_homeGasPrice,
_requiredBlockConfirmations,
_erc677token,
_foreignDailyLimit,
_foreignMaxPerTx,
_owner,
_feeManager,
_homeFee,
_foreignFee
);
_setBlockRewardContract(_feeManager, _blockReward);
setInitialize(true);

return isInitialized();
}

function blockRewardContract() public view returns(address) {
address blockReward;
address feeManager = feeManagerContract();
bytes memory callData = abi.encodeWithSignature("blockRewardContract()");

assembly {
let result := callcode(gas, feeManager, 0x0, add(callData, 0x20), mload(callData), 0, 32)
blockReward := mload(0)

switch result
case 0 { revert(0, 0) }
}

return blockReward;
}

function setBlockRewardContract(address _blockReward) public onlyOwner {
address feeManager = feeManagerContract();
_setBlockRewardContract(feeManager, _blockReward);
}

function _setBlockRewardContract(address _feeManager, address _blockReward) internal {
require(_feeManager.delegatecall(abi.encodeWithSignature("setBlockRewardContract(address)", _blockReward)));
}
}
49 changes: 35 additions & 14 deletions test/erc_to_erc/home_bridge.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Web3Utils = require('web3-utils');
const HomeBridge = artifacts.require("HomeBridgeErcToErc.sol");
const POSDAOHomeBridge = artifacts.require("POSDAOHomeBridgeErcToErc.sol");
const EternalStorageProxy = artifacts.require("EternalStorageProxy.sol");
const BridgeValidators = artifacts.require("BridgeValidators.sol");
const ERC677BridgeToken = artifacts.require("ERC677BridgeToken.sol");
Expand Down Expand Up @@ -633,7 +634,7 @@ contract('HomeBridge_ERC20_to_ERC20', async (accounts) => {
token = await ERC677BridgeToken.new("Some ERC20", "RSZT", 18);
rewardableValidators = await RewardableValidators.new()
await rewardableValidators.initialize(requiredSignatures, validators, rewards, owner).should.be.fulfilled
homeBridge = await HomeBridge.new()
homeBridge = await POSDAOHomeBridge.new()
homeFee = web3.toBigNumber(web3.toWei(0.002, "ether"))
foreignFee = web3.toBigNumber(web3.toWei(0.002, "ether"))
blockRewardContract = await BlockReward.new()
Expand All @@ -646,11 +647,11 @@ contract('HomeBridge_ERC20_to_ERC20', async (accounts) => {
'0'.should.be.bignumber.equal(await homeBridge.maxPerTx())
false.should.be.equal(await homeBridge.isInitialized())

await homeBridge.rewardableInitialize(ZERO_ADDRESS, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, blockRewardContract.address, feeManager.address, homeFee, foreignFee).should.be.rejectedWith(ERROR_MSG);
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, 0, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, blockRewardContract.address, feeManager.address, homeFee, foreignFee).should.be.rejectedWith(ERROR_MSG);
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, 0, foreignDailyLimit, token.address, foreignMaxPerTx, owner, blockRewardContract.address, feeManager.address, homeFee, foreignFee).should.be.rejectedWith(ERROR_MSG);
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, blockRewardContract.address, ZERO_ADDRESS, homeFee, foreignFee).should.be.rejectedWith(ERROR_MSG);
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, blockRewardContract.address, feeManager.address, homeFee, foreignFee).should.be.fulfilled;
await homeBridge.rewardableInitialize(ZERO_ADDRESS, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, feeManager.address, homeFee, foreignFee, blockRewardContract.address).should.be.rejectedWith(ERROR_MSG);
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, 0, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, feeManager.address, homeFee, foreignFee, blockRewardContract.address).should.be.rejectedWith(ERROR_MSG);
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, 0, foreignDailyLimit, token.address, foreignMaxPerTx, owner, feeManager.address, homeFee, foreignFee, blockRewardContract.address).should.be.rejectedWith(ERROR_MSG);
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, ZERO_ADDRESS, homeFee, foreignFee, blockRewardContract.address).should.be.rejectedWith(ERROR_MSG);
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, feeManager.address, homeFee, foreignFee, blockRewardContract.address).should.be.fulfilled;

true.should.be.equal(await homeBridge.isInitialized())
rewardableValidators.address.should.be.equal(await homeBridge.validatorContract());
Expand All @@ -672,10 +673,12 @@ contract('HomeBridge_ERC20_to_ERC20', async (accounts) => {
bridgeHomeFee.should.be.bignumber.equal(homeFee)
const bridgeForeignFee = await homeBridge.getForeignFee()
bridgeForeignFee.should.be.bignumber.equal(foreignFee)
const blockReward = await homeBridge.blockRewardContract()
blockReward.should.be.equals(blockRewardContract.address)
})
it('can update fee contract', async () => {
const feeManager = await FeeManagerErcToErcPOSDAO.new()
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, blockRewardContract.address, feeManager.address, homeFee, foreignFee).should.be.fulfilled;
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, feeManager.address, homeFee, foreignFee, blockRewardContract.address).should.be.fulfilled;

// Given
const newFeeManager = await FeeManagerErcToErcPOSDAO.new()
Expand All @@ -689,7 +692,7 @@ contract('HomeBridge_ERC20_to_ERC20', async (accounts) => {
})
it('can update fee', async () => {
const feeManager = await FeeManagerErcToErcPOSDAO.new()
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, blockRewardContract.address, feeManager.address, homeFee, foreignFee).should.be.fulfilled;
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, feeManager.address, homeFee, foreignFee, blockRewardContract.address).should.be.fulfilled;

// Given
const newHomeFee = web3.toBigNumber(web3.toWei(0.1, "ether"))
Expand All @@ -711,12 +714,29 @@ contract('HomeBridge_ERC20_to_ERC20', async (accounts) => {
const bothDirectionsModeHash = '0xd7de965f'

// When
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, blockRewardContract.address, feeManager.address, homeFee, foreignFee).should.be.fulfilled;
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, feeManager.address, homeFee, foreignFee, blockRewardContract.address).should.be.fulfilled;

// Then
const feeManagerMode = await homeBridge.getFeeManagerMode()
feeManagerMode.should.be.equals(bothDirectionsModeHash)
})
it('should be able to set blockReward contract', async () => {
// Given
const feeManager = await FeeManagerErcToErcPOSDAO.new()

await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, feeManager.address, homeFee, foreignFee, blockRewardContract.address).should.be.fulfilled;

const blockReward = await homeBridge.blockRewardContract()
blockReward.should.be.equals(blockRewardContract.address)

// When
const newBlockRewardContract = await BlockReward.new()
await homeBridge.setBlockRewardContract(newBlockRewardContract.address).should.be.fulfilled

// Then
const newBlockReward = await homeBridge.blockRewardContract()
newBlockReward.should.be.equals(newBlockRewardContract.address)
})
})
describe('#onTokenTransfer', async () => {
let homeBridge
Expand Down Expand Up @@ -744,6 +764,7 @@ contract('HomeBridge_ERC20_to_ERC20', async (accounts) => {
})
it('should trigger UserRequestForSignature with fee subtracted', async () => {
// Given
const homeBridge = await POSDAOHomeBridge.new()
const owner = accounts[0]
const user = accounts[4]
const validators = [accounts[1]]
Expand All @@ -757,7 +778,7 @@ contract('HomeBridge_ERC20_to_ERC20', async (accounts) => {
const homeFee = web3.toBigNumber(web3.toWei(0.001, "ether"))
const foreignFee = web3.toBigNumber(web3.toWei(0.001, "ether"))

await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, blockRewardContract.address, feeManager.address, homeFee, foreignFee).should.be.fulfilled;
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, feeManager.address, homeFee, foreignFee, blockRewardContract.address).should.be.fulfilled;
const value = halfEther
const finalValue = value.mul(web3.toBigNumber(1 - fee))
await token.mint(user, value, {from: owner}).should.be.fulfilled;
Expand All @@ -779,12 +800,12 @@ contract('HomeBridge_ERC20_to_ERC20', async (accounts) => {
token = await ERC677BridgeTokenRewardable.new("Some ERC20", "RSZT", 18);
rewardableValidators = await RewardableValidators.new()
feeManager = await FeeManagerErcToErcPOSDAO.new()
homeBridge = await HomeBridge.new()
homeBridge = await POSDAOHomeBridge.new()
fee = 0.001
homeFee = web3.toBigNumber(web3.toWei(fee, "ether"))
foreignFee = web3.toBigNumber(web3.toWei(fee, "ether"))
blockRewardContract = await BlockReward.new()
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, blockRewardContract.address, feeManager.address, homeFee, foreignFee).should.be.fulfilled;
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, feeManager.address, homeFee, foreignFee, blockRewardContract.address).should.be.fulfilled;
})
it('should distribute fee to one validator', async () => {
// Given
Expand Down Expand Up @@ -942,12 +963,12 @@ contract('HomeBridge_ERC20_to_ERC20', async (accounts) => {
token = await ERC677BridgeTokenRewardable.new("Some ERC20", "RSZT", 18);
rewardableValidators = await RewardableValidators.new()
feeManager = await FeeManagerErcToErcPOSDAO.new()
homeBridge = await HomeBridge.new()
homeBridge = await POSDAOHomeBridge.new()
fee = 0.001
homeFee = web3.toBigNumber(web3.toWei(fee, "ether"))
foreignFee = web3.toBigNumber(web3.toWei(fee, "ether"))
blockRewardContract = await BlockReward.new()
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, blockRewardContract.address, feeManager.address, homeFee, foreignFee).should.be.fulfilled;
await homeBridge.rewardableInitialize(rewardableValidators.address, oneEther, halfEther, minPerTx, gasPrice, requireBlockConfirmations, token.address, foreignDailyLimit, foreignMaxPerTx, owner, feeManager.address, homeFee, foreignFee, blockRewardContract.address).should.be.fulfilled;
})
it('should distribute fee to one validator', async () => {
// Given
Expand Down

0 comments on commit 8ab76b9

Please sign in to comment.