-
Notifications
You must be signed in to change notification settings - Fork 66
Feature: Update all capital pool unit tests to use ethers instead of truffle/web3 #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
0xdewy
merged 2 commits into
feature/upgrade-capital-pool-assets
from
feature/update-pool-tests-to-ethers
Jan 4, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,161 +1,154 @@ | ||
const { artifacts, web3 } = require('hardhat'); | ||
const { | ||
constants: { ZERO_ADDRESS }, | ||
ether, | ||
expectRevert, | ||
} = require('@openzeppelin/test-helpers'); | ||
const { assert } = require('chai'); | ||
const { ethers } = require('hardhat'); | ||
const { expect } = require('chai'); | ||
const { AddressZero, WeiPerEther } = ethers.constants; | ||
const { parseEther } = ethers.utils; | ||
const { hex } = require('../utils').helpers; | ||
const { | ||
governanceContracts: [governance], | ||
} = require('../utils').accounts; | ||
const { BN } = web3.utils; | ||
|
||
const PriceFeedOracle = artifacts.require('PriceFeedOracle'); | ||
const ChainlinkAggregatorMock = artifacts.require('ChainlinkAggregatorMock'); | ||
|
||
describe('addAsset', function () { | ||
it('reverts when not called by goverance', async function () { | ||
const { pool, otherAsset } = this; | ||
|
||
await expectRevert( | ||
pool.addAsset(otherAsset.address, 18, '0', '1', '0', false), | ||
await expect(pool.addAsset(otherAsset.address, 18, '0', '1', '0', false)).to.be.revertedWith( | ||
'Caller is not authorized to govern', | ||
); | ||
await expectRevert( | ||
pool.addAsset(otherAsset.address, 18, '0', '1', '0', true), | ||
await expect(pool.addAsset(otherAsset.address, 18, '0', '1', '0', true)).to.be.revertedWith( | ||
'Caller is not authorized to govern', | ||
); | ||
}); | ||
|
||
it('reverts when asset address is zero address', async function () { | ||
const { pool } = this; | ||
const [governance] = this.accounts.governanceContracts; | ||
|
||
await expectRevert( | ||
pool.addAsset(ZERO_ADDRESS, 18, '0', '1', '0', false, { from: governance }), | ||
await expect(pool.connect(governance).addAsset(AddressZero, 18, '0', '1', '0', false)).to.be.revertedWith( | ||
'Pool: Asset is zero address', | ||
); | ||
|
||
await expectRevert( | ||
pool.addAsset(ZERO_ADDRESS, 18, '0', '1', '0', true, { from: governance }), | ||
await expect(pool.connect(governance).addAsset(AddressZero, 18, '0', '1', '0', true)).to.be.revertedWith( | ||
'Pool: Asset is zero address', | ||
); | ||
}); | ||
|
||
it('reverts when max < min', async function () { | ||
const { pool, otherAsset } = this; | ||
const [governance] = this.accounts.governanceContracts; | ||
|
||
await expectRevert( | ||
pool.addAsset(otherAsset.address, 18, '1', '0', '0', true, { from: governance }), | ||
await expect(pool.connect(governance).addAsset(otherAsset.address, 18, '1', '0', '0', true)).to.be.revertedWith( | ||
'Pool: max < min', | ||
); | ||
await expectRevert( | ||
pool.addAsset(otherAsset.address, 18, '1', '0', '0', false, { from: governance }), | ||
await expect(pool.connect(governance).addAsset(otherAsset.address, 18, '1', '0', '0', false)).to.be.revertedWith( | ||
'Pool: max < min', | ||
); | ||
}); | ||
|
||
it('reverts when max slippage ratio > 1', async function () { | ||
const { pool, otherAsset } = this; | ||
|
||
await expectRevert( | ||
pool.addAsset(otherAsset.address, 18, '0', '1', 10001 /* 100.01% */, false, { from: governance }), | ||
'Pool: Max slippage ratio > 1', | ||
); | ||
const [governance] = this.accounts.governanceContracts; | ||
await expect( | ||
pool.connect(governance).addAsset(otherAsset.address, 18, '0', '1', '10001' /* 100.01% */, false), | ||
).to.be.revertedWith('Pool: Max slippage ratio > 1'); | ||
|
||
// should work with slippage rate = 1 | ||
await pool.addAsset(otherAsset.address, 18, '0', '1', 10000 /* 100% */, false, { from: governance }); | ||
await pool.connect(governance).addAsset(otherAsset.address, 18, '0', '1', '10000', false); | ||
}); | ||
|
||
it('reverts when asset exists', async function () { | ||
const { pool, dai } = this; | ||
const [governance] = this.accounts.governanceContracts; | ||
|
||
await expectRevert( | ||
pool.addAsset(dai.address, 18, '0', '1', '0', false, { from: governance }), | ||
await expect(pool.connect(governance).addAsset(dai.address, 18, '0', '1', '0', false)).to.be.revertedWith( | ||
'Pool: Asset exists', | ||
); | ||
await expect(pool.connect(governance).addAsset(dai.address, 18, '0', '1', '0', true)).to.be.revertedWith( | ||
'Pool: Asset exists', | ||
); | ||
await expectRevert(pool.addAsset(dai.address, 18, '0', '1', '0', true, { from: governance }), 'Pool: Asset exists'); | ||
}); | ||
|
||
it('reverts when asset lacks an oracle', async function () { | ||
const { pool } = this; | ||
const [governance] = this.accounts.governanceContracts; | ||
|
||
const arbitraryAddress = '0x47ec31abc6b86e49933dC7B2969EBEbE3De662cA'; | ||
|
||
await expectRevert( | ||
pool.addAsset(arbitraryAddress, 18, '0', '1', '0', true, { from: governance }), | ||
await expect(pool.connect(governance).addAsset(arbitraryAddress, 18, '0', '1', '0', true)).to.be.revertedWith( | ||
'Pool: Asset lacks oracle', | ||
); | ||
}); | ||
|
||
it('should correctly add the asset with its min, max, and slippage ratio', async function () { | ||
const { pool, dai, stETH, chainlinkDAI, chainlinkSteth } = this; | ||
const [governance] = this.accounts.governanceContracts; | ||
|
||
const ERC20Mock = artifacts.require('ERC20Mock'); | ||
const token = await ERC20Mock.new(); | ||
const ChainlinkAggregatorMock = await ethers.getContractFactory('ChainlinkAggregatorMock'); | ||
const PriceFeedOracle = await ethers.getContractFactory('PriceFeedOracle'); | ||
const ERC20Mock = await ethers.getContractFactory('ERC20Mock'); | ||
const token = await ERC20Mock.deploy(); | ||
|
||
const chainlinkNewAsset = await ChainlinkAggregatorMock.new(); | ||
await chainlinkNewAsset.setLatestAnswer(new BN((1e18).toString())); | ||
const priceFeedOracle = await PriceFeedOracle.new( | ||
const chainlinkNewAsset = await ChainlinkAggregatorMock.deploy(); | ||
await chainlinkNewAsset.setLatestAnswer(WeiPerEther); | ||
const priceFeedOracle = await PriceFeedOracle.deploy( | ||
[dai.address, stETH.address, token.address], | ||
[chainlinkDAI.address, chainlinkSteth.address, chainlinkNewAsset.address], | ||
[18, 18, 18], | ||
); | ||
|
||
await pool.updateAddressParameters(hex('PRC_FEED'), priceFeedOracle.address, { from: governance }); | ||
await pool.connect(governance).updateAddressParameters(hex('PRC_FEED'), priceFeedOracle.address); | ||
|
||
await pool.addAsset(token.address, 18, '1', '2', '3', true, { from: governance }); | ||
await token.mint(pool.address, ether('100')); | ||
await pool.connect(governance).addAsset(token.address, 18, '1', '2', '3', true); | ||
await token.mint(pool.address, parseEther('100')); | ||
|
||
const assetDetails = await pool.getAssetSwapDetails(token.address); | ||
const { minAmount, maxAmount, maxSlippageRatio } = assetDetails; | ||
|
||
assert.strictEqual(minAmount.toString(), '1'); | ||
assert.strictEqual(maxAmount.toString(), '2'); | ||
assert.strictEqual(maxSlippageRatio.toString(), '3'); | ||
expect(minAmount).to.be.equal(1); | ||
expect(maxAmount).to.be.equal(2); | ||
expect(maxSlippageRatio).to.be.equal(3); | ||
}); | ||
|
||
it('should correctly add the asset to either investment or cover asset arrays', async function () { | ||
const { pool, dai, stETH, chainlinkDAI, chainlinkSteth } = this; | ||
const [governance] = this.accounts.governanceContracts; | ||
|
||
const ERC20Mock = artifacts.require('ERC20Mock'); | ||
const ChainlinkAggregatorMock = await ethers.getContractFactory('ChainlinkAggregatorMock'); | ||
const PriceFeedOracle = await ethers.getContractFactory('PriceFeedOracle'); | ||
const ERC20Mock = await ethers.getContractFactory('ERC20Mock'); | ||
|
||
const coverAsset = await ERC20Mock.new(); | ||
const investmentAsset = await ERC20Mock.new(); | ||
const coverAsset = await ERC20Mock.deploy(); | ||
const investmentAsset = await ERC20Mock.deploy(); | ||
|
||
const chainlinkNewAsset = await ChainlinkAggregatorMock.new(); | ||
await chainlinkNewAsset.setLatestAnswer(new BN((1e18).toString())); | ||
const chainlinkNewAsset = await ChainlinkAggregatorMock.deploy(); | ||
await chainlinkNewAsset.setLatestAnswer(WeiPerEther); | ||
|
||
const priceFeedOracle = await PriceFeedOracle.new( | ||
const priceFeedOracle = await PriceFeedOracle.deploy( | ||
[dai.address, stETH.address, coverAsset.address, investmentAsset.address], | ||
[chainlinkDAI.address, chainlinkSteth.address, chainlinkNewAsset.address, chainlinkNewAsset.address], | ||
[18, 18, 18, 18], | ||
); | ||
|
||
await pool.updateAddressParameters(hex('PRC_FEED'), priceFeedOracle.address, { from: governance }); | ||
await pool.connect(governance).updateAddressParameters(hex('PRC_FEED'), priceFeedOracle.address); | ||
|
||
// Cover asset | ||
{ | ||
const token = coverAsset; | ||
await pool.addAsset(token.address, 18, '1', '2', '3', true, { from: governance }); | ||
await pool.connect(governance).addAsset(token.address, 18, '1', '2', '3', true); | ||
const coverAssets = await pool.getCoverAssets(); | ||
const investmentAssets = await pool.getInvestmentAssets(); | ||
assert.strictEqual(coverAssets[coverAssets.length - 1].assetAddress, token.address); | ||
assert.strictEqual(coverAssets[coverAssets.length - 1].decimals, '18'); | ||
expect(coverAssets[coverAssets.length - 1].assetAddress).to.be.equal(token.address); | ||
expect(coverAssets[coverAssets.length - 1].decimals).to.be.equal(18); | ||
const insertedInInvestmentAssets = !!investmentAssets.find(x => x.assetAddress === token.address); | ||
assert.strictEqual(insertedInInvestmentAssets, false); | ||
expect(insertedInInvestmentAssets).to.be.equal(false); | ||
} | ||
|
||
// Investment asset | ||
{ | ||
const token = investmentAsset; | ||
await pool.addAsset(token.address, 8, '4', '5', '6', false, { from: governance }); | ||
await pool.connect(governance).addAsset(token.address, 8, '4', '5', '6', false); | ||
const coverAssets = await pool.getCoverAssets(); | ||
const investmentAssets = await pool.getInvestmentAssets(); | ||
assert.strictEqual(investmentAssets[investmentAssets.length - 1].assetAddress, token.address); | ||
assert.strictEqual(investmentAssets[investmentAssets.length - 1].decimals, '8'); | ||
expect(investmentAssets[investmentAssets.length - 1].assetAddress).to.be.equal(token.address); | ||
expect(investmentAssets[investmentAssets.length - 1].decimals).to.be.equal(8); | ||
const insertedInCoverAssets = !!coverAssets.find(x => x.assetAddress === token.address); | ||
assert.strictEqual(insertedInCoverAssets, false); | ||
expect(insertedInCoverAssets).to.be.equal(false); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was reverting without the 0 padding. I wasn't sure if the other ones were ok, so I added it to the others also.