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.
This commit is contained in:
committed by
Francisco Giordano
parent
c2ad8c3f57
commit
8fd072cf8e
@ -1,59 +0,0 @@
|
||||
import ether from '../helpers/ether';
|
||||
import EVMRevert from '../helpers/EVMRevert';
|
||||
|
||||
const BigNumber = web3.BigNumber;
|
||||
|
||||
require('chai')
|
||||
.use(require('chai-as-promised'))
|
||||
.use(require('chai-bignumber')(BigNumber))
|
||||
.should();
|
||||
|
||||
const RefundVault = artifacts.require('RefundVault');
|
||||
|
||||
contract('RefundVault', function ([_, owner, wallet, investor]) {
|
||||
const value = ether(42);
|
||||
|
||||
beforeEach(async function () {
|
||||
this.vault = await RefundVault.new(wallet, { from: owner });
|
||||
});
|
||||
|
||||
it('should accept contributions', async function () {
|
||||
await this.vault.deposit(investor, { value, from: owner }).should.be.fulfilled;
|
||||
});
|
||||
|
||||
it('should not refund contribution during active state', async function () {
|
||||
await this.vault.deposit(investor, { value, from: owner });
|
||||
await this.vault.refund(investor).should.be.rejectedWith(EVMRevert);
|
||||
});
|
||||
|
||||
it('only owner can enter refund mode', async function () {
|
||||
await this.vault.enableRefunds({ from: _ }).should.be.rejectedWith(EVMRevert);
|
||||
await this.vault.enableRefunds({ from: owner }).should.be.fulfilled;
|
||||
});
|
||||
|
||||
it('should refund contribution after entering refund mode', async function () {
|
||||
await this.vault.deposit(investor, { value, from: owner });
|
||||
await this.vault.enableRefunds({ from: owner });
|
||||
|
||||
const pre = web3.eth.getBalance(investor);
|
||||
await this.vault.refund(investor);
|
||||
const post = web3.eth.getBalance(investor);
|
||||
|
||||
post.minus(pre).should.be.bignumber.equal(value);
|
||||
});
|
||||
|
||||
it('only owner can close', async function () {
|
||||
await this.vault.close({ from: _ }).should.be.rejectedWith(EVMRevert);
|
||||
await this.vault.close({ from: owner }).should.be.fulfilled;
|
||||
});
|
||||
|
||||
it('should forward funds to wallet after closing', async function () {
|
||||
await this.vault.deposit(investor, { value, from: owner });
|
||||
|
||||
const pre = web3.eth.getBalance(wallet);
|
||||
await this.vault.close({ from: owner });
|
||||
const post = web3.eth.getBalance(wallet);
|
||||
|
||||
post.minus(pre).should.be.bignumber.equal(value);
|
||||
});
|
||||
});
|
||||
@ -14,7 +14,6 @@ require('chai')
|
||||
|
||||
const SampleCrowdsale = artifacts.require('SampleCrowdsale');
|
||||
const SampleCrowdsaleToken = artifacts.require('SampleCrowdsaleToken');
|
||||
const RefundVault = artifacts.require('RefundVault');
|
||||
|
||||
contract('SampleCrowdsale', function ([owner, wallet, investor]) {
|
||||
const RATE = new BigNumber(10);
|
||||
@ -32,12 +31,10 @@ contract('SampleCrowdsale', function ([owner, wallet, investor]) {
|
||||
this.afterClosingTime = this.closingTime + duration.seconds(1);
|
||||
|
||||
this.token = await SampleCrowdsaleToken.new({ from: owner });
|
||||
this.vault = await RefundVault.new(wallet, { from: owner });
|
||||
this.crowdsale = await SampleCrowdsale.new(
|
||||
this.openingTime, this.closingTime, RATE, wallet, CAP, this.token.address, GOAL
|
||||
);
|
||||
await this.token.transferOwnership(this.crowdsale.address);
|
||||
await this.vault.transferOwnership(this.crowdsale.address);
|
||||
});
|
||||
|
||||
it('should create crowdsale with correct parameters', async function () {
|
||||
|
||||
41
test/payment/ConditionalEscrow.test.js
Normal file
41
test/payment/ConditionalEscrow.test.js
Normal file
@ -0,0 +1,41 @@
|
||||
import shouldBehaveLikeEscrow from './Escrow.behaviour';
|
||||
import EVMRevert from '../helpers/EVMRevert';
|
||||
|
||||
const BigNumber = web3.BigNumber;
|
||||
|
||||
require('chai')
|
||||
.use(require('chai-bignumber')(BigNumber))
|
||||
.should();
|
||||
|
||||
const ConditionalEscrowMock = artifacts.require('ConditionalEscrowMock');
|
||||
|
||||
contract('ConditionalEscrow', function (accounts) {
|
||||
const owner = accounts[0];
|
||||
|
||||
beforeEach(async function () {
|
||||
this.escrow = await ConditionalEscrowMock.new({ from: owner });
|
||||
});
|
||||
|
||||
context('when withdrawal is allowed', function () {
|
||||
beforeEach(async function () {
|
||||
await Promise.all(accounts.map(payee => this.escrow.setAllowed(payee, true)));
|
||||
});
|
||||
|
||||
shouldBehaveLikeEscrow(owner, accounts.slice(1));
|
||||
});
|
||||
|
||||
context('when withdrawal is disallowed', function () {
|
||||
const amount = web3.toWei(23.0, 'ether');
|
||||
const payee = accounts[1];
|
||||
|
||||
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 this.escrow.withdraw(payee, { from: owner }).should.be.rejectedWith(EVMRevert);
|
||||
});
|
||||
});
|
||||
});
|
||||
98
test/payment/Escrow.behaviour.js
Normal file
98
test/payment/Escrow.behaviour.js
Normal file
@ -0,0 +1,98 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
13
test/payment/Escrow.test.js
Normal file
13
test/payment/Escrow.test.js
Normal file
@ -0,0 +1,13 @@
|
||||
import shouldBehaveLikeEscrow from './Escrow.behaviour';
|
||||
|
||||
const Escrow = artifacts.require('Escrow');
|
||||
|
||||
contract('Escrow', function (accounts) {
|
||||
const owner = accounts[0];
|
||||
|
||||
beforeEach(async function () {
|
||||
this.escrow = await Escrow.new({ from: owner });
|
||||
});
|
||||
|
||||
shouldBehaveLikeEscrow(owner, accounts.slice(1));
|
||||
});
|
||||
@ -1,71 +1,62 @@
|
||||
var PullPaymentMock = artifacts.require('PullPaymentMock');
|
||||
const BigNumber = web3.BigNumber;
|
||||
|
||||
require('chai')
|
||||
.use(require('chai-bignumber')(BigNumber))
|
||||
.should();
|
||||
|
||||
const PullPaymentMock = artifacts.require('PullPaymentMock');
|
||||
|
||||
contract('PullPayment', function (accounts) {
|
||||
let ppce;
|
||||
let amount = 17 * 1e18;
|
||||
const amount = web3.toWei(17.0, 'ether');
|
||||
|
||||
beforeEach(async function () {
|
||||
ppce = await PullPaymentMock.new({ value: amount });
|
||||
this.contract = await PullPaymentMock.new({ value: amount });
|
||||
});
|
||||
|
||||
it('can\'t call asyncSend externally', async function () {
|
||||
assert.isUndefined(ppce.asyncSend);
|
||||
assert.isUndefined(this.contract.asyncSend);
|
||||
});
|
||||
|
||||
it('can record an async payment correctly', async function () {
|
||||
let AMOUNT = 100;
|
||||
await ppce.callSend(accounts[0], AMOUNT);
|
||||
let paymentsToAccount0 = await ppce.payments(accounts[0]);
|
||||
let totalPayments = await ppce.totalPayments();
|
||||
const AMOUNT = 100;
|
||||
await this.contract.callTransfer(accounts[0], AMOUNT);
|
||||
|
||||
assert.equal(totalPayments, AMOUNT);
|
||||
assert.equal(paymentsToAccount0, AMOUNT);
|
||||
const paymentsToAccount0 = await this.contract.payments(accounts[0]);
|
||||
paymentsToAccount0.should.be.bignumber.equal(AMOUNT);
|
||||
});
|
||||
|
||||
it('can add multiple balances on one account', async function () {
|
||||
await ppce.callSend(accounts[0], 200);
|
||||
await ppce.callSend(accounts[0], 300);
|
||||
let paymentsToAccount0 = await ppce.payments(accounts[0]);
|
||||
let totalPayments = await ppce.totalPayments();
|
||||
|
||||
assert.equal(totalPayments, 500);
|
||||
assert.equal(paymentsToAccount0, 500);
|
||||
await this.contract.callTransfer(accounts[0], 200);
|
||||
await this.contract.callTransfer(accounts[0], 300);
|
||||
const paymentsToAccount0 = await this.contract.payments(accounts[0]);
|
||||
paymentsToAccount0.should.be.bignumber.equal(500);
|
||||
});
|
||||
|
||||
it('can add balances on multiple accounts', async function () {
|
||||
await ppce.callSend(accounts[0], 200);
|
||||
await ppce.callSend(accounts[1], 300);
|
||||
await this.contract.callTransfer(accounts[0], 200);
|
||||
await this.contract.callTransfer(accounts[1], 300);
|
||||
|
||||
let paymentsToAccount0 = await ppce.payments(accounts[0]);
|
||||
assert.equal(paymentsToAccount0, 200);
|
||||
const paymentsToAccount0 = await this.contract.payments(accounts[0]);
|
||||
paymentsToAccount0.should.be.bignumber.equal(200);
|
||||
|
||||
let paymentsToAccount1 = await ppce.payments(accounts[1]);
|
||||
assert.equal(paymentsToAccount1, 300);
|
||||
|
||||
let totalPayments = await ppce.totalPayments();
|
||||
assert.equal(totalPayments, 500);
|
||||
const paymentsToAccount1 = await this.contract.payments(accounts[1]);
|
||||
paymentsToAccount1.should.be.bignumber.equal(300);
|
||||
});
|
||||
|
||||
it('can withdraw payment', async function () {
|
||||
let payee = accounts[1];
|
||||
let initialBalance = web3.eth.getBalance(payee);
|
||||
const payee = accounts[1];
|
||||
const initialBalance = web3.eth.getBalance(payee);
|
||||
|
||||
await ppce.callSend(payee, amount);
|
||||
await this.contract.callTransfer(payee, amount);
|
||||
|
||||
let payment1 = await ppce.payments(payee);
|
||||
assert.equal(payment1, amount);
|
||||
const payment1 = await this.contract.payments(payee);
|
||||
payment1.should.be.bignumber.equal(amount);
|
||||
|
||||
let totalPayments = await ppce.totalPayments();
|
||||
assert.equal(totalPayments, amount);
|
||||
await this.contract.withdrawPayments({ from: payee });
|
||||
const payment2 = await this.contract.payments(payee);
|
||||
payment2.should.be.bignumber.equal(0);
|
||||
|
||||
await ppce.withdrawPayments({ from: payee });
|
||||
let payment2 = await ppce.payments(payee);
|
||||
assert.equal(payment2, 0);
|
||||
|
||||
totalPayments = await ppce.totalPayments();
|
||||
assert.equal(totalPayments, 0);
|
||||
|
||||
let balance = web3.eth.getBalance(payee);
|
||||
assert(Math.abs(balance - initialBalance - amount) < 1e16);
|
||||
const balance = web3.eth.getBalance(payee);
|
||||
Math.abs(balance - initialBalance - amount).should.be.lt(1e16);
|
||||
});
|
||||
});
|
||||
|
||||
104
test/payment/RefundEscrow.test.js
Normal file
104
test/payment/RefundEscrow.test.js
Normal file
@ -0,0 +1,104 @@
|
||||
import EVMRevert from '../helpers/EVMRevert';
|
||||
import expectEvent from '../helpers/expectEvent';
|
||||
|
||||
const BigNumber = web3.BigNumber;
|
||||
|
||||
require('chai')
|
||||
.use(require('chai-bignumber')(BigNumber))
|
||||
.should();
|
||||
|
||||
const RefundEscrow = artifacts.require('RefundEscrow');
|
||||
|
||||
contract('RefundEscrow', function ([owner, beneficiary, refundee1, refundee2]) {
|
||||
const amount = web3.toWei(54.0, 'ether');
|
||||
const refundees = [refundee1, refundee2];
|
||||
|
||||
beforeEach(async function () {
|
||||
this.escrow = await RefundEscrow.new(beneficiary, { from: owner });
|
||||
});
|
||||
|
||||
context('active state', function () {
|
||||
it('accepts deposits', async function () {
|
||||
await this.escrow.deposit(refundee1, { from: owner, value: amount });
|
||||
|
||||
const deposit = await this.escrow.depositsOf(refundee1);
|
||||
deposit.should.be.bignumber.equal(amount);
|
||||
});
|
||||
|
||||
it('does not refund refundees', async function () {
|
||||
await this.escrow.deposit(refundee1, { from: owner, value: amount });
|
||||
await this.escrow.withdraw(refundee1).should.be.rejectedWith(EVMRevert);
|
||||
});
|
||||
|
||||
it('does not allow beneficiary withdrawal', async function () {
|
||||
await this.escrow.deposit(refundee1, { from: owner, value: amount });
|
||||
await this.escrow.beneficiaryWithdraw().should.be.rejectedWith(EVMRevert);
|
||||
});
|
||||
});
|
||||
|
||||
it('only owner can enter closed state', async function () {
|
||||
await this.escrow.close({ from: beneficiary }).should.be.rejectedWith(EVMRevert);
|
||||
|
||||
const receipt = await this.escrow.close({ from: owner });
|
||||
|
||||
await expectEvent.inLogs(receipt.logs, 'Closed');
|
||||
});
|
||||
|
||||
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 this.escrow.deposit(refundee1, { from: owner, value: amount }).should.be.rejectedWith(EVMRevert);
|
||||
});
|
||||
|
||||
it('does not refund refundees', async function () {
|
||||
await this.escrow.withdraw(refundee1).should.be.rejectedWith(EVMRevert);
|
||||
});
|
||||
|
||||
it('allows beneficiary withdrawal', async function () {
|
||||
const beneficiaryInitialBalance = await web3.eth.getBalance(beneficiary);
|
||||
await this.escrow.beneficiaryWithdraw();
|
||||
const beneficiaryFinalBalance = await web3.eth.getBalance(beneficiary);
|
||||
|
||||
beneficiaryFinalBalance.sub(beneficiaryInitialBalance).should.be.bignumber.equal(amount * refundees.length);
|
||||
});
|
||||
});
|
||||
|
||||
it('only owner can enter refund state', async function () {
|
||||
await this.escrow.enableRefunds({ from: beneficiary }).should.be.rejectedWith(EVMRevert);
|
||||
|
||||
const receipt = await this.escrow.enableRefunds({ from: owner });
|
||||
|
||||
await expectEvent.inLogs(receipt.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 this.escrow.deposit(refundee1, { from: owner, value: amount }).should.be.rejectedWith(EVMRevert);
|
||||
});
|
||||
|
||||
it('refunds refundees', async function () {
|
||||
for (let refundee of [refundee1, refundee2]) {
|
||||
const refundeeInitialBalance = await web3.eth.getBalance(refundee);
|
||||
await this.escrow.withdraw(refundee);
|
||||
const refundeeFinalBalance = await web3.eth.getBalance(refundee);
|
||||
|
||||
refundeeFinalBalance.sub(refundeeInitialBalance).should.be.bignumber.equal(amount);
|
||||
}
|
||||
});
|
||||
|
||||
it('does not allow beneficiary withdrawal', async function () {
|
||||
await this.escrow.beneficiaryWithdraw().should.be.rejectedWith(EVMRevert);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user