diff --git a/packages/perennial-oracle/contracts/types/ChainlinkAggregator.sol b/packages/perennial-oracle/contracts/types/ChainlinkAggregator.sol index 742dafe5..e3ae83d7 100644 --- a/packages/perennial-oracle/contracts/types/ChainlinkAggregator.sol +++ b/packages/perennial-oracle/contracts/types/ChainlinkAggregator.sol @@ -150,7 +150,12 @@ library ChainlinkAggregatorLib { // If the found timestamp is not greater than target timestamp or no max was found, then the desired round does // not exist in this phase - if (maxTimestamp <= targetTimestamp || maxTimestamp == type(uint256).max) return 0; + if ((minTimestamp <= targetTimestamp || minTimestamp == type(uint256).max) + && (maxTimestamp <= targetTimestamp || maxTimestamp == type(uint256).max)) return 0; + + // If minTimestamp is greater than targetTimestamp then return it, since it is closer to + // the target than maxTimestamp + if (minTimestamp > targetTimestamp) return _aggregatorRoundIdToProxyRoundId(phaseId, uint80(minRoundId)); return _aggregatorRoundIdToProxyRoundId(phaseId, uint80(maxRoundId)); } diff --git a/packages/perennial-oracle/test/integration/ChainlinkFeedOracle/ChainlinkFeedOracle.integrationTest.ts b/packages/perennial-oracle/test/integration/ChainlinkFeedOracle/ChainlinkFeedOracle.integrationTest.ts index 36a70205..2fc6d733 100644 --- a/packages/perennial-oracle/test/integration/ChainlinkFeedOracle/ChainlinkFeedOracle.integrationTest.ts +++ b/packages/perennial-oracle/test/integration/ChainlinkFeedOracle/ChainlinkFeedOracle.integrationTest.ts @@ -294,7 +294,7 @@ describe('ChainlinkFeedOracle', () => { // Next round comes from phase 4. // We check if there is another round in phase 3 // There is, but it is after phase4Data[1] so we ignore it and perform a search - // The search will phase 4 starts at round 2 (version 4), so this is version 4 + (544 - 2) = 546 + // The search will phase 4 starts at round 1 (version 4), so this is version 4 + (544 - 1) = 547 await aggregatorMock._setLatestRoundData(...p4r544) const expectedPrice = p4r544.answer.mul(10 ** 10) @@ -303,22 +303,22 @@ describe('ChainlinkFeedOracle', () => { expect(returnValue.price).to.equal(expectedPrice) expect(returnValue.timestamp).to.equal(p4r544.updatedAt) - expect(returnValue.version).to.equal(546) + expect(returnValue.version).to.equal(547) const currentVersion = await oracle.currentVersion() expect(currentVersion.price).to.equal(expectedPrice) expect(currentVersion.timestamp).to.equal(p4r544.updatedAt) - expect(currentVersion.version).to.equal(546) + expect(currentVersion.version).to.equal(547) - const atVersion5 = await oracle.atVersion(546) + const atVersion5 = await oracle.atVersion(547) expect(atVersion5.price).to.equal(expectedPrice) expect(atVersion5.timestamp).to.equal(p4r544.updatedAt) - expect(atVersion5.version).to.equal(546) + expect(atVersion5.version).to.equal(547) - const p4r2 = await aggregatorProxy.getRoundData(buildChainlinkRoundId(4, 2)) + const p4r1 = await aggregatorProxy.getRoundData(buildChainlinkRoundId(4, 1)) const atVersion4 = await oracle.atVersion(4) - expect(atVersion4.price).to.equal(p4r2.answer.mul(10 ** 10)) - expect(atVersion4.timestamp).to.equal(p4r2.updatedAt) + expect(atVersion4.price).to.equal(p4r1.answer.mul(10 ** 10)) + expect(atVersion4.timestamp).to.equal(p4r1.updatedAt) expect(atVersion4.version).to.equal(4) const atVersion3 = await oracle.atVersion(3) @@ -340,7 +340,7 @@ describe('ChainlinkFeedOracle', () => { // Next round comes from phase 4. // We check if there is another round in phase 3 // There is not - // The search will find phase 4 starts at round 2 (version 4), so this is version 4 + (544 - 2) = 546 + // The search will find phase 4 starts at round 1 (version 4), so this is version 4 + (544 - 1) = 547 await aggregatorMock._setLatestRoundData(...p4r544) const expectedPrice = p4r544.answer.mul(10 ** 10) @@ -349,22 +349,22 @@ describe('ChainlinkFeedOracle', () => { expect(returnValue.price).to.equal(expectedPrice) expect(returnValue.timestamp).to.equal(p4r544.updatedAt) - expect(returnValue.version).to.equal(546) + expect(returnValue.version).to.equal(547) const currentVersion = await oracle.currentVersion() expect(currentVersion.price).to.equal(expectedPrice) expect(currentVersion.timestamp).to.equal(p4r544.updatedAt) - expect(currentVersion.version).to.equal(546) + expect(currentVersion.version).to.equal(547) - const atVersion5 = await oracle.atVersion(546) + const atVersion5 = await oracle.atVersion(547) expect(atVersion5.price).to.equal(expectedPrice) expect(atVersion5.timestamp).to.equal(p4r544.updatedAt) - expect(atVersion5.version).to.equal(546) + expect(atVersion5.version).to.equal(547) - const p4r2 = await aggregatorProxy.getRoundData(buildChainlinkRoundId(4, 2)) + const p4r1 = await aggregatorProxy.getRoundData(buildChainlinkRoundId(4, 1)) const atVersion4 = await oracle.atVersion(4) - expect(atVersion4.price).to.equal(p4r2.answer.mul(10 ** 10)) - expect(atVersion4.timestamp).to.equal(p4r2.updatedAt) + expect(atVersion4.price).to.equal(p4r1.answer.mul(10 ** 10)) + expect(atVersion4.timestamp).to.equal(p4r1.updatedAt) expect(atVersion4.version).to.equal(4) const atVersion3 = await oracle.atVersion(3)