Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Integral atomic relayer (Event driven version) #674

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

IntegralHQ
Copy link

No description provided.

@IntegralHQ IntegralHQ changed the title Update Integral integration to use event driven update model Add Integral atomic relayer (Event driven version) May 10, 2024
@IntegralHQ
Copy link
Author

Hi All,

A few notes with this PR:

  1. We made some changes to Oracle.ts in in Uniswap-v3/contract-math so that we could reuse their oracle code logic. Our system relies on the TWAP oracle built into Uniswap. Since they already had implemented the majority of the code (with some inadvertent bugs), we simply fixed the bugs and used the existing libraries.

Since there could be some concern that these changes may impact the Uniswap integration, I've added comments to the PR with explanations of our changes.

  1. Behind the scenes of the TWAP oracle from Uniswap is the accumulation of ticks. The number of ticks can be quite large depending the cardinality set on each Uniswap pair (it's user configurable). Since there is quite of data to pull initially, tests run a bit longer. Once the data has been pulled, the internal state is kept in sync by listening to events.

@@ -23,7 +23,7 @@ export class Oracle {
): OracleObservation {
const delta = blockTimestamp - last.blockTimestamp;
return {
blockTimestamp: state.blockTimestamp,
blockTimestamp,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we simply matched the .ts implementation with the .sol implementation: https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/Oracle.sol#L39

You can see that new observations also update the blockTimestamp in the Solidity code.

Furthermore, since Uniswap's pricing does not depend on the oracle logic, you can be rest assured this won't negatively impact the Uniswap quoting mechanics.

@@ -93,12 +93,12 @@ export class Oracle {
let beforeOrAt;
let atOrAfter;
while (true) {
i = (l + r) / 2;
i = Math.floor((l + r) / 2);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another bug fix to the original Uniswap .ts implementation. In Solidity, division is done on integers so there's implicitly a floor. We added floor manually to match the EVM execution: https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/Oracle.sol#L164


beforeOrAt = state.observations[i % cardinality];

// we've landed on an uninitialized tick, keep searching higher (more recently)
if (!beforeOrAt.initialized) {
if (!beforeOrAt || !beforeOrAt.initialized) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Solidity, accessing a member of an undefined struct won't cause any issues since the values will be returned as 0 or empty. But in Javascript, accessing a memory on a undefined object will cause the system to crash. We simply added a check that the object should be defined before accessing its member here: https://github.com/Uniswap/v3-core/blob/main/contracts/libraries/Oracle.sol#L169

@@ -150,7 +150,8 @@ export class Oracle {
}

beforeOrAt = state.observations[(index + 1) % cardinality];
if (!beforeOrAt.initialized) beforeOrAt = state.observations[0];
if (!beforeOrAt || !beforeOrAt.initialized)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant