Skip to content

Commit

Permalink
on changeSupply round up in the favour of the protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrowDom committed Nov 29, 2024
1 parent dbb3434 commit 93545c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
6 changes: 4 additions & 2 deletions contracts/contracts/token/OUSD.sol
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,11 @@ contract OUSD is Governable {
? MAX_SUPPLY
: _newTotalSupply;

uint256 rebasingSupply = totalSupply - nonRebasingSupply;
// round up in the favour of the protocol
rebasingCreditsPerToken_ =
(rebasingCredits_ * 1e18) /
(totalSupply - nonRebasingSupply);
(rebasingCredits_ * 1e18 + rebasingSupply - 1) /
rebasingSupply;

require(rebasingCreditsPerToken_ > 0, "Invalid change in supply");

Expand Down
25 changes: 21 additions & 4 deletions contracts/test/token/ousd.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,12 @@ describe("Token", function () {
.div(utils.parseUnits("1", 18))
.add(1);

await expect(rebasingCredits).to.equal(
initialRebasingCredits.add(creditsAdded)
const resultingCredits = initialRebasingCredits.add(creditsAdded);
// when calling changeSupply(rebase) OUSD contract can round down by 1 WEI.
await expect(rebasingCredits).to.gte(
resultingCredits.sub(BigNumber.from("1"))
);
await expect(rebasingCredits).to.lte(resultingCredits);

expect(await ousd.totalSupply()).to.approxEqual(
initialTotalSupply.add(utils.parseUnits("200", 18))
Expand Down Expand Up @@ -695,9 +698,23 @@ describe("Token", function () {

// Contract originally contained $200, now has $202.
// Matt should have (99/200) * 202 OUSD
await expect(matt).has.a.balanceOf("99.99", ousd);
// because rebase rounds down in protocol's favour resulting user balances can be off by 1 WEI
const mattExpectedBalance = ousdUnits("99.99");
await expect(await ousd.balanceOf(matt.address)).to.be.gte(
mattExpectedBalance.sub(BigNumber.from("1"))
);
await expect(await ousd.balanceOf(matt.address)).to.be.lte(
mattExpectedBalance
);

const annaExpectedBalance = ousdUnits("1.01");
// Anna should have (1/200) * 202 OUSD
await expect(anna).has.a.balanceOf("1.01", ousd);
await expect(await ousd.balanceOf(anna.address)).to.be.gte(
annaExpectedBalance.sub(BigNumber.from("1"))
);
await expect(await ousd.balanceOf(anna.address)).to.be.lte(
annaExpectedBalance
);
});

it("Should mint correct amounts on non-rebasing account without previously set creditsPerToken", async () => {
Expand Down

0 comments on commit 93545c3

Please sign in to comment.