Skip to content

Commit

Permalink
fix: modify to use to_json_{vec,binary}, from_json
Browse files Browse the repository at this point in the history
Because to_{vec,binary}, from_{slice,binary} has been deprecated (CosmWasm/cosmwasm#1886)
  • Loading branch information
da1suk8 committed Jan 17, 2024
1 parent 78b634a commit 8e6c1c1
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 49 deletions.
34 changes: 17 additions & 17 deletions contracts/query-queue/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{
entry_point, from_slice, to_binary, to_vec, Deps, DepsMut, Env, MessageInfo, QueryResponse,
Response, StdResult, Storage,
entry_point, from_json, to_json_binary, to_json_vec, Deps, DepsMut, Env, MessageInfo,
QueryResponse, Response, StdResult, Storage,
};

use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
Expand Down Expand Up @@ -57,11 +57,11 @@ fn write_queue_address(storage: &mut dyn Storage, addr: String) {
let config = Config {
queue_address: addr,
};
storage.set(CONFIG_KEY, &to_vec(&config).unwrap());
storage.set(CONFIG_KEY, &to_json_vec(&config).unwrap());
}

fn read_queue_address(storage: &dyn Storage) -> String {
let config: Config = from_slice(&storage.get(CONFIG_KEY).unwrap()).unwrap();
let config: Config = from_json(&storage.get(CONFIG_KEY).unwrap()).unwrap();
config.queue_address
}

Expand Down Expand Up @@ -96,11 +96,11 @@ fn handle_change_address(deps: DepsMut, address: String) -> StdResult<Response>
#[entry_point]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
match msg {
QueryMsg::Raw { key } => to_binary(&query_raw(deps, key)?),
QueryMsg::Count {} => to_binary(&query_count(deps, msg)?),
QueryMsg::Sum {} => to_binary(&query_sum(deps, msg)?),
QueryMsg::Reducer {} => to_binary(&query_reducer(deps, msg)?),
QueryMsg::List {} => to_binary(&query_list(deps, msg)?),
QueryMsg::Raw { key } => to_json_binary(&query_raw(deps, key)?),
QueryMsg::Count {} => to_json_binary(&query_count(deps, msg)?),
QueryMsg::Sum {} => to_json_binary(&query_sum(deps, msg)?),
QueryMsg::Reducer {} => to_json_binary(&query_reducer(deps, msg)?),
QueryMsg::List {} => to_json_binary(&query_list(deps, msg)?),
}
}

Expand All @@ -110,7 +110,7 @@ fn query_raw(deps: Deps, key: u32) -> StdResult<RawResponse> {
match response {
None => Ok(RawResponse { item: None }),
Some(v) => {
let i: Item = from_slice(v.as_slice())?;
let i: Item = from_json(v.as_slice())?;
Ok(RawResponse {
item: Some(i.value),
})
Expand Down Expand Up @@ -175,22 +175,22 @@ mod tests {
contract_addr: _,
msg,
} => {
let q_msg: QueryMsg = from_slice(msg).unwrap();
let q_msg: QueryMsg = from_json(msg).unwrap();
match q_msg {
QueryMsg::Count {} => SystemResult::Ok(ContractResult::Ok(
to_binary(&CountResponse { count: 1 }).unwrap(),
to_json_binary(&CountResponse { count: 1 }).unwrap(),
)),
QueryMsg::Sum {} => SystemResult::Ok(ContractResult::Ok(
to_binary(&SumResponse { sum: 42 }).unwrap(),
to_json_binary(&SumResponse { sum: 42 }).unwrap(),
)),
QueryMsg::Reducer {} => SystemResult::Ok(ContractResult::Ok(
to_binary(&ReducerResponse {
to_json_binary(&ReducerResponse {
counters: vec![(42, 0)],
})
.unwrap(),
)),
QueryMsg::List {} => SystemResult::Ok(ContractResult::Ok(
to_binary(&ListResponse {
to_json_binary(&ListResponse {
empty: vec![],
early: vec![0],
late: vec![],
Expand All @@ -204,7 +204,7 @@ mod tests {
contract_addr: _,
key: _,
} => SystemResult::Ok(ContractResult::Ok(
to_binary(&RawQueryResponse { value: 42 }).unwrap(),
to_json_binary(&RawQueryResponse { value: 42 }).unwrap(),
)),
_ => SystemResult::Err(SystemError::Unknown {}),
});
Expand All @@ -214,7 +214,7 @@ mod tests {
#[test]
fn test_instantiate() {
let deps = create_contract();
let config: Config = from_slice(&deps.storage.get(CONFIG_KEY).unwrap()).unwrap();
let config: Config = from_json(&deps.storage.get(CONFIG_KEY).unwrap()).unwrap();
assert_eq!(config.queue_address, QUEUE_ADDRESS);
}

Expand Down
14 changes: 7 additions & 7 deletions contracts/query-queue/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{
from_binary, from_slice, to_binary, Binary, ContractResult, MessageInfo, Response, SystemError,
from_json, to_json_binary, Binary, ContractResult, MessageInfo, Response, SystemError,
SystemResult, WasmQuery,
};
use cosmwasm_vm::{
Expand Down Expand Up @@ -58,10 +58,10 @@ fn create_contract() -> (Instance<MockApi, MockStorage, MockQuerier>, MessageInf
addr: contract_addr.to_string(),
});
};
let q_msg: QueryMsg = from_slice(msg).unwrap();
let q_msg: QueryMsg = from_json(msg).unwrap();
match q_msg {
QueryMsg::Sum {} => SystemResult::Ok(ContractResult::Ok(
to_binary(&SumResponse { sum: 42 }).unwrap(),
to_json_binary(&SumResponse { sum: 42 }).unwrap(),
)),
_ => SystemResult::Err(SystemError::Unknown {}),
}
Expand All @@ -86,7 +86,7 @@ fn create_contract() -> (Instance<MockApi, MockStorage, MockQuerier>, MessageInf
fn instantiate_and_query() {
let (mut instance, _) = create_contract();
let data = query(&mut instance, mock_env(), QueryMsg::Sum {}).unwrap();
let res: SumResponse = from_binary(&data).unwrap();
let res: SumResponse = from_json(&data).unwrap();
assert_eq!(res.sum, 42);
}

Expand Down Expand Up @@ -168,7 +168,7 @@ fn create_integrated_query_contract() -> (Instance<MockApi, MockStorage, MockQue
addr: contract_addr.to_string(),
});
};
let q_msg: QueryMsg = from_slice(msg).unwrap();
let q_msg: QueryMsg = from_json(msg).unwrap();
let res = query(&mut queue_instance, mock_env(), q_msg);
SystemResult::Ok(res)
}
Expand Down Expand Up @@ -208,9 +208,9 @@ fn create_integrated_query_contract() -> (Instance<MockApi, MockStorage, MockQue
fn integration_query_contract_queue() {
let (mut query_instance, _) = create_integrated_query_contract();
let data = query(&mut query_instance, mock_env(), QueryMsg::Sum {}).unwrap();
let res: SumResponse = from_binary(&data).unwrap();
let res: SumResponse = from_json(&data).unwrap();
assert_eq!(res.sum, 42);
let data = query(&mut query_instance, mock_env(), QueryMsg::Raw { key: 0 }).unwrap();
let res: RawResponse = from_binary(&data).unwrap();
let res: RawResponse = from_json(&data).unwrap();
assert_eq!(res.item, Some(42));
}
12 changes: 6 additions & 6 deletions contracts/voting-with-uuid/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::state::{
save_poll, Poll, PollStatus, State, TokenManager, Voter,
};
use cosmwasm_std::{
attr, coin, entry_point, new_uuid, to_binary, Addr, BankMsg, Binary, Coin, CosmosMsg, Deps,
DepsMut, Env, MessageInfo, Response, StdError, StdResult, Storage, Uint128, Uuid,
attr, coin, entry_point, new_uuid, to_json_binary, Addr, BankMsg, Binary, Coin, CosmosMsg,
Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult, Storage, Uint128, Uuid,
};

pub const VOTING_TOKEN: &str = "voting_token";
Expand Down Expand Up @@ -219,7 +219,7 @@ pub fn create_poll(
)
.add_attribute("end_height", new_poll.end_height.to_string())
.add_attribute("start_height", start_height.unwrap_or(0).to_string())
.set_data(to_binary(&CreatePollResponse { poll_id })?);
.set_data(to_json_binary(&CreatePollResponse { poll_id })?);
Ok(r)
}

Expand Down Expand Up @@ -434,7 +434,7 @@ pub fn make_seq_id(
#[entry_point]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Config {} => to_binary(&load_config(deps.storage)?),
QueryMsg::Config {} => to_json_binary(&load_config(deps.storage)?),
QueryMsg::TokenStake { address } => {
token_balance(deps, deps.api.addr_validate(address.as_str())?)
}
Expand All @@ -456,7 +456,7 @@ fn query_poll(deps: Deps, poll_id: Uuid) -> StdResult<Binary> {
start_height: poll.start_height,
description: poll.description,
};
to_binary(&resp)
to_json_binary(&resp)
}

fn token_balance(deps: Deps, address: Addr) -> StdResult<Binary> {
Expand All @@ -466,5 +466,5 @@ fn token_balance(deps: Deps, address: Addr) -> StdResult<Binary> {
token_balance: token_manager.token_balance,
};

to_binary(&resp)
to_json_binary(&resp)
}
14 changes: 7 additions & 7 deletions contracts/voting-with-uuid/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{
from_slice, storage_keys::namespace_with_key, to_vec, Addr, StdError, StdResult, Storage,
from_json, storage_keys::namespace_with_key, to_json_vec, Addr, StdError, StdResult, Storage,
Uint128, Uuid,
};
use schemars::JsonSchema;
Expand Down Expand Up @@ -52,29 +52,29 @@ pub struct Poll {
}

pub fn save_config(storage: &mut dyn Storage, item: &State) -> StdResult<()> {
storage.set(CONFIG_KEY, &to_vec(item)?);
storage.set(CONFIG_KEY, &to_json_vec(item)?);
Ok(())
}

pub fn load_config(storage: &dyn Storage) -> StdResult<State> {
storage
.get(CONFIG_KEY)
.ok_or_else(|| StdError::not_found("config"))
.and_then(|v| from_slice(&v))
.and_then(|v| from_json(&v))
}

pub fn save_poll(storage: &mut dyn Storage, key: &Uuid, poll: &Poll) -> StdResult<()> {
storage.set(
&namespace_with_key(&[POLL_KEY], key.as_bytes()),
&to_vec(poll)?,
&to_json_vec(poll)?,
);
Ok(())
}

pub fn may_load_poll(storage: &dyn Storage, key: &Uuid) -> StdResult<Option<Poll>> {
storage
.get(&namespace_with_key(&[POLL_KEY], key.as_bytes()))
.map(|v| from_slice(&v))
.map(|v| from_json(&v))
.transpose()
}

Expand All @@ -89,15 +89,15 @@ pub fn save_bank(
) -> StdResult<()> {
storage.set(
&namespace_with_key(&[BANK_KEY], key.as_bytes()),
&to_vec(token_manager)?,
&to_json_vec(token_manager)?,
);
Ok(())
}

pub fn may_load_bank(storage: &dyn Storage, key: &Addr) -> StdResult<Option<TokenManager>> {
storage
.get(&namespace_with_key(&[BANK_KEY], key.as_bytes()))
.map(|v| from_slice(&v))
.map(|v| from_json(&v))
.transpose()
}

Expand Down
12 changes: 6 additions & 6 deletions contracts/voting-with-uuid/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cosmwasm_std::testing::{
mock_dependencies, mock_dependencies_with_balance, mock_env, mock_info,
};
use cosmwasm_std::{
attr, coins, from_binary, Addr, BankMsg, Coin, CosmosMsg, DepsMut, Env, MessageInfo, Response,
attr, coins, from_json, Addr, BankMsg, Coin, CosmosMsg, DepsMut, Env, MessageInfo, Response,
StdError, Timestamp, Uint128, Uuid,
};
use std::str::FromStr;
Expand Down Expand Up @@ -201,7 +201,7 @@ fn fails_end_poll_before_end_height() {

let poll_id = Uuid::from_str("849c1f99-e882-53e6-8e63-e5aa001359c2").unwrap();
let res = query(deps.as_ref(), mock_env(), QueryMsg::Poll { poll_id }).unwrap();
let value: PollResponse = from_binary(&res).unwrap();
let value: PollResponse = from_json(&res).unwrap();
assert_eq!(Some(10001), value.end_height);

let poll_id = Uuid::from_str("849c1f99-e882-53e6-8e63-e5aa001359c2").unwrap();
Expand Down Expand Up @@ -298,7 +298,7 @@ fn happy_days_end_poll() {
]
);
let res = query(deps.as_ref(), mock_env(), QueryMsg::Poll { poll_id }).unwrap();
let value: PollResponse = from_binary(&res).unwrap();
let value: PollResponse = from_json(&res).unwrap();
assert_eq!(PollStatus::Passed, value.status);
}

Expand Down Expand Up @@ -337,7 +337,7 @@ fn end_poll_zero_quorum() {
);

let res = query(deps.as_ref(), env, QueryMsg::Poll { poll_id }).unwrap();
let value: PollResponse = from_binary(&res).unwrap();
let value: PollResponse = from_json(&res).unwrap();
assert_eq!(PollStatus::Rejected, value.status);
}

Expand Down Expand Up @@ -415,7 +415,7 @@ fn end_poll_quorum_rejected() {
);

let res = query(deps.as_ref(), mock_env(), QueryMsg::Poll { poll_id }).unwrap();
let value: PollResponse = from_binary(&res).unwrap();
let value: PollResponse = from_json(&res).unwrap();
assert_eq!(PollStatus::Rejected, value.status);
}

Expand Down Expand Up @@ -492,7 +492,7 @@ fn end_poll_nay_rejected() {
);

let res = query(deps.as_ref(), mock_env(), QueryMsg::Poll { poll_id }).unwrap();
let value: PollResponse = from_binary(&res).unwrap();
let value: PollResponse = from_json(&res).unwrap();
assert_eq!(PollStatus::Rejected, value.status);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/std/src/results/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ mod tests {
use super::super::BankMsg;
use super::*;
use crate::results::submessages::{ReplyOn, UNUSED_MSG_ID};
use crate::{attr, coins, from_slice, to_vec, Addr, Coin, ContractResult, Event, IntoEvent};
use crate::{
attr, coins, from_json, to_json_vec, Addr, Coin, ContractResult, Event, IntoEvent,
};

#[test]
fn response_add_attributes_works() {
Expand Down
16 changes: 11 additions & 5 deletions packages/std/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::Deref;
use std::str::FromStr;
use uuid as raw_uuid;

use crate::{from_slice, to_vec};
use crate::{from_json, to_json_vec};
use crate::{Api, Env, StdResult, Storage};

/// Uuid Provides a Uuid that can be used deterministically.
Expand Down Expand Up @@ -41,13 +41,16 @@ const CONTRACT_UUID_SEQ_NUM_KEY: &[u8] = b"contract_uuid_seq_num";
pub fn new_uuid(env: &Env, storage: &mut dyn Storage, api: &dyn Api) -> StdResult<Uuid> {
let raw_seq_num = storage.get(CONTRACT_UUID_SEQ_NUM_KEY);
let seq_num: u16 = match raw_seq_num {
Some(data) => from_slice(&data).unwrap(),
Some(data) => from_json(&data).unwrap(),
None => 0,
};
let next_seq_num: u16 = seq_num.wrapping_add(1);

let uuid_name = format!("{} {} {}", env.contract.address, env.block.height, seq_num);
storage.set(CONTRACT_UUID_SEQ_NUM_KEY, &(to_vec(&next_seq_num).unwrap()));
storage.set(
CONTRACT_UUID_SEQ_NUM_KEY,
&(to_json_vec(&next_seq_num).unwrap()),
);

Uuid::new_v5(
api,
Expand Down Expand Up @@ -78,7 +81,7 @@ impl FromStr for Uuid {
mod tests {
use crate::testing::{mock_env, MockApi, MockStorage};
use crate::{new_uuid, Uuid};
use crate::{to_vec, Addr, Storage};
use crate::{to_json_vec, Addr, Storage};
use std::str::FromStr;
use uuid as raw_uuid;

Expand Down Expand Up @@ -126,7 +129,10 @@ mod tests {
env.contract.address = Addr::unchecked("link1qyqszqgpqyqszqgpqyqszqgpqyqszqgp8apuk5");
env.block.height = u64::MAX;
let stor: &mut dyn Storage = &mut storage;
stor.set(CONTRACT_UUID_SEQ_NUM_KEY, &(to_vec(&u16::MAX).unwrap()));
stor.set(
CONTRACT_UUID_SEQ_NUM_KEY,
&(to_json_vec(&u16::MAX).unwrap()),
);

let uuid = new_uuid(&env, &mut storage, &api);
assert!(uuid.is_ok());
Expand Down

0 comments on commit 8e6c1c1

Please sign in to comment.