Transpile 7bce2b72
This commit is contained in:
36
test/utils/escrow/ConditionalEscrow.test.js
Normal file
36
test/utils/escrow/ConditionalEscrow.test.js
Normal file
@ -0,0 +1,36 @@
|
||||
const { ether, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { shouldBehaveLikeEscrow } = require('./Escrow.behavior');
|
||||
|
||||
const ConditionalEscrowMock = artifacts.require('ConditionalEscrowMock');
|
||||
|
||||
contract('ConditionalEscrow', function (accounts) {
|
||||
const [ owner, payee, ...otherAccounts ] = accounts;
|
||||
|
||||
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');
|
||||
|
||||
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 expectRevert(this.escrow.withdraw(payee, { from: owner }),
|
||||
'ConditionalEscrow: payee is not allowed to withdraw',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
94
test/utils/escrow/Escrow.behavior.js
Normal file
94
test/utils/escrow/Escrow.behavior.js
Normal file
@ -0,0 +1,94 @@
|
||||
const { balance, ether, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
function shouldBehaveLikeEscrow (owner, [payee1, payee2]) {
|
||||
const amount = ether('42');
|
||||
|
||||
describe('as an escrow', function () {
|
||||
describe('deposits', function () {
|
||||
it('can accept a single deposit', async function () {
|
||||
await this.escrow.deposit(payee1, { from: owner, value: amount });
|
||||
|
||||
expect(await balance.current(this.escrow.address)).to.be.bignumber.equal(amount);
|
||||
|
||||
expect(await this.escrow.depositsOf(payee1)).to.be.bignumber.equal(amount);
|
||||
});
|
||||
|
||||
it('can accept an empty deposit', async function () {
|
||||
await this.escrow.deposit(payee1, { from: owner, value: 0 });
|
||||
});
|
||||
|
||||
it('only the owner can deposit', async function () {
|
||||
await expectRevert(this.escrow.deposit(payee1, { from: payee2 }),
|
||||
'Ownable: caller is not the owner',
|
||||
);
|
||||
});
|
||||
|
||||
it('emits a deposited event', async function () {
|
||||
const { logs } = await this.escrow.deposit(payee1, { from: owner, 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: owner, value: amount });
|
||||
await this.escrow.deposit(payee1, { from: owner, value: amount.muln(2) });
|
||||
|
||||
expect(await balance.current(this.escrow.address)).to.be.bignumber.equal(amount.muln(3));
|
||||
|
||||
expect(await this.escrow.depositsOf(payee1)).to.be.bignumber.equal(amount.muln(3));
|
||||
});
|
||||
|
||||
it('can track deposits to multiple accounts', async function () {
|
||||
await this.escrow.deposit(payee1, { from: owner, value: amount });
|
||||
await this.escrow.deposit(payee2, { from: owner, value: amount.muln(2) });
|
||||
|
||||
expect(await balance.current(this.escrow.address)).to.be.bignumber.equal(amount.muln(3));
|
||||
|
||||
expect(await this.escrow.depositsOf(payee1)).to.be.bignumber.equal(amount);
|
||||
|
||||
expect(await this.escrow.depositsOf(payee2)).to.be.bignumber.equal(amount.muln(2));
|
||||
});
|
||||
});
|
||||
|
||||
describe('withdrawals', async function () {
|
||||
it('can withdraw payments', async function () {
|
||||
const balanceTracker = await balance.tracker(payee1);
|
||||
|
||||
await this.escrow.deposit(payee1, { from: owner, value: amount });
|
||||
await this.escrow.withdraw(payee1, { from: owner });
|
||||
|
||||
expect(await balanceTracker.delta()).to.be.bignumber.equal(amount);
|
||||
|
||||
expect(await balance.current(this.escrow.address)).to.be.bignumber.equal('0');
|
||||
expect(await this.escrow.depositsOf(payee1)).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('can do an empty withdrawal', async function () {
|
||||
await this.escrow.withdraw(payee1, { from: owner });
|
||||
});
|
||||
|
||||
it('only the owner can withdraw', async function () {
|
||||
await expectRevert(this.escrow.withdraw(payee1, { from: payee1 }),
|
||||
'Ownable: caller is not the owner',
|
||||
);
|
||||
});
|
||||
|
||||
it('emits a withdrawn event', async function () {
|
||||
await this.escrow.deposit(payee1, { from: owner, value: amount });
|
||||
const { logs } = await this.escrow.withdraw(payee1, { from: owner });
|
||||
expectEvent.inLogs(logs, 'Withdrawn', {
|
||||
payee: payee1,
|
||||
weiAmount: amount,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
shouldBehaveLikeEscrow,
|
||||
};
|
||||
14
test/utils/escrow/Escrow.test.js
Normal file
14
test/utils/escrow/Escrow.test.js
Normal file
@ -0,0 +1,14 @@
|
||||
require('@openzeppelin/test-helpers');
|
||||
const { shouldBehaveLikeEscrow } = require('./Escrow.behavior');
|
||||
|
||||
const Escrow = artifacts.require('Escrow');
|
||||
|
||||
contract('Escrow', function (accounts) {
|
||||
const [ owner, ...otherAccounts ] = accounts;
|
||||
|
||||
beforeEach(async function () {
|
||||
this.escrow = await Escrow.new({ from: owner });
|
||||
});
|
||||
|
||||
shouldBehaveLikeEscrow(owner, otherAccounts);
|
||||
});
|
||||
148
test/utils/escrow/RefundEscrow.test.js
Normal file
148
test/utils/escrow/RefundEscrow.test.js
Normal file
@ -0,0 +1,148 @@
|
||||
const { balance, constants, ether, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { ZERO_ADDRESS } = constants;
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const RefundEscrow = artifacts.require('RefundEscrow');
|
||||
|
||||
contract('RefundEscrow', function (accounts) {
|
||||
const [ owner, beneficiary, refundee1, refundee2 ] = accounts;
|
||||
|
||||
const amount = ether('54');
|
||||
const refundees = [refundee1, refundee2];
|
||||
|
||||
it('requires a non-null beneficiary', async function () {
|
||||
await expectRevert(
|
||||
RefundEscrow.new(ZERO_ADDRESS, { from: owner }), 'RefundEscrow: beneficiary is the zero address',
|
||||
);
|
||||
});
|
||||
|
||||
context('once deployed', function () {
|
||||
beforeEach(async function () {
|
||||
this.escrow = await RefundEscrow.new(beneficiary, { from: owner });
|
||||
});
|
||||
|
||||
context('active state', function () {
|
||||
it('has beneficiary and state', async function () {
|
||||
expect(await this.escrow.beneficiary()).to.equal(beneficiary);
|
||||
expect(await this.escrow.state()).to.be.bignumber.equal('0');
|
||||
});
|
||||
|
||||
it('accepts deposits', async function () {
|
||||
await this.escrow.deposit(refundee1, { from: owner, value: amount });
|
||||
|
||||
expect(await this.escrow.depositsOf(refundee1)).to.be.bignumber.equal(amount);
|
||||
});
|
||||
|
||||
it('does not refund refundees', async function () {
|
||||
await this.escrow.deposit(refundee1, { from: owner, value: amount });
|
||||
await expectRevert(this.escrow.withdraw(refundee1),
|
||||
'ConditionalEscrow: payee is not allowed to withdraw',
|
||||
);
|
||||
});
|
||||
|
||||
it('does not allow beneficiary withdrawal', async function () {
|
||||
await this.escrow.deposit(refundee1, { from: owner, value: amount });
|
||||
await expectRevert(this.escrow.beneficiaryWithdraw(),
|
||||
'RefundEscrow: beneficiary can only withdraw while closed',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('only the owner can enter closed state', async function () {
|
||||
await expectRevert(this.escrow.close({ from: beneficiary }),
|
||||
'Ownable: caller is not the owner',
|
||||
);
|
||||
|
||||
const { logs } = await this.escrow.close({ from: owner });
|
||||
expectEvent.inLogs(logs, 'RefundsClosed');
|
||||
});
|
||||
|
||||
context('closed state', function () {
|
||||
beforeEach(async function () {
|
||||
await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: owner, value: amount })));
|
||||
|
||||
await this.escrow.close({ from: owner });
|
||||
});
|
||||
|
||||
it('rejects deposits', async function () {
|
||||
await expectRevert(this.escrow.deposit(refundee1, { from: owner, value: amount }),
|
||||
'RefundEscrow: can only deposit while active',
|
||||
);
|
||||
});
|
||||
|
||||
it('does not refund refundees', async function () {
|
||||
await expectRevert(this.escrow.withdraw(refundee1),
|
||||
'ConditionalEscrow: payee is not allowed to withdraw',
|
||||
);
|
||||
});
|
||||
|
||||
it('allows beneficiary withdrawal', async function () {
|
||||
const balanceTracker = await balance.tracker(beneficiary);
|
||||
await this.escrow.beneficiaryWithdraw();
|
||||
expect(await balanceTracker.delta()).to.be.bignumber.equal(amount.muln(refundees.length));
|
||||
});
|
||||
|
||||
it('prevents entering the refund state', async function () {
|
||||
await expectRevert(this.escrow.enableRefunds({ from: owner }),
|
||||
'RefundEscrow: can only enable refunds while active',
|
||||
);
|
||||
});
|
||||
|
||||
it('prevents re-entering the closed state', async function () {
|
||||
await expectRevert(this.escrow.close({ from: owner }),
|
||||
'RefundEscrow: can only close while active',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('only the owner can enter refund state', async function () {
|
||||
await expectRevert(this.escrow.enableRefunds({ from: beneficiary }),
|
||||
'Ownable: caller is not the owner',
|
||||
);
|
||||
|
||||
const { logs } = await this.escrow.enableRefunds({ from: owner });
|
||||
expectEvent.inLogs(logs, 'RefundsEnabled');
|
||||
});
|
||||
|
||||
context('refund state', function () {
|
||||
beforeEach(async function () {
|
||||
await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: owner, value: amount })));
|
||||
|
||||
await this.escrow.enableRefunds({ from: owner });
|
||||
});
|
||||
|
||||
it('rejects deposits', async function () {
|
||||
await expectRevert(this.escrow.deposit(refundee1, { from: owner, value: amount }),
|
||||
'RefundEscrow: can only deposit while active',
|
||||
);
|
||||
});
|
||||
|
||||
it('refunds refundees', async function () {
|
||||
for (const refundee of [refundee1, refundee2]) {
|
||||
const balanceTracker = await balance.tracker(refundee);
|
||||
await this.escrow.withdraw(refundee, { from: owner });
|
||||
expect(await balanceTracker.delta()).to.be.bignumber.equal(amount);
|
||||
}
|
||||
});
|
||||
|
||||
it('does not allow beneficiary withdrawal', async function () {
|
||||
await expectRevert(this.escrow.beneficiaryWithdraw(),
|
||||
'RefundEscrow: beneficiary can only withdraw while closed',
|
||||
);
|
||||
});
|
||||
|
||||
it('prevents entering the closed state', async function () {
|
||||
await expectRevert(this.escrow.close({ from: owner }),
|
||||
'RefundEscrow: can only close while active',
|
||||
);
|
||||
});
|
||||
|
||||
it('prevents re-entering the refund state', async function () {
|
||||
await expectRevert(this.escrow.enableRefunds({ from: owner }),
|
||||
'RefundEscrow: can only enable refunds while active',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user