Files
openzeppelin-contracts/test/payment/Escrow.behaviour.js
Nicolás Venturo 8fd072cf8e Escrows (#1014)
* Added basic Escrow

* PullPayment now uses an Escrow, removing all trust from the contract

* Abstracted the Escrow tests to a behaviour

* Added ConditionalEscrow

* Added RefundableEscrow.

* RefundableCrowdsale now uses a RefundEscrow, removed RefundVault.

* Renaming after code review.

* Added log test helper.

* Now allowing empty deposits and withdrawals.

* Style fixes.

* Minor review comments.

* Add Deposited and Withdrawn events, removed Refunded

* The base Escrow is now Ownable, users of it (owners) must provide methods to access it.
2018-07-03 18:54:55 -03:00

99 lines
3.8 KiB
JavaScript

import expectEvent from '../helpers/expectEvent';
import EVMRevert from '../helpers/EVMRevert';
const BigNumber = web3.BigNumber;
require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();
export default function (owner, [payee1, payee2]) {
const amount = web3.toWei(42.0, 'ether');
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 });
const balance = await web3.eth.getBalance(this.escrow.address);
const deposit = await this.escrow.depositsOf(payee1);
balance.should.be.bignumber.equal(amount);
deposit.should.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 this.escrow.deposit(payee1, { from: payee2 }).should.be.rejectedWith(EVMRevert);
});
it('emits a deposited event', async function () {
const receipt = await this.escrow.deposit(payee1, { from: owner, value: amount });
const event = await expectEvent.inLogs(receipt.logs, 'Deposited', { payee: payee1 });
event.args.weiAmount.should.be.bignumber.equal(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 * 2 });
const balance = await web3.eth.getBalance(this.escrow.address);
const deposit = await this.escrow.depositsOf(payee1);
balance.should.be.bignumber.equal(amount * 3);
deposit.should.be.bignumber.equal(amount * 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 * 2 });
const balance = await web3.eth.getBalance(this.escrow.address);
const depositPayee1 = await this.escrow.depositsOf(payee1);
const depositPayee2 = await this.escrow.depositsOf(payee2);
balance.should.be.bignumber.equal(amount * 3);
depositPayee1.should.be.bignumber.equal(amount);
depositPayee2.should.be.bignumber.equal(amount * 2);
});
});
describe('withdrawals', async function () {
it('can withdraw payments', async function () {
const payeeInitialBalance = await web3.eth.getBalance(payee1);
await this.escrow.deposit(payee1, { from: owner, value: amount });
await this.escrow.withdraw(payee1, { from: owner });
const escrowBalance = await web3.eth.getBalance(this.escrow.address);
const finalDeposit = await this.escrow.depositsOf(payee1);
const payeeFinalBalance = await web3.eth.getBalance(payee1);
escrowBalance.should.be.bignumber.equal(0);
finalDeposit.should.be.bignumber.equal(0);
payeeFinalBalance.sub(payeeInitialBalance).should.be.bignumber.equal(amount);
});
it('can do an empty withdrawal', async function () {
await this.escrow.withdraw(payee1, { from: owner });
});
it('only the owner can withdraw', async function () {
await this.escrow.withdraw(payee1, { from: payee1 }).should.be.rejectedWith(EVMRevert);
});
it('emits a withdrawn event', async function () {
await this.escrow.deposit(payee1, { from: owner, value: amount });
const receipt = await this.escrow.withdraw(payee1, { from: owner });
const event = await expectEvent.inLogs(receipt.logs, 'Withdrawn', { payee: payee1 });
event.args.weiAmount.should.be.bignumber.equal(amount);
});
});
});
};