Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
creotiv committed Oct 8, 2021
1 parent 55af719 commit 49b89c6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pids
*.seed
*.pid.lock

# Bindings for contracts
bindings

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ services:
- 8545
command: >
--mnemonic "amused glory pen avocado toilet dragon entry kitchen cliff retreat canyon danger"
--port 8545
37 changes: 12 additions & 25 deletions test/hermes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Smart-contract code can be found in `contracts/HermesImplementation.sol`.
*/

const { BN, expectEvent } = require('@openzeppelin/test-helpers')
const { BN } = require('@openzeppelin/test-helpers')
const {
generateChannelId,
topUpTokens,
Expand All @@ -17,6 +17,9 @@ const {
signIdentityRegistration,
generatePromise
} = require('./utils/client.js')
const {
assertEvent
} = require('./utils/tests.js')

const MystToken = artifacts.require("TestMystToken")
const Registry = artifacts.require("Registry")
Expand Down Expand Up @@ -105,9 +108,7 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
promise = generatePromise(amountToPay, Zero, channelState, operator, identityA.address)
var res = await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})
assertEvent(res, 'PromiseSettled',{"lock":"0x"+promise.lock.toString('hex')})

const balanceAfter = await token.balanceOf(beneficiaryA)
balanceAfter.should.be.bignumber.equal(balanceBefore.add(amountToPay))
Expand Down Expand Up @@ -160,9 +161,7 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
promise = generatePromise(amountToPay, new BN(0), channelState, operator)
var res = await hermes.settlePromise(identityB.address, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})
assertEvent(res, 'PromiseSettled',{"lock":"0x"+promise.lock.toString('hex')})

const balanceAfter = await token.balanceOf(beneficiaryB)
balanceAfter.should.be.bignumber.equal(balanceBefore.add(amountToPay))
Expand Down Expand Up @@ -215,9 +214,7 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
const promise = generatePromise(amountToPay, fee, channelState, operator)
var res =await hermes.settlePromise(identityC.address, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})
assertEvent(res, 'PromiseSettled',{"lock":"0x"+promise.lock.toString('hex')})

const beneficiaryBalanceAfter = await token.balanceOf(beneficiaryC)
beneficiaryBalanceAfter.should.be.bignumber.equal(beneficiaryBalanceBefore.add(amountToPay))
Expand All @@ -239,9 +236,7 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
promise = generatePromise(amountToPay, fee, channelState, operator, identityC.address)
var res =await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})
assertEvent(res, 'PromiseSettled',{"lock":"0x"+promise.lock.toString('hex')})

const beneficiaryBalance = await token.balanceOf(beneficiaryC)
beneficiaryBalance.should.be.bignumber.equal(initialBeneficiaryBalance.add(maxStake)) // there is not possible to settle more than maxStake in one tx
Expand All @@ -253,9 +248,7 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
// Settle previous promise to get rest of promised coins
var res =await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})
assertEvent(res, 'PromiseSettled',{"lock":"0x"+promise.lock.toString('hex')})

const beneficiaryBalance = await token.balanceOf(beneficiaryC)
beneficiaryBalance.should.be.bignumber.equal(initialBeneficiaryBalance.add(new BN('1000'))) // 1000 should be left after previous promise
Expand All @@ -275,9 +268,7 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
promise = generatePromise(amountToPay, new BN(0), channelState, operator, identityC.address)
var res =await hermes.settleWithDEX(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})
assertEvent(res, 'PromiseSettled',{"lock":"0x"+promise.lock.toString('hex')})

const balanceAfter = new BN(await web3.eth.getBalance(beneficiaryC))
balanceAfter.should.be.bignumber.equal(balanceBefore.add(expectedETHAmount))
Expand Down Expand Up @@ -360,9 +351,7 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
const promise = generatePromise(amountToLend, Zero, channelState, operator, identityD.address)
var res = await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})
assertEvent(res, 'PromiseSettled',{"lock":"0x"+promise.lock.toString('hex')})

// Ensure that amountToLend is bigger than stake + locked in channels funds
let minimalExpectedBalance = await hermes.minimalExpectedBalance()
Expand Down Expand Up @@ -412,9 +401,7 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
const promise = generatePromise(new BN(700), Zero, channelState, operator)
var res = await hermes.settlePromise(identityC.address, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})
assertEvent(res, 'PromiseSettled',{"lock":"0x"+promise.lock.toString('hex')})

// Withdraw request should be rejected and no funds moved
const initialBalance = await token.balanceOf(hermes.address)
Expand Down
24 changes: 10 additions & 14 deletions test/hermesChannelSettlement.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Tested functions can be found in smart-contract code at `contracts/HermesImplementation.sol`.
*/

const { BN, expectEvent } = require('@openzeppelin/test-helpers')
const { BN } = require('@openzeppelin/test-helpers')
const { randomBytes } = require('crypto')
const {
generateChannelId,
Expand All @@ -17,6 +17,10 @@ const {
signChannelBeneficiaryChange,
generatePromise
} = require('./utils/client.js')
const {
assertEvent
} = require('./utils/tests.js')
const { expect } = require('chai')

const MystToken = artifacts.require("TestMystToken")
const HermesImplementation = artifacts.require("TestHermesImplementation")
Expand Down Expand Up @@ -90,10 +94,8 @@ contract("Channel openinig via settlement tests", ([txMaker, beneficiaryA, benef
const promise = generatePromise(amountToPay, Zero, channelState, operator, providerA.address)
var res = await hermes.settleWithBeneficiary(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature, beneficiaryA, beneficiaryChangeSignature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

assertEvent(res, 'PromiseSettled',{"lock":"0x"+promise.lock.toString('hex')})

const balanceAfter = await token.balanceOf(beneficiaryA)
balanceAfter.should.be.bignumber.equal(balanceBefore.add(amountToPay))

Expand All @@ -116,9 +118,7 @@ contract("Channel openinig via settlement tests", ([txMaker, beneficiaryA, benef
const promise = generatePromise(amountToPay, Zero, channelState, operator, providerA.address)
var res =await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})
assertEvent(res, 'PromiseSettled',{"lock":"0x"+promise.lock.toString('hex')})

// Promise can settle even more than its stake (up to maxStake)
const balanceAfter = await token.balanceOf(beneficiaryA)
Expand All @@ -142,9 +142,7 @@ contract("Channel openinig via settlement tests", ([txMaker, beneficiaryA, benef

let res = await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})
assertEvent(res, 'PromiseSettled',{"lock":"0x"+promise.lock.toString('hex')})

const balanceAfter = await token.balanceOf(beneficiaryA)
balanceAfter.should.be.bignumber.equal(balanceBefore.add(maxStake))
Expand All @@ -166,9 +164,7 @@ contract("Channel openinig via settlement tests", ([txMaker, beneficiaryA, benef
const promise = generatePromise(amountToPay, transactorFee, channelState, operator, providerA.address)
var res = await hermes.settleIntoStake(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})
assertEvent(res, 'PromiseSettled',{"lock":"0x"+promise.lock.toString('hex')})

// It should have increased stake
const channelStakeAfter = (await hermes.channels(channelId)).stake
Expand Down
21 changes: 21 additions & 0 deletions test/utils/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { expect } = require('chai')

const assertEvent = function(response, eventName, kwargs) {
let event = {}
for(let i=0; i<response.logs.length;i++) {
if (response.logs[i].event === eventName) {
event = response.logs[i];
break
}
}
expect(event.event).to.be.equal(eventName)
if (kwargs) {
for(let k in kwargs) {
expect(event.args[k]).to.be.equal(kwargs[k])
}
}
}

module.exports = {
assertEvent
}

0 comments on commit 49b89c6

Please sign in to comment.