From 87012bad1ddc57239084de4cef836ee9ea976fc2 Mon Sep 17 00:00:00 2001 From: Patrick McKelvy Date: Wed, 26 Jul 2023 16:16:52 -0400 Subject: [PATCH] useAvailable when setting throttles. --- contracts/p1/RToken.sol | 2 ++ test/RToken.test.ts | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/contracts/p1/RToken.sol b/contracts/p1/RToken.sol index 942e2d2501..f68a43a53e 100644 --- a/contracts/p1/RToken.sol +++ b/contracts/p1/RToken.sol @@ -442,6 +442,7 @@ contract RTokenP1 is ComponentP1, ERC20PermitUpgradeable, IRToken { require(params.amtRate >= MIN_THROTTLE_RATE_AMT, "issuance amtRate too small"); require(params.amtRate <= MAX_THROTTLE_RATE_AMT, "issuance amtRate too big"); require(params.pctRate <= MAX_THROTTLE_PCT_AMT, "issuance pctRate too big"); + issuanceThrottle.useAvailable(totalSupply(), 0); emit IssuanceThrottleSet(issuanceThrottle.params, params); issuanceThrottle.params = params; } @@ -451,6 +452,7 @@ contract RTokenP1 is ComponentP1, ERC20PermitUpgradeable, IRToken { require(params.amtRate >= MIN_THROTTLE_RATE_AMT, "redemption amtRate too small"); require(params.amtRate <= MAX_THROTTLE_RATE_AMT, "redemption amtRate too big"); require(params.pctRate <= MAX_THROTTLE_PCT_AMT, "redemption pctRate too big"); + redemptionThrottle.useAvailable(totalSupply(), 0); emit RedemptionThrottleSet(redemptionThrottle.params, params); redemptionThrottle.params = params; } diff --git a/test/RToken.test.ts b/test/RToken.test.ts index c13c235db7..21fede116c 100644 --- a/test/RToken.test.ts +++ b/test/RToken.test.ts @@ -224,6 +224,26 @@ describe(`RTokenP${IMPLEMENTATION} contract`, () => { ).to.be.revertedWith('issuance pctRate too big') }) + it.only('Should account for accrued value when updating issuance throttle parameters', async () => { + await advanceTime(12*5*60) // 60 minutes, charge fully + const issuanceThrottleParams = { amtRate: fp('60'), pctRate: fp('0.1') } + + await rToken.connect(owner).setIssuanceThrottleParams(issuanceThrottleParams) + let params = await rToken.issuanceThrottleParams() + expect(params[0]).to.equal(issuanceThrottleParams.amtRate) + expect(params[1]).to.equal(issuanceThrottleParams.pctRate) + + await Promise.all(tokens.map((t) => t.connect(addr1).approve(rToken.address, initialBal))) + await rToken.connect(addr1).issue(fp('20')) + expect(await rToken.issuanceAvailable()).to.equal(fp('40')) + + await setNextBlockTimestamp(await getLatestBlockTimestamp() + 12*5*10) // 10 minutes + + issuanceThrottleParams.amtRate = fp('100') + await rToken.connect(owner).setIssuanceThrottleParams(issuanceThrottleParams) + expect(await rToken.issuanceAvailable()).to.equal(fp('50')) + }) + it('Should allow to update redemption throttle if Owner and perform validations', async () => { const redemptionThrottleParams = { amtRate: fp('1'), pctRate: fp('0.1') } await expect( @@ -262,6 +282,30 @@ describe(`RTokenP${IMPLEMENTATION} contract`, () => { ).to.be.revertedWith('redemption pctRate too big') }) + it.only('Should account for accrued value when updating redemption throttle parameters', async () => { + await advanceTime(12*5*60) // 60 minutes, charge fully + const issuanceThrottleParams = { amtRate: fp('100'), pctRate: fp('0.1') } + const redemptionThrottleParams = { amtRate: fp('60'), pctRate: fp('0.1') } + + await rToken.connect(owner).setIssuanceThrottleParams(issuanceThrottleParams) + await rToken.connect(owner).setRedemptionThrottleParams(redemptionThrottleParams) + let params = await rToken.redemptionThrottleParams() + expect(params[0]).to.equal(redemptionThrottleParams.amtRate) + expect(params[1]).to.equal(redemptionThrottleParams.pctRate) + + await Promise.all(tokens.map((t) => t.connect(addr1).approve(rToken.address, initialBal))) + await rToken.connect(addr1).issue(fp('100')) + expect(await rToken.redemptionAvailable()).to.equal(fp('60')) + await rToken.connect(addr1).redeem(fp('30')) + expect(await rToken.redemptionAvailable()).to.equal(fp('30')) + + await setNextBlockTimestamp(await getLatestBlockTimestamp() + 12*5*10) // 10 minutes + + redemptionThrottleParams.amtRate = fp('100') + await rToken.connect(owner).setRedemptionThrottleParams(redemptionThrottleParams) + expect(await rToken.redemptionAvailable()).to.equal(fp('40')) + }) + it('Should return a price of 0 if the assets become unregistered', async () => { const startPrice = await basketHandler.price()