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

Connecting to offchain data - Updated Oracles lesson #495

Conversation

0xCipherCoder
Copy link
Contributor

@0xCipherCoder 0xCipherCoder commented Sep 21, 2024

Problem

Summary of Changes

  • Updated code snippets with the latest anchor and switchboard-solana version
  • Fixed content with updated version changes
  • Fixed grammar and styling
  • Fixed as per guidelines

Fixes #
Unboxed PRs
Burry Escrow code updated - solana-developers/burry-escrow#4

@mikemaccana I need your help in creating an additional branch for the challenge solution.

Copy link
Collaborator

@mikemaccana mikemaccana left a comment

Choose a reason for hiding this comment

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

Just small changes and formatting, plus I need some help understanding the tests. See. above.

Comment on lines 110 to 115
Solana has a rich ecosystem of oracle providers. Some notable ones include
[Pyth Network](https://pyth.network), [Switchboard](https://switchboard.xyz),
[Chainlink](https://chain.link), and
[DIA](https://www.diadata.org/solana-price-oracles/). Two of the most well-known
are [Pyth](https://pyth.network) and [Switchboard](https://switchboard.xyz),
each with unique design choices.
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's some duplication here from the old text. I suggest:

Solana has a rich ecosystem of oracle providers. Some notable ones include:

 - [**Pyth**](https://www.pyth.network/price-feeds) ...
 - [**Switchboard**](https://example.com) ...
 - [**Chainlink**](https://example.com) ...
 - [DIA](https://www.diadata.org/solana-price-oracles/) ...
 
 We'll use Switchboard in this lesson, but the concepts are common to most oracles and you should pick the oracle that best meets your needs. 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes resolved as per suggestion.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry @0xCipherCoder I should be more specific.

Don't do:

some text

 - Pyth
 - Switchboard
 - Chainlink
 - Dia 

some text

 - Pyth
 - Switchboard

Instead, replace all of that with:

some text

 - Pyth
 - Switchboard
 - Chainlink
 - Dia 

I hope that makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just curious should we then remove description that has been given again for Pyth and Switchboard? Just keep the original oneliner description then go ahead with switchboard.

Copy link
Collaborator

@mikemaccana mikemaccana Sep 24, 2024

Choose a reason for hiding this comment

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

Remove the second list, keep all the information (including more detailed explanations for Python and switchboard) but use a single list.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated content list as per suggestion.

content/courses/connecting-to-offchain-data/oracles.md Outdated Show resolved Hide resolved
content/courses/connecting-to-offchain-data/oracles.md Outdated Show resolved Hide resolved
content/courses/connecting-to-offchain-data/oracles.md Outdated Show resolved Hide resolved
content/courses/connecting-to-offchain-data/oracles.md Outdated Show resolved Hide resolved
content/courses/connecting-to-offchain-data/oracles.md Outdated Show resolved Hide resolved
// derive escrow address
const [escrowState] = await anchor.web3.PublicKey.findProgramAddressSync(
[Buffer.from("MICHAEL BURRY"), payer.publicKey.toBuffer()],
it("fails to withdraw while price is below UnlockPrice", async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand how this works, precisely how we are simulating the current price of SOL to be able to test this. Can you explain it to me?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mikemaccana Here is the explainer - Anchor Toml reference -
The test environment clones the Switchboard SOL/USD price feed from the devnet to the local test validator.
This cloned feed maintains its last known state, providing a fixed, realistic SOL price for the tests.
Then tests interact with this local copy of the feed, fetching the SOL price as if it were live data.
While the price doesn't update in real time, it allows consistent testing against a known SOL price point. In essence, we are working with a "snapshot" of the real SOL price feed, frozen at the time of cloning when we start testing with anchor test command. For this to work we have added an additional config in Anchor.toml file -

[test]
startup_wait = 5000
shutdown_wait = 2000
upgradeable = false

[test.validator]
bind_address = "0.0.0.0"
url = "https://api.devnet.solana.com"
ledger = ".anchor/test-ledger"
rpc_port = 8899

[[test.validator.clone]] # switchboard-solana devnet programID
address = "SW1TCH7qEPTdLsDHRgPuMQjbQxKdH2aBStViMFnt64f"

[[test.validator.clone]] # switchboard-solana devnet IDL
address = "Fi8vncGpNKbq62gPo56G4toCehWNy77GgqGkTaAF5Lkk"

# This clones the SOL/USD price feed from devnet to local test validator.
[[test.validator.clone]] # switchboard-solana SOL/USD Feed
address = "GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR"

This PublicKey used in tests corresponds to the cloned feed in a local environment.

const SOL_USD_SWITCHBOARD_FEED = new PublicKey(
  "GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR"
);

Let me know if you still have doubts.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I understand account cloning, but I don't understand why
it withdraws from escrow and it fails to withdraw while price is below UnlockPrice both pass when SOL_USD_SWITCHBOARD_FEED is not changing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay got the concerns. Although SOL_USD_SWITCHBOARD_FEED is not changing we are changing the price in test as given below to simulate the escrow behaviour.

const unlockPrice = solPrice.plus(PRICE_OFFSET).toNumber();

Copy link
Collaborator

Choose a reason for hiding this comment

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

Aha! Can you please add a comment in the tests to say this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes added comments in both tests.

Copy link
Collaborator

@mikemaccana mikemaccana left a comment

Choose a reason for hiding this comment

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

Getting very close, see two comments below.

Comment on lines 110 to 115
Solana has a rich ecosystem of oracle providers. Some notable ones include
[Pyth Network](https://pyth.network), [Switchboard](https://switchboard.xyz),
[Chainlink](https://chain.link), and
[DIA](https://www.diadata.org/solana-price-oracles/). Two of the most well-known
are [Pyth](https://pyth.network) and [Switchboard](https://switchboard.xyz),
each with unique design choices.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry @0xCipherCoder I should be more specific.

Don't do:

some text

 - Pyth
 - Switchboard
 - Chainlink
 - Dia 

some text

 - Pyth
 - Switchboard

Instead, replace all of that with:

some text

 - Pyth
 - Switchboard
 - Chainlink
 - Dia 

I hope that makes sense.

// derive escrow address
const [escrowState] = await anchor.web3.PublicKey.findProgramAddressSync(
[Buffer.from("MICHAEL BURRY"), payer.publicKey.toBuffer()],
it("fails to withdraw while price is below UnlockPrice", async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I understand account cloning, but I don't understand why
it withdraws from escrow and it fails to withdraw while price is below UnlockPrice both pass when SOL_USD_SWITCHBOARD_FEED is not changing.

@mikemaccana
Copy link
Collaborator

mikemaccana commented Sep 24, 2024

Thanks @0xCipherCoder !

@mikemaccana mikemaccana merged commit 3c1633e into solana-foundation:main Sep 24, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants