Skip to content

Commit

Permalink
Refactor claimTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
patitonar committed Jun 28, 2019
1 parent 5fafafb commit f87c1af
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
9 changes: 5 additions & 4 deletions contracts/ERC677BridgeToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import "openzeppelin-solidity/contracts/token/ERC20/MintableToken.sol";
import "openzeppelin-solidity/contracts/token/ERC20/DetailedERC20.sol";
import "./IBurnableMintableERC677Token.sol";
import "./ERC677Receiver.sol";
import "./libraries/Token.sol";
import "./upgradeable_contracts/Claimable.sol";


contract ERC677BridgeToken is
IBurnableMintableERC677Token,
DetailedERC20,
BurnableToken,
MintableToken {
MintableToken,
Claimable {

address public bridgeContract;

Expand Down Expand Up @@ -93,7 +94,7 @@ contract ERC677BridgeToken is
revert();
}

function claimTokens(address _token, address _to) public onlyOwner {
Token.claimTokens(_token, _to);
function claimTokens(address _token, address _to) public onlyOwner validAddress(_to) {
claimValues(_token, _to);
}
}
8 changes: 4 additions & 4 deletions contracts/upgradeable_contracts/BasicBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import "../upgradeability/EternalStorage.sol";
import "../libraries/SafeMath.sol";
import "./Validatable.sol";
import "./Ownable.sol";
import "../libraries/Token.sol";
import "./Claimable.sol";


contract BasicBridge is EternalStorage, Validatable, Ownable, OwnedUpgradeability {
contract BasicBridge is EternalStorage, Validatable, Ownable, OwnedUpgradeability, Claimable {
using SafeMath for uint256;

event GasPriceChanged(uint256 gasPrice);
Expand Down Expand Up @@ -127,8 +127,8 @@ contract BasicBridge is EternalStorage, Validatable, Ownable, OwnedUpgradeabilit
return executionDailyLimit() >= nextLimit && _amount <= executionMaxPerTx();
}

function claimTokens(address _token, address _to) public onlyIfOwnerOfProxy {
Token.claimTokens(_token, _to);
function claimTokens(address _token, address _to) public onlyIfOwnerOfProxy validAddress(_to) {
claimValues(_token, _to);
}

function isContract(address _addr) internal view returns (bool)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
pragma solidity 0.4.24;

import "openzeppelin-solidity/contracts/token/ERC20/ERC20Basic.sol";
import "./Sacrifice.sol";

library Token {
contract Claimable {

function claimTokens(address _token, address _to) internal {
modifier validAddress(address _to) {
require(_to != address(0));
_;
}

function claimValues(address _token, address _to) internal {
if (_token == address(0)) {
_to.transfer(address(this).balance);
return;
claimNativeCoins(_to);
} else {
claimErc20Tokens(_token, _to);
}
}

function claimNativeCoins(address _to) internal {
uint256 value = address(this).balance;
if (!_to.send(value)) {
(new Sacrifice).value(value)(_to);
}
}

function claimErc20Tokens(address _token, address _to) internal {
ERC20Basic token = ERC20Basic(_token);
uint256 balance = token.balanceOf(this);
safeTransfer(_token, _to, balance);
Expand Down
27 changes: 27 additions & 0 deletions test/native_to_erc/home_bridge_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,33 @@ contract('HomeBridge', async accounts => {
expect(await tokenMock.balanceOf(homeBridge.address)).to.be.bignumber.equal(ZERO)
expect(await tokenMock.balanceOf(accounts[3])).to.be.bignumber.equal(halfEther)
})
it('should work for native coins', async () => {
const storageProxy = await EternalStorageProxy.new()
const data = homeContract.contract.methods
.initialize(
validatorContract.address,
oneEther.toString(),
halfEther.toString(),
'1',
gasPrice,
requireBlockConfirmations,
oneEther.toString(),
halfEther.toString(),
owner
)
.encodeABI()
await storageProxy.upgradeToAndCall('1', homeContract.address, data).should.be.fulfilled
const homeBridge = await HomeBridge.at(storageProxy.address)

const balanceBefore = toBN(await web3.eth.getBalance(accounts[3]))

await homeBridge.sendTransaction({ from: accounts[2], value: halfEther }).should.be.fulfilled
expect(toBN(await web3.eth.getBalance(homeBridge.address))).to.be.bignumber.equal(halfEther)

await homeBridge.claimTokens(ZERO_ADDRESS, accounts[3], { from: owner }).should.be.fulfilled
expect(toBN(await web3.eth.getBalance(homeBridge.address))).to.be.bignumber.equal(ZERO)
expect(toBN(await web3.eth.getBalance(accounts[3]))).to.be.bignumber.equal(balanceBefore.add(halfEther))
})
})

describe('#rewardableInitialize', async () => {
Expand Down

0 comments on commit f87c1af

Please sign in to comment.