-
Notifications
You must be signed in to change notification settings - Fork 308
[cosmwasm] add testing module #585
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
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 |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| pub mod error; | ||
| pub mod testing; | ||
|
|
||
| pub use pyth_sdk::{ | ||
| Price, | ||
| PriceFeed, | ||
|
|
||
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,87 @@ | ||
| use { | ||
| crate::{ | ||
| error::PythContractError, | ||
| PriceFeed, | ||
| PriceFeedResponse, | ||
| PriceIdentifier, | ||
| QueryMsg, | ||
| }, | ||
| cosmwasm_std::{ | ||
| from_binary, | ||
| to_binary, | ||
| Binary, | ||
| Coin, | ||
| ContractResult, | ||
| QuerierResult, | ||
| SystemError, | ||
| SystemResult, | ||
| }, | ||
| std::{ | ||
| collections::HashMap, | ||
| time::Duration, | ||
| u128, | ||
| }, | ||
| }; | ||
|
|
||
| /// Mock version of Pyth for testing cosmwasm contracts. | ||
| /// This mock stores some price feeds and responds to query messages. | ||
| #[derive(Clone)] | ||
| pub struct MockPyth { | ||
| pub valid_time_period: Duration, | ||
| pub fee_per_vaa: Coin, | ||
| pub feeds: HashMap<PriceIdentifier, PriceFeed>, | ||
| } | ||
|
|
||
| impl MockPyth { | ||
| /// Create a new `MockPyth`. You can either provide the full list of price feeds up front, | ||
| /// or add them later via `add_feed`. | ||
| pub fn new(valid_time_period: Duration, fee_per_vaa: Coin, feeds: &[PriceFeed]) -> Self { | ||
| let mut feeds_map = HashMap::new(); | ||
| for feed in feeds { | ||
| feeds_map.insert(feed.id, *feed); | ||
| } | ||
|
|
||
| MockPyth { | ||
| valid_time_period, | ||
| fee_per_vaa, | ||
| feeds: feeds_map, | ||
| } | ||
| } | ||
|
|
||
| /// Add a price feed that will be returned on queries. | ||
| pub fn add_feed(&mut self, feed: PriceFeed) { | ||
| self.feeds.insert(feed.id, feed); | ||
| } | ||
|
|
||
| /// TODO: Update this with example contracts -> Handler for processing query messages. See the tests in `contract.rs` for how to use this | ||
| /// handler within your tests. | ||
| pub fn handle_wasm_query(&self, msg: &Binary) -> QuerierResult { | ||
| let query_msg = from_binary::<QueryMsg>(msg); | ||
| match query_msg { | ||
| Ok(QueryMsg::PriceFeed { id }) => match self.feeds.get(&id) { | ||
| Some(feed) => { | ||
| SystemResult::Ok(to_binary(&PriceFeedResponse { price_feed: *feed }).into()) | ||
| } | ||
| None => SystemResult::Ok(ContractResult::from(Err( | ||
| PythContractError::PriceFeedNotFound, | ||
| ))), | ||
| }, | ||
| Ok(QueryMsg::GetValidTimePeriod) => { | ||
| SystemResult::Ok(to_binary(&self.valid_time_period).into()) | ||
| } | ||
| Ok(QueryMsg::GetUpdateFee { vaas }) => { | ||
| let new_amount = self | ||
| .fee_per_vaa | ||
| .amount | ||
| .u128() | ||
| .checked_mul(vaas.len() as u128) | ||
| .unwrap(); | ||
| SystemResult::Ok(to_binary(&Coin::new(new_amount, &self.fee_per_vaa.denom)).into()) | ||
| } | ||
| Err(_e) => SystemResult::Err(SystemError::InvalidRequest { | ||
| error: "Invalid message".into(), | ||
| request: msg.clone(), | ||
| }), | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i note that contract.rs isn't included here at the moment. if you're going to add that in a future PR, great. If not, we should document how to use this somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the example contract here - https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/cosmwasm/examples/cw-contract/src/contract.rs
Will update the example in future PRs. This Todo will be resolved then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merging this rn