Skip to content

Commit 75e2742

Browse files
Adapt the Solana best-practice example to the new api + fix CI (#85)
* Adapt to pyth sdk api changes * Update the readme as well * Remove unnecessary dependency * Try to solve the failing test of solana-sdk/test-contract * Make all 1.10.40 1.13.3 * Update the cli tools to 1.14.7 * Add a comment to explain why 14.x is used Co-authored-by: Yunhao Zhang <yz2327@cornell.edu>
1 parent bc06391 commit 75e2742

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

.github/workflows/pyth-sdk-solana.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ jobs:
3434
run: sudo apt-get update && sudo apt-get install libudev-dev
3535
- name: Install Solana Binaries
3636
run: |
37-
sh -c "$(curl -sSfL https://release.solana.com/v1.10.40/install)"
37+
# Installing 1.14.x cli tools to have sbf instead of bpf. bpf does not work anymore.
38+
sh -c "$(curl -sSfL https://release.solana.com/v1.14.7/install)"
3839
echo "/home/runner/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH
3940
- name: Build
4041
run: cargo build --verbose

examples/sol-contract/Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@ crate-type = ["cdylib", "lib"]
1010
[dependencies]
1111
borsh = "0.9"
1212
arrayref = "0.3.6"
13-
solana-program = "1.10.40"
13+
solana-program = "=1.13.3"
1414
pyth-sdk-solana = { path = "../../pyth-sdk-solana", version = "0.6.1" }
15-
16-
[dev-dependencies]
17-
solana-sdk = "1.10.40"
18-
solana-client = "1.10.40"

examples/sol-contract/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ Pyth SDK is used in the `Loan2Value` instruction in `src/processor.rs`.
1717
For the loan, the code first reads the unit price from the Pyth oracle.
1818
```rust
1919
let feed1 = load_price_feed_from_account_info(pyth_loan_account)?;
20-
let result1 = feed1.get_current_price().ok_or(ProgramError::Custom(3))?;
20+
let current_timestamp1 = Clock::get()?.unix_timestamp;
21+
let result1 = feed1.get_price_no_older_than(current_timestamp1, 60).ok_or(ProgramError::Custom(3))?;
2122
```
2223

2324
And then calculate the loan value given the quantity of the loan.
@@ -49,5 +50,5 @@ We assume that you have installed `cargo`, `solana`, `npm` and `node`.
4950
# Deploy the example contract
5051
> scripts/deploy.sh
5152
# Invoke the example contract
52-
> scripts/invoke.ts
53+
> scripts/invoke.sh
5354
```

examples/sol-contract/src/processor.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use solana_program::msg;
1111
use solana_program::program_error::ProgramError;
1212
use solana_program::program_memory::sol_memcpy;
1313
use solana_program::pubkey::Pubkey;
14+
use solana_program::sysvar::clock::Clock;
15+
use solana_program::sysvar::Sysvar;
1416

1517
use borsh::{
1618
BorshDeserialize,
@@ -84,7 +86,10 @@ pub fn process_instruction(
8486
// Here is more explanation on confidence interval in Pyth:
8587
// https://docs.pyth.network/consume-data/best-practices
8688
let feed1 = load_price_feed_from_account_info(pyth_loan_account)?;
87-
let result1 = feed1.get_current_price().ok_or(ProgramError::Custom(3))?;
89+
let current_timestamp1 = Clock::get()?.unix_timestamp;
90+
let result1 = feed1
91+
.get_price_no_older_than(current_timestamp1, 60)
92+
.ok_or(ProgramError::Custom(3))?;
8893
let loan_max_price = result1
8994
.price
9095
.checked_add(result1.conf as i64)
@@ -103,7 +108,10 @@ pub fn process_instruction(
103108
// Here is more explanation on confidence interval in Pyth:
104109
// https://docs.pyth.network/consume-data/best-practices
105110
let feed2 = load_price_feed_from_account_info(pyth_collateral_account)?;
106-
let result2 = feed2.get_current_price().ok_or(ProgramError::Custom(3))?;
111+
let current_timestamp2 = Clock::get()?.unix_timestamp;
112+
let result2 = feed2
113+
.get_price_no_older_than(current_timestamp2, 60)
114+
.ok_or(ProgramError::Custom(3))?;
107115
let collateral_min_price = result2
108116
.price
109117
.checked_sub(result2.conf as i64)

pyth-sdk-solana/test-contract/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ no-entrypoint = []
99

1010
[dependencies]
1111
pyth-sdk-solana = { path = "../", version = "0.6.1" }
12-
solana-program = "1.10.40"
12+
solana-program = "=1.13.3"
1313
bytemuck = "1.7.2"
1414
borsh = "0.9"
1515
borsh-derive = "0.9.0"
1616

1717
[dev-dependencies]
18-
solana-program-test = "1.10.40"
19-
solana-client = "1.10.40"
20-
solana-sdk = "1.10.40"
18+
solana-program-test = "=1.13.3"
19+
solana-client = "=1.13.3"
20+
solana-sdk = "=1.13.3"
2121

2222
[lib]
2323
crate-type = ["cdylib", "lib"]

0 commit comments

Comments
 (0)