Skip to content

Commit 65bb4c3

Browse files
authored
Merge pull request #11 from sofa-org/fix/c4-audit
fix oracles and update the test
2 parents d90d4c0 + d06f104 commit 65bb4c3

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

contracts/oracles/HlOracle.sol

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import "../interfaces/IAutomatedFunctionsConsumer.sol";
88
contract HlOracle is AutomationCompatibleInterface {
99
mapping(uint256 => uint256[2]) public settlePrices;
1010
IAutomatedFunctionsConsumer internal immutable AUTOMATED_FUNCTIONS_CONSUMER;
11-
uint256 private latestExpiryUpdated = 0;
11+
uint256 public latestExpiryUpdated = 0;
1212

1313
event Settled(uint256 expiry, uint256[2] settlePrices);
1414

@@ -56,8 +56,16 @@ contract HlOracle is AutomationCompatibleInterface {
5656

5757
for (uint256 i = 1; i < missedDays; i++) {
5858
uint256 missedExpiry = latestExpiryUpdated + i * 86400;
59-
settlePrices[missedExpiry][0] = startPrices[0] + (currentPrices[0] - startPrices[0]) * i / missedDays;
60-
settlePrices[missedExpiry][1] = startPrices[1] + (currentPrices[1] - startPrices[1]) * i / missedDays;
59+
if (startPrices[0] > currentPrices[0]) {
60+
settlePrices[missedExpiry][0] = startPrices[0] - (startPrices[0] - currentPrices[0]) * i / missedDays;
61+
} else {
62+
settlePrices[missedExpiry][0] = startPrices[0] + (currentPrices[0] - startPrices[0]) * i / missedDays;
63+
}
64+
if (startPrices[1] > currentPrices[1]) {
65+
settlePrices[missedExpiry][1] = startPrices[1] - (startPrices[1] - currentPrices[1]) * i / missedDays;
66+
} else {
67+
settlePrices[missedExpiry][1] = startPrices[1] + (currentPrices[1] - startPrices[1]) * i / missedDays;
68+
}
6169
emit Settled(missedExpiry, settlePrices[missedExpiry]);
6270
}
6371
}

contracts/oracles/SpotOracle.sol

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
77
contract SpotOracle {
88
mapping(uint256 => uint256) public settlePrices;
99
AggregatorV3Interface immutable internal PRICEFEED;
10-
uint256 private latestExpiryUpdated = 0;
10+
uint256 public latestExpiryUpdated = 0;
1111

1212
event Settled(uint256 expiry, uint256 settlePrice);
1313

@@ -29,7 +29,12 @@ contract SpotOracle {
2929

3030
for (uint256 i = 1; i < missedDays; i++) {
3131
uint256 missedExpiry = latestExpiryUpdated + i * 86400;
32-
uint256 missedDayPrice = startPrice + (currentPrice - startPrice) * i / missedDays;
32+
uint256 missedDayPrice;
33+
if (startPrice > currentPrice) {
34+
missedDayPrice = startPrice - (startPrice - currentPrice) * i / missedDays;
35+
} else {
36+
missedDayPrice = startPrice + (currentPrice - startPrice) * i / missedDays;
37+
}
3338
settlePrices[missedExpiry] = missedDayPrice;
3439
emit Settled(missedExpiry, missedDayPrice);
3540
}

test/oracles/HlOracle.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,38 @@ describe("HlOracle", function () {
2828
expect(await oracle.checkUpkeep('0x')).to.deep.equal([ true, '0x' ]);
2929
await expect(oracle.performUpkeep('0x')).to.emit(oracle, 'Settled');
3030
});
31+
32+
it("should assign prices for missed days correctly", async function () {
33+
const { oracle, aggregator } = await loadFixture(deployFixture);
34+
//30000, 32000
35+
await aggregator.setLatestResponse("0x00000000000000000000000000000000000000000000065a4da25d3016c000000000000000000000000000000000000000000000000006c6b935b8bbd4000000");
36+
expect(await oracle.checkUpkeep('0x')).to.deep.equal([ true, '0x' ]);
37+
await expect(oracle.performUpkeep('0x')).to.emit(oracle, 'Settled');
38+
39+
// Move time forward by 3 days
40+
await ethers.provider.send("evm_increaseTime", [3 * 86400]);
41+
await ethers.provider.send("evm_mine", []);
42+
43+
const latestExpiryUpdated0 = await oracle.latestExpiryUpdated();
44+
45+
//27000, 35000
46+
await aggregator.setLatestResponse("0x0000000000000000000000000000000000000000000005b7ac4553de7ae000000000000000000000000000000000000000000000000007695a92c20d6fe00000");
47+
expect(await oracle.checkUpkeep('0x')).to.deep.equal([ true, '0x' ]);
48+
await expect(oracle.performUpkeep('0x')).to.emit(oracle, 'Settled');
49+
50+
for (let i = 1; i < 3; i++) {
51+
const missedExpiry = latestExpiryUpdated0.toNumber() + i * 86400;
52+
const missedPrice0 = await oracle.settlePrices(missedExpiry, 0);
53+
const missedPrice1 = await oracle.settlePrices(missedExpiry, 1);
54+
55+
expect(missedPrice0).to.be.equal(parseEther((30000-1000*i).toString()));
56+
expect(missedPrice1).to.be.equal(parseEther((32000+1000*i).toString()));
57+
}
58+
const latestExpiryUpdated1 = await oracle.latestExpiryUpdated();
59+
expect(latestExpiryUpdated1).to.be.equal(latestExpiryUpdated0.toNumber() + 3 * 86400);
60+
const settlePrice0 = await oracle.settlePrices(latestExpiryUpdated1, 0);
61+
const settlePrice1 = await oracle.settlePrices(latestExpiryUpdated1, 1);
62+
expect(settlePrice0).to.be.equal(parseEther('27000'));
63+
expect(settlePrice1).to.be.equal(parseEther('35000'));
64+
});
3165
});

0 commit comments

Comments
 (0)