Skip to content

Commit

Permalink
add custom hook addr validation; add unit tests for custom hook and c…
Browse files Browse the repository at this point in the history
…ustom metadata
  • Loading branch information
udit-gulati committed May 2, 2024
1 parent b86a6a8 commit b5ecd54
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
29 changes: 22 additions & 7 deletions contracts/warp/cw20/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ fn transfer_remote(
.route
.expect("route not found");

// validate hook if present
if let Some(ref custom_hook) = hook {
let _ = deps.api.addr_validate(&custom_hook)?;
}

let mut msgs: Vec<CosmosMsg> = vec![];

// push token transfer msg
Expand Down Expand Up @@ -298,6 +303,7 @@ mod test {
const OWNER: &str = "owner";
const MAILBOX: &str = "mailbox";
const TOKEN: &str = "token";
const CUSTOM_HOOK: &str = "custom_hook";

const CW20_BRIDGED_CODE_ID: u64 = 1;
const CW20_BRIDGED_NAME: &str = "cw20-created";
Expand Down Expand Up @@ -515,15 +521,17 @@ mod test {
}

#[rstest]
#[case(1, gen_bz(32), token_mode_bridged())]
#[case(1, gen_bz(32), token_mode_collateral())]
#[case(1, gen_bz(32), token_mode_bridged(), Some(CUSTOM_HOOK), None)]
#[case(1, gen_bz(32), token_mode_collateral(), None, Some(gen_bz(100)))]
#[should_panic(expected = "route not found")]
#[case(2, gen_bz(32), token_mode_collateral())]
#[case(2, gen_bz(32), token_mode_collateral(), None, None)]
fn test_transfer_remote(
#[values("osmo", "neutron")] hrp: &str,
#[case] domain: u32,
#[case] route: HexBinary,
#[case] token_mode: Cw20TokenMode,
#[case] custom_hook: Option<&str>,
#[case] custom_metadata: Option<HexBinary>,
) {
let (mut deps, _) = deps(
vec![(1, route.clone())],
Expand All @@ -542,8 +550,8 @@ mod test {
dest_domain: domain,
recipient: recipient.clone(),
amount: Uint128::new(100),
hook: None,
metadata: None,
hook: custom_hook.map(|h| h.to_string()),
metadata: custom_metadata.clone(),
},
vec![],
);
Expand All @@ -566,8 +574,15 @@ mod test {
metadata: HexBinary::default(),
};

let dispatch_msg =
mailbox::dispatch(MAILBOX, domain, route, warp_msg.into(), None, None, vec![]).unwrap();
let dispatch_msg = mailbox::dispatch(
MAILBOX,
domain,
route,
warp_msg.into(),
custom_hook.map(|h| h.to_string()),
custom_metadata,
vec![],
).unwrap();

match token_mode {
TokenModeMsg::Bridged(_) => {
Expand Down
26 changes: 17 additions & 9 deletions contracts/warp/native/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ fn transfer_remote(
.route
.expect("route not found");

// validate hook if present
if let Some(ref custom_hook) = hook {
let _ = deps.api.addr_validate(&custom_hook)?;
}

let mut msgs: Vec<CosmosMsg> = vec![];

if mode == TokenMode::Bridged {
Expand Down Expand Up @@ -320,6 +325,7 @@ mod test {
const OWNER: &str = "owner";
const MAILBOX: &str = "mailbox";
const DENOM: &str = "utest";
const CUSTOM_HOOK: &str = "custom_hook";

#[fixture]
fn metadata(#[default(true)] empty: bool) -> Option<Metadata> {
Expand Down Expand Up @@ -514,20 +520,22 @@ mod test {
}

#[rstest]
#[case(1, gen_bz(32), gen_bz(32), vec![coin(100, DENOM)])]
#[case(1, gen_bz(32), gen_bz(32), vec![coin(100, DENOM), coin(100, "uatom")])]
#[case(1, gen_bz(32), gen_bz(32), vec![coin(100, DENOM)], Some(CUSTOM_HOOK), None)]
#[case(1, gen_bz(32), gen_bz(32), vec![coin(100, DENOM), coin(100, "uatom")], None, Some(gen_bz(100)))]
#[should_panic(expected = "route not found")]
#[case(2, gen_bz(32), gen_bz(32), vec![coin(100, DENOM)])]
#[case(2, gen_bz(32), gen_bz(32), vec![coin(100, DENOM)], None, None)]
#[should_panic(expected = "no funds sent")]
#[case(1, gen_bz(32), gen_bz(32), vec![])]
#[case(1, gen_bz(32), gen_bz(32), vec![], None, None)]
#[should_panic(expected = "no funds sent")]
#[case(1, gen_bz(32), gen_bz(32), vec![coin(100, "uatom")])]
#[case(1, gen_bz(32), gen_bz(32), vec![coin(100, "uatom")], None, None)]
fn test_transfer_remote(
mut deps: TestDeps,
#[case] dest_domain: u32,
#[case] dest_router: HexBinary,
#[case] dest_recipient: HexBinary,
#[case] funds: Vec<Coin>,
#[case] custom_hook: Option<&str>,
#[case] custom_metadata: Option<HexBinary>,
) {
set_route(
deps.as_mut().storage,
Expand All @@ -546,8 +554,8 @@ mod test {
dest_domain,
recipient: dest_recipient.clone(),
amount: Uint128::new(50),
hook: None,
metadata: None,
hook: custom_hook.map(|h| h.to_string()),
metadata: custom_metadata.clone(),
},
funds.clone(),
);
Expand All @@ -567,8 +575,8 @@ mod test {
metadata: HexBinary::default(),
}
.into(),
None,
None,
custom_hook.map(|h| h.to_string()),
custom_metadata,
[
vec![coin(50, DENOM)],
funds.into_iter().filter(|v| v.denom != DENOM).collect()
Expand Down

0 comments on commit b5ecd54

Please sign in to comment.