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

Feature/fixes #79

Merged
merged 5 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion contracts/warp-controller/src/reply/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,20 @@ pub fn execute_job(
"failed_invalid_job_status",
));
} else {
let hydrated_vars: String = deps.querier.query_wasm_smart(
config.resolver_address.clone(),
&resolver::QueryMsg::QueryHydrateVars(resolver::QueryHydrateVarsMsg {
vars: finished_job.vars,
warp_account_addr: Some(finished_job.account.to_string()),
external_inputs: None,
}),
)?;

// vars are updated to next job iteration
let new_vars: String = deps.querier.query_wasm_smart(
config.resolver_address.clone(),
&resolver::QueryMsg::QueryApplyVarFn(resolver::QueryApplyVarFnMsg {
vars: finished_job.vars,
vars: hydrated_vars,
status: finished_job.status.clone(),
warp_account_addr: Some(finished_job.account.to_string()),
}),
Expand Down
98 changes: 75 additions & 23 deletions contracts/warp-resolver/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,27 @@
request: &QueryRequest<Empty>,
) -> SystemResult<ContractResult<Binary>> {
match &request {
QueryRequest::Wasm(WasmQuery::Smart {
contract_addr,
msg: _,
}) => {
// Mock logic for the Wasm::Smart case
// Here for simplicity, we return the contract_addr and msg as is.

// Mock logic for the Wasm::Smart case
// Here we return a JSON object with "address" and "msg" fields.
let response: String = json!({
"address": contract_addr,
"msg": "Mock message"
})
.to_string();

SystemResult::Ok(ContractResult::Ok(to_binary(&response).unwrap()))
QueryRequest::Wasm(WasmQuery::Smart { contract_addr, msg }) => {

Check warning on line 104 in contracts/warp-resolver/src/tests.rs

View workflow job for this annotation

GitHub Actions / Test

unused variable: `msg`
// Check if the query is for the vault contract address to get subaccount_id
if contract_addr == "mock_vault_contract_addr" {
// Simulate response with subaccount_id
let response = json!({
"config": {
"base": {
"subaccount_id": "0xecde8308ee5413d67288fae2abfc94dabeb16bd9000000000000000000000022"
}
}
});
SystemResult::Ok(ContractResult::Ok(to_binary(&response).unwrap()))
} else {
// Default mock response for other smart contract queries
let response = json!({
"address": contract_addr,
"msg": "Mock message"
})
.to_string();
SystemResult::Ok(ContractResult::Ok(to_binary(&response).unwrap()))
}
}
QueryRequest::Bank(BankQuery::Balance {
address: contract_addr,
Expand Down Expand Up @@ -219,10 +224,8 @@
init_fn: QueryExpr {
selector: "$".to_string(),
query: QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: "contract_addr".to_string(),
msg: Binary::from(
r#"{"test":"eyJhZGRyZXNzIjoiY29udHJhY3RfYWRkciIsIm1zZyI6Ik1vY2sgbWVzc2FnZSJ9"}"#.as_bytes()
),
contract_addr: "$warp.variable.var4".to_string(),
msg: Binary::from(r#"{"test":"$warp.variable.var1"}"#.as_bytes()),
}),
},
value: Some(r#"{"address":"contract_addr","msg":"Mock message"}"#.to_string()),
Expand Down Expand Up @@ -277,8 +280,8 @@
init_fn: QueryExpr {
selector: "$".to_string(),
query: QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: "static_value".to_string(),
msg: Binary::from(r#"{"test": "static_value"}"#.as_bytes()),
contract_addr: "$warp.variable.var1".to_string(),
msg: Binary::from(r#"{"test": "$warp.variable.var1"}"#.as_bytes()),
}),
},
value: Some(r#"{"address":"static_value","msg":"Mock message"}"#.to_string()),
Expand Down Expand Up @@ -332,7 +335,7 @@
init_fn: QueryExpr {
selector: "$".to_string(),
query: QueryRequest::Bank(BankQuery::Balance {
address: "static_value".to_string(),
address: "$warp.variable.var1".to_string(),
denom: "denom".to_string(),
}),
},
Expand Down Expand Up @@ -588,3 +591,52 @@
}))
)
}

#[test]
fn test_hydrate_static_and_custom_query_vars() {
let deps = mock_dependencies();
let env = mock_env();

// Assuming 'vault_contract_addr' and 'vault_strategy_denom' are known and used for the query
let vault_contract_addr = "mock_vault_contract_addr".to_string();

// Static variable initialization similar to the SDK code for 'subaccount_id'
let subaccount_id = Variable::Query(QueryVariable {
kind: VariableKind::String,
name: "subaccount_id".to_string(),
encode: false,
value: None,
init_fn: QueryExpr {
selector: "$.config.base.subaccount_id".to_string(),
query: QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: vault_contract_addr.clone(),
msg: to_binary(&json!({"base": {"config": {}}})).unwrap(),
}),
},
reinitialize: true,
update_fn: None,
});

let next_config = Variable::Query(QueryVariable {
name: "next_config".to_string(),
kind: VariableKind::Json,
init_fn: QueryExpr {
selector: "$.config".to_string(),
query: QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: vault_contract_addr.clone(),
msg: to_binary(&json!({ "base": { "config": {} } })).unwrap(),
}),
},
value: None,
reinitialize: true,
update_fn: None,
encode: false,
});

// Hydrate variables
let vars = vec![subaccount_id, next_config];
// let vars = vec![next_config];
let hydrated_vars = hydrate_vars(deps.as_ref(), env, vars, None, None).unwrap();

println!("{:?}", hydrated_vars);
}
62 changes: 58 additions & 4 deletions contracts/warp-resolver/src/util/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,62 @@ pub fn resolve_query_expr_string(
let r = Ref::new(&value);
let resolved = resolve_path(r, expr.selector)?;

Ok(resolved
.string()
.ok_or(ContractError::DecodeError {})?
.to_string())
let v = resolved.value().ok_or(ContractError::DecodeError {})?;

let json = Json { value: v.clone() };

Ok(json.to_string())
}

pub struct Json {
value: json_codec_wasm::Json,
}

impl Json {
fn to_json_string(&self, quote_strings: bool) -> String {
match &self.value {
json_codec_wasm::Json::Bool(b) => b.to_string(),
json_codec_wasm::Json::I128(i) => i.to_string(),
json_codec_wasm::Json::U128(u) => u.to_string(),
json_codec_wasm::Json::String(s) => {
if quote_strings {
format!("\"{}\"", s)
} else {
s.to_string()
}
}
json_codec_wasm::Json::Array(arr) => {
let items: Vec<String> = arr
.iter()
.map(|item| {
Json {
value: item.clone(),
}
.to_json_string(true)
})
.collect();
format!("[{}]", items.join(", "))
}
json_codec_wasm::Json::Object(obj) => {
let items: Vec<String> = obj
.iter()
.map(|(k, v)| {
format!(
"\"{}\":{}",
k,
Json { value: v.clone() }.to_json_string(true)
)
})
.collect();
format!("{{{}}}", items.join(","))
}
json_codec_wasm::Json::Null => "null".to_string(),
}
}
}

impl ToString for Json {
fn to_string(&self) -> String {
self.to_json_string(false)
}
}
85 changes: 63 additions & 22 deletions contracts/warp-resolver/src/util/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,62 +228,98 @@ pub fn hydrate_vars(
}
Variable::Query(mut v) => {
if v.reinitialize || v.value.is_none() {
v.init_fn = replace_references(v.init_fn, &hydrated_vars)?;
let replaced_init_fn = replace_references(v.init_fn.clone(), &hydrated_vars)?;

match v.kind {
VariableKind::String => {
v.value = Some(
// \"$warp.variable\" => \"VALUE"\
resolve_query_expr_string(deps, env.clone(), v.init_fn.clone())?
.to_string(),
resolve_query_expr_string(
deps,
env.clone(),
replaced_init_fn.clone(),
)?
.to_string(),
)
}
VariableKind::Uint => {
v.value = Some(
resolve_query_expr_uint(deps, env.clone(), v.init_fn.clone())?
.to_string(),
resolve_query_expr_uint(
deps,
env.clone(),
replaced_init_fn.clone(),
)?
.to_string(),
)
}
VariableKind::Int => {
v.value = Some(
resolve_query_expr_int(deps, env.clone(), v.init_fn.clone())?
.to_string(),
resolve_query_expr_int(
deps,
env.clone(),
replaced_init_fn.clone(),
)?
.to_string(),
)
}
VariableKind::Decimal => {
v.value = Some(
resolve_query_expr_decimal(deps, env.clone(), v.init_fn.clone())?
.to_string(),
resolve_query_expr_decimal(
deps,
env.clone(),
replaced_init_fn.clone(),
)?
.to_string(),
)
}
VariableKind::Timestamp => {
v.value = Some(
resolve_query_expr_int(deps, env.clone(), v.init_fn.clone())?
.to_string(),
resolve_query_expr_int(
deps,
env.clone(),
replaced_init_fn.clone(),
)?
.to_string(),
)
}
VariableKind::Bool => {
v.value = Some(
resolve_query_expr_bool(deps, env.clone(), v.init_fn.clone())?
.to_string(),
resolve_query_expr_bool(
deps,
env.clone(),
replaced_init_fn.clone(),
)?
.to_string(),
)
}
VariableKind::Amount => {
v.value = Some(
resolve_query_expr_uint(deps, env.clone(), v.init_fn.clone())?
.to_string(),
resolve_query_expr_uint(
deps,
env.clone(),
replaced_init_fn.clone(),
)?
.to_string(),
)
}
VariableKind::Asset => {
v.value = Some(
resolve_query_expr_string(deps, env.clone(), v.init_fn.clone())?
.to_string(),
resolve_query_expr_string(
deps,
env.clone(),
replaced_init_fn.clone(),
)?
.to_string(),
)
}
VariableKind::Json => {
v.value = Some(
resolve_query_expr_string(deps, env.clone(), v.init_fn.clone())?
.to_string(),
resolve_query_expr_string(
deps,
env.clone(),
replaced_init_fn.clone(),
)?
.to_string(),
)
}
}
Expand Down Expand Up @@ -663,9 +699,14 @@ fn replace_in_struct<T: Serialize + DeserializeOwned>(
msg: "Failed to convert struct to JSON.".to_string(),
})?;
let updated_struct_as_json = replace_in_struct_string(struct_as_json, vars)?;
serde_json_wasm::from_str(&updated_struct_as_json).map_err(|_| ContractError::HydrationError {
msg: "Failed to convert JSON back to struct.".to_string(),
})

let replaced_value = serde_json_wasm::from_str(&updated_struct_as_json).map_err(|_| {
ContractError::HydrationError {
msg: "Failed to convert JSON back to struct.".to_string(),
}
})?;

Ok(replaced_value)
}

fn replace_in_struct_string(value: String, vars: &[Variable]) -> Result<String, ContractError> {
Expand Down
Loading