-
Notifications
You must be signed in to change notification settings - Fork 58
Add support of anchor deserialzation #93
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
03ce6e8
Add support of anchor deserialization
yhzhang0128 c86cc96
cleanup
yhzhang0128 d2629b8
cleanup
yhzhang0128 8e7eea9
Remove the redundant automatically_derived
yhzhang0128 9ba7c80
Use PriceAccount for anchor recommended deserialization
yhzhang0128 55e84b7
It seems to work!
yhzhang0128 fa69289
It works
yhzhang0128 5e168ce
Use PriceFeed and implement Deref
yhzhang0128 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ target | |
**/*.rs.bk | ||
node_modules | ||
test-ledger | ||
program_address.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
examples/sol-anchor-contract/programs/example-sol-anchor-contract/src/error.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
#[error_code] | ||
pub enum ErrorCode { | ||
#[msg("You are not authorized to perform this action.")] | ||
Unauthorized, | ||
#[msg("The config has already been initialized.")] | ||
ReInitialize, | ||
#[msg("The config has not been initialized.")] | ||
UnInitialize, | ||
#[msg("Argument is invalid.")] | ||
InvalidArgument, | ||
#[msg("An overflow occurs.")] | ||
Overflow, | ||
#[msg("Pyth has an internal error.")] | ||
PythError, | ||
#[msg("Pyth price oracle is offline.")] | ||
PythOffline, | ||
#[msg("The loan value is higher than the collateral value.")] | ||
LoanValueTooHigh, | ||
#[msg("Program should not try to serialize a price account.")] | ||
TryToSerializePriceAccount, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
examples/sol-anchor-contract/programs/example-sol-anchor-contract/src/state.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::ops::Deref; | ||
use std::str::FromStr; | ||
use anchor_lang::prelude::*; | ||
use pyth_sdk_solana::state::load_price_account; | ||
|
||
use crate::ErrorCode; | ||
|
||
#[account] | ||
pub struct AdminConfig { | ||
pub loan_price_feed_id: Pubkey, | ||
pub collateral_price_feed_id: Pubkey, | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct PriceFeed (pyth_sdk::PriceFeed); | ||
|
||
impl anchor_lang::Owner for PriceFeed { | ||
fn owner() -> Pubkey { | ||
// Make sure the owner is the pyth oracle account on solana devnet | ||
let oracle_addr = "gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s"; | ||
return Pubkey::from_str(&oracle_addr).unwrap(); | ||
} | ||
} | ||
|
||
impl anchor_lang::AccountDeserialize for PriceFeed { | ||
fn try_deserialize_unchecked(data: &mut &[u8]) -> Result<Self>{ | ||
let account = load_price_account(data) | ||
.map_err(|_x| error!(ErrorCode::PythError))?; | ||
|
||
// Use a dummy key since the key field will be removed from the SDK | ||
let zeros: [u8; 32] = [0; 32]; | ||
let dummy_key = Pubkey::new(&zeros); | ||
let feed = account.to_price_feed(&dummy_key); | ||
return Ok(PriceFeed(feed)); | ||
} | ||
} | ||
|
||
impl anchor_lang::AccountSerialize for PriceFeed { | ||
fn try_serialize<W: std::io::Write>(&self, _writer: &mut W,) -> std::result::Result<(), Error> { | ||
Err(error!(ErrorCode::TryToSerializePriceAccount)) | ||
} | ||
} | ||
|
||
impl Deref for PriceFeed { | ||
type Target = pyth_sdk::PriceFeed; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.