Skip to content

Commit

Permalink
Use single json deserialize function
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Sep 22, 2023
1 parent 91bc9af commit 9ae1384
Show file tree
Hide file tree
Showing 46 changed files with 299 additions and 358 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ and this project adheres to
- cosmwasm-std: Add `is_negative` for `Int{64,128,256,512}` ([#1867]).
- cosmwasm-std: Add `TryFrom<Uint{256,512}> for Uint64` and
`TryFrom<Uint{A}> for Int{B}` where `A >= B` ([#1870]).
- cosmwasm-std: Add `to_json_{vec,binary,string}` and
`from_json_{slice,binary,str}` and deprecate `to_{vec,binary}` in favor of
`to_json_{vec,binary}` and `from_{slice,binary}` in favor of
`from_json_{slice,binary}`. ([#1886])
- cosmwasm-std: Add `to_json_{vec,binary,string}` and `from_json` and deprecate
`to_{vec,binary}` in favor of `to_json_{vec,binary}` and `from_{slice,binary}`
in favor of `from_json`. ([#1886])

[#1854]: https://github.com/CosmWasm/cosmwasm/pull/1854
[#1861]: https://github.com/CosmWasm/cosmwasm/pull/1861
Expand Down
28 changes: 14 additions & 14 deletions contracts/crypto-verify/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ mod tests {
mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage,
};
use cosmwasm_std::{
from_json_slice, Binary, OwnedDeps, RecoverPubkeyError, StdError, VerificationError,
from_json, Binary, OwnedDeps, RecoverPubkeyError, StdError, VerificationError,
};
use hex_literal::hex;

Expand Down Expand Up @@ -274,7 +274,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_json_slice(&raw).unwrap();
let res: VerifyResponse = from_json(&raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}
Expand All @@ -296,7 +296,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_json_slice(&raw).unwrap();
let res: VerifyResponse = from_json(&raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: false });
}
Expand Down Expand Up @@ -339,7 +339,7 @@ mod tests {
signer_address: signer_address.into(),
};
let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_json_slice(&raw).unwrap();
let res: VerifyResponse = from_json(&raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}
Expand All @@ -360,7 +360,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_json_slice(&raw).unwrap();
let res: VerifyResponse = from_json(&raw).unwrap();
assert_eq!(res, VerifyResponse { verifies: false });
}

Expand All @@ -380,7 +380,7 @@ mod tests {
signer_address: signer_address.into(),
};
let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_json_slice(&raw).unwrap();
let res: VerifyResponse = from_json(&raw).unwrap();
assert_eq!(res, VerifyResponse { verifies: false });

// Broken signature
Expand Down Expand Up @@ -447,7 +447,7 @@ mod tests {
v,
};
let raw = query(deps.as_ref(), mock_env(), msg).unwrap();
let res: VerifyResponse = from_json_slice(&raw).unwrap();
let res: VerifyResponse = from_json(&raw).unwrap();
assert_eq!(res, VerifyResponse { verifies: true });
}

Expand Down Expand Up @@ -475,7 +475,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_json_slice(&raw).unwrap();
let res: VerifyResponse = from_json(&raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}
Expand Down Expand Up @@ -508,7 +508,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_json_slice(&raw).unwrap();
let res: VerifyResponse = from_json(&raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}
Expand Down Expand Up @@ -542,7 +542,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_json_slice(&raw).unwrap();
let res: VerifyResponse = from_json(&raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}
Expand Down Expand Up @@ -573,7 +573,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_json_slice(&raw).unwrap();
let res: VerifyResponse = from_json(&raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: false });
}
Expand Down Expand Up @@ -626,7 +626,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_json_slice(&raw).unwrap();
let res: VerifyResponse = from_json(&raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}
Expand All @@ -648,7 +648,7 @@ mod tests {
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_json_slice(&raw).unwrap();
let res: VerifyResponse = from_json(&raw).unwrap();

assert_eq!(res, VerifyResponse { verifies: false });
}
Expand Down Expand Up @@ -683,7 +683,7 @@ mod tests {
let query_msg = QueryMsg::ListVerificationSchemes {};

let raw = query(deps.as_ref(), mock_env(), query_msg).unwrap();
let res: ListVerificationsResponse = from_json_slice(&raw).unwrap();
let res: ListVerificationsResponse = from_json(&raw).unwrap();

assert_eq!(
res,
Expand Down
7 changes: 3 additions & 4 deletions contracts/cyberpunk/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ mod tests {
use cosmwasm_std::testing::{
mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage,
};
use cosmwasm_std::{from_json_binary, DenomMetadata, DenomUnit, OwnedDeps};
use cosmwasm_std::{from_json, DenomMetadata, DenomUnit, OwnedDeps};

fn setup() -> OwnedDeps<MockStorage, MockApi, MockQuerier> {
let mut deps = mock_dependencies();
Expand Down Expand Up @@ -274,12 +274,11 @@ mod tests {
);

let symbols: Vec<DenomMetadata> =
from_json_binary(&query(deps.as_ref(), mock_env(), QueryMsg::Denoms {}).unwrap())
.unwrap();
from_json(&query(deps.as_ref(), mock_env(), QueryMsg::Denoms {}).unwrap()).unwrap();

assert_eq!(symbols.len(), 98);

let denom: DenomMetadata = from_json_binary(
let denom: DenomMetadata = from_json(
&query(
deps.as_ref(),
mock_env(),
Expand Down
6 changes: 3 additions & 3 deletions contracts/cyberpunk/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! });
//! 4. Anywhere you see query(&deps, ...) you must replace it with query(&mut deps, ...)

use cosmwasm_std::{from_json_binary, Empty, Env, Response};
use cosmwasm_std::{from_json, Empty, Env, Response};
use cosmwasm_vm::testing::{
execute, instantiate, mock_env, mock_info, mock_instance, mock_instance_with_gas_limit, query,
};
Expand Down Expand Up @@ -152,13 +152,13 @@ fn test_env() {
)
.unwrap();

let received_env: Env = from_json_binary(&res.data.unwrap()).unwrap();
let received_env: Env = from_json(&res.data.unwrap()).unwrap();

assert_eq!(received_env, env);

let env = mock_env();
let received_env: Env =
from_json_binary(&query(&mut deps, env.clone(), QueryMsg::MirrorEnv {}).unwrap()).unwrap();
from_json(&query(&mut deps, env.clone(), QueryMsg::MirrorEnv {}).unwrap()).unwrap();

assert_eq!(received_env, env);
}
10 changes: 5 additions & 5 deletions contracts/floaty/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{
entry_point, from_json_slice, to_json_binary, to_json_vec, AllBalanceResponse, BankMsg, Deps,
entry_point, from_json, to_json_binary, to_json_vec, AllBalanceResponse, BankMsg, Deps,
DepsMut, Env, Event, MessageInfo, QueryResponse, Response, StdError, StdResult,
};

Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn execute(
.storage
.get(CONFIG_KEY)
.ok_or_else(|| StdError::not_found("State"))?;
let state: State = from_json_slice(&data)?;
let state: State = from_json(&data)?;

if info.sender == state.verifier {
let to_addr = state.beneficiary;
Expand Down Expand Up @@ -77,7 +77,7 @@ fn query_verifier(deps: Deps) -> StdResult<VerifierResponse> {
.storage
.get(CONFIG_KEY)
.ok_or_else(|| StdError::not_found("State"))?;
let state: State = from_json_slice(&data)?;
let state: State = from_json(&data)?;
Ok(VerifierResponse {
verifier: state.verifier.into(),
})
Expand Down Expand Up @@ -124,7 +124,7 @@ mod tests {

// it worked, let's check the state
let data = deps.storage.get(CONFIG_KEY).expect("no data stored");
let state: State = from_json_slice(&data).unwrap();
let state: State = from_json(&data).unwrap();
assert_eq!(state, expected_state);
}

Expand Down Expand Up @@ -246,7 +246,7 @@ mod tests {

// state should not change
let data = deps.storage.get(CONFIG_KEY).expect("no data stored");
let state: State = from_json_slice(&data).unwrap();
let state: State = from_json(&data).unwrap();
assert_eq!(
state,
State {
Expand Down
16 changes: 8 additions & 8 deletions contracts/hackatom/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use sha2::{Digest, Sha256};

use cosmwasm_std::{
entry_point, from_json_slice, to_json_binary, to_json_vec, Addr, AllBalanceResponse, Api,
BankMsg, CanonicalAddr, Deps, DepsMut, Env, Event, MessageInfo, QueryRequest, QueryResponse,
Response, StdError, StdResult, WasmMsg, WasmQuery,
entry_point, from_json, to_json_binary, to_json_vec, Addr, AllBalanceResponse, Api, BankMsg,
CanonicalAddr, Deps, DepsMut, Env, Event, MessageInfo, QueryRequest, QueryResponse, Response,
StdError, StdResult, WasmMsg, WasmQuery,
};

use crate::errors::HackError;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, Ha
.storage
.get(CONFIG_KEY)
.ok_or_else(|| StdError::not_found("State"))?;
let mut config: State = from_json_slice(&data)?;
let mut config: State = from_json(&data)?;
config.verifier = deps.api.addr_validate(&msg.verifier)?;
deps.storage.set(CONFIG_KEY, &to_json_vec(&config)?);

Expand Down Expand Up @@ -85,7 +85,7 @@ fn do_release(deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response, Ha
.storage
.get(CONFIG_KEY)
.ok_or_else(|| StdError::not_found("State"))?;
let state: State = from_json_slice(&data)?;
let state: State = from_json(&data)?;

if info.sender == state.verifier {
let to_addr = state.beneficiary;
Expand Down Expand Up @@ -262,7 +262,7 @@ fn query_verifier(deps: Deps) -> StdResult<VerifierResponse> {
.storage
.get(CONFIG_KEY)
.ok_or_else(|| StdError::not_found("State"))?;
let state: State = from_json_slice(&data)?;
let state: State = from_json(&data)?;
Ok(VerifierResponse {
verifier: state.verifier.into(),
})
Expand Down Expand Up @@ -336,7 +336,7 @@ mod tests {

// it worked, let's check the state
let data = deps.storage.get(CONFIG_KEY).expect("no data stored");
let state: State = from_json_slice(&data).unwrap();
let state: State = from_json(&data).unwrap();
assert_eq!(state, expected_state);
}

Expand Down Expand Up @@ -514,7 +514,7 @@ mod tests {

// state should not change
let data = deps.storage.get(CONFIG_KEY).expect("no data stored");
let state: State = from_json_slice(&data).unwrap();
let state: State = from_json(&data).unwrap();
assert_eq!(
state,
State {
Expand Down
8 changes: 4 additions & 4 deletions contracts/hackatom/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
//! 4. Anywhere you see query(&deps, ...) you must replace it with query(&mut deps, ...)

use cosmwasm_std::{
assert_approx_eq, coins, from_json_binary, to_json_vec, Addr, AllBalanceResponse, BankMsg,
Binary, ContractResult, Empty, Response, SubMsg,
assert_approx_eq, coins, from_json, to_json_vec, Addr, AllBalanceResponse, BankMsg, Binary,
ContractResult, Empty, Response, SubMsg,
};
use cosmwasm_vm::{
call_execute, from_slice,
Expand Down Expand Up @@ -183,15 +183,15 @@ fn querier_callbacks_work() {
// querying with balance gets the balance
let query_msg = QueryMsg::OtherBalance { address: rich_addr };
let query_response = query(&mut deps, mock_env(), query_msg).unwrap();
let bal: AllBalanceResponse = from_json_binary(&query_response).unwrap();
let bal: AllBalanceResponse = from_json(&query_response).unwrap();
assert_eq!(bal.amount, rich_balance);

// querying other accounts gets none
let query_msg = QueryMsg::OtherBalance {
address: String::from("someone else"),
};
let query_response = query(&mut deps, mock_env(), query_msg).unwrap();
let bal: AllBalanceResponse = from_json_binary(&query_response).unwrap();
let bal: AllBalanceResponse = from_json(&query_response).unwrap();
assert_eq!(bal.amount, vec![]);
}

Expand Down
19 changes: 8 additions & 11 deletions contracts/ibc-reflect-send/src/ibc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
entry_point, from_json_slice, to_json_binary, DepsMut, Env, IbcBasicResponse,
IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcMsg, IbcOrder, IbcPacketAckMsg,
entry_point, from_json, to_json_binary, DepsMut, Env, IbcBasicResponse, IbcChannelCloseMsg,
IbcChannelConnectMsg, IbcChannelOpenMsg, IbcMsg, IbcOrder, IbcPacketAckMsg,
IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse, Never, StdError, StdResult,
};

Expand Down Expand Up @@ -108,21 +108,18 @@ pub fn ibc_packet_ack(
// which local channel was this packet send from
let caller = msg.original_packet.src.channel_id;
// we need to parse the ack based on our request
let packet: PacketMsg = from_json_slice(&msg.original_packet.data)?;
let packet: PacketMsg = from_json(&msg.original_packet.data)?;
match packet {
PacketMsg::Dispatch { .. } => {
let res: AcknowledgementMsg<DispatchResponse> =
from_json_slice(&msg.acknowledgement.data)?;
let res: AcknowledgementMsg<DispatchResponse> = from_json(&msg.acknowledgement.data)?;
acknowledge_dispatch(deps, caller, res)
}
PacketMsg::WhoAmI {} => {
let res: AcknowledgementMsg<WhoAmIResponse> =
from_json_slice(&msg.acknowledgement.data)?;
let res: AcknowledgementMsg<WhoAmIResponse> = from_json(&msg.acknowledgement.data)?;
acknowledge_who_am_i(deps, caller, res)
}
PacketMsg::Balances {} => {
let res: AcknowledgementMsg<BalancesResponse> =
from_json_slice(&msg.acknowledgement.data)?;
let res: AcknowledgementMsg<BalancesResponse> = from_json(&msg.acknowledgement.data)?;
acknowledge_balances(deps, env, caller, res)
}
}
Expand Down Expand Up @@ -308,7 +305,7 @@ mod tests {
channel_id: channel_id.into(),
};
let r = query(deps.as_ref(), mock_env(), q).unwrap();
let acct: AccountResponse = from_json_slice(&r).unwrap();
let acct: AccountResponse = from_json(&r).unwrap();
assert!(acct.remote_addr.is_none());
assert!(acct.remote_balance.is_empty());
assert_eq!(0, acct.last_update_time.nanos());
Expand All @@ -322,7 +319,7 @@ mod tests {
channel_id: channel_id.into(),
};
let r = query(deps.as_ref(), mock_env(), q).unwrap();
let acct: AccountResponse = from_json_slice(&r).unwrap();
let acct: AccountResponse = from_json(&r).unwrap();
assert_eq!(acct.remote_addr.unwrap(), remote_addr);
assert!(acct.remote_balance.is_empty());
assert_eq!(0, acct.last_update_time.nanos());
Expand Down
Loading

0 comments on commit 9ae1384

Please sign in to comment.