Skip to content
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

fix: failing integration test #28

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion contracts/treasury/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ pub fn instantiate(
msg: InstantiateMsg,
) -> ContractResult<Response> {
cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
execute::init(deps, info, msg.admin, msg.type_urls, msg.grant_configs)
execute::init(
deps,
info,
msg.admin,
msg.type_urls,
msg.grant_configs,
msg.fee_config,
)
}

#[entry_point]
Expand Down
68 changes: 44 additions & 24 deletions contracts/treasury/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use cosmos_sdk_proto::cosmos::authz::v1beta1::{QueryGrantsRequest, QueryGrantsResponse};

Check failure on line 1 in contracts/treasury/src/execute.rs

View workflow job for this annotation

GitHub Actions / Lints

unused import: `QueryGrantsResponse`

Check warning on line 1 in contracts/treasury/src/execute.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `QueryGrantsResponse`
use cosmos_sdk_proto::cosmos::feegrant::v1beta1::{QueryAllowanceRequest, QueryAllowanceResponse};
use cosmos_sdk_proto::traits::MessageExt;
use cosmwasm_std::{Addr, CosmosMsg, DepsMut, Env, Event, MessageInfo, Order, Response};
use pbjson_types::Timestamp;
use serde_json::Value;

use crate::error::ContractError::{
AuthzGrantMismatch, AuthzGrantNotFound, ConfigurationMismatch, Unauthorized,
AuthzGrantMismatch, AuthzGrantNoAuthorization, AuthzGrantNotFound, ConfigurationMismatch,
Unauthorized,
};
use crate::error::ContractResult;
use crate::grant::allowance::format_allowance;
Expand All @@ -18,6 +20,7 @@
admin: Option<Addr>,
type_urls: Vec<String>,
grant_configs: Vec<GrantConfig>,
fee_config: FeeConfig,
) -> ContractResult<Response> {
let treasury_admin = match admin {
None => info.sender,
Expand All @@ -33,6 +36,8 @@
GRANT_CONFIGS.save(deps.storage, type_urls[i].clone(), &grant_configs[i])?;
}

FEE_CONFIG.save(deps.storage, &fee_config)?;

Ok(Response::new().add_event(
Event::new("create_treasury_instance")
.add_attributes(vec![("admin", treasury_admin.into_string())]),
Expand Down Expand Up @@ -133,32 +138,46 @@
.to_bytes()?;
let authz_query_res =
deps.querier
.query::<QueryGrantsResponse>(&cosmwasm_std::QueryRequest::Stargate {
.query::<Value>(&cosmwasm_std::QueryRequest::Stargate {
path: "/cosmos.authz.v1beta1.Query/Grants".to_string(),
data: authz_query_msg_bytes.into(),
})?;

let grants = &authz_query_res.grants;
let grants = &authz_query_res["grants"];
// grant queries with a granter, grantee and type_url should always result
// in only one result, unless the grant is optional
if grants.clone().is_empty() && !grant_config.optional {
if !grants.is_array() {
return Err(AuthzGrantNotFound { msg_type_url });
} else {
match grants.first() {
None => return Err(AuthzGrantNotFound { msg_type_url }),
Some(grant) => {
match grant.clone().authorization {
None => return Err(AuthzGrantNotFound { msg_type_url }),
Some(auth) => {
// the authorization must match the one in the config
if grant_config.authorization.ne(&auth.into()) {
return Err(AuthzGrantMismatch);
}
}
}
}
}
}
let grant = grants[0].clone();
if grant.is_null() {
return Err(AuthzGrantNotFound { msg_type_url });
}
let auth = &grant["authorization"];
if auth.is_null() {
return Err(AuthzGrantNoAuthorization);
}
if grant_config.authorization.ne(auth) {
return Err(AuthzGrantMismatch);
}
// if grants.clone().is_empty() && !grant_config.optional {
// return Err(AuthzGrantNotFound { msg_type_url });
// } else {
// match grants.first() {
// None => return Err(AuthzGrantNotFound { msg_type_url }),
// Some(grant) => {
// match grant.clone().authorization {
// None => return Err(AuthzGrantNotFound { msg_type_url }),
// Some(auth) => {
// // the authorization must match the one in the config
// if grant_config.authorization.ne(&auth.into()) {
// return Err(AuthzGrantMismatch);
// }
// }
// }
// }
// }
// }
}
// at this point, all the authz grants in the grant_config are verified

Expand All @@ -176,7 +195,7 @@
let expiration_time = env.block.time.plus_seconds(seconds as u64);
Some(Timestamp {
seconds: expiration_time.seconds() as i64,
nanos: expiration_time.nanos() as i32,
nanos: expiration_time.subsec_nanos() as i32,
})
}
};
Expand Down Expand Up @@ -205,12 +224,13 @@
grantee: authz_grantee.to_string(),
}
.to_bytes()?;
let feegrant_query_res = deps.querier.query::<QueryAllowanceResponse>(
&cosmwasm_std::QueryRequest::Stargate {
let feegrant_query_res = deps
.querier
.query::<QueryAllowanceResponse>(&cosmwasm_std::QueryRequest::Stargate {
path: "/cosmos.feegrant.v1beta1.Query/Allowance".to_string(),
data: feegrant_query_msg_bytes.into(),
},
)?;
})
.unwrap_or_default();

let mut msgs: Vec<CosmosMsg> = Vec::new();
if feegrant_query_res.allowance.is_some() {
Expand Down
3 changes: 2 additions & 1 deletion contracts/treasury/src/grant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ pub mod allowance;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Binary;
use prost::bytes::Bytes;
use serde_json::Value;

#[cw_serde]
pub struct GrantConfig {
description: String,
pub authorization: Any,
pub authorization: Value,
pub optional: bool,
}

Expand Down
Loading