From c680fecf11c9352772e40a51b4e015fdce150a24 Mon Sep 17 00:00:00 2001 From: Yunhao Zhang Date: Mon, 31 Oct 2022 17:16:33 -0400 Subject: [PATCH 1/4] Adapt to pyth sdk api changes --- examples/sol-contract/README.md | 2 +- examples/sol-contract/src/processor.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/sol-contract/README.md b/examples/sol-contract/README.md index 9ff52e6..f71e761 100644 --- a/examples/sol-contract/README.md +++ b/examples/sol-contract/README.md @@ -49,5 +49,5 @@ We assume that you have installed `cargo`, `solana`, `npm` and `node`. # Deploy the example contract > scripts/deploy.sh # Invoke the example contract -> scripts/invoke.ts +> scripts/invoke.sh ``` diff --git a/examples/sol-contract/src/processor.rs b/examples/sol-contract/src/processor.rs index cf100d6..cbb1d2d 100644 --- a/examples/sol-contract/src/processor.rs +++ b/examples/sol-contract/src/processor.rs @@ -8,6 +8,8 @@ use solana_program::account_info::{ }; use solana_program::entrypoint::ProgramResult; use solana_program::msg; +use solana_program::sysvar::Sysvar; +use solana_program::sysvar::clock::Clock; use solana_program::program_error::ProgramError; use solana_program::program_memory::sol_memcpy; use solana_program::pubkey::Pubkey; @@ -84,7 +86,8 @@ pub fn process_instruction( // Here is more explanation on confidence interval in Pyth: // https://docs.pyth.network/consume-data/best-practices let feed1 = load_price_feed_from_account_info(pyth_loan_account)?; - let result1 = feed1.get_current_price().ok_or(ProgramError::Custom(3))?; + let current_timestamp1 = Clock::get()?.unix_timestamp; + let result1 = feed1.get_price_no_older_than(current_timestamp1, 60).ok_or(ProgramError::Custom(3))?; let loan_max_price = result1 .price .checked_add(result1.conf as i64) @@ -103,7 +106,8 @@ pub fn process_instruction( // Here is more explanation on confidence interval in Pyth: // https://docs.pyth.network/consume-data/best-practices let feed2 = load_price_feed_from_account_info(pyth_collateral_account)?; - let result2 = feed2.get_current_price().ok_or(ProgramError::Custom(3))?; + let current_timestamp2 = Clock::get()?.unix_timestamp; + let result2 = feed2.get_price_no_older_than(current_timestamp2, 60).ok_or(ProgramError::Custom(3))?; let collateral_min_price = result2 .price .checked_sub(result2.conf as i64) From 5b0088e1580b8217ddd06280004a41b4f8af14b1 Mon Sep 17 00:00:00 2001 From: Yunhao Zhang Date: Mon, 31 Oct 2022 17:23:44 -0400 Subject: [PATCH 2/4] Update the readme as well --- examples/sol-contract/README.md | 3 ++- examples/sol-contract/src/processor.rs | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/sol-contract/README.md b/examples/sol-contract/README.md index f71e761..24b92db 100644 --- a/examples/sol-contract/README.md +++ b/examples/sol-contract/README.md @@ -17,7 +17,8 @@ Pyth SDK is used in the `Loan2Value` instruction in `src/processor.rs`. For the loan, the code first reads the unit price from the Pyth oracle. ```rust let feed1 = load_price_feed_from_account_info(pyth_loan_account)?; -let result1 = feed1.get_current_price().ok_or(ProgramError::Custom(3))?; +let current_timestamp1 = Clock::get()?.unix_timestamp; +let result1 = feed1.get_price_no_older_than(current_timestamp1, 60).ok_or(ProgramError::Custom(3))?; ``` And then calculate the loan value given the quantity of the loan. diff --git a/examples/sol-contract/src/processor.rs b/examples/sol-contract/src/processor.rs index cbb1d2d..ef5a488 100644 --- a/examples/sol-contract/src/processor.rs +++ b/examples/sol-contract/src/processor.rs @@ -8,11 +8,11 @@ use solana_program::account_info::{ }; use solana_program::entrypoint::ProgramResult; use solana_program::msg; -use solana_program::sysvar::Sysvar; -use solana_program::sysvar::clock::Clock; use solana_program::program_error::ProgramError; use solana_program::program_memory::sol_memcpy; use solana_program::pubkey::Pubkey; +use solana_program::sysvar::clock::Clock; +use solana_program::sysvar::Sysvar; use borsh::{ BorshDeserialize, @@ -87,7 +87,9 @@ pub fn process_instruction( // https://docs.pyth.network/consume-data/best-practices let feed1 = load_price_feed_from_account_info(pyth_loan_account)?; let current_timestamp1 = Clock::get()?.unix_timestamp; - let result1 = feed1.get_price_no_older_than(current_timestamp1, 60).ok_or(ProgramError::Custom(3))?; + let result1 = feed1 + .get_price_no_older_than(current_timestamp1, 60) + .ok_or(ProgramError::Custom(3))?; let loan_max_price = result1 .price .checked_add(result1.conf as i64) @@ -107,7 +109,9 @@ pub fn process_instruction( // https://docs.pyth.network/consume-data/best-practices let feed2 = load_price_feed_from_account_info(pyth_collateral_account)?; let current_timestamp2 = Clock::get()?.unix_timestamp; - let result2 = feed2.get_price_no_older_than(current_timestamp2, 60).ok_or(ProgramError::Custom(3))?; + let result2 = feed2 + .get_price_no_older_than(current_timestamp2, 60) + .ok_or(ProgramError::Custom(3))?; let collateral_min_price = result2 .price .checked_sub(result2.conf as i64) From 4f9b6a490ae110a0d8f2664475ae097ef534828f Mon Sep 17 00:00:00 2001 From: Yunhao Zhang Date: Mon, 31 Oct 2022 19:51:26 -0400 Subject: [PATCH 3/4] Remove unnecessary dependency --- examples/sol-contract/Cargo.toml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/examples/sol-contract/Cargo.toml b/examples/sol-contract/Cargo.toml index 38e2ba5..9b1f861 100644 --- a/examples/sol-contract/Cargo.toml +++ b/examples/sol-contract/Cargo.toml @@ -12,7 +12,3 @@ borsh = "0.9" arrayref = "0.3.6" solana-program = "1.10.40" pyth-sdk-solana = { path = "../../pyth-sdk-solana", version = "0.6.1" } - -[dev-dependencies] -solana-sdk = "1.10.40" -solana-client = "1.10.40" From fc7014b52270117f51fc88fee326aea9612110e8 Mon Sep 17 00:00:00 2001 From: Yunhao Zhang Date: Tue, 1 Nov 2022 16:10:52 -0400 Subject: [PATCH 4/4] Try to solve the failing test of solana-sdk/test-contract --- examples/sol-contract/Cargo.toml | 2 +- pyth-sdk-solana/test-contract/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/sol-contract/Cargo.toml b/examples/sol-contract/Cargo.toml index 9b1f861..4cc36d3 100644 --- a/examples/sol-contract/Cargo.toml +++ b/examples/sol-contract/Cargo.toml @@ -10,5 +10,5 @@ crate-type = ["cdylib", "lib"] [dependencies] borsh = "0.9" arrayref = "0.3.6" -solana-program = "1.10.40" +solana-program = "=1.10.40" pyth-sdk-solana = { path = "../../pyth-sdk-solana", version = "0.6.1" } diff --git a/pyth-sdk-solana/test-contract/Cargo.toml b/pyth-sdk-solana/test-contract/Cargo.toml index 754f44c..27ef397 100644 --- a/pyth-sdk-solana/test-contract/Cargo.toml +++ b/pyth-sdk-solana/test-contract/Cargo.toml @@ -9,7 +9,7 @@ no-entrypoint = [] [dependencies] pyth-sdk-solana = { path = "../", version = "0.6.1" } -solana-program = "1.10.40" +solana-program = "=1.10.40" bytemuck = "1.7.2" borsh = "0.9" borsh-derive = "0.9.0"