Skip to content

Commit

Permalink
Merge f361374 into c8632cd
Browse files Browse the repository at this point in the history
  • Loading branch information
patitonar committed Apr 20, 2020
2 parents c8632cd + f361374 commit df67058
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 0 deletions.
Expand Up @@ -12,6 +12,10 @@ import "../Claimable.sol";
import "../VersionableBridge.sol";
import "../../libraries/Bytes.sol";

/**
* @title BasicAMBErc677ToErc677
* @dev Common functionality for erc677-to-erc677 mediator intended to work on top of AMB bridge.
*/
contract BasicAMBErc677ToErc677 is
Initializable,
Ownable,
Expand All @@ -23,6 +27,7 @@ contract BasicAMBErc677ToErc677 is
BaseERC677Bridge
{
event FailedMessageFixed(bytes32 indexed dataHash, address recipient, uint256 value);
event TokensBridged(address indexed recipient, uint256 value, bytes32 indexed messageId);

bytes32 internal constant BRIDGE_CONTRACT = 0x811bbb11e8899da471f0e69a3ed55090fc90215227fc5fb1cb0d6e962ea7b74f; // keccak256(abi.encodePacked("bridgeContract"))
bytes32 internal constant MEDIATOR_CONTRACT = 0x98aa806e31e94a687a31c65769cb99670064dd7f5a87526da075c5fb4eab9880; // keccak256(abi.encodePacked("mediatorContract"))
Expand Down
Expand Up @@ -2,10 +2,22 @@ pragma solidity 0.4.24;

import "./BasicAMBErc677ToErc677.sol";

/**
* @title ForeignAMBErc677ToErc677
* @dev Foreign side implementation for erc677-to-erc677 mediator intended to work on top of AMB bridge.
* It is designed to be used as an implementation contract of EternalStorageProxy contract.
*/
contract ForeignAMBErc677ToErc677 is BasicAMBErc677ToErc677 {
/**
* @dev Executes action on the request to withdraw tokens relayed from the other network
* @param _recipient address of tokens receiver
* @param _value amount of bridged tokens
*/
function executeActionOnBridgedTokens(address _recipient, uint256 _value) internal {
uint256 value = _value.div(10**decimalShift());
bytes32 txHash = transactionHash();
erc677token().transfer(_recipient, value);
emit TokensBridged(_recipient, value, txHash);
}

function bridgeSpecificActionsOnTokenTransfer(
Expand Down
Expand Up @@ -3,6 +3,11 @@ pragma solidity 0.4.24;
import "./BasicStakeTokenMediator.sol";
import "../../interfaces/IBurnableMintableERC677Token.sol";

/**
* @title ForeignStakeTokenMediator
* @dev Foreign side implementation for stake token mediator intended to work on top of AMB bridge.
* It is designed to be used as an implementation contract of EternalStorageProxy contract.
*/
contract ForeignStakeTokenMediator is BasicStakeTokenMediator {
/**
* @dev Executes action on the request to withdraw tokens relayed from the other network
Expand All @@ -11,7 +16,9 @@ contract ForeignStakeTokenMediator is BasicStakeTokenMediator {
*/
function executeActionOnBridgedTokens(address _recipient, uint256 _value) internal {
uint256 value = _value.div(10**decimalShift());
bytes32 txHash = transactionHash();
_transferWithOptionalMint(_recipient, value);
emit TokensBridged(_recipient, value, txHash);
}

/**
Expand Down
Expand Up @@ -3,10 +3,22 @@ pragma solidity 0.4.24;
import "./BasicAMBErc677ToErc677.sol";
import "../../interfaces/IBurnableMintableERC677Token.sol";

/**
* @title HomeAMBErc677ToErc677
* @dev Home side implementation for erc677-to-erc677 mediator intended to work on top of AMB bridge.
* It is designed to be used as an implementation contract of EternalStorageProxy contract.
*/
contract HomeAMBErc677ToErc677 is BasicAMBErc677ToErc677 {
/**
* @dev Executes action on the request to deposit tokens relayed from the other network
* @param _recipient address of tokens receiver
* @param _value amount of bridged tokens
*/
function executeActionOnBridgedTokens(address _recipient, uint256 _value) internal {
uint256 value = _value.mul(10**decimalShift());
bytes32 txHash = transactionHash();
IBurnableMintableERC677Token(erc677token()).mint(_recipient, value);
emit TokensBridged(_recipient, value, txHash);
}

function bridgeSpecificActionsOnTokenTransfer(ERC677 _token, address _from, uint256 _value, bytes _data) internal {
Expand Down
Expand Up @@ -6,6 +6,11 @@ import "../BlockRewardBridge.sol";
import "./HomeStakeTokenFeeManager.sol";
import "../../interfaces/IBurnableMintableERC677Token.sol";

/**
* @title HomeStakeTokenMediator
* @dev Home side implementation for stake token mediator intended to work on top of AMB bridge.
* It is designed to be used as an implementation contract of EternalStorageProxy contract.
*/
contract HomeStakeTokenMediator is BasicStakeTokenMediator, HomeStakeTokenFeeManager {
bytes32 internal constant MINT_HANDLER = 0x8a8236f871f2bbb44f59e8c68b82f7587d19c987e09aba39148cc97ea004a32e; // keccak256(abi.encodePacked("mintHandler"))

Expand Down Expand Up @@ -119,7 +124,9 @@ contract HomeStakeTokenMediator is BasicStakeTokenMediator, HomeStakeTokenFeeMan
*/
function executeActionOnBridgedTokens(address _recipient, uint256 _value) internal {
uint256 value = _value.mul(10**decimalShift());
bytes32 txHash = transactionHash();
getMintHandler().mint(_recipient, value);
emit TokensBridged(_recipient, value, txHash);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/amb_erc677_to_erc677/foreign_bridge.test.js
Expand Up @@ -177,6 +177,12 @@ contract('ForeignAMBErc677ToErc677', async accounts => {
expect(await foreignBridge.totalExecutedPerDay(currentDay)).to.be.bignumber.equal(oneEther)
expect(await erc677Token.balanceOf(foreignBridge.address)).to.be.bignumber.equal(oneEther)
expect(await erc677Token.balanceOf(user)).to.be.bignumber.equal(oneEther)

const TokensBridgedEvent = await getEvents(foreignBridge, { event: 'TokensBridged' })
expect(TokensBridgedEvent.length).to.be.equal(1)
expect(TokensBridgedEvent[0].returnValues.recipient).to.be.equal(user)
expect(TokensBridgedEvent[0].returnValues.value).to.be.equal(oneEther.toString())
expect(TokensBridgedEvent[0].returnValues.messageId).to.be.equal(exampleTxHash)
})
it('should transfer locked tokens on message from amb with decimal shift of two', async () => {
// Given
Expand Down Expand Up @@ -232,6 +238,12 @@ contract('ForeignAMBErc677ToErc677', async accounts => {
expect(await foreignBridge.totalExecutedPerDay(currentDay)).to.be.bignumber.equal(valueOnHome)
expect(await erc677Token.balanceOf(foreignBridge.address)).to.be.bignumber.equal(twoEthers.sub(valueOnForeign))
expect(await erc677Token.balanceOf(user)).to.be.bignumber.equal(valueOnForeign)

const TokensBridgedEvent = await getEvents(foreignBridge, { event: 'TokensBridged' })
expect(TokensBridgedEvent.length).to.be.equal(1)
expect(TokensBridgedEvent[0].returnValues.recipient).to.be.equal(user)
expect(TokensBridgedEvent[0].returnValues.value).to.be.equal(valueOnForeign.toString())
expect(TokensBridgedEvent[0].returnValues.messageId).to.be.equal(exampleTxHash)
})
it('should emit AmountLimitExceeded and not transfer tokens when out of execution limits', async () => {
// Given
Expand Down Expand Up @@ -268,6 +280,9 @@ contract('ForeignAMBErc677ToErc677', async accounts => {
expect(outOfLimitEvent[0].returnValues.recipient).to.be.equal(user)
expect(outOfLimitEvent[0].returnValues.value).to.be.equal(twoEthers.toString())
expect(outOfLimitEvent[0].returnValues.transactionHash).to.be.equal(exampleTxHash)

const TokensBridgedEvent = await getEvents(foreignBridge, { event: 'TokensBridged' })
expect(TokensBridgedEvent.length).to.be.equal(0)
})
})
})
15 changes: 15 additions & 0 deletions test/amb_erc677_to_erc677/home_bridge.test.js
Expand Up @@ -183,6 +183,12 @@ contract('HomeAMBErc677ToErc677', async accounts => {
expect(events[0].returnValues.amount).to.be.equal(oneEther.toString())
expect(await erc677Token.totalSupply()).to.be.bignumber.equal(oneEther)
expect(await erc677Token.balanceOf(user)).to.be.bignumber.equal(oneEther)

const TokensBridgedEvent = await getEvents(homeBridge, { event: 'TokensBridged' })
expect(TokensBridgedEvent.length).to.be.equal(1)
expect(TokensBridgedEvent[0].returnValues.recipient).to.be.equal(user)
expect(TokensBridgedEvent[0].returnValues.value).to.be.equal(oneEther.toString())
expect(TokensBridgedEvent[0].returnValues.messageId).to.be.equal(exampleTxHash)
})
it('should mint tokens on message from amb with decimal shift of two', async () => {
// Given
Expand Down Expand Up @@ -241,6 +247,12 @@ contract('HomeAMBErc677ToErc677', async accounts => {
expect(events[0].returnValues.amount).to.be.equal(valueOnHome.toString())
expect(await erc677Token.totalSupply()).to.be.bignumber.equal(valueOnHome)
expect(await erc677Token.balanceOf(user)).to.be.bignumber.equal(valueOnHome)

const TokensBridgedEvent = await getEvents(homeBridge, { event: 'TokensBridged' })
expect(TokensBridgedEvent.length).to.be.equal(1)
expect(TokensBridgedEvent[0].returnValues.recipient).to.be.equal(user)
expect(TokensBridgedEvent[0].returnValues.value).to.be.equal(valueOnHome.toString())
expect(TokensBridgedEvent[0].returnValues.messageId).to.be.equal(exampleTxHash)
})
it('should emit AmountLimitExceeded and not mint tokens when out of execution limits', async () => {
// Given
Expand Down Expand Up @@ -278,6 +290,9 @@ contract('HomeAMBErc677ToErc677', async accounts => {
expect(outOfLimitEvent[0].returnValues.recipient).to.be.equal(user)
expect(outOfLimitEvent[0].returnValues.value).to.be.equal(twoEthers.toString())
expect(outOfLimitEvent[0].returnValues.transactionHash).to.be.equal(exampleTxHash)

const TokensBridgedEvent = await getEvents(homeBridge, { event: 'TokensBridged' })
expect(TokensBridgedEvent.length).to.be.equal(0)
})
})
})
24 changes: 24 additions & 0 deletions test/stake_token_mediators/foreign_mediator.test.js
Expand Up @@ -78,6 +78,12 @@ contract('ForeignStakeTokenMediator', async accounts => {
expect(await token.totalSupply()).to.be.bignumber.equal(twoEthers)
expect(await token.balanceOf(user)).to.be.bignumber.equal(halfEther)
expect(await token.balanceOf(foreignMediator.address)).to.be.bignumber.equal(twoEthers.sub(halfEther))

const events = await getEvents(foreignMediator, { event: 'TokensBridged' })
expect(events.length).to.be.equal(1)
expect(events[0].returnValues.recipient).to.be.equal(user)
expect(events[0].returnValues.value).to.be.equal(halfEther.toString())
expect(events[0].returnValues.messageId).to.be.equal(exampleTxHash)
})

it('should use all tokens from bridge balance', async () => {
Expand All @@ -100,6 +106,12 @@ contract('ForeignStakeTokenMediator', async accounts => {
expect(await token.totalSupply()).to.be.bignumber.equal(halfEther)
expect(await token.balanceOf(user)).to.be.bignumber.equal(halfEther)
expect(await token.balanceOf(foreignMediator.address)).to.be.bignumber.equal(ZERO)

const events = await getEvents(foreignMediator, { event: 'TokensBridged' })
expect(events.length).to.be.equal(1)
expect(events[0].returnValues.recipient).to.be.equal(user)
expect(events[0].returnValues.value).to.be.equal(halfEther.toString())
expect(events[0].returnValues.messageId).to.be.equal(exampleTxHash)
})

it('should mint lacking tokens', async () => {
Expand All @@ -122,6 +134,12 @@ contract('ForeignStakeTokenMediator', async accounts => {
expect(await token.totalSupply()).to.be.bignumber.equal(ether('0.6'))
expect(await token.balanceOf(user)).to.be.bignumber.equal(ether('0.6'))
expect(await token.balanceOf(foreignMediator.address)).to.be.bignumber.equal(ZERO)

const events = await getEvents(foreignMediator, { event: 'TokensBridged' })
expect(events.length).to.be.equal(1)
expect(events[0].returnValues.recipient).to.be.equal(user)
expect(events[0].returnValues.value).to.be.equal(ether('0.6').toString())
expect(events[0].returnValues.messageId).to.be.equal(exampleTxHash)
})

it('should mint lacking tokens, zero initial balance', async () => {
Expand All @@ -143,6 +161,12 @@ contract('ForeignStakeTokenMediator', async accounts => {
expect(await token.totalSupply()).to.be.bignumber.equal(halfEther)
expect(await token.balanceOf(user)).to.be.bignumber.equal(halfEther)
expect(await token.balanceOf(foreignMediator.address)).to.be.bignumber.equal(ZERO)

const events = await getEvents(foreignMediator, { event: 'TokensBridged' })
expect(events.length).to.be.equal(1)
expect(events[0].returnValues.recipient).to.be.equal(user)
expect(events[0].returnValues.value).to.be.equal(halfEther.toString())
expect(events[0].returnValues.messageId).to.be.equal(exampleTxHash)
})
})

Expand Down
6 changes: 6 additions & 0 deletions test/stake_token_mediators/home_mediator.test.js
Expand Up @@ -301,6 +301,12 @@ contract('HomeStakeTokenMediator', async accounts => {
expect(await token.totalSupply()).to.be.bignumber.equal(halfEther)
expect(await token.balanceOf(user)).to.be.bignumber.equal(halfEther)
expect(await token.balanceOf(homeMediator.address)).to.be.bignumber.equal(ZERO)

const events = await getEvents(homeMediator, { event: 'TokensBridged' })
expect(events.length).to.be.equal(1)
expect(events[0].returnValues.recipient).to.be.equal(user)
expect(events[0].returnValues.value).to.be.equal(halfEther.toString())
expect(events[0].returnValues.messageId).to.be.equal(exampleTxHash)
})
})

Expand Down

0 comments on commit df67058

Please sign in to comment.