Skip to content

Commit 8e8471b

Browse files
committed
Remove usage of query_all_balances from ibc-reflect
1 parent f9f3feb commit 8e8471b

File tree

6 files changed

+42
-88
lines changed

6 files changed

+42
-88
lines changed

contracts/ibc-reflect/schema/ibc/acknowledgement_msg_balances.json renamed to contracts/ibc-reflect/schema/ibc/acknowledgement_msg_balance.json

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
3-
"title": "AcknowledgementMsgBalances",
3+
"title": "AcknowledgementMsgBalance",
44
"description": "A custom acknowledgement type. The success type `T` depends on the PacketMsg variant.\n\nThis could be refactored to use [StdAck] at some point. However, it has a different success variant name (\"ok\" vs. \"result\") and a JSON payload instead of a binary payload.\n\n[StdAck]: https://github.com/CosmWasm/cosmwasm/issues/1512",
55
"oneOf": [
66
{
@@ -10,7 +10,7 @@
1010
],
1111
"properties": {
1212
"ok": {
13-
"$ref": "#/definitions/BalancesResponse"
13+
"$ref": "#/definitions/BalanceResponse"
1414
}
1515
},
1616
"additionalProperties": false
@@ -29,22 +29,19 @@
2929
}
3030
],
3131
"definitions": {
32-
"BalancesResponse": {
32+
"BalanceResponse": {
3333
"description": "This is the success response we send on ack for PacketMsg::Balance. Just acknowledge success or error",
3434
"type": "object",
3535
"required": [
3636
"account",
37-
"balances"
37+
"balance"
3838
],
3939
"properties": {
4040
"account": {
4141
"type": "string"
4242
},
43-
"balances": {
44-
"type": "array",
45-
"items": {
46-
"$ref": "#/definitions/Coin"
47-
}
43+
"balance": {
44+
"$ref": "#/definitions/Coin"
4845
}
4946
},
5047
"additionalProperties": false

contracts/ibc-reflect/schema/ibc/packet_msg.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,19 @@
4242
{
4343
"type": "object",
4444
"required": [
45-
"balances"
45+
"balance"
4646
],
4747
"properties": {
48-
"balances": {
48+
"balance": {
4949
"type": "object",
50+
"required": [
51+
"denom"
52+
],
53+
"properties": {
54+
"denom": {
55+
"type": "string"
56+
}
57+
},
5058
"additionalProperties": false
5159
}
5260
},

contracts/ibc-reflect/src/bin/schema.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use cosmwasm_schema::{export_schema, export_schema_with_title, schema_for, write
44
use cosmwasm_std::Empty;
55

66
use ibc_reflect::msg::{
7-
AcknowledgementMsg, BalancesResponse, DispatchResponse, ExecuteMsg, InstantiateMsg, PacketMsg,
7+
AcknowledgementMsg, BalanceResponse, DispatchResponse, ExecuteMsg, InstantiateMsg, PacketMsg,
88
QueryMsg, WhoAmIResponse,
99
};
1010

@@ -23,9 +23,9 @@ fn main() {
2323
out_dir.push("ibc");
2424
export_schema(&schema_for!(PacketMsg), &out_dir);
2525
export_schema_with_title(
26-
&schema_for!(AcknowledgementMsg<BalancesResponse>),
26+
&schema_for!(AcknowledgementMsg<BalanceResponse>),
2727
&out_dir,
28-
"AcknowledgementMsgBalances",
28+
"AcknowledgementMsgBalance",
2929
);
3030
export_schema_with_title(
3131
&schema_for!(AcknowledgementMsg<DispatchResponse>),

contracts/ibc-reflect/src/contract.rs

Lines changed: 19 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use cosmwasm_std::{
2-
entry_point, from_json, to_json_binary, wasm_execute, BankMsg, Binary, CosmosMsg, Deps,
3-
DepsMut, Empty, Env, Event, Ibc3ChannelOpenResponse, IbcAcknowledgement, IbcBasicResponse,
4-
IbcChannelCloseMsg, IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcMsg,
5-
IbcOrder, IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse,
6-
MessageInfo, Never, QueryResponse, Reply, Response, StdError, StdResult, SubMsg,
7-
SubMsgResponse, SubMsgResult, WasmMsg,
2+
entry_point, from_json, to_json_binary, wasm_execute, Binary, CosmosMsg, Deps, DepsMut, Empty,
3+
Env, Event, Ibc3ChannelOpenResponse, IbcAcknowledgement, IbcBasicResponse, IbcChannelCloseMsg,
4+
IbcChannelConnectMsg, IbcChannelOpenMsg, IbcChannelOpenResponse, IbcMsg, IbcOrder,
5+
IbcPacketAckMsg, IbcPacketReceiveMsg, IbcPacketTimeoutMsg, IbcReceiveResponse, MessageInfo,
6+
Never, QueryResponse, Reply, Response, StdError, StdResult, SubMsg, SubMsgResponse,
7+
SubMsgResult, WasmMsg,
88
};
99

1010
use crate::msg::{
11-
AccountInfo, AccountResponse, AcknowledgementMsg, BalancesResponse, DispatchResponse,
11+
AccountInfo, AccountResponse, AcknowledgementMsg, BalanceResponse, DispatchResponse,
1212
ExecuteMsg, InstantiateMsg, ListAccountsResponse, PacketMsg, QueryMsg, ReflectExecuteMsg,
1313
ReturnMsgsResponse, WhoAmIResponse,
1414
};
@@ -206,42 +206,20 @@ pub fn ibc_channel_connect(
206206
}
207207

208208
#[entry_point]
209-
/// On closed channel, we take all tokens from reflect contract to this contract.
210-
/// We also delete the channel entry from accounts.
209+
/// On closed channel, we delete the channel entry from accounts.
211210
pub fn ibc_channel_close(
212211
deps: DepsMut,
213-
env: Env,
212+
_env: Env,
214213
msg: IbcChannelCloseMsg,
215214
) -> StdResult<IbcBasicResponse> {
216215
let channel = msg.channel();
217216
// get contract address and remove lookup
218217
let channel_id = channel.endpoint.channel_id.as_str();
219-
let reflect_addr = load_account(deps.storage, channel_id)?;
220218
remove_account(deps.storage, channel_id);
221219

222-
// transfer current balance if any (steal the money)
223-
#[allow(deprecated)]
224-
let amount = deps.querier.query_all_balances(&reflect_addr)?;
225-
let messages: Vec<SubMsg<Empty>> = if !amount.is_empty() {
226-
let bank_msg = BankMsg::Send {
227-
to_address: env.contract.address.into(),
228-
amount,
229-
};
230-
let reflect_msg = ReflectExecuteMsg::ReflectMsg {
231-
msgs: vec![bank_msg.into()],
232-
};
233-
let wasm_msg = wasm_execute(reflect_addr, &reflect_msg, vec![])?;
234-
vec![SubMsg::new(wasm_msg)]
235-
} else {
236-
vec![]
237-
};
238-
let steal_funds = !messages.is_empty();
239-
240220
Ok(IbcBasicResponse::new()
241-
.add_submessages(messages)
242221
.add_attribute("action", "ibc_close")
243-
.add_attribute("channel_id", channel_id)
244-
.add_attribute("steal_funds", steal_funds.to_string()))
222+
.add_attribute("channel_id", channel_id))
245223
}
246224

247225
/// this is a no-op just to test how this integrates with wasmd
@@ -274,7 +252,7 @@ pub fn ibc_packet_receive(
274252
match msg {
275253
PacketMsg::Dispatch { msgs } => receive_dispatch(deps, caller, msgs),
276254
PacketMsg::WhoAmI {} => receive_who_am_i(deps, caller),
277-
PacketMsg::Balances {} => receive_balances(deps, caller),
255+
PacketMsg::Balance { denom } => receive_balance(deps, caller, denom),
278256
PacketMsg::Panic {} => execute_panic(),
279257
PacketMsg::ReturnErr { text } => execute_error(text),
280258
PacketMsg::ReturnMsgs { msgs } => execute_return_msgs(msgs),
@@ -301,18 +279,17 @@ fn receive_who_am_i(deps: DepsMut, caller: String) -> StdResult<IbcReceiveRespon
301279
Ok(IbcReceiveResponse::new(acknowledgement).add_attribute("action", "receive_who_am_i"))
302280
}
303281

304-
// processes PacketMsg::Balances variant
305-
fn receive_balances(deps: DepsMut, caller: String) -> StdResult<IbcReceiveResponse> {
282+
// processes PacketMsg::Balance variant
283+
fn receive_balance(deps: DepsMut, caller: String, denom: String) -> StdResult<IbcReceiveResponse> {
306284
let account = load_account(deps.storage, &caller)?;
307-
#[allow(deprecated)]
308-
let balances = deps.querier.query_all_balances(&account)?;
309-
let response = BalancesResponse {
285+
let balance = deps.querier.query_balance(&account, denom)?;
286+
let response = BalanceResponse {
310287
account: account.into(),
311-
balances,
288+
balance,
312289
};
313290
let acknowledgement = to_json_binary(&AcknowledgementMsg::Ok(response))?;
314291
// and we are golden
315-
Ok(IbcReceiveResponse::new(acknowledgement).add_attribute("action", "receive_balances"))
292+
Ok(IbcReceiveResponse::new(acknowledgement).add_attribute("action", "receive_balance"))
316293
}
317294

318295
// processes PacketMsg::Dispatch variant
@@ -381,7 +358,6 @@ mod tests {
381358
message_info, mock_dependencies, mock_env, mock_ibc_channel_close_init,
382359
mock_ibc_channel_connect_ack, mock_ibc_channel_open_init, mock_ibc_channel_open_try,
383360
mock_ibc_packet_recv, mock_wasmd_attr, MockApi, MockQuerier, MockStorage,
384-
MOCK_CONTRACT_ADDR,
385361
};
386362
use cosmwasm_std::{attr, coin, coins, from_json, BankMsg, OwnedDeps, WasmMsg};
387363

@@ -638,7 +614,7 @@ mod tests {
638614
// acknowledgement is an error
639615
let ack: AcknowledgementMsg<DispatchResponse> =
640616
from_json(res.acknowledgement.unwrap()).unwrap();
641-
assert_eq!(ack.unwrap_err(), "invalid packet: Error parsing into type ibc_reflect::msg::PacketMsg: unknown variant `reflect_code_id`, expected one of `dispatch`, `who_am_i`, `balances`, `panic`, `return_err`, `return_msgs`, `no_ack` at line 1 column 18");
617+
assert_eq!(ack.unwrap_err(), "invalid packet: Error parsing into type ibc_reflect::msg::PacketMsg: unknown variant `reflect_code_id`, expected one of `dispatch`, `who_am_i`, `balance`, `panic`, `return_err`, `return_msgs`, `no_ack` at line 1 column 18");
642618
}
643619

644620
#[test]
@@ -658,38 +634,11 @@ mod tests {
658634
let raw = query(deps.as_ref(), mock_env(), QueryMsg::ListAccounts {}).unwrap();
659635
let res: ListAccountsResponse = from_json(raw).unwrap();
660636
assert_eq!(1, res.accounts.len());
661-
#[allow(deprecated)]
662-
let balance = deps.as_ref().querier.query_all_balances(&account).unwrap();
663-
assert_eq!(funds, balance);
664637

665638
// close the channel
666639
let channel = mock_ibc_channel_close_init(channel_id, IbcOrder::Ordered, IBC_APP_VERSION);
667640
let res = ibc_channel_close(deps.as_mut(), mock_env(), channel).unwrap();
668-
669-
// it pulls out all money from the reflect contract
670-
assert_eq!(1, res.messages.len());
671-
if let CosmosMsg::Wasm(WasmMsg::Execute {
672-
contract_addr, msg, ..
673-
}) = &res.messages[0].msg
674-
{
675-
assert_eq!(contract_addr, account.as_str());
676-
let reflect: ReflectExecuteMsg = from_json(msg).unwrap();
677-
match reflect {
678-
ReflectExecuteMsg::ReflectMsg { msgs } => {
679-
assert_eq!(1, msgs.len());
680-
assert_eq!(
681-
&msgs[0],
682-
&BankMsg::Send {
683-
to_address: MOCK_CONTRACT_ADDR.into(),
684-
amount: funds
685-
}
686-
.into()
687-
)
688-
}
689-
}
690-
} else {
691-
panic!("Unexpected message: {:?}", &res.messages[0]);
692-
}
641+
assert_eq!(res.messages.len(), 0);
693642

694643
// and removes the account lookup
695644
let raw = query(deps.as_ref(), mock_env(), QueryMsg::ListAccounts {}).unwrap();

contracts/ibc-reflect/src/msg.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub enum ReflectExecuteMsg {
5757
pub enum PacketMsg {
5858
Dispatch { msgs: Vec<CosmosMsg> },
5959
WhoAmI {},
60-
Balances {},
60+
Balance { denom: String },
6161
Panic {},
6262
ReturnErr { text: String },
6363
ReturnMsgs { msgs: Vec<CosmosMsg> },
@@ -108,9 +108,9 @@ pub struct WhoAmIResponse {
108108
/// This is the success response we send on ack for PacketMsg::Balance.
109109
/// Just acknowledge success or error
110110
#[cw_serde]
111-
pub struct BalancesResponse {
111+
pub struct BalanceResponse {
112112
pub account: String,
113-
pub balances: Vec<Coin>,
113+
pub balance: Coin,
114114
}
115115

116116
/// This is the success response we send on ack for PacketMsg::ReturnMsgs.

contracts/ibc-reflect/tests/integration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,5 +298,5 @@ fn handle_dispatch_packet() {
298298
// acknowledgement is an error
299299
let ack: AcknowledgementMsg<DispatchResponse> =
300300
from_slice(&res.acknowledgement.unwrap(), DESERIALIZATION_LIMIT).unwrap();
301-
assert_eq!(ack.unwrap_err(), "invalid packet: Error parsing into type ibc_reflect::msg::PacketMsg: unknown variant `reflect_code_id`, expected one of `dispatch`, `who_am_i`, `balances`, `panic`, `return_err`, `return_msgs`, `no_ack` at line 1 column 18");
301+
assert_eq!(ack.unwrap_err(), "invalid packet: Error parsing into type ibc_reflect::msg::PacketMsg: unknown variant `reflect_code_id`, expected one of `dispatch`, `who_am_i`, `balance`, `panic`, `return_err`, `return_msgs`, `no_ack` at line 1 column 18");
302302
}

0 commit comments

Comments
 (0)