Skip to content

Commit

Permalink
Require new collateral is sound when normalizing weights of a new bas…
Browse files Browse the repository at this point in the history
…ket (#1105)
  • Loading branch information
tbrent authored Apr 2, 2024
1 parent ce79856 commit 639f232
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions contracts/p0/BasketHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ contract BasketHandlerP0 is ComponentP0, IBasketHandler {
uint192 newP; // {UoA/BU}
for (uint256 i = 0; i < len; ++i) {
ICollateral coll = main.assetRegistry().toColl(erc20s[i]); // reverts if unregistered
require(coll.status() == CollateralStatus.SOUND, "unsound new collateral");

(low, high) = coll.price(); // {UoA/tok}
require(low > 0 && high < FIX_MAX, "invalid price");
Expand Down
1 change: 1 addition & 0 deletions contracts/p1/mixins/BasketLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ library BasketLibP1 {
uint192 newPrice; // {UoA/BU}
for (uint256 i = 0; i < len; ++i) {
ICollateral coll = assetRegistry.toColl(erc20s[i]); // reverts if unregistered
require(coll.status() == CollateralStatus.SOUND, "unsound new collateral");

(uint192 low, uint192 high) = coll.price(); // {UoA/tok}
require(low > 0 && high < FIX_MAX, "invalid price");
Expand Down
29 changes: 26 additions & 3 deletions test/Main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2136,11 +2136,10 @@ describe(`MainP${IMPLEMENTATION} contract`, () => {
).to.be.revertedWith('new target weights')
})

it('Should normalize price by USD for index RTokens', async () => {
// Basket starts out worth $1 and holding USD targets
it('Should normalize by price for index RTokens', async () => {
// Throughout this test the $ value of the RToken should remain

// Group the 4 USD tokens together
// Set initial basket
await indexBH.connect(owner).setPrimeBasket([token0.address], [fp('1')])
await indexBH.connect(owner).refreshBasket()
let [erc20s, tokAmts] = await indexBH.quote(fp('1'), 0)
Expand Down Expand Up @@ -2195,6 +2194,30 @@ describe(`MainP${IMPLEMENTATION} contract`, () => {
expect(tokAmts[1]).to.equal(fp('0.5'))
})

it('Should not normalize by price when the current basket is unsound', async () => {
await indexBH.connect(owner).setPrimeBasket([token0.address], [fp('1')])
await indexBH.connect(owner).refreshBasket()
await setOraclePrice(collateral0.address, fp('0.5'))
await assetRegistry.refresh()
expect(await collateral0.status()).to.equal(CollateralStatus.IFFY)
expect(await collateral1.status()).to.equal(CollateralStatus.SOUND)
await expect(
indexBH.connect(owner).setPrimeBasket([token1.address], [fp('1')])
).to.be.revertedWith('unsound basket')
})

it('Should not normalize by price if the new collateral is unsound', async () => {
await indexBH.connect(owner).setPrimeBasket([token0.address], [fp('1')])
await indexBH.connect(owner).refreshBasket()
await setOraclePrice(collateral1.address, fp('0.5'))
await assetRegistry.refresh()
expect(await collateral0.status()).to.equal(CollateralStatus.SOUND)
expect(await collateral1.status()).to.equal(CollateralStatus.IFFY)
await expect(
indexBH.connect(owner).setPrimeBasket([token1.address], [fp('1')])
).to.be.revertedWith('unsound new collateral')
})

describe('Custom Redemption', () => {
const issueAmount = fp('10000')
let usdcChainlink: MockV3Aggregator
Expand Down

0 comments on commit 639f232

Please sign in to comment.