Skip to content
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
9 changes: 5 additions & 4 deletions contracts/refs/OracleRef.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ abstract contract OracleRef is IOracleRef, CoreRef {
}
require(valid, "OracleRef: oracle invalid");

// Invert the oracle price if necessary
if (doInvert) {
_peg = invert(_peg);
}

// Scale the oracle price by token decimals delta if necessary
uint256 scalingFactor;
if (decimalsNormalizer < 0) {
Expand All @@ -102,10 +107,6 @@ abstract contract OracleRef is IOracleRef, CoreRef {
_peg = _peg.mul(scalingFactor);
}

// Invert the oracle price if necessary
if (doInvert) {
_peg = invert(_peg);
}
return _peg;
}

Expand Down
25 changes: 18 additions & 7 deletions test/unit/refs/OracleRef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,28 @@ describe('OracleRef', () => {
);
});

it('positive decimal normalizer scales down', async function () {
await this.oracleRef.connect(impersonatedSigners[governorAddress]).setDecimalsNormalizer(4),
expect((await this.oracleRef.connect(impersonatedSigners[userAddress]).readOracle())[0]).to.be.equal(
'200000000000'
);
it('positive decimal normalizer scales up', async function () {
// Raw peg price: 500000000000000000000, 500e18
// Inversion is set to True by default in ReserveStabilizer
// Price after inversion = (1e18 * 1e18) / 500e18 = 0.2e16 = 2e15
// Scaling factor is set to 1e4. After applying scaling factor
// 2e15 * 1e4 = 2e19 = 20000000000000000000
await this.oracleRef.connect(impersonatedSigners[governorAddress]).setDecimalsNormalizer(4);

expect((await this.oracleRef.connect(impersonatedSigners[userAddress]).readOracle())[0]).to.be.equal(
'20000000000000000000'
);
});

it('negative decimal normalizer scales up', async function () {
it('negative decimal normalizer scales down', async function () {
// Raw peg price: 500000000000000000000, 500e18
// Inversion is set to True by default in ReserveStabilizer
// Price after inversion = (1e18 * 1e18) / 500e18 = 0.2e16 = 2e15
// Scaling factor is set to -1e4. After applying scaling factor
// 2e15 / 1e4 = 2e11 = 200000000000
await this.oracleRef.connect(impersonatedSigners[governorAddress]).setDecimalsNormalizer(-4),
expect((await this.oracleRef.connect(impersonatedSigners[userAddress]).readOracle())[0]).to.be.equal(
'20000000000000000000'
'200000000000'
);
});
});
Expand Down