Skip to content

Commit

Permalink
Add handleBridgedTokens unit tests for amb erc677 to erc677
Browse files Browse the repository at this point in the history
  • Loading branch information
patitonar committed Aug 16, 2019
1 parent 3cdbcf8 commit 66e8bbe
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 8 deletions.
12 changes: 4 additions & 8 deletions test/amb_erc677_to_erc677/AMBErc677ToErc677Behavior.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const ERC677BridgeToken = artifacts.require('ERC677BridgeToken.sol')
const HomeAMB = artifacts.require('HomeAMB.sol')
const BridgeValidators = artifacts.require('BridgeValidators.sol')
const AMBMock = artifacts.require('AMBMock.sol')

const { expect } = require('chai')
const { ZERO_ADDRESS, toBN, ERROR_MSG } = require('../setup')
Expand All @@ -21,11 +20,8 @@ function shouldBehaveLikeBasicAMBErc677ToErc677(otherSideMediatorContract, accou
let mediatorContract
let erc677Token
beforeEach(async () => {
const validatorContract = await BridgeValidators.new()
const authorities = [accounts[1], accounts[2]]
await validatorContract.initialize(1, authorities, owner)
bridgeContract = await HomeAMB.new()
await bridgeContract.initialize(validatorContract.address, maxGasPerTx, '1', '1', owner)
bridgeContract = await AMBMock.new()
await bridgeContract.setMaxGasPerTx(maxGasPerTx)
mediatorContract = await otherSideMediatorContract.new()
erc677Token = await ERC677BridgeToken.new('test', 'TST', 18)
})
Expand Down Expand Up @@ -221,7 +217,7 @@ function shouldBehaveLikeBasicAMBErc677ToErc677(otherSideMediatorContract, accou

expect(await contract.bridgeContract()).to.be.equal(bridgeContract.address)

const newBridgeContract = await HomeAMB.new()
const newBridgeContract = await AMBMock.new()

await contract.setBridgeContract(newBridgeContract.address, { from: user }).should.be.rejectedWith(ERROR_MSG)
await contract.setBridgeContract(notAContractAddress, { from: owner }).should.be.rejectedWith(ERROR_MSG)
Expand Down
60 changes: 60 additions & 0 deletions test/amb_erc677_to_erc677/foreign_bridge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const HomeAMBErc677ToErc677 = artifacts.require('HomeAMBErc677ToErc677.sol')
const ERC677BridgeToken = artifacts.require('ERC677BridgeToken.sol')
const ForeignAMB = artifacts.require('ForeignAMB.sol')
const BridgeValidators = artifacts.require('BridgeValidators.sol')
const AMBMock = artifacts.require('AMBMock.sol')

const { expect } = require('chai')
const { shouldBehaveLikeBasicAMBErc677ToErc677 } = require('./AMBErc677ToErc677Behavior.test')
Expand Down Expand Up @@ -81,4 +82,63 @@ contract('ForeignAMBErc677ToErc677', async accounts => {
expect(await foreignBridge.totalSpentPerDay(currentDay)).to.be.bignumber.equal(halfEther)
})
})
describe('handleBridgedTokens', () => {
beforeEach(async () => {
ambBridgeContract = await AMBMock.new()
await ambBridgeContract.setMaxGasPerTx(maxGasPerTx)
mediatorContract = await HomeAMBErc677ToErc677.new()
erc677Token = await ERC677BridgeToken.new('test', 'TST', 18)

foreignBridge = await ForeignAMBErc677ToErc677.new()
await foreignBridge.initialize(
ambBridgeContract.address,
mediatorContract.address,
erc677Token.address,
dailyLimit,
maxPerTx,
minPerTx,
executionDailyLimit,
executionMaxPerTx,
maxGasPerTx,
owner
).should.be.fulfilled
await erc677Token.mint(foreignBridge.address, twoEthers, { from: owner }).should.be.fulfilled
await erc677Token.transferOwnership(foreignBridge.address)
})
it('should transfer locked tokens on message from amb', async () => {
// Given
const currentDay = await foreignBridge.getCurrentDay()
expect(await foreignBridge.totalExecutedPerDay(currentDay)).to.be.bignumber.equal(ZERO)
const initialEvents = await getEvents(erc677Token, { event: 'Transfer' })
expect(initialEvents.length).to.be.equal(0)
expect(await erc677Token.balanceOf(foreignBridge.address)).to.be.bignumber.equal(twoEthers)
expect(await erc677Token.balanceOf(user)).to.be.bignumber.equal(ZERO)

// can't be called by user
await foreignBridge.handleBridgedTokens(user, oneEther, { from: user }).should.be.rejectedWith(ERROR_MSG)
// can't be called by owner
await foreignBridge.handleBridgedTokens(user, oneEther, { from: owner }).should.be.rejectedWith(ERROR_MSG)

const data = await foreignBridge.contract.methods.handleBridgedTokens(user, oneEther.toString()).encodeABI()
const outOfLimitValueData = await foreignBridge.contract.methods
.handleBridgedTokens(user, twoEthers.toString())
.encodeABI()

// message must be generated by mediator contract on the other network
await ambBridgeContract.executeMessageCall(foreignBridge.address, owner, data).should.be.rejectedWith(ERROR_MSG)

// out of limit value
await ambBridgeContract
.executeMessageCall(foreignBridge.address, mediatorContract.address, outOfLimitValueData)
.should.be.rejectedWith(ERROR_MSG)

await ambBridgeContract.executeMessageCall(foreignBridge.address, mediatorContract.address, data).should.be
.fulfilled

// Then
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)
})
})
})
61 changes: 61 additions & 0 deletions test/amb_erc677_to_erc677/home_bridge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const HomeAMBErc677ToErc677 = artifacts.require('HomeAMBErc677ToErc677.sol')
const ForeignAMBErc677ToErc677 = artifacts.require('ForeignAMBErc677ToErc677.sol')
const ERC677BridgeToken = artifacts.require('ERC677BridgeToken.sol')
const HomeAMB = artifacts.require('HomeAMB.sol')
const AMBMock = artifacts.require('AMBMock.sol')
const BridgeValidators = artifacts.require('BridgeValidators.sol')

const { expect } = require('chai')
Expand Down Expand Up @@ -88,4 +89,64 @@ contract('HomeAMBErc677ToErc677', async accounts => {
})
})
})
describe('handleBridgedTokens', () => {
beforeEach(async () => {
ambBridgeContract = await AMBMock.new()
await ambBridgeContract.setMaxGasPerTx(maxGasPerTx)
mediatorContract = await ForeignAMBErc677ToErc677.new()
erc677Token = await ERC677BridgeToken.new('test', 'TST', 18)

homeBridge = await HomeAMBErc677ToErc677.new()
await homeBridge.initialize(
ambBridgeContract.address,
mediatorContract.address,
erc677Token.address,
dailyLimit,
maxPerTx,
minPerTx,
executionDailyLimit,
executionMaxPerTx,
maxGasPerTx,
owner
).should.be.fulfilled
await erc677Token.transferOwnership(homeBridge.address)
})
it('should mint tokens on message from amb', async () => {
// Given
const currentDay = await homeBridge.getCurrentDay()
expect(await homeBridge.totalExecutedPerDay(currentDay)).to.be.bignumber.equal(ZERO)
const initialEvents = await getEvents(erc677Token, { event: 'Mint' })
expect(initialEvents.length).to.be.equal(0)
expect(await erc677Token.totalSupply()).to.be.bignumber.equal(ZERO)

// can't be called by user
await homeBridge.handleBridgedTokens(user, oneEther, { from: user }).should.be.rejectedWith(ERROR_MSG)
// can't be called by owner
await homeBridge.handleBridgedTokens(user, oneEther, { from: owner }).should.be.rejectedWith(ERROR_MSG)

const data = await homeBridge.contract.methods.handleBridgedTokens(user, oneEther.toString()).encodeABI()
const outOfLimitValueData = await homeBridge.contract.methods
.handleBridgedTokens(user, twoEthers.toString())
.encodeABI()

// message must be generated by mediator contract on the other network
await ambBridgeContract.executeMessageCall(homeBridge.address, owner, data).should.be.rejectedWith(ERROR_MSG)

// out of limit value
await ambBridgeContract
.executeMessageCall(homeBridge.address, mediatorContract.address, outOfLimitValueData)
.should.be.rejectedWith(ERROR_MSG)

await ambBridgeContract.executeMessageCall(homeBridge.address, mediatorContract.address, data).should.be.fulfilled

// Then
expect(await homeBridge.totalExecutedPerDay(currentDay)).to.be.bignumber.equal(oneEther)
const events = await getEvents(erc677Token, { event: 'Mint' })
expect(events.length).to.be.equal(1)
expect(events[0].returnValues.to).to.be.equal(user)
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)
})
})
})

0 comments on commit 66e8bbe

Please sign in to comment.