Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend ERC677BridgeToken contract for xDai-POA bridge #129

Merged
merged 7 commits into from
Jan 4, 2019
Merged
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
2 changes: 1 addition & 1 deletion contracts/ERC677BridgeToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ contract ERC677BridgeToken is
}

function isContract(address _addr)
private
internal
view
returns (bool)
{
Expand Down
66 changes: 66 additions & 0 deletions contracts/ERC677BridgeTokenRewardable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
pragma solidity 0.4.24;

import "./ERC677BridgeToken.sol";


contract ERC677BridgeTokenRewardable is ERC677BridgeToken {

address public blockRewardContract;
address public validatorSetContract;

constructor(
string _name,
string _symbol,
uint8 _decimals
) public ERC677BridgeToken(_name, _symbol, _decimals) {}

function setBlockRewardContract(address _blockRewardContract) onlyOwner public {
require(_blockRewardContract != address(0) && isContract(_blockRewardContract));
blockRewardContract = _blockRewardContract;
}

function setValidatorSetContract(address _validatorSetContract) onlyOwner public {
require(_validatorSetContract != address(0) && isContract(_validatorSetContract));
validatorSetContract = _validatorSetContract;
}

modifier onlyBlockRewardContract() {
require(msg.sender == blockRewardContract);
_;
}

modifier onlyValidatorSetContract() {
require(msg.sender == validatorSetContract);
_;
}

function mintReward(address[] _receivers, uint256[] _rewards) external onlyBlockRewardContract {
for (uint256 i = 0; i < _receivers.length; i++) {
address to = _receivers[i];
uint256 amount = _rewards[i];

// Mint `amount` for `to`
totalSupply_ = totalSupply_.add(amount);
akolotov marked this conversation as resolved.
Show resolved Hide resolved
balances[to] = balances[to].add(amount);
emit Mint(to, amount);
emit Transfer(address(0), to, amount);
}
}

function stake(address _staker, uint256 _amount) external onlyValidatorSetContract {
// Transfer `_amount` from `_staker` to `validatorSetContract`
require(_amount <= balances[_staker]);
balances[_staker] = balances[_staker].sub(_amount);
akolotov marked this conversation as resolved.
Show resolved Hide resolved
balances[validatorSetContract] = balances[validatorSetContract].add(_amount);
emit Transfer(_staker, validatorSetContract, _amount);
}

function withdraw(address _staker, uint256 _amount) external onlyValidatorSetContract {
// Transfer `_amount` from `validatorSetContract` to `_staker`
require(_amount <= balances[validatorSetContract]);
balances[validatorSetContract] = balances[validatorSetContract].sub(_amount);
akolotov marked this conversation as resolved.
Show resolved Hide resolved
balances[_staker] = balances[_staker].add(_amount);
emit Transfer(validatorSetContract, _staker, _amount);
}

}
6 changes: 6 additions & 0 deletions contracts/test/ValidatorSet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pragma solidity 0.4.24;


contract ValidatorSet {
constructor() {}
}
6 changes: 5 additions & 1 deletion deploy/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ HOME_MIN_AMOUNT_PER_TX=500000000000000000
HOME_REQUIRED_BLOCK_CONFIRMATIONS=1
HOME_GAS_PRICE=1000000000

#for bridge erc to native mode
#for bridge erc_to_native and native_to_erc mode
BLOCK_REWARD_ADDRESS=

FOREIGN_RPC_URL=https://sokol.poa.network
Expand All @@ -39,3 +39,7 @@ REQUIRED_NUMBER_OF_VALIDATORS=1
#If several validators are used, list them separated by space without quotes
#E.g. VALIDATORS=0x 0x 0x
VALIDATORS=0x

#for bridge native_to_erc mode
DEPLOY_REWARDABLE_TOKEN=false
DPOS_VALIDATOR_SET_ADDRESS=
10 changes: 10 additions & 0 deletions deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ REQUIRED_NUMBER_OF_VALIDATORS=1
# the Foreign network to confirm that the finalized agreement was transferred
# correctly to the Foreign network.
VALIDATORS=0x 0x 0x

# The flag defining whether to use ERC677BridgeTokenRewardable contract instead of
# ERC677BridgeToken.
DEPLOY_REWARDABLE_TOKEN=false
# The address of ValidatorSet contract used by ERC677BridgeTokenRewardable contract.
# Makes sense only when DEPLOY_REWARDABLE_TOKEN=true
DPOS_VALIDATOR_SET_ADDRESS=0x
# The address of BlockReward contract used by ERC677BridgeTokenRewardable contract.
# Makes sense only when DEPLOY_REWARDABLE_TOKEN=true
BLOCK_REWARD_ADDRESS=0x
```


Expand Down
5 changes: 4 additions & 1 deletion deploy/src/loadEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ if (BRIDGE_MODE === 'NATIVE_TO_ERC') {
BRIDGEABLE_TOKEN_DECIMALS: envalid.num(),
FOREIGN_DAILY_LIMIT: bigNumValidator(),
FOREIGN_MAX_AMOUNT_PER_TX: bigNumValidator(),
FOREIGN_MIN_AMOUNT_PER_TX: bigNumValidator()
FOREIGN_MIN_AMOUNT_PER_TX: bigNumValidator(),
DEPLOY_REWARDABLE_TOKEN: envalid.bool(),
DPOS_VALIDATOR_SET_ADDRESS: addressValidator(),
BLOCK_REWARD_ADDRESS: addressValidator()
}
}
if (BRIDGE_MODE === 'ERC_TO_ERC') {
Expand Down
38 changes: 36 additions & 2 deletions deploy/src/native_to_erc/foreign.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { deployContract, privateKeyToAddress, sendRawTxForeign } = require('../de
const { web3Foreign, deploymentPrivateKey, FOREIGN_RPC_URL } = require('../web3')

const ERC677BridgeToken = require('../../../build/contracts/ERC677BridgeToken.json')
const ERC677BridgeTokenRewardable = require('../../../build/contracts/ERC677BridgeTokenRewardable.json')
const EternalStorageProxy = require('../../../build/contracts/EternalStorageProxy.json')
const BridgeValidators = require('../../../build/contracts/BridgeValidators.json')
const ForeignBridge = require('../../../build/contracts/ForeignBridgeNativeToErc.json')
Expand All @@ -27,7 +28,10 @@ const {
BRIDGEABLE_TOKEN_SYMBOL,
BRIDGEABLE_TOKEN_DECIMALS,
HOME_DAILY_LIMIT,
HOME_MAX_AMOUNT_PER_TX
HOME_MAX_AMOUNT_PER_TX,
DEPLOY_REWARDABLE_TOKEN,
akolotov marked this conversation as resolved.
Show resolved Hide resolved
BLOCK_REWARD_ADDRESS,
DPOS_VALIDATOR_SET_ADDRESS
} = env

const DEPLOYMENT_ACCOUNT_ADDRESS = privateKeyToAddress(DEPLOYMENT_ACCOUNT_PRIVATE_KEY)
Expand All @@ -40,7 +44,7 @@ async function deployForeign() {

console.log('\n[Foreign] deploying BRIDGEABLE_TOKEN_SYMBOL token')
const erc677bridgeToken = await deployContract(
ERC677BridgeToken,
DEPLOY_REWARDABLE_TOKEN ? ERC677BridgeTokenRewardable : ERC677BridgeToken,
[BRIDGEABLE_TOKEN_NAME, BRIDGEABLE_TOKEN_SYMBOL, BRIDGEABLE_TOKEN_DECIMALS],
{ from: DEPLOYMENT_ACCOUNT_ADDRESS, network: 'foreign', nonce: foreignNonce }
)
Expand Down Expand Up @@ -212,6 +216,36 @@ async function deployForeign() {
assert.strictEqual(Web3Utils.hexToNumber(setBridgeContract.status), 1, 'Transaction Failed')
foreignNonce++

if (DEPLOY_REWARDABLE_TOKEN) {
console.log('\nset BlockReward contract on ERC677BridgeTokenRewardable')
akolotov marked this conversation as resolved.
Show resolved Hide resolved
const setBlockRewardContractData = await erc677bridgeToken.methods
.setBlockRewardContract(BLOCK_REWARD_ADDRESS)
.encodeABI({ from: DEPLOYMENT_ACCOUNT_ADDRESS })
const setBlockRewardContract = await sendRawTxForeign({
data: setBlockRewardContractData,
nonce: foreignNonce,
to: erc677bridgeToken.options.address,
privateKey: deploymentPrivateKey,
url: FOREIGN_RPC_URL
})
assert.equal(Web3Utils.hexToNumber(setBlockRewardContract.status), 1, 'Transaction Failed')
foreignNonce++

console.log('\nset ValidatorSet contract on ERC677BridgeTokenRewardable')
const setValidatorSetContractData = await erc677bridgeToken.methods
.setValidatorSetContract(DPOS_VALIDATOR_SET_ADDRESS)
.encodeABI({ from: DEPLOYMENT_ACCOUNT_ADDRESS })
const setValidatorSetContract = await sendRawTxForeign({
data: setValidatorSetContractData,
nonce: foreignNonce,
to: erc677bridgeToken.options.address,
privateKey: deploymentPrivateKey,
url: FOREIGN_RPC_URL
})
assert.equal(Web3Utils.hexToNumber(setValidatorSetContract.status), 1, 'Transaction Failed')
foreignNonce++
}

console.log('transferring ownership of ERC677BridgeToken token to foreignBridge contract')
const txOwnershipData = await erc677bridgeToken.methods
.transferOwnership(foreignBridgeStorage.options.address)
Expand Down
22 changes: 22 additions & 0 deletions test/mockContracts/ERC677BridgeTokenRewardableMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pragma solidity 0.4.24;

import '../../contracts/ERC677BridgeTokenRewardable.sol';


contract ERC677BridgeTokenRewardableMock is ERC677BridgeTokenRewardable {

constructor(
string _name,
string _symbol,
uint8 _decimals
) public ERC677BridgeTokenRewardable(_name, _symbol, _decimals) {}

function setBlockRewardContractMock(address _blockRewardContract) public {
blockRewardContract = _blockRewardContract;
}

function setValidatorSetContractMock(address _validatorSetContract) public {
validatorSetContract = _validatorSetContract;
}

}
Loading