Skip to content
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

useAvailable when setting throttles. #872

Merged
merged 6 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contracts/p0/RToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ contract RTokenP0 is ComponentP0, 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;
}
Expand All @@ -346,6 +347,7 @@ contract RTokenP0 is ComponentP0, 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;
}
Expand Down
2 changes: 2 additions & 0 deletions contracts/p1/RToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
52 changes: 52 additions & 0 deletions test/RToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,26 @@ describe(`RTokenP${IMPLEMENTATION} contract`, () => {
).to.be.revertedWith('issuance pctRate too big')
})

it('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)
const 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(
Expand Down Expand Up @@ -262,6 +282,30 @@ describe(`RTokenP${IMPLEMENTATION} contract`, () => {
).to.be.revertedWith('redemption pctRate too big')
})

it('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)
const 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()

Expand Down Expand Up @@ -367,6 +411,8 @@ describe(`RTokenP${IMPLEMENTATION} contract`, () => {
await Promise.all(
tokens.map((t) => t.connect(addr1).approve(rToken.address, MAX_THROTTLE_AMT_RATE))
)
// advance time
await advanceTime(12 * 5 * 60) // 60 minutes, charge fully
await rToken.connect(addr1).issue(MAX_THROTTLE_AMT_RATE)
expect(await rToken.totalSupply()).to.equal(MAX_THROTTLE_AMT_RATE)
expect(await rToken.basketsNeeded()).to.equal(MAX_THROTTLE_AMT_RATE)
Expand Down Expand Up @@ -1370,6 +1416,9 @@ describe(`RTokenP${IMPLEMENTATION} contract`, () => {
redemptionThrottleParams.pctRate = bn(0)
await rToken.connect(owner).setRedemptionThrottleParams(redemptionThrottleParams)

// advance time
await advanceTime(12 * 5 * 60) // 60 minutes, charge fully

// Check redemption throttle
expect(await rToken.redemptionAvailable()).to.equal(redemptionThrottleParams.amtRate)

Expand Down Expand Up @@ -2183,6 +2232,9 @@ describe(`RTokenP${IMPLEMENTATION} contract`, () => {
redemptionThrottleParams.pctRate = bn(0)
await rToken.connect(owner).setRedemptionThrottleParams(redemptionThrottleParams)

// advance time
await advanceTime(12 * 5 * 60) // 60 minutes, charge fully

// Check redemption throttle
expect(await rToken.redemptionAvailable()).to.equal(redemptionThrottleParams.amtRate)

Expand Down
6 changes: 6 additions & 0 deletions test/ZTradingExtremes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@ describeExtreme(`Trading Extreme Values (${SLOW ? 'slow mode' : 'fast mode'})`,
const noThrottle = { amtRate: MAX_THROTTLE_AMT_RATE, pctRate: 0 }
await rToken.setIssuanceThrottleParams(noThrottle)
await rToken.setRedemptionThrottleParams(noThrottle)

await advanceTime(12 * 5 * 60) // 60 minutes, charge fully

await rToken.connect(addr1).issue(rTokenSupply)
expect(await rToken.balanceOf(addr1.address)).to.equal(rTokenSupply)

Expand Down Expand Up @@ -651,6 +654,9 @@ describeExtreme(`Trading Extreme Values (${SLOW ? 'slow mode' : 'fast mode'})`,
const noThrottle = { amtRate: MAX_THROTTLE_AMT_RATE, pctRate: 0 }
await rToken.setIssuanceThrottleParams(noThrottle)
await rToken.setRedemptionThrottleParams(noThrottle)

await advanceTime(12 * 5 * 60) // 60 minutes, charge fully

await rToken.connect(addr1).issue(rTokenSupply)
expect(await rToken.balanceOf(addr1.address)).to.equal(rTokenSupply)

Expand Down