Skip to content
Closed
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
16 changes: 8 additions & 8 deletions contracts/p1/StRSR.sol
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ abstract contract StRSRP1 is Initializable, ComponentP1, IStRSR, EIP712Upgradeab

// newStakeRSR: {qRSR} = D18 * {qStRSR} / D18{qStRSR/qRSR}
uint256 newStakeRSR = (FIX_ONE_256 * totalStakes + (stakeRate - 1)) / stakeRate;
uint256 rsrAmount = stakeRSR - newStakeRSR;
uint256 rsrAmount = newStakeRSR < stakeRSR ? stakeRSR - newStakeRSR : 0;
stakeRSR = newStakeRSR;

// Create draft
Expand Down Expand Up @@ -324,7 +324,7 @@ abstract contract StRSRP1 is Initializable, ComponentP1, IStRSR, EIP712Upgradeab
uint256 newTotalDrafts = totalDrafts - draftAmount;
// newDraftRSR: {qRSR} = {qDrafts} * D18 / D18{qDrafts/qRSR}
uint256 newDraftRSR = (newTotalDrafts * FIX_ONE_256 + (draftRate - 1)) / draftRate;
uint256 rsrAmount = draftRSR - newDraftRSR;
uint256 rsrAmount = newDraftRSR < draftRSR ? draftRSR - newDraftRSR : 0;

if (rsrAmount == 0) return;

Expand Down Expand Up @@ -371,7 +371,7 @@ abstract contract StRSRP1 is Initializable, ComponentP1, IStRSR, EIP712Upgradeab
uint256 newTotalDrafts = totalDrafts - draftAmount;
// newDraftRSR: {qRSR} = {qDrafts} * D18 / D18{qDrafts/qRSR}
uint256 newDraftRSR = (newTotalDrafts * FIX_ONE_256 + (draftRate - 1)) / draftRate;
uint256 rsrAmount = draftRSR - newDraftRSR;
uint256 rsrAmount = newDraftRSR < draftRSR ? draftRSR - newDraftRSR : 0;

if (rsrAmount == 0) return;

Expand Down Expand Up @@ -445,7 +445,7 @@ abstract contract StRSRP1 is Initializable, ComponentP1, IStRSR, EIP712Upgradeab
// update stakeRate, possibly beginning a new stake era
if (stakeRSR != 0) {
// Downcast is safe: totalStakes is 1e38 at most so expression maximum value is 1e56
stakeRate = uint192((FIX_ONE_256 * totalStakes + (stakeRSR - 1)) / stakeRSR);
stakeRate = uint192((FIX_ONE_256 * totalStakes) / stakeRSR);
}
if (stakeRSR == 0 || stakeRate > MAX_STAKE_RATE) {
seizedRSR += stakeRSR;
Expand All @@ -460,7 +460,7 @@ abstract contract StRSRP1 is Initializable, ComponentP1, IStRSR, EIP712Upgradeab
// update draftRate, possibly beginning a new draft era
if (draftRSR != 0) {
// Downcast is safe: totalDrafts is 1e38 at most so expression maximum value is 1e56
draftRate = uint192((FIX_ONE_256 * totalDrafts + (draftRSR - 1)) / draftRSR);
draftRate = uint192((FIX_ONE_256 * totalDrafts) / draftRSR);
}

if (draftRSR == 0 || draftRate > MAX_DRAFT_RATE) {
Expand Down Expand Up @@ -620,7 +620,7 @@ abstract contract StRSRP1 is Initializable, ComponentP1, IStRSR, EIP712Upgradeab
// if totalStakes == 0, then stakeRSR == 0
stakeRate = (stakeRSR == 0 || totalStakes == 0)
? FIX_ONE
: uint192((totalStakes * FIX_ONE_256 + (stakeRSR - 1)) / stakeRSR);
: uint192((totalStakes * FIX_ONE_256) / stakeRSR);

emit RewardsPaid(payout);
emit ExchangeRateSet(initRate, exchangeRate());
Expand All @@ -646,7 +646,7 @@ abstract contract StRSRP1 is Initializable, ComponentP1, IStRSR, EIP712Upgradeab
draftRSR += rsrAmount;
// newTotalDrafts: {qDrafts} = D18{qDrafts/qRSR} * {qRSR} / D18
uint256 newTotalDrafts = (draftRate * draftRSR) / FIX_ONE;
uint256 draftAmount = newTotalDrafts - totalDrafts;
uint256 draftAmount = totalDrafts < newTotalDrafts ? newTotalDrafts - totalDrafts : 0;
totalDrafts = newTotalDrafts;

// Push drafts into account's draft queue
Expand Down Expand Up @@ -727,7 +727,7 @@ abstract contract StRSRP1 is Initializable, ComponentP1, IStRSR, EIP712Upgradeab
uint256 newStakeRSR = stakeRSR + rsrAmount;
// newTotalStakes: {qStRSR} = D18{qStRSR/qRSR} * {qRSR} / D18
uint256 newTotalStakes = (stakeRate * newStakeRSR) / FIX_ONE;
uint256 stakeAmount = newTotalStakes - totalStakes;
uint256 stakeAmount = totalStakes < newTotalStakes ? newTotalStakes - totalStakes : 0;

// Transfer RSR from account to this contract
stakeRSR += rsrAmount;
Expand Down
6 changes: 3 additions & 3 deletions test/ZZStRSR.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ describe(`StRSRP${IMPLEMENTATION} contract`, () => {

// Calculate payout amount
const addedRSRStake = amountAdded.sub(decayFn(amountAdded, 1)) // 1 round
const newRate: BigNumber = fp(stake.add(addedRSRStake)).div(stake)
const newRate: BigNumber = fp(stake.add(addedRSRStake)).div(stake).add(1)

// Payout rewards
await expectEvents(stRSR.payoutRewards(), [
Expand Down Expand Up @@ -1483,7 +1483,7 @@ describe(`StRSRP${IMPLEMENTATION} contract`, () => {

// Calculate payout amount
const addedRSRStake = amountAdded.sub(decayFn(amountAdded, 1)) // 1 round
const newRate: BigNumber = fp(stake.add(addedRSRStake)).div(stake)
const newRate: BigNumber = fp(stake.add(addedRSRStake)).div(stake).add(1)

// Payout rewards
await expect(stRSR.payoutRewards()).to.emit(stRSR, 'ExchangeRateSet')
Expand Down Expand Up @@ -1755,7 +1755,7 @@ describe(`StRSRP${IMPLEMENTATION} contract`, () => {
expect(await stRSR.balanceOf(addr2.address)).to.equal(amount)
expect(await stRSR.balanceOf(addr3.address)).to.equal(amount)

const newRate = fp(amount.mul(3).sub(amount2)).div(amount.mul(3))
const newRate = fp(amount.mul(3).sub(amount2)).div(amount.mul(3)).add(1)

// Seize RSR
await whileImpersonating(backingManager.address, async (signer) => {
Expand Down