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

Chainlink threeway oracles #4

Merged
merged 9 commits into from
Oct 2, 2023
Merged

Conversation

QGarchery
Copy link
Contributor

@QGarchery QGarchery commented Sep 21, 2023

Handles the following cases:

  • A/C, C/D and B/D are feeds (typically, WBTC/BTC, BTC/USD, USDC/USD).
  • A/C and C/B are feeds (typically, WBTC/BTC and BTC/ETH).

Notice that it could also replace the simple oracle (instantiate the second base feed with the address 0)

Theory

Let's call four different tokens:

  • $a_1$ with decimals $d_1$
  • $a_2$ with decimals $d_2$
  • $a_3$ with decimals $d_3$
  • $b$ with decimals $d_b$.

Assume that we want an oracle for $a_1$ quoted in $b$. Assume that we have:

  • a feed $f_1$ to go from $a_1$ to $a_2$, with decimals $o_1$
  • a feed $f_2$ to go from $a_2$ to $a_3$, with decimals $o_2$
  • a feed $g$ to go from $b$ to $a_3$, with decimals $o_g$

Let's write:

  • $f_1 = p_1 * 10^{o_1 + d_1 - d_2}$, with $p_1$ denoting the price in real numbers of WEI of asset $a_1$ quoted in WEI of $a_2$
  • $f_2 = p_2 * 10^{o_2 + d_2 - d_3}$, with $p_2$ denoting the price in real numbers of WEI of asset $a_2$ quoted in WEI of $a_3$
  • $g = p_b * 10^{o_g + d_b - d_3}$, with $p_b$ denoting the price in real numbers of WEI of asset $b$ quoted in WEI of $a_3$

We want the result to be $$\frac{p_1 * p_2}{p_b} * 10^{36}$$
We can achieve that result with the following computation
$$\frac{f_1 * f_2 * SCALE}{g}$$
where $$SCALE = 10^{36 + (o_g + d_b) - (o_1 + o_2 + d_1)}$$

@QGarchery QGarchery self-assigned this Sep 21, 2023
@QGarchery QGarchery marked this pull request as ready for review September 21, 2023 15:22
@MathisGD MathisGD self-requested a review October 1, 2023 23:14
src/chainlink/OracleThreeWay.sol Outdated Show resolved Hide resolved
src/chainlink/OracleThreeWay.sol Outdated Show resolved Hide resolved
@MathisGD MathisGD merged commit 539b6b4 into feat/chainlink Oct 2, 2023
1 check passed
@MathisGD MathisGD deleted the feat/chainlink-threeway branch October 2, 2023 12:17
@QGarchery QGarchery mentioned this pull request Oct 5, 2023
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.

2 participants