Skip to content

Commit

Permalink
Merge pull request #11 from sofa-org/fix/c4-audit
Browse files Browse the repository at this point in the history
fix oracles and update the test
  • Loading branch information
sofa-org authored May 19, 2024
2 parents d90d4c0 + d06f104 commit 65bb4c3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
14 changes: 11 additions & 3 deletions contracts/oracles/HlOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "../interfaces/IAutomatedFunctionsConsumer.sol";
contract HlOracle is AutomationCompatibleInterface {
mapping(uint256 => uint256[2]) public settlePrices;
IAutomatedFunctionsConsumer internal immutable AUTOMATED_FUNCTIONS_CONSUMER;
uint256 private latestExpiryUpdated = 0;
uint256 public latestExpiryUpdated = 0;

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

Expand Down Expand Up @@ -56,8 +56,16 @@ contract HlOracle is AutomationCompatibleInterface {

for (uint256 i = 1; i < missedDays; i++) {
uint256 missedExpiry = latestExpiryUpdated + i * 86400;
settlePrices[missedExpiry][0] = startPrices[0] + (currentPrices[0] - startPrices[0]) * i / missedDays;
settlePrices[missedExpiry][1] = startPrices[1] + (currentPrices[1] - startPrices[1]) * i / missedDays;
if (startPrices[0] > currentPrices[0]) {
settlePrices[missedExpiry][0] = startPrices[0] - (startPrices[0] - currentPrices[0]) * i / missedDays;
} else {
settlePrices[missedExpiry][0] = startPrices[0] + (currentPrices[0] - startPrices[0]) * i / missedDays;
}
if (startPrices[1] > currentPrices[1]) {
settlePrices[missedExpiry][1] = startPrices[1] - (startPrices[1] - currentPrices[1]) * i / missedDays;
} else {
settlePrices[missedExpiry][1] = startPrices[1] + (currentPrices[1] - startPrices[1]) * i / missedDays;
}
emit Settled(missedExpiry, settlePrices[missedExpiry]);
}
}
Expand Down
9 changes: 7 additions & 2 deletions contracts/oracles/SpotOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract SpotOracle {
mapping(uint256 => uint256) public settlePrices;
AggregatorV3Interface immutable internal PRICEFEED;
uint256 private latestExpiryUpdated = 0;
uint256 public latestExpiryUpdated = 0;

event Settled(uint256 expiry, uint256 settlePrice);

Expand All @@ -29,7 +29,12 @@ contract SpotOracle {

for (uint256 i = 1; i < missedDays; i++) {
uint256 missedExpiry = latestExpiryUpdated + i * 86400;
uint256 missedDayPrice = startPrice + (currentPrice - startPrice) * i / missedDays;
uint256 missedDayPrice;
if (startPrice > currentPrice) {
missedDayPrice = startPrice - (startPrice - currentPrice) * i / missedDays;
} else {
missedDayPrice = startPrice + (currentPrice - startPrice) * i / missedDays;
}
settlePrices[missedExpiry] = missedDayPrice;
emit Settled(missedExpiry, missedDayPrice);
}
Expand Down
34 changes: 34 additions & 0 deletions test/oracles/HlOracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,38 @@ describe("HlOracle", function () {
expect(await oracle.checkUpkeep('0x')).to.deep.equal([ true, '0x' ]);
await expect(oracle.performUpkeep('0x')).to.emit(oracle, 'Settled');
});

it("should assign prices for missed days correctly", async function () {
const { oracle, aggregator } = await loadFixture(deployFixture);
//30000, 32000
await aggregator.setLatestResponse("0x00000000000000000000000000000000000000000000065a4da25d3016c000000000000000000000000000000000000000000000000006c6b935b8bbd4000000");
expect(await oracle.checkUpkeep('0x')).to.deep.equal([ true, '0x' ]);
await expect(oracle.performUpkeep('0x')).to.emit(oracle, 'Settled');

// Move time forward by 3 days
await ethers.provider.send("evm_increaseTime", [3 * 86400]);
await ethers.provider.send("evm_mine", []);

const latestExpiryUpdated0 = await oracle.latestExpiryUpdated();

//27000, 35000
await aggregator.setLatestResponse("0x0000000000000000000000000000000000000000000005b7ac4553de7ae000000000000000000000000000000000000000000000000007695a92c20d6fe00000");
expect(await oracle.checkUpkeep('0x')).to.deep.equal([ true, '0x' ]);
await expect(oracle.performUpkeep('0x')).to.emit(oracle, 'Settled');

for (let i = 1; i < 3; i++) {
const missedExpiry = latestExpiryUpdated0.toNumber() + i * 86400;
const missedPrice0 = await oracle.settlePrices(missedExpiry, 0);
const missedPrice1 = await oracle.settlePrices(missedExpiry, 1);

expect(missedPrice0).to.be.equal(parseEther((30000-1000*i).toString()));
expect(missedPrice1).to.be.equal(parseEther((32000+1000*i).toString()));
}
const latestExpiryUpdated1 = await oracle.latestExpiryUpdated();
expect(latestExpiryUpdated1).to.be.equal(latestExpiryUpdated0.toNumber() + 3 * 86400);
const settlePrice0 = await oracle.settlePrices(latestExpiryUpdated1, 0);
const settlePrice1 = await oracle.settlePrices(latestExpiryUpdated1, 1);
expect(settlePrice0).to.be.equal(parseEther('27000'));
expect(settlePrice1).to.be.equal(parseEther('35000'));
});
});

0 comments on commit 65bb4c3

Please sign in to comment.