Skip to content

Commit

Permalink
align cw20-ics20
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Mar 11, 2024
1 parent 01c915d commit 4935744
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions contracts/cw20-ics20/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ schemars = { workspace = true }
semver = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
easy-addr = { workspace = true }
24 changes: 15 additions & 9 deletions contracts/cw20-ics20/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ mod test {
use cosmwasm_std::testing::{mock_env, mock_info, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{coin, coins, CosmosMsg, IbcMsg, StdError, Uint128};

use easy_addr::addr;

use crate::state::ChannelState;
use cw_utils::PaymentError;

Expand Down Expand Up @@ -426,8 +428,8 @@ mod test {

#[test]
fn proper_checks_on_execute_native() {
let foobar = "foobar";
let foreign = "foreign-address";
let foobar = addr!("foobar");
let foreign = addr!("foreign-address");

let send_channel = "channel-5";
let mut deps = setup(&[send_channel, "channel-10"], &[]);
Expand Down Expand Up @@ -491,9 +493,9 @@ mod test {
#[test]
fn proper_checks_on_execute_cw20() {
let send_channel = "channel-15";
let cw20_addr = "my-token";
let foreign = "foreign-address";
let sender = "my-account";
let cw20_addr = addr!("my-token");
let foreign = addr!("foreign-address");
let sender = addr!("my-account");
let mut deps = setup(&["channel-3", send_channel], &[(cw20_addr, 123456)]);

let transfer = TransferMsg {
Expand Down Expand Up @@ -542,15 +544,18 @@ mod test {
let send_channel = "channel-15";
let mut deps = setup(&[send_channel], &[]);

let cw20_addr = "my-token";
let my_account = addr!("my-account");
let cw20_addr = addr!("my-token");
let foreign = addr!("foreign-address");

let transfer = TransferMsg {
channel: send_channel.to_string(),
remote_address: "foreign-address".to_string(),
remote_address: foreign.to_string(),
timeout: Some(7777),
memo: None,
};
let msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
sender: "my-account".into(),
sender: my_account.into(),
amount: Uint128::new(888777666),
msg: to_json_binary(&transfer).unwrap(),
});
Expand Down Expand Up @@ -578,13 +583,14 @@ mod test {
fn v3_migration_works() {
// basic state with one channel
let send_channel = "channel-15";
let cw20_addr = "my-token";
let cw20_addr = addr!("my-token");
let native = "ucosm";
let mut deps = setup(&[send_channel], &[(cw20_addr, 123456)]);

// mock that we sent some tokens in both native and cw20 (TODO: cw20)
// balances set high
deps.querier
.bank
.update_balance(MOCK_CONTRACT_ADDR, coins(50000, native));
// pretend this is an old contract - set version explicitly
set_contract_version(deps.as_mut().storage, CONTRACT_NAME, MIGRATE_VERSION_3).unwrap();
Expand Down
53 changes: 27 additions & 26 deletions contracts/cw20-ics20/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ pub fn ibc_packet_receive(
let packet = msg.packet;

do_ibc_packet_receive(deps, &packet).or_else(|err| {
Ok(IbcReceiveResponse::new()
.set_ack(ack_fail(err.to_string()))
.add_attributes(vec![
Ok(
IbcReceiveResponse::new(ack_fail(err.to_string())).add_attributes(vec![
attr("action", "receive"),
attr("success", "false"),
attr("error", err.to_string()),
]))
]),
)
})
}

Expand Down Expand Up @@ -265,8 +265,7 @@ fn do_ibc_packet_receive(
let mut submsg = SubMsg::reply_on_error(send, RECEIVE_ID);
submsg.gas_limit = gas_limit;

let res = IbcReceiveResponse::new()
.set_ack(ack_success())
let res = IbcReceiveResponse::new(ack_success())
.add_submessage(submsg)
.add_attribute("action", "receive")
.add_attribute("sender", msg.sender)
Expand Down Expand Up @@ -403,9 +402,11 @@ mod test {
use crate::contract::{execute, migrate, query_channel};
use crate::msg::{ExecuteMsg, MigrateMsg, TransferMsg};
use cosmwasm_std::testing::{mock_env, mock_info};
use cosmwasm_std::{coins, to_json_vec, IbcEndpoint, IbcMsg, IbcTimeout, Timestamp};
use cosmwasm_std::{coins, to_json_vec, Addr, IbcEndpoint, IbcMsg, IbcTimeout, Timestamp};
use cw20::Cw20ReceiveMsg;

use easy_addr::addr;

#[test]
fn check_ack_json() {
let success = Ics20Ack::Result(b"1".into());
Expand Down Expand Up @@ -496,11 +497,11 @@ mod test {
#[test]
fn send_receive_cw20() {
let send_channel = "channel-9";
let cw20_addr = "token-addr";
let cw20_denom = "cw20:token-addr";
let local_rcpt = "local-rcpt";
let local_sender = "local-sender";
let remote_rcpt = "remote-rcpt";
let cw20_addr = addr!("token-addr");
let cw20_denom = concat!("cw20:", addr!("token-addr"));
let local_rcpt = addr!("local-rcpt");
let local_sender = addr!("local-sender");
let remote_rcpt = addr!("remote-rcpt");
let gas_limit = 1234567;
let mut deps = setup(
&["channel-1", "channel-7", send_channel],
Expand All @@ -513,10 +514,10 @@ mod test {
mock_receive_packet(send_channel, 1876543210, cw20_denom, local_rcpt);

// cannot receive this denom yet
let msg = IbcPacketReceiveMsg::new(recv_packet.clone());
let msg = IbcPacketReceiveMsg::new(recv_packet.clone(), Addr::unchecked(""));
let res = ibc_packet_receive(deps.as_mut(), mock_env(), msg).unwrap();
assert!(res.messages.is_empty());
let ack: Ics20Ack = from_json(res.acknowledgement).unwrap();
let ack: Ics20Ack = from_json(res.acknowledgement.unwrap()).unwrap();
let no_funds = Ics20Ack::Error(ContractError::InsufficientFunds {}.to_string());
assert_eq!(ack, no_funds);

Expand Down Expand Up @@ -559,21 +560,21 @@ mod test {
assert_eq!(state.total_sent, vec![Amount::cw20(987654321, cw20_addr)]);

// cannot receive more than we sent
let msg = IbcPacketReceiveMsg::new(recv_high_packet);
let msg = IbcPacketReceiveMsg::new(recv_high_packet, Addr::unchecked(""));
let res = ibc_packet_receive(deps.as_mut(), mock_env(), msg).unwrap();
assert!(res.messages.is_empty());
let ack: Ics20Ack = from_json(res.acknowledgement).unwrap();
let ack: Ics20Ack = from_json(res.acknowledgement.unwrap()).unwrap();
assert_eq!(ack, no_funds);

// we can receive less than we sent
let msg = IbcPacketReceiveMsg::new(recv_packet);
let msg = IbcPacketReceiveMsg::new(recv_packet, Addr::unchecked(""));
let res = ibc_packet_receive(deps.as_mut(), mock_env(), msg).unwrap();
assert_eq!(1, res.messages.len());
assert_eq!(
cw20_payment(876543210, cw20_addr, local_rcpt, Some(gas_limit)),
res.messages[0]
);
let ack: Ics20Ack = from_json(res.acknowledgement).unwrap();
let ack: Ics20Ack = from_json(res.acknowledgement.unwrap()).unwrap();
assert!(matches!(ack, Ics20Ack::Result(_)));

// TODO: we need to call the reply block
Expand All @@ -596,10 +597,10 @@ mod test {
let recv_high_packet = mock_receive_packet(send_channel, 1876543210, denom, "local-rcpt");

// cannot receive this denom yet
let msg = IbcPacketReceiveMsg::new(recv_packet.clone());
let msg = IbcPacketReceiveMsg::new(recv_packet.clone(), Addr::unchecked(""));
let res = ibc_packet_receive(deps.as_mut(), mock_env(), msg).unwrap();
assert!(res.messages.is_empty());
let ack: Ics20Ack = from_json(res.acknowledgement).unwrap();
let ack: Ics20Ack = from_json(res.acknowledgement.unwrap()).unwrap();
let no_funds = Ics20Ack::Error(ContractError::InsufficientFunds {}.to_string());
assert_eq!(ack, no_funds);

Expand All @@ -619,21 +620,21 @@ mod test {
assert_eq!(state.total_sent, vec![Amount::native(987654321, denom)]);

// cannot receive more than we sent
let msg = IbcPacketReceiveMsg::new(recv_high_packet);
let msg = IbcPacketReceiveMsg::new(recv_high_packet, Addr::unchecked(""));
let res = ibc_packet_receive(deps.as_mut(), mock_env(), msg).unwrap();
assert!(res.messages.is_empty());
let ack: Ics20Ack = from_json(res.acknowledgement).unwrap();
let ack: Ics20Ack = from_json(res.acknowledgement.unwrap()).unwrap();
assert_eq!(ack, no_funds);

// we can receive less than we sent
let msg = IbcPacketReceiveMsg::new(recv_packet);
let msg = IbcPacketReceiveMsg::new(recv_packet, Addr::unchecked(""));
let res = ibc_packet_receive(deps.as_mut(), mock_env(), msg).unwrap();
assert_eq!(1, res.messages.len());
assert_eq!(
native_payment(876543210, denom, "local-rcpt"),
res.messages[0]
);
let ack: Ics20Ack = from_json(res.acknowledgement).unwrap();
let ack: Ics20Ack = from_json(res.acknowledgement.unwrap()).unwrap();
assert!(matches!(ack, Ics20Ack::Result(_)));

// only need to call reply block on error case
Expand All @@ -647,7 +648,7 @@ mod test {
#[test]
fn check_gas_limit_handles_all_cases() {
let send_channel = "channel-9";
let allowed = "foobar";
let allowed = addr!("foobar");
let allowed_gas = 777666;
let mut deps = setup(&[send_channel], &[(allowed, allowed_gas)]);

Expand All @@ -656,7 +657,7 @@ mod test {
assert_eq!(limit, Some(allowed_gas));

// non-allow list will error
let random = "tokenz";
let random = addr!("tokenz");
check_gas_limit(deps.as_ref(), &Amount::cw20(500, random)).unwrap_err();

// add default_gas_limit
Expand Down
2 changes: 1 addition & 1 deletion contracts/cw20-ics20/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn setup(
let instantiate_msg = InitMsg {
default_gas_limit: None,
default_timeout: DEFAULT_TIMEOUT,
gov_contract: "gov".to_string(),
gov_contract: deps.api.addr_make("gov").to_string(),
allowlist,
};
let info = mock_info("anyone", &[]);
Expand Down

0 comments on commit 4935744

Please sign in to comment.