mirror of
https://github.com/th30d4y/OpenLearnX.git
synced 2026-05-26 19:26:33 +00:00
Fix .gitignore: stop tracking ignored files
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
const { balance, constants, ether, expectEvent, send, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { ZERO_ADDRESS } = constants;
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const PaymentSplitter = artifacts.require('PaymentSplitter');
|
||||
const ERC20 = artifacts.require('$ERC20');
|
||||
|
||||
contract('PaymentSplitter', function (accounts) {
|
||||
const [owner, payee1, payee2, payee3, nonpayee1, payer1] = accounts;
|
||||
|
||||
const amount = ether('1');
|
||||
|
||||
it('rejects an empty set of payees', async function () {
|
||||
await expectRevert(PaymentSplitter.new([], []), 'PaymentSplitter: no payees');
|
||||
});
|
||||
|
||||
it('rejects more payees than shares', async function () {
|
||||
await expectRevert(
|
||||
PaymentSplitter.new([payee1, payee2, payee3], [20, 30]),
|
||||
'PaymentSplitter: payees and shares length mismatch',
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects more shares than payees', async function () {
|
||||
await expectRevert(
|
||||
PaymentSplitter.new([payee1, payee2], [20, 30, 40]),
|
||||
'PaymentSplitter: payees and shares length mismatch',
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects null payees', async function () {
|
||||
await expectRevert(
|
||||
PaymentSplitter.new([payee1, ZERO_ADDRESS], [20, 30]),
|
||||
'PaymentSplitter: account is the zero address',
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects zero-valued shares', async function () {
|
||||
await expectRevert(PaymentSplitter.new([payee1, payee2], [20, 0]), 'PaymentSplitter: shares are 0');
|
||||
});
|
||||
|
||||
it('rejects repeated payees', async function () {
|
||||
await expectRevert(PaymentSplitter.new([payee1, payee1], [20, 30]), 'PaymentSplitter: account already has shares');
|
||||
});
|
||||
|
||||
context('once deployed', function () {
|
||||
beforeEach(async function () {
|
||||
this.payees = [payee1, payee2, payee3];
|
||||
this.shares = [20, 10, 70];
|
||||
|
||||
this.contract = await PaymentSplitter.new(this.payees, this.shares);
|
||||
this.token = await ERC20.new('MyToken', 'MT');
|
||||
await this.token.$_mint(owner, ether('1000'));
|
||||
});
|
||||
|
||||
it('has total shares', async function () {
|
||||
expect(await this.contract.totalShares()).to.be.bignumber.equal('100');
|
||||
});
|
||||
|
||||
it('has payees', async function () {
|
||||
await Promise.all(
|
||||
this.payees.map(async (payee, index) => {
|
||||
expect(await this.contract.payee(index)).to.equal(payee);
|
||||
expect(await this.contract.released(payee)).to.be.bignumber.equal('0');
|
||||
expect(await this.contract.releasable(payee)).to.be.bignumber.equal('0');
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
describe('accepts payments', function () {
|
||||
it('Ether', async function () {
|
||||
await send.ether(owner, this.contract.address, amount);
|
||||
|
||||
expect(await balance.current(this.contract.address)).to.be.bignumber.equal(amount);
|
||||
});
|
||||
|
||||
it('Token', async function () {
|
||||
await this.token.transfer(this.contract.address, amount, { from: owner });
|
||||
|
||||
expect(await this.token.balanceOf(this.contract.address)).to.be.bignumber.equal(amount);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shares', function () {
|
||||
it('stores shares if address is payee', async function () {
|
||||
expect(await this.contract.shares(payee1)).to.be.bignumber.not.equal('0');
|
||||
});
|
||||
|
||||
it('does not store shares if address is not payee', async function () {
|
||||
expect(await this.contract.shares(nonpayee1)).to.be.bignumber.equal('0');
|
||||
});
|
||||
});
|
||||
|
||||
describe('release', function () {
|
||||
describe('Ether', function () {
|
||||
it('reverts if no funds to claim', async function () {
|
||||
await expectRevert(this.contract.release(payee1), 'PaymentSplitter: account is not due payment');
|
||||
});
|
||||
it('reverts if non-payee want to claim', async function () {
|
||||
await send.ether(payer1, this.contract.address, amount);
|
||||
await expectRevert(this.contract.release(nonpayee1), 'PaymentSplitter: account has no shares');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Token', function () {
|
||||
it('reverts if no funds to claim', async function () {
|
||||
await expectRevert(
|
||||
this.contract.release(this.token.address, payee1),
|
||||
'PaymentSplitter: account is not due payment',
|
||||
);
|
||||
});
|
||||
it('reverts if non-payee want to claim', async function () {
|
||||
await this.token.transfer(this.contract.address, amount, { from: owner });
|
||||
await expectRevert(
|
||||
this.contract.release(this.token.address, nonpayee1),
|
||||
'PaymentSplitter: account has no shares',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('tracks releasable and released', function () {
|
||||
it('Ether', async function () {
|
||||
await send.ether(payer1, this.contract.address, amount);
|
||||
const payment = amount.divn(10);
|
||||
expect(await this.contract.releasable(payee2)).to.be.bignumber.equal(payment);
|
||||
await this.contract.release(payee2);
|
||||
expect(await this.contract.releasable(payee2)).to.be.bignumber.equal('0');
|
||||
expect(await this.contract.released(payee2)).to.be.bignumber.equal(payment);
|
||||
});
|
||||
|
||||
it('Token', async function () {
|
||||
await this.token.transfer(this.contract.address, amount, { from: owner });
|
||||
const payment = amount.divn(10);
|
||||
expect(await this.contract.releasable(this.token.address, payee2, {})).to.be.bignumber.equal(payment);
|
||||
await this.contract.release(this.token.address, payee2);
|
||||
expect(await this.contract.releasable(this.token.address, payee2, {})).to.be.bignumber.equal('0');
|
||||
expect(await this.contract.released(this.token.address, payee2)).to.be.bignumber.equal(payment);
|
||||
});
|
||||
});
|
||||
|
||||
describe('distributes funds to payees', function () {
|
||||
it('Ether', async function () {
|
||||
await send.ether(payer1, this.contract.address, amount);
|
||||
|
||||
// receive funds
|
||||
const initBalance = await balance.current(this.contract.address);
|
||||
expect(initBalance).to.be.bignumber.equal(amount);
|
||||
|
||||
// distribute to payees
|
||||
|
||||
const tracker1 = await balance.tracker(payee1);
|
||||
const receipt1 = await this.contract.release(payee1);
|
||||
const profit1 = await tracker1.delta();
|
||||
expect(profit1).to.be.bignumber.equal(ether('0.20'));
|
||||
expectEvent(receipt1, 'PaymentReleased', { to: payee1, amount: profit1 });
|
||||
|
||||
const tracker2 = await balance.tracker(payee2);
|
||||
const receipt2 = await this.contract.release(payee2);
|
||||
const profit2 = await tracker2.delta();
|
||||
expect(profit2).to.be.bignumber.equal(ether('0.10'));
|
||||
expectEvent(receipt2, 'PaymentReleased', { to: payee2, amount: profit2 });
|
||||
|
||||
const tracker3 = await balance.tracker(payee3);
|
||||
const receipt3 = await this.contract.release(payee3);
|
||||
const profit3 = await tracker3.delta();
|
||||
expect(profit3).to.be.bignumber.equal(ether('0.70'));
|
||||
expectEvent(receipt3, 'PaymentReleased', { to: payee3, amount: profit3 });
|
||||
|
||||
// end balance should be zero
|
||||
expect(await balance.current(this.contract.address)).to.be.bignumber.equal('0');
|
||||
|
||||
// check correct funds released accounting
|
||||
expect(await this.contract.totalReleased()).to.be.bignumber.equal(initBalance);
|
||||
});
|
||||
|
||||
it('Token', async function () {
|
||||
expect(await this.token.balanceOf(payee1)).to.be.bignumber.equal('0');
|
||||
expect(await this.token.balanceOf(payee2)).to.be.bignumber.equal('0');
|
||||
expect(await this.token.balanceOf(payee3)).to.be.bignumber.equal('0');
|
||||
|
||||
await this.token.transfer(this.contract.address, amount, { from: owner });
|
||||
|
||||
expectEvent(await this.contract.release(this.token.address, payee1), 'ERC20PaymentReleased', {
|
||||
token: this.token.address,
|
||||
to: payee1,
|
||||
amount: ether('0.20'),
|
||||
});
|
||||
|
||||
await this.token.transfer(this.contract.address, amount, { from: owner });
|
||||
|
||||
expectEvent(await this.contract.release(this.token.address, payee1), 'ERC20PaymentReleased', {
|
||||
token: this.token.address,
|
||||
to: payee1,
|
||||
amount: ether('0.20'),
|
||||
});
|
||||
|
||||
expectEvent(await this.contract.release(this.token.address, payee2), 'ERC20PaymentReleased', {
|
||||
token: this.token.address,
|
||||
to: payee2,
|
||||
amount: ether('0.20'),
|
||||
});
|
||||
|
||||
expectEvent(await this.contract.release(this.token.address, payee3), 'ERC20PaymentReleased', {
|
||||
token: this.token.address,
|
||||
to: payee3,
|
||||
amount: ether('1.40'),
|
||||
});
|
||||
|
||||
expect(await this.token.balanceOf(payee1)).to.be.bignumber.equal(ether('0.40'));
|
||||
expect(await this.token.balanceOf(payee2)).to.be.bignumber.equal(ether('0.20'));
|
||||
expect(await this.token.balanceOf(payee3)).to.be.bignumber.equal(ether('1.40'));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
const { time } = require('@nomicfoundation/hardhat-network-helpers');
|
||||
const { expectEvent } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
function releasedEvent(token, amount) {
|
||||
return token ? ['ERC20Released', { token: token.address, amount }] : ['EtherReleased', { amount }];
|
||||
}
|
||||
|
||||
function shouldBehaveLikeVesting(beneficiary) {
|
||||
it('check vesting schedule', async function () {
|
||||
const [vestedAmount, releasable, ...args] = this.token
|
||||
? ['vestedAmount(address,uint64)', 'releasable(address)', this.token.address]
|
||||
: ['vestedAmount(uint64)', 'releasable()'];
|
||||
|
||||
for (const timestamp of this.schedule) {
|
||||
await time.increaseTo(timestamp);
|
||||
const vesting = this.vestingFn(timestamp);
|
||||
|
||||
expect(await this.mock.methods[vestedAmount](...args, timestamp)).to.be.bignumber.equal(vesting);
|
||||
|
||||
expect(await this.mock.methods[releasable](...args)).to.be.bignumber.equal(vesting);
|
||||
}
|
||||
});
|
||||
|
||||
it('execute vesting schedule', async function () {
|
||||
const [release, ...args] = this.token ? ['release(address)', this.token.address] : ['release()'];
|
||||
|
||||
let released = web3.utils.toBN(0);
|
||||
const before = await this.getBalance(beneficiary);
|
||||
|
||||
{
|
||||
const receipt = await this.mock.methods[release](...args);
|
||||
|
||||
await expectEvent.inTransaction(receipt.tx, this.mock, ...releasedEvent(this.token, '0'));
|
||||
|
||||
await this.checkRelease(receipt, beneficiary, '0');
|
||||
|
||||
expect(await this.getBalance(beneficiary)).to.be.bignumber.equal(before);
|
||||
}
|
||||
|
||||
for (const timestamp of this.schedule) {
|
||||
await time.setNextBlockTimestamp(timestamp);
|
||||
const vested = this.vestingFn(timestamp);
|
||||
|
||||
const receipt = await this.mock.methods[release](...args);
|
||||
await expectEvent.inTransaction(receipt.tx, this.mock, ...releasedEvent(this.token, vested.sub(released)));
|
||||
|
||||
await this.checkRelease(receipt, beneficiary, vested.sub(released));
|
||||
|
||||
expect(await this.getBalance(beneficiary)).to.be.bignumber.equal(before.add(vested));
|
||||
|
||||
released = vested;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
shouldBehaveLikeVesting,
|
||||
};
|
||||
@@ -0,0 +1,67 @@
|
||||
const { constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
|
||||
const { web3 } = require('@openzeppelin/test-helpers/src/setup');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const VestingWallet = artifacts.require('VestingWallet');
|
||||
const ERC20 = artifacts.require('$ERC20');
|
||||
|
||||
const { shouldBehaveLikeVesting } = require('./VestingWallet.behavior');
|
||||
|
||||
const min = (...args) => args.slice(1).reduce((x, y) => (x.lt(y) ? x : y), args[0]);
|
||||
|
||||
contract('VestingWallet', function (accounts) {
|
||||
const [sender, beneficiary] = accounts;
|
||||
|
||||
const amount = web3.utils.toBN(web3.utils.toWei('100'));
|
||||
const duration = web3.utils.toBN(4 * 365 * 86400); // 4 years
|
||||
|
||||
beforeEach(async function () {
|
||||
this.start = (await time.latest()).addn(3600); // in 1 hour
|
||||
this.mock = await VestingWallet.new(beneficiary, this.start, duration);
|
||||
});
|
||||
|
||||
it('rejects zero address for beneficiary', async function () {
|
||||
await expectRevert(
|
||||
VestingWallet.new(constants.ZERO_ADDRESS, this.start, duration),
|
||||
'VestingWallet: beneficiary is zero address',
|
||||
);
|
||||
});
|
||||
|
||||
it('check vesting contract', async function () {
|
||||
expect(await this.mock.beneficiary()).to.be.equal(beneficiary);
|
||||
expect(await this.mock.start()).to.be.bignumber.equal(this.start);
|
||||
expect(await this.mock.duration()).to.be.bignumber.equal(duration);
|
||||
});
|
||||
|
||||
describe('vesting schedule', function () {
|
||||
beforeEach(async function () {
|
||||
this.schedule = Array(64)
|
||||
.fill()
|
||||
.map((_, i) => web3.utils.toBN(i).mul(duration).divn(60).add(this.start));
|
||||
this.vestingFn = timestamp => min(amount, amount.mul(timestamp.sub(this.start)).div(duration));
|
||||
});
|
||||
|
||||
describe('Eth vesting', function () {
|
||||
beforeEach(async function () {
|
||||
await web3.eth.sendTransaction({ from: sender, to: this.mock.address, value: amount });
|
||||
this.getBalance = account => web3.eth.getBalance(account).then(web3.utils.toBN);
|
||||
this.checkRelease = () => {};
|
||||
});
|
||||
|
||||
shouldBehaveLikeVesting(beneficiary);
|
||||
});
|
||||
|
||||
describe('ERC20 vesting', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = await ERC20.new('Name', 'Symbol');
|
||||
this.getBalance = account => this.token.balanceOf(account);
|
||||
this.checkRelease = (receipt, to, value) =>
|
||||
expectEvent.inTransaction(receipt.tx, this.token, 'Transfer', { from: this.mock.address, to, value });
|
||||
|
||||
await this.token.$_mint(this.mock.address, amount);
|
||||
});
|
||||
|
||||
shouldBehaveLikeVesting(beneficiary);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user