-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
181e3c7
commit 4e1d4ef
Showing
3 changed files
with
209 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,192 @@ | ||
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; | ||
import { expect } from 'chai'; | ||
import { deployMockContract } from 'ethereum-waffle'; | ||
import { Contract, Signer } from 'ethers'; | ||
import { ethers, artifacts } from 'hardhat'; | ||
import { BigNumber, Contract, ContractFactory } from 'ethers'; | ||
import { ethers } from 'hardhat'; | ||
|
||
const { getSigners } = ethers; | ||
const { constants, getContractFactory, getSigners, utils } = ethers; | ||
const { AddressZero, Zero } = constants; | ||
const { parseEther: toWei } = utils; | ||
|
||
describe('Vault', () => { | ||
let TokenVaultFactory: ContractFactory; | ||
let tokenVault: Contract; | ||
let token: Contract; | ||
|
||
let wallet1: SignerWithAddress; | ||
let wallet2: SignerWithAddress; | ||
let owner: SignerWithAddress; | ||
let manager: SignerWithAddress; | ||
let wallet3: SignerWithAddress; | ||
let wallet4: SignerWithAddress; | ||
|
||
let constructorTest = false; | ||
|
||
beforeEach(async () => { | ||
[wallet1, wallet2, wallet3, wallet4] = await getSigners(); | ||
[owner, manager, wallet3, wallet4] = await getSigners(); | ||
|
||
const ERC20MintableContract = await getContractFactory('ERC20Mintable', owner); | ||
token = await ERC20MintableContract.deploy('PoolTogether', 'POOL'); | ||
|
||
const IERC20Artifact = await artifacts.readArtifact('IERC20'); | ||
token = await deployMockContract(wallet1 as Signer, IERC20Artifact.abi); | ||
TokenVaultFactory = await getContractFactory('TokenVault', owner); | ||
|
||
const TokenVaultFactory = await ethers.getContractFactory('TokenVault', wallet1); | ||
tokenVault = await TokenVaultFactory.deploy(wallet1.address); | ||
if (!constructorTest) { | ||
tokenVault = await TokenVaultFactory.deploy(owner.address); | ||
} | ||
}); | ||
|
||
describe('constructor', () => { | ||
beforeEach(() => { | ||
constructorTest = true; | ||
}); | ||
|
||
afterEach(() => { | ||
constructorTest = false; | ||
}); | ||
|
||
it('should set the owner', async () => { | ||
expect(await tokenVault.owner()).to.equal(wallet1.address) | ||
}) | ||
}) | ||
tokenVault = await TokenVaultFactory.deploy(owner.address); | ||
expect(await tokenVault.owner()).to.equal(owner.address); | ||
}); | ||
|
||
it('should fail if owner is address zero', async () => { | ||
await expect(TokenVaultFactory.deploy(AddressZero)).to.be.revertedWith( | ||
'TVault/owner-not-zero-address', | ||
); | ||
}); | ||
}); | ||
|
||
describe('setApproval()', () => { | ||
it('should allow the owner to approve accounts', async () => { | ||
await tokenVault.setApproved(wallet2.address, true) | ||
expect(await tokenVault.approved(wallet2.address)).to.equal(true) | ||
}) | ||
}) | ||
await expect(tokenVault.setApproval(wallet3.address, true)) | ||
.to.emit(tokenVault, 'Approved') | ||
.withArgs(wallet3.address, true); | ||
|
||
expect(await tokenVault.approved(wallet3.address)).to.equal(true); | ||
}); | ||
|
||
it('should fail if not owner', async () => { | ||
await expect( | ||
tokenVault.connect(manager).setApproval(wallet3.address, true), | ||
).to.be.revertedWith('Ownable/caller-not-owner'); | ||
}); | ||
}); | ||
|
||
describe('increaseERC20Allowance()', () => { | ||
it('should allow owners to increase approval amount', async () => { | ||
await tokenVault.setApproved(wallet2.address, true) | ||
await token.mock.allowance.withArgs(tokenVault.address, wallet2.address).returns('0') | ||
await token.mock.approve.withArgs(wallet2.address, '1111').returns(true) | ||
await tokenVault.increaseERC20Allowance(token.address, wallet2.address, '1111') | ||
}) | ||
|
||
it('should allow managers to increase approval amount', async () => { | ||
await tokenVault.setManager(wallet3.address) | ||
await tokenVault.setApproved(wallet2.address, true) | ||
await token.mock.allowance.withArgs(tokenVault.address, wallet2.address).returns('0') | ||
await token.mock.approve.withArgs(wallet2.address, '1111').returns(true) | ||
await tokenVault.connect(wallet3).increaseERC20Allowance(token.address, wallet2.address, '1111') | ||
}) | ||
}) | ||
let increaseAllowanceAmount: BigNumber; | ||
|
||
beforeEach(async () => { | ||
increaseAllowanceAmount = toWei('1111'); | ||
}); | ||
|
||
it('should allow owner to increase approval amount', async () => { | ||
await tokenVault.setApproval(wallet3.address, true); | ||
await tokenVault.increaseERC20Allowance( | ||
token.address, | ||
wallet3.address, | ||
increaseAllowanceAmount, | ||
); | ||
|
||
expect(await token.allowance(tokenVault.address, wallet3.address)).to.equal( | ||
increaseAllowanceAmount, | ||
); | ||
}); | ||
|
||
it('should allow manager to increase approval amount', async () => { | ||
await tokenVault.setApproval(wallet3.address, true); | ||
await tokenVault.setManager(manager.address); | ||
|
||
await tokenVault | ||
.connect(manager) | ||
.increaseERC20Allowance(token.address, wallet3.address, increaseAllowanceAmount); | ||
|
||
expect(await token.allowance(tokenVault.address, wallet3.address)).to.equal( | ||
increaseAllowanceAmount, | ||
); | ||
}); | ||
|
||
it('should fail if spender is not approved', async () => { | ||
await expect( | ||
tokenVault.increaseERC20Allowance( | ||
token.address, | ||
wallet3.address, | ||
increaseAllowanceAmount, | ||
), | ||
).to.be.revertedWith('TVault/spender-not-approved'); | ||
}); | ||
|
||
it('should fail if not owner or manager', async () => { | ||
await tokenVault.setManager(wallet3.address); | ||
|
||
await expect( | ||
tokenVault | ||
.connect(wallet4) | ||
.increaseERC20Allowance( | ||
token.address, | ||
manager.address, | ||
increaseAllowanceAmount, | ||
), | ||
).to.be.revertedWith('Manageable/caller-not-manager-or-owner'); | ||
}); | ||
}); | ||
|
||
describe('decreaseERC20Allowance()', () => { | ||
it('should allow manager to decrease approval amount', async () => { | ||
await token.mock.allowance.withArgs(tokenVault.address, wallet2.address).returns('1111') | ||
await token.mock.approve.withArgs(wallet2.address, '111').returns(true) | ||
await tokenVault.decreaseERC20Allowance(token.address, wallet2.address, '1000') | ||
}) | ||
|
||
it('should allow manager to decrease approval amount', async () => { | ||
await tokenVault.setManager(wallet3.address) | ||
await token.mock.allowance.withArgs(tokenVault.address, wallet2.address).returns('1111') | ||
await token.mock.approve.withArgs(wallet2.address, '0').returns(true) | ||
await tokenVault.connect(wallet3).decreaseERC20Allowance(token.address, wallet2.address, '1111') | ||
}) | ||
}) | ||
let decreaseAllowanceAmount: BigNumber; | ||
let increaseAllowanceAmount: BigNumber; | ||
|
||
beforeEach(async () => { | ||
decreaseAllowanceAmount = toWei('111'); | ||
increaseAllowanceAmount = toWei('1111'); | ||
|
||
await tokenVault.setApproval(wallet3.address, true); | ||
await tokenVault.increaseERC20Allowance( | ||
token.address, | ||
wallet3.address, | ||
increaseAllowanceAmount, | ||
); | ||
}); | ||
|
||
it('should allow owner to decrease approval amount', async () => { | ||
await tokenVault.decreaseERC20Allowance( | ||
token.address, | ||
wallet3.address, | ||
decreaseAllowanceAmount, | ||
); | ||
|
||
expect(await token.allowance(tokenVault.address, wallet3.address)).to.equal( | ||
increaseAllowanceAmount.sub(decreaseAllowanceAmount), | ||
); | ||
}); | ||
|
||
it('should allow owner to decrease approval amount', async () => { | ||
await tokenVault.setManager(manager.address); | ||
|
||
await tokenVault | ||
.connect(manager) | ||
.decreaseERC20Allowance(token.address, wallet3.address, decreaseAllowanceAmount); | ||
|
||
expect(await token.allowance(tokenVault.address, wallet3.address)).to.equal( | ||
increaseAllowanceAmount.sub(decreaseAllowanceAmount), | ||
); | ||
}); | ||
|
||
it('should decrease the full approval amount', async () => { | ||
await tokenVault.decreaseERC20Allowance( | ||
token.address, | ||
wallet3.address, | ||
increaseAllowanceAmount, | ||
); | ||
|
||
expect(await token.allowance(tokenVault.address, wallet3.address)).to.equal(Zero); | ||
}); | ||
|
||
it('should fail if not owner of manager', async () => { | ||
await expect( | ||
tokenVault | ||
.connect(wallet4) | ||
.decreaseERC20Allowance( | ||
token.address, | ||
wallet3.address, | ||
decreaseAllowanceAmount, | ||
), | ||
).to.be.revertedWith('Manageable/caller-not-manager-or-owner'); | ||
}); | ||
}); | ||
}); |