From c680fecf11c9352772e40a51b4e015fdce150a24 Mon Sep 17 00:00:00 2001 From: Yunhao Zhang Date: Mon, 31 Oct 2022 17:16:33 -0400 Subject: [PATCH 1/7] 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/7] 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/7] 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/7] 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" From 5c0047a03e9d905fcce0614b680d5fdb0a1b5fe3 Mon Sep 17 00:00:00 2001 From: Ali Behjati Date: Thu, 3 Nov 2022 12:15:56 +0100 Subject: [PATCH 5/7] Make all 1.10.40 1.13.3 --- .github/workflows/pyth-sdk-solana.yml | 2 +- examples/sol-contract/Cargo.toml | 2 +- pyth-sdk-solana/test-contract/Cargo.toml | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/pyth-sdk-solana.yml b/.github/workflows/pyth-sdk-solana.yml index 6a891ce..0512b20 100644 --- a/.github/workflows/pyth-sdk-solana.yml +++ b/.github/workflows/pyth-sdk-solana.yml @@ -34,7 +34,7 @@ jobs: run: sudo apt-get update && sudo apt-get install libudev-dev - name: Install Solana Binaries run: | - sh -c "$(curl -sSfL https://release.solana.com/v1.10.40/install)" + sh -c "$(curl -sSfL https://release.solana.com/v1.13.3/install)" echo "/home/runner/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH - name: Build run: cargo build --verbose diff --git a/examples/sol-contract/Cargo.toml b/examples/sol-contract/Cargo.toml index 4cc36d3..647549c 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.13.3" 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 27ef397..b9ef683 100644 --- a/pyth-sdk-solana/test-contract/Cargo.toml +++ b/pyth-sdk-solana/test-contract/Cargo.toml @@ -9,15 +9,15 @@ no-entrypoint = [] [dependencies] pyth-sdk-solana = { path = "../", version = "0.6.1" } -solana-program = "=1.10.40" +solana-program = "=1.13.3" bytemuck = "1.7.2" borsh = "0.9" borsh-derive = "0.9.0" [dev-dependencies] -solana-program-test = "1.10.40" -solana-client = "1.10.40" -solana-sdk = "1.10.40" +solana-program-test = "=1.13.3" +solana-client = "=1.13.3" +solana-sdk = "=1.13.3" [lib] crate-type = ["cdylib", "lib"] From 0bc1d829e55b213cb9f731a5dd566012390e1990 Mon Sep 17 00:00:00 2001 From: Ali Behjati Date: Thu, 3 Nov 2022 12:45:25 +0100 Subject: [PATCH 6/7] Update the cli tools to 1.14.7 --- .github/workflows/pyth-sdk-solana.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pyth-sdk-solana.yml b/.github/workflows/pyth-sdk-solana.yml index 0512b20..a106c9f 100644 --- a/.github/workflows/pyth-sdk-solana.yml +++ b/.github/workflows/pyth-sdk-solana.yml @@ -34,7 +34,7 @@ jobs: run: sudo apt-get update && sudo apt-get install libudev-dev - name: Install Solana Binaries run: | - sh -c "$(curl -sSfL https://release.solana.com/v1.13.3/install)" + sh -c "$(curl -sSfL https://release.solana.com/v1.14.7/install)" echo "/home/runner/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH - name: Build run: cargo build --verbose From 3ae2695d782538396ddab16350708f155e717f6e Mon Sep 17 00:00:00 2001 From: Ali Behjati Date: Thu, 3 Nov 2022 13:08:23 +0100 Subject: [PATCH 7/7] Add a comment to explain why 14.x is used --- .github/workflows/pyth-sdk-solana.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pyth-sdk-solana.yml b/.github/workflows/pyth-sdk-solana.yml index a106c9f..d02aa03 100644 --- a/.github/workflows/pyth-sdk-solana.yml +++ b/.github/workflows/pyth-sdk-solana.yml @@ -34,6 +34,7 @@ jobs: run: sudo apt-get update && sudo apt-get install libudev-dev - name: Install Solana Binaries run: | + # Installing 1.14.x cli tools to have sbf instead of bpf. bpf does not work anymore. sh -c "$(curl -sSfL https://release.solana.com/v1.14.7/install)" echo "/home/runner/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH - name: Build