Skip to content

Commit

Permalink
Merge pull request #228 from InjectiveLabs/feat/stargate-tester-authz…
Browse files Browse the repository at this point in the history
…-ext

Feat/stargate tester authz ext
  • Loading branch information
maxrobot authored Sep 16, 2024
2 parents 0d5be4e + e43ad9a commit cd84611
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 76 deletions.
61 changes: 0 additions & 61 deletions contracts/atomic-order-example/.circleci/config.yml

This file was deleted.

6 changes: 3 additions & 3 deletions contracts/atomic-order-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ cosmwasm-storage = { workspace = true }
cw-storage-plus = { workspace = true }
cw-utils = { workspace = true }
cw2 = { workspace = true }
injective-cosmwasm = { workspace = true, path = "../../packages/injective-cosmwasm" }
injective-math = { workspace = true, path = "../../packages/injective-math" }
injective-std = { workspace = true, path = "../../packages/injective-std" }
injective-cosmwasm = { path = "../../packages/injective-cosmwasm" }
injective-math = { path = "../../packages/injective-math" }
injective-std = { path = "../../packages/injective-std" }
prost = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion contracts/dummy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ optimize = """docker run --rm -v "$(pwd)":/code \
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
injective-cosmwasm = { workspace = true, path = "../../packages/injective-cosmwasm" }
injective-cosmwasm = { path = "../../packages/injective-cosmwasm" }
schemars = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
8 changes: 4 additions & 4 deletions contracts/injective-cosmwasm-mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
injective-cosmwasm = { workspace = true, path = "../../packages/injective-cosmwasm" }
injective-math = { workspace = true, path = "../../packages/injective-math" }
injective-std = { workspace = true, path = "../../packages/injective-std" }
injective-cosmwasm = { path = "../../packages/injective-cosmwasm" }
injective-math = { path = "../../packages/injective-math" }
injective-std = { path = "../../packages/injective-std" }
prost = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
injective-test-tube = { workspace = true }
injective-testing = { workspace = true, path = "../../packages/injective-testing" }
injective-testing = { path = "../../packages/injective-testing" }
6 changes: 3 additions & 3 deletions contracts/injective-cosmwasm-stargate-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
injective-cosmwasm = { workspace = true }
injective-math = { workspace = true }
injective-std = { workspace = true }
injective-cosmwasm = { path = "../../packages/injective-cosmwasm" }
injective-math = { path = "../../packages/injective-math" }
injective-std = { path = "../../packages/injective-std" }
prost = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
Expand Down
154 changes: 154 additions & 0 deletions contracts/injective-cosmwasm-stargate-example/src/testing/authz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
use crate::{
encode_helper::encode_proto_message,
msg::{QueryMsg, QueryStargateResponse},
utils::{execute_all_authorizations, ExchangeType, Setup},
};
use cosmos_sdk_proto::cosmos::authz::v1beta1::{QueryGranteeGrantsRequest, QueryGranterGrantsRequest, QueryGrantsRequest};
use injective_test_tube::RunnerError::QueryError;
use injective_test_tube::{Account, Module, RunnerResult, Wasm};

use crate::testing::type_helpers::{Authorization, Grants, StargateQueryGranteeGrantsResponse, StargateQueryGranterGrantsResponse};
use crate::utils::get_stargate_query_result;

#[test]
#[cfg_attr(not(feature = "integration"), ignore)]
fn test_query_grantee_grants() {
let env = Setup::new(ExchangeType::None);
let wasm = Wasm::new(&env.app);

execute_all_authorizations(&env.app, &env.users[0].account, env.users[1].account.address().to_string());
execute_all_authorizations(&env.app, &env.users[2].account, env.users[1].account.address().to_string());

let query_msg = QueryMsg::QueryStargateRaw {
path: "/cosmos.authz.v1beta1.Query/GranteeGrants".to_string(),
query_request: encode_proto_message(QueryGranteeGrantsRequest {
grantee: env.users[1].account.address().to_string(),
pagination: None,
}),
};

let messages = vec![
"/injective.exchange.v1beta1.MsgBatchUpdateOrders",
"/injective.exchange.v1beta1.MsgCreateDerivativeLimitOrder",
"/injective.exchange.v1beta1.MsgCreateDerivativeMarketOrder",
"/injective.exchange.v1beta1.MsgCreateSpotLimitOrder",
"/injective.exchange.v1beta1.MsgWithdraw",
];

let response_user0 = create_stargate_response(
messages.clone(),
env.users[0].account.address().to_string(),
env.users[1].account.address().to_string(),
);
let response_user2 = create_stargate_response(
messages,
env.users[2].account.address().to_string(),
env.users[1].account.address().to_string(),
);

let combined_grants = response_user0
.grants
.into_iter()
.chain(response_user2.grants.into_iter())
.collect::<Vec<_>>();
let query_result = get_stargate_query_result::<StargateQueryGranteeGrantsResponse>(wasm.query(&env.contract_address, &query_msg)).unwrap();

let all_grants_present = combined_grants.iter().all(|grant| query_result.grants.contains(grant));
let no_extra_grants = combined_grants.len() == query_result.grants.len();

assert!(all_grants_present);
assert!(no_extra_grants);
}

#[test]
#[cfg_attr(not(feature = "integration"), ignore)]
fn test_query_granter_grants() {
let env = Setup::new(ExchangeType::None);
let wasm = Wasm::new(&env.app);

execute_all_authorizations(&env.app, &env.users[0].account, env.users[1].account.address().to_string());

let query_msg = QueryMsg::QueryStargateRaw {
path: "/cosmos.authz.v1beta1.Query/GranterGrants".to_string(),
query_request: encode_proto_message(QueryGranterGrantsRequest {
granter: env.users[0].account.address().to_string(),
pagination: None,
}),
};

let query_result = get_stargate_query_result::<StargateQueryGranterGrantsResponse>(wasm.query(&env.contract_address, &query_msg)).unwrap();
assert_eq!(query_result.grants.len(), 5);

let query_msg = QueryMsg::QueryStargateRaw {
path: "/cosmos.authz.v1beta1.Query/GranterGrants".to_string(),
query_request: encode_proto_message(QueryGranterGrantsRequest {
granter: env.users[2].account.address().to_string(),
pagination: None,
}),
};

let query_result = get_stargate_query_result::<StargateQueryGranterGrantsResponse>(wasm.query(&env.contract_address, &query_msg)).unwrap();
assert_eq!(query_result.grants.len(), 0);
}

#[test]
#[cfg_attr(not(feature = "integration"), ignore)]
fn test_query_grants() {
let env = Setup::new(ExchangeType::None);
let wasm = Wasm::new(&env.app);

execute_all_authorizations(&env.app, &env.users[0].account, env.users[1].account.address().to_string());

let query_msg = QueryMsg::QueryStargateRaw {
path: "/cosmos.authz.v1beta1.Query/Grants".to_string(),
query_request: encode_proto_message(QueryGrantsRequest {
granter: env.users[0].account.address().to_string(),
grantee: env.users[1].account.address().to_string(),
msg_type_url: "/injective.exchange.v1beta1.MsgCreateDerivativeMarketOrder".to_string(),
pagination: None,
}),
};

let contract_response: QueryStargateResponse = wasm.query(&env.contract_address, &query_msg).unwrap();
println!("{:?}", contract_response);
// let query_result = get_stargate_query_result::<GranteeGrantsResponse>(wasm.query(&env.contract_address, &query_msg)).unwrap();
// println!("{:?}", query_result);

let query_msg = QueryMsg::QueryStargateRaw {
path: "/cosmos.authz.v1beta1.Query/Grants".to_string(),
query_request: encode_proto_message(QueryGrantsRequest {
granter: env.users[2].account.address().to_string(),
grantee: env.users[1].account.address().to_string(),
msg_type_url: "/injective.exchange.v1beta1.MsgCreateDerivativeMarketOrder".to_string(),
pagination: None,
}),
};

let contract_response: RunnerResult<QueryStargateResponse> = wasm.query(&env.contract_address, &query_msg);
println!("{:?}", contract_response);

if let Err(QueryError { msg }) = contract_response {
assert_eq!(
msg, "Generic error: Querier contract error: codespace: authz, code: 2: query wasm contract failed",
"The error message does not match the expected value"
);
} else {
assert!(false, "Expected an error, but got a success: {:?}", contract_response);
}
}

fn create_stargate_response(messages: Vec<&str>, granter: String, grantee: String) -> StargateQueryGranteeGrantsResponse {
let grants = messages
.into_iter()
.map(|msg| Grants {
granter: granter.clone(),
grantee: grantee.clone(),
authorization: Authorization {
type_str: "/cosmos.authz.v1beta1.GenericAuthorization".to_string(),
msg: msg.to_string(),
},
})
.collect();

StargateQueryGranteeGrantsResponse { grants }
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod authz;
mod test_auction;
mod test_auth;
mod test_bank;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,27 @@ pub struct DenomUnit {
pub struct Pagination {
// Define fields based on your pagination structure, if any
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct StargateQueryGranteeGrantsResponse {
pub grants: Vec<Grants>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct StargateQueryGranterGrantsResponse {
pub grants: Vec<Grants>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Grants {
pub granter: String,
pub grantee: String,
pub authorization: Authorization,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Authorization {
#[serde(rename = "@type")]
pub type_str: String,
pub msg: String,
}
2 changes: 1 addition & 1 deletion packages/injective-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ version = "1.13.0"
[dependencies]
chrono = { workspace = true }
cosmwasm-std = { workspace = true }
injective-std-derive = { workspace = true, path = "../injective-std-derive" }
injective-std-derive = { path = "../injective-std-derive" }
prost = { workspace = true }
prost-types = { workspace = true }
schemars = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions packages/injective-testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ anyhow = { workspace = true }
base64 = { workspace = true }
cosmwasm-std = { workspace = true }
cw-multi-test = { workspace = true }
injective-cosmwasm = { workspace = true, path = "../injective-cosmwasm" }
injective-math = { workspace = true, path = "../injective-math" }
injective-std = { workspace = true, path = "../injective-std" }
injective-cosmwasm = { path = "../injective-cosmwasm" }
injective-math = { path = "../injective-math" }
injective-std = { path = "../injective-std" }
injective-test-tube = { workspace = true }
prost = { workspace = true }
rand = { workspace = true }
Expand Down

0 comments on commit cd84611

Please sign in to comment.