Skip to content

Commit

Permalink
fixing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmckelvy1 committed Sep 19, 2024
1 parent 5d0445e commit d8198f9
Showing 1 changed file with 52 additions and 19 deletions.
71 changes: 52 additions & 19 deletions test/RToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1261,14 +1261,14 @@ describe(`RTokenP${IMPLEMENTATION} contract`, () => {
).to.be.revertedWith('invalid basketNonce')
})

context('And redemption throttling', function () {
context.only('And redemption throttling', function () {
// the fixture-configured redemption throttle uses 5%
let redemptionThrottleParams: ThrottleParams
let redeemAmount: BigNumber

beforeEach(async function () {
redemptionThrottleParams = {
amtRate: fp('2'), // 2 RToken,
amtRate: fp('2e6'), // 2e6 RToken,
pctRate: fp('0.1'), // 10%
}
await rToken.connect(owner).setRedemptionThrottleParams(redemptionThrottleParams)
Expand All @@ -1280,8 +1280,21 @@ describe(`RTokenP${IMPLEMENTATION} contract`, () => {
await advanceTime(3600)
})

async function issueNTimes(n: number) {
for (let i = 0; i < n; i++) {
await rToken.connect(addr1).issue(config.issuanceThrottle.amtRate)
await advanceTime(3600)
}
}

it('Should calculate redemption limit correctly', async function () {
redeemAmount = issueAmount.mul(redemptionThrottleParams.pctRate).div(fp('1'))
await rToken.connect(addr1).issue(config.issuanceThrottle.amtRate.sub(issueAmount))
await advanceTime(3600)
await issueNTimes(21)
redeemAmount = config.issuanceThrottle.amtRate
.mul(22)
.mul(redemptionThrottleParams.pctRate)
.div(fp('1'))
expect(await rToken.redemptionAvailable()).to.equal(redeemAmount)
})

Expand Down Expand Up @@ -1310,12 +1323,19 @@ describe(`RTokenP${IMPLEMENTATION} contract`, () => {

it('Should revert on overly-large redemption #fast', async function () {
redeemAmount = issueAmount.mul(redemptionThrottleParams.pctRate).div(fp('1'))
expect(await rToken.redemptionAvailable()).to.equal(redeemAmount)
expect(await rToken.redemptionAvailable()).to.equal(issueAmount)

// Check issuance throttle - full
expect(await rToken.issuanceAvailable()).to.equal(config.issuanceThrottle.amtRate)

redeemAmount = issueAmount.mul(redemptionThrottleParams.pctRate).div(fp('1'))
await rToken.connect(addr1).issue(config.issuanceThrottle.amtRate.sub(issueAmount))
await advanceTime(3600)
await issueNTimes(21)
redeemAmount = config.issuanceThrottle.amtRate
.mul(22)
.mul(redemptionThrottleParams.pctRate)
.div(fp('1'))

await expect(rToken.connect(addr1).redeem(redeemAmount.add(1))).to.be.revertedWith(
'supply change throttled'
)
Expand All @@ -1329,16 +1349,17 @@ describe(`RTokenP${IMPLEMENTATION} contract`, () => {
})

it('Should support 1e48 amtRate throttles', async function () {
const throttles = JSON.parse(JSON.stringify(config.redemptionThrottle))
throttles.amtRate = bn('1e48')
await rToken.connect(owner).setIssuanceThrottleParams(throttles)
await rToken.connect(owner).setRedemptionThrottleParams(throttles)
const issuanceThrottle = JSON.parse(JSON.stringify(config.issuanceThrottle))
issuanceThrottle.amtRate = bn('1e48').mul(50).div(100)
const redemptionThrottle = JSON.parse(JSON.stringify(config.redemptionThrottle))
redemptionThrottle.amtRate = bn('1e48')
await rToken.connect(owner).setThrottleParams(issuanceThrottle, redemptionThrottle)

// Mint collateral
await token0.mint(addr1.address, throttles.amtRate)
await token1.mint(addr1.address, throttles.amtRate)
await token2.mint(addr1.address, throttles.amtRate)
await token3.mint(addr1.address, throttles.amtRate)
await token0.mint(addr1.address, issuanceThrottle.amtRate)
await token1.mint(addr1.address, issuanceThrottle.amtRate)
await token2.mint(addr1.address, issuanceThrottle.amtRate)
await token3.mint(addr1.address, issuanceThrottle.amtRate)

// Provide approvals
await Promise.all(
Expand All @@ -1347,22 +1368,34 @@ describe(`RTokenP${IMPLEMENTATION} contract`, () => {

// Charge throttle
await advanceTime(3600)
expect(await rToken.issuanceAvailable()).to.equal(throttles.amtRate)
expect(await rToken.issuanceAvailable()).to.equal(issuanceThrottle.amtRate)

// Issue
await rToken.connect(addr1).issue(throttles.amtRate)
expect(await rToken.balanceOf(addr1.address)).to.equal(issueAmount.add(throttles.amtRate))
await rToken.connect(addr1).issue(issuanceThrottle.amtRate)
expect(await rToken.balanceOf(addr1.address)).to.equal(
issueAmount.add(issuanceThrottle.amtRate)
)

// Redeem
expect(await rToken.redemptionAvailable()).to.equal(throttles.amtRate)
await rToken.connect(addr1).redeem(throttles.amtRate)
expect(await rToken.redemptionAvailable()).to.equal(
issuanceThrottle.amtRate.add(issueAmount)
)
await rToken.connect(addr1).redeem(issuanceThrottle.amtRate)
expect(await rToken.balanceOf(addr1.address)).to.equal(issueAmount)
})

it('Should use amtRate if pctRate is zero', async function () {
redeemAmount = redemptionThrottleParams.amtRate
redemptionThrottleParams.pctRate = bn(0)
await rToken.connect(owner).setRedemptionThrottleParams(redemptionThrottleParams)
const issuanceThrottleParams = {
amtRate: fp('1e6'), // 1e6 RToken,
pctRate: fp(0), // 0%
}
await rToken
.connect(owner)
.setThrottleParams(issuanceThrottleParams, redemptionThrottleParams)

await issueNTimes(22)

// Large redemption should fail
await expect(rToken.connect(addr1).redeem(redeemAmount.add(1))).to.be.revertedWith(
Expand Down

0 comments on commit d8198f9

Please sign in to comment.