Skip to content

Commit

Permalink
Merge pull request #19 from pooltogether/fixes/cmichel-81
Browse files Browse the repository at this point in the history
fix(contract): check address params in initialize
  • Loading branch information
PierrickGT authored Jul 7, 2021
2 parents 8a74057 + 7f7a871 commit 0ea93ca
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 27 deletions.
6 changes: 5 additions & 1 deletion contracts/yield-source/ATokenYieldSource.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ contract ATokenYieldSource is ERC20Upgradeable, IProtocolYieldSource, AssetManag
initializer
returns (bool)
{
require(address(_aToken) != address(0), "ATokenYieldSource/aToken-not-zero-address");
aToken = _aToken;

require(address(_lendingPoolAddressesProviderRegistry) != address(0), "ATokenYieldSource/lendingPoolRegistry-not-zero-address");
lendingPoolAddressesProviderRegistry = _lendingPoolAddressesProviderRegistry;

require(_owner != address(0), "ATokenYieldSource/owner-not-zero-address");
__Ownable_init();
transferOwnership(_owner);

Expand Down Expand Up @@ -201,7 +205,7 @@ contract ATokenYieldSource is ERC20Upgradeable, IProtocolYieldSource, AssetManag
function supplyTokenTo(uint256 mintAmount, address to) external override nonReentrant {
uint256 shares = _tokenToShares(mintAmount);

require(shares > 0, "ATokenYieldSource/shares-equal-zero");
require(shares > 0, "ATokenYieldSource/shares-gt-zero");
_depositToAave(mintAmount);
_mint(to, shares);

Expand Down
132 changes: 106 additions & 26 deletions test/ATokenYieldSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,23 @@ describe('ATokenYieldSource', () => {
let erc20Token: ERC20;
let underlyingToken: IERC20Upgradeable;

// Numerical error tests for shares decreasing
let isInitializeTest = false;

const initializeATokenYieldSource = async (
aTokenAddress: string,
lendingPoolAddressesProviderRegistryAddress: string,
decimals: number,
owner: string
) => {
await aTokenYieldSource.initialize(
aTokenAddress,
lendingPoolAddressesProviderRegistryAddress,
decimals,
'Test',
'TEST',
owner,
);
};

beforeEach(async () => {
const { deployMockContract } = waffle;
Expand Down Expand Up @@ -88,10 +104,8 @@ describe('ATokenYieldSource', () => {

debug('deploying ATokenYieldSource instance...');

const ATokenYieldSource = await ethers.getContractFactory(
'ATokenYieldSourceHarness',
);
const hardhatATokenYieldSourceHarness = await ATokenYieldSource.deploy()
const ATokenYieldSource = await ethers.getContractFactory('ATokenYieldSourceHarness');
const hardhatATokenYieldSourceHarness = await ATokenYieldSource.deploy();

aTokenYieldSource = (await ethers.getContractAt(
'ATokenYieldSourceHarness',
Expand All @@ -102,15 +116,74 @@ describe('ATokenYieldSource', () => {
await underlyingToken.mock.allowance.withArgs(aTokenYieldSource.address, lendingPool.address).returns(ethers.constants.Zero);
await underlyingToken.mock.approve.withArgs(lendingPool.address, ethers.constants.MaxUint256).returns(true);

await aTokenYieldSource.initialize(
aToken.address,
lendingPoolAddressesProviderRegistry.address,
18,
"Test",
"TEST",
yieldSourceOwner.address
);
if (!isInitializeTest) {
await initializeATokenYieldSource(
aToken.address,
lendingPoolAddressesProviderRegistry.address,
18,
yieldSourceOwner.address
);
}
});

describe('initialize()', () => {
let randomWalletAddress: string;

before(() => {
isInitializeTest = true;
});

beforeEach(() => {
randomWalletAddress = ethers.Wallet.createRandom().address;
});

after(() => {
isInitializeTest = false;
});

it('should fail if aToken is address zero', async () => {
await expect(
initializeATokenYieldSource(
ethers.constants.AddressZero,
lendingPoolAddressesProviderRegistry.address,
18,
yieldSourceOwner.address
),
).to.be.revertedWith('ATokenYieldSource/aToken-not-zero-address');
});

it('should fail if lendingPoolAddressesProviderRegistry is address zero', async () => {
await expect(
initializeATokenYieldSource(
aToken.address,
ethers.constants.AddressZero,
18,
yieldSourceOwner.address
),
).to.be.revertedWith('ATokenYieldSource/lendingPoolRegistry-not-zero-address');
});

it('should fail if owner is address zero', async () => {
await expect(
initializeATokenYieldSource(
aToken.address,
lendingPoolAddressesProviderRegistry.address,
18,
ethers.constants.AddressZero
),
).to.be.revertedWith('ATokenYieldSource/owner-not-zero-address');
});

it('should fail if token decimal is not greater than 0', async () => {
await expect(
initializeATokenYieldSource(
aToken.address,
lendingPoolAddressesProviderRegistry.address,
0,
yieldSourceOwner.address
),
).to.be.revertedWith('ATokenYieldSource/decimals-gt-zero');
});
});

describe('create()', () => {
Expand Down Expand Up @@ -164,7 +237,7 @@ describe('ATokenYieldSource', () => {
});

it('should return 0 if tokens param is 0', async () => {
expect(await aTokenYieldSource.tokenToShares("0")).to.equal("0");
expect(await aTokenYieldSource.tokenToShares('0')).to.equal('0');
});

it('should return tokens if totalSupply is 0', async () => {
Expand All @@ -177,7 +250,9 @@ describe('ATokenYieldSource', () => {
.withArgs(aTokenYieldSource.address)
.returns(toWei('0.000000000000000005'));

expect(await aTokenYieldSource.tokenToShares(toWei('0.000000000000000005'))).to.equal(toWei('1'));
expect(await aTokenYieldSource.tokenToShares(toWei('0.000000000000000005'))).to.equal(
toWei('1'),
);
});

it('should return shares even if aToken total supply increases', async () => {
Expand All @@ -187,21 +262,25 @@ describe('ATokenYieldSource', () => {

expect(await aTokenYieldSource.tokenToShares(toWei('1'))).to.equal(toWei('2'));

await aToken.mock.balanceOf.withArgs(aTokenYieldSource.address).returns(ethers.utils.parseUnits('100', 36));
await aToken.mock.balanceOf
.withArgs(aTokenYieldSource.address)
.returns(ethers.utils.parseUnits('100', 36));
expect(await aTokenYieldSource.tokenToShares(toWei('1'))).to.equal(2);
});

it('should fail to return shares if aToken total supply increases too much', async () => { // failing here

it('should fail to return shares if aToken total supply increases too much', async () => {
await aTokenYieldSource.mint(yieldSourceOwner.address, toWei('100'));
await aTokenYieldSource.mint(wallet2.address, toWei('100'));
await aToken.mock.balanceOf.withArgs(aTokenYieldSource.address).returns(toWei('100'));

expect(await aTokenYieldSource.tokenToShares(toWei('1'))).to.equal(toWei('2'));

await aToken.mock.balanceOf.withArgs(aTokenYieldSource.address).returns(ethers.utils.parseUnits('100', 37));
await expect(aTokenYieldSource.supplyTokenTo(toWei('1'), wallet2.address)).to.be.revertedWith('ATokenYieldSource/shares-equal-zero');

await aToken.mock.balanceOf
.withArgs(aTokenYieldSource.address)
.returns(ethers.utils.parseUnits('100', 37));
await expect(aTokenYieldSource.supplyTokenTo(toWei('1'), wallet2.address)).to.be.revertedWith(
'ATokenYieldSource/shares-gt-zero',
);
});
});

Expand All @@ -222,7 +301,9 @@ describe('ATokenYieldSource', () => {
await aTokenYieldSource.mint(yieldSourceOwner.address, toWei('0.000000000000000005'));
await aToken.mock.balanceOf.withArgs(aTokenYieldSource.address).returns(toWei('100'));

expect(await aTokenYieldSource.sharesToToken(toWei('0.000000000000000005'))).to.equal(toWei('100'));
expect(await aTokenYieldSource.sharesToToken(toWei('0.000000000000000005'))).to.equal(
toWei('100'),
);
});

it('should return tokens even if aToken total supply increases', async () => {
Expand All @@ -232,7 +313,9 @@ describe('ATokenYieldSource', () => {

expect(await aTokenYieldSource.sharesToToken(toWei('2'))).to.equal(toWei('1'));

await aToken.mock.balanceOf.withArgs(aTokenYieldSource.address).returns(ethers.utils.parseUnits('100', 36));
await aToken.mock.balanceOf
.withArgs(aTokenYieldSource.address)
.returns(ethers.utils.parseUnits('100', 36));
expect(await aTokenYieldSource.sharesToToken(2)).to.equal(toWei('1'));
});
});
Expand Down Expand Up @@ -459,6 +542,3 @@ describe('ATokenYieldSource', () => {
});
});
});
function toHex(arg0: number) {
throw new Error('Function not implemented.');
}

0 comments on commit 0ea93ca

Please sign in to comment.