Moved Escrows into an escrow subdirectory, improved docs. (#1430)

* Moved Escrows into an escrow subdirectory, improved docs.

* Fixed escrow mock.

* Fixed some more imports.
This commit is contained in:
Nicolás Venturo
2018-10-17 17:22:25 -03:00
committed by GitHub
parent cfef58361f
commit f3df2dab3d
10 changed files with 35 additions and 24 deletions

View File

@ -0,0 +1,40 @@
const { shouldBehaveLikeEscrow } = require('./Escrow.behavior');
const shouldFail = require('../../helpers/shouldFail');
const { ether } = require('../../helpers/ether');
const BigNumber = web3.BigNumber;
require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();
const ConditionalEscrowMock = artifacts.require('ConditionalEscrowMock');
contract('ConditionalEscrow', function ([_, owner, payee, ...otherAccounts]) {
beforeEach(async function () {
this.escrow = await ConditionalEscrowMock.new({ from: owner });
});
context('when withdrawal is allowed', function () {
beforeEach(async function () {
await Promise.all(otherAccounts.map(payee => this.escrow.setAllowed(payee, true)));
});
shouldBehaveLikeEscrow(owner, otherAccounts);
});
context('when withdrawal is disallowed', function () {
const amount = ether(23.0);
beforeEach(async function () {
await this.escrow.setAllowed(payee, false);
});
it('reverts on withdrawals', async function () {
await this.escrow.deposit(payee, { from: owner, value: amount });
await shouldFail.reverting(this.escrow.withdraw(payee, { from: owner }));
});
});
});

View File

@ -0,0 +1,99 @@
const expectEvent = require('../../helpers/expectEvent');
const shouldFail = require('../../helpers/shouldFail');
const { ethGetBalance } = require('../../helpers/web3');
const { ether } = require('../../helpers/ether');
const BigNumber = web3.BigNumber;
require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();
function shouldBehaveLikeEscrow (primary, [payee1, payee2]) {
const amount = ether(42.0);
describe('as an escrow', function () {
describe('deposits', function () {
it('can accept a single deposit', async function () {
await this.escrow.deposit(payee1, { from: primary, value: amount });
(await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount);
(await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount);
});
it('can accept an empty deposit', async function () {
await this.escrow.deposit(payee1, { from: primary, value: 0 });
});
it('only the primary account can deposit', async function () {
await shouldFail.reverting(this.escrow.deposit(payee1, { from: payee2 }));
});
it('emits a deposited event', async function () {
const { logs } = await this.escrow.deposit(payee1, { from: primary, value: amount });
expectEvent.inLogs(logs, 'Deposited', {
payee: payee1,
weiAmount: amount,
});
});
it('can add multiple deposits on a single account', async function () {
await this.escrow.deposit(payee1, { from: primary, value: amount });
await this.escrow.deposit(payee1, { from: primary, value: amount * 2 });
(await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount * 3);
(await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount * 3);
});
it('can track deposits to multiple accounts', async function () {
await this.escrow.deposit(payee1, { from: primary, value: amount });
await this.escrow.deposit(payee2, { from: primary, value: amount * 2 });
(await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(amount * 3);
(await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(amount);
(await this.escrow.depositsOf(payee2)).should.be.bignumber.equal(amount * 2);
});
});
describe('withdrawals', async function () {
it('can withdraw payments', async function () {
const payeeInitialBalance = await ethGetBalance(payee1);
await this.escrow.deposit(payee1, { from: primary, value: amount });
await this.escrow.withdraw(payee1, { from: primary });
(await ethGetBalance(this.escrow.address)).should.be.bignumber.equal(0);
(await this.escrow.depositsOf(payee1)).should.be.bignumber.equal(0);
const payeeFinalBalance = await ethGetBalance(payee1);
payeeFinalBalance.sub(payeeInitialBalance).should.be.bignumber.equal(amount);
});
it('can do an empty withdrawal', async function () {
await this.escrow.withdraw(payee1, { from: primary });
});
it('only the primary account can withdraw', async function () {
await shouldFail.reverting(this.escrow.withdraw(payee1, { from: payee1 }));
});
it('emits a withdrawn event', async function () {
await this.escrow.deposit(payee1, { from: primary, value: amount });
const { logs } = await this.escrow.withdraw(payee1, { from: primary });
expectEvent.inLogs(logs, 'Withdrawn', {
payee: payee1,
weiAmount: amount,
});
});
});
});
}
module.exports = {
shouldBehaveLikeEscrow,
};

View File

@ -0,0 +1,11 @@
const { shouldBehaveLikeEscrow } = require('./Escrow.behavior');
const Escrow = artifacts.require('Escrow');
contract('Escrow', function ([_, primary, ...otherAccounts]) {
beforeEach(async function () {
this.escrow = await Escrow.new({ from: primary });
});
shouldBehaveLikeEscrow(primary, otherAccounts);
});

View File

@ -0,0 +1,133 @@
const shouldFail = require('../../helpers/shouldFail');
const expectEvent = require('../../helpers/expectEvent');
const { ethGetBalance } = require('../../helpers/web3');
const { ether } = require('../../helpers/ether');
const { ZERO_ADDRESS } = require('../../helpers/constants');
const BigNumber = web3.BigNumber;
require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();
const RefundEscrow = artifacts.require('RefundEscrow');
contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee2]) {
const amount = ether(54.0);
const refundees = [refundee1, refundee2];
it('requires a non-null beneficiary', async function () {
await shouldFail.reverting(
RefundEscrow.new(ZERO_ADDRESS, { from: primary })
);
});
context('once deployed', function () {
beforeEach(async function () {
this.escrow = await RefundEscrow.new(beneficiary, { from: primary });
});
context('active state', function () {
it('has beneficiary and state', async function () {
(await this.escrow.beneficiary()).should.be.equal(beneficiary);
(await this.escrow.state()).should.be.bignumber.equal(0);
});
it('accepts deposits', async function () {
await this.escrow.deposit(refundee1, { from: primary, value: amount });
(await this.escrow.depositsOf(refundee1)).should.be.bignumber.equal(amount);
});
it('does not refund refundees', async function () {
await this.escrow.deposit(refundee1, { from: primary, value: amount });
await shouldFail.reverting(this.escrow.withdraw(refundee1));
});
it('does not allow beneficiary withdrawal', async function () {
await this.escrow.deposit(refundee1, { from: primary, value: amount });
await shouldFail.reverting(this.escrow.beneficiaryWithdraw());
});
});
it('only the primary account can enter closed state', async function () {
await shouldFail.reverting(this.escrow.close({ from: beneficiary }));
const { logs } = await this.escrow.close({ from: primary });
expectEvent.inLogs(logs, 'RefundsClosed');
});
context('closed state', function () {
beforeEach(async function () {
await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: primary, value: amount })));
await this.escrow.close({ from: primary });
});
it('rejects deposits', async function () {
await shouldFail.reverting(this.escrow.deposit(refundee1, { from: primary, value: amount }));
});
it('does not refund refundees', async function () {
await shouldFail.reverting(this.escrow.withdraw(refundee1));
});
it('allows beneficiary withdrawal', async function () {
const beneficiaryInitialBalance = await ethGetBalance(beneficiary);
await this.escrow.beneficiaryWithdraw();
const beneficiaryFinalBalance = await ethGetBalance(beneficiary);
beneficiaryFinalBalance.sub(beneficiaryInitialBalance).should.be.bignumber.equal(amount * refundees.length);
});
it('prevents entering the refund state', async function () {
await shouldFail.reverting(this.escrow.enableRefunds({ from: primary }));
});
it('prevents re-entering the closed state', async function () {
await shouldFail.reverting(this.escrow.close({ from: primary }));
});
});
it('only the primary account can enter refund state', async function () {
await shouldFail.reverting(this.escrow.enableRefunds({ from: beneficiary }));
const { logs } = await this.escrow.enableRefunds({ from: primary });
expectEvent.inLogs(logs, 'RefundsEnabled');
});
context('refund state', function () {
beforeEach(async function () {
await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: primary, value: amount })));
await this.escrow.enableRefunds({ from: primary });
});
it('rejects deposits', async function () {
await shouldFail.reverting(this.escrow.deposit(refundee1, { from: primary, value: amount }));
});
it('refunds refundees', async function () {
for (const refundee of [refundee1, refundee2]) {
const refundeeInitialBalance = await ethGetBalance(refundee);
await this.escrow.withdraw(refundee, { from: primary });
const refundeeFinalBalance = await ethGetBalance(refundee);
refundeeFinalBalance.sub(refundeeInitialBalance).should.be.bignumber.equal(amount);
}
});
it('does not allow beneficiary withdrawal', async function () {
await shouldFail.reverting(this.escrow.beneficiaryWithdraw());
});
it('prevents entering the closed state', async function () {
await shouldFail.reverting(this.escrow.close({ from: primary }));
});
it('prevents re-entering the refund state', async function () {
await shouldFail.reverting(this.escrow.enableRefunds({ from: primary }));
});
});
});
});