Skip to content

Commit

Permalink
flexible hrp acceptance (#43)
Browse files Browse the repository at this point in the history
* refacotring deps

* sort deps

* remove unused imports

* resolve clippy issue

* wip

* test bech32 with various hrp

* bech32
  • Loading branch information
byeongsu-hong authored Oct 11, 2023
1 parent 3fe56f5 commit e5064f9
Show file tree
Hide file tree
Showing 24 changed files with 230 additions and 98 deletions.
1 change: 1 addition & 0 deletions contracts/default-hook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ hpl-ownable.workspace = true
hpl-interface.workspace = true

[dev-dependencies]
rstest.workspace = true
anyhow.workspace = true
37 changes: 22 additions & 15 deletions contracts/default-hook/src/execute/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ mod test {
testing::{mock_dependencies, mock_info},
Addr, Storage,
};
use rstest::rstest;

use super::*;
const ADDR1_VALUE: &str = "addr1";
Expand All @@ -105,13 +106,14 @@ mod test {
.unwrap();
}

#[test]
fn test_set_hook() {
#[rstest]
#[case(Addr::unchecked("osmo109ns4u04l44kqdkvp876hukd3hxz8zzm7809el"))]
#[case(Addr::unchecked("neutron1d6a3j0kkpc8eac0j8h6ypyevfz8hd3qnsyg35p"))]
fn test_set_hook(#[case] hook: Addr) {
let mut deps = mock_dependencies();
mock_owner(deps.as_mut().storage, ADDR1_VALUE);

let destination = 11155111;
let hook = Addr::unchecked("osmo109ns4u04l44kqdkvp876hukd3hxz8zzm7809el");

let unauthorized = set_hook(
deps.as_mut(),
Expand Down Expand Up @@ -159,21 +161,26 @@ mod test {
);
}

#[test]
fn test_set_hooks() {
#[rstest]
#[case(&[
(5, Addr::unchecked("osmo109ns4u04l44kqdkvp876hukd3hxz8zzm7809el")),
(11155111, Addr::unchecked("osmo1mhnkm6fwaq53yzu7c0r3khhy60v04vse4c6gk5"))
])]
#[case(&[
(5, Addr::unchecked("neutron1d6a3j0kkpc8eac0j8h6ypyevfz8hd3qnsyg35p")),
(11155111, Addr::unchecked("neutron1mhnkm6fwaq53yzu7c0r3khhy60v04vseeuq66p"))
])]
fn test_set_hooks(#[case] configs: &[(u32, Addr)]) {
let mut deps = mock_dependencies();
mock_owner(deps.as_mut().storage, ADDR1_VALUE);

let hooks = vec![
HookConfig {
destination: 5,
hook: Addr::unchecked("osmo109ns4u04l44kqdkvp876hukd3hxz8zzm7809el"),
},
HookConfig {
destination: 11155111,
hook: Addr::unchecked("osmo1mhnkm6fwaq53yzu7c0r3khhy60v04vse4c6gk5"),
},
];
let hooks: Vec<_> = configs
.iter()
.map(|v| HookConfig {
destination: v.0,
hook: v.1.clone(),
})
.collect();

let unauthorized =
set_hooks(deps.as_mut(), mock_info(ADDR2_VALUE, &[]), hooks.clone()).unwrap_err();
Expand Down
8 changes: 5 additions & 3 deletions contracts/default-hook/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ mod test {
Addr, HexBinary, Uint256,
};
use hpl_interface::hook::HookConfig;
use rstest::rstest;

use super::*;

Expand Down Expand Up @@ -100,8 +101,10 @@ mod test {
)
}

#[test]
fn test_quote_dispatch() {
#[rstest]
#[case(Addr::unchecked("osmo109ns4u04l44kqdkvp876hukd3hxz8zzm7809el"))]
#[case(Addr::unchecked("neutron1d6a3j0kkpc8eac0j8h6ypyevfz8hd3qnsyg35p"))]
fn test_quote_dispatch(#[case] hook: Addr) {
let mut deps = mock_dependencies();

deps.querier.update_wasm(
Expand All @@ -117,7 +120,6 @@ mod test {
},
);

let hook = Addr::unchecked("osmo109ns4u04l44kqdkvp876hukd3hxz8zzm7809el");
let hook_config = HookConfig {
hook,
destination: 11155111,
Expand Down
1 change: 1 addition & 0 deletions contracts/domain-routing-hook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ hpl-ownable.workspace = true
hpl-interface.workspace = true

[dev-dependencies]
rstest.workspace = true
anyhow.workspace = true
8 changes: 5 additions & 3 deletions contracts/domain-routing-hook/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ mod test {
Addr, HexBinary, Uint256,
};
use hpl_interface::hook::HookConfig;
use rstest::rstest;

use super::*;

Expand Down Expand Up @@ -100,8 +101,10 @@ mod test {
)
}

#[test]
fn test_quote_dispatch() {
#[rstest]
#[case(Addr::unchecked("osmo109ns4u04l44kqdkvp876hukd3hxz8zzm7809el"))]
#[case(Addr::unchecked("neutron1d6a3j0kkpc8eac0j8h6ypyevfz8hd3qnsyg35p"))]
fn test_quote_dispatch(#[case] hook: Addr) {
let mut deps = mock_dependencies();

deps.querier.update_wasm(
Expand All @@ -117,7 +120,6 @@ mod test {
},
);

let hook = Addr::unchecked("osmo109ns4u04l44kqdkvp876hukd3hxz8zzm7809el");
let hook_config = HookConfig {
hook,
destination: 11155111,
Expand Down
19 changes: 15 additions & 4 deletions contracts/mock-msg-receiver/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,36 @@ use cosmwasm_std::{
attr, to_binary, Deps, DepsMut, Env, Event, MessageInfo, QueryResponse, Response, StdResult,
};
use cw2::set_contract_version;
use cw_storage_plus::Item;
use hpl_interface::{ism, mailbox, types::bech32_encode};

use crate::{CONTRACT_NAME, CONTRACT_VERSION};

#[cw_serde]
pub struct InstantiateMsg {}
pub struct InstantiateMsg {
pub hrp: String,
}

#[cw_serde]
pub struct MigrateMsg {}

#[cw_serde]
pub struct ExecuteMsg {}

pub const HRP_KEY: &str = "hrp";
pub const HRP: Item<String> = Item::new(HRP_KEY);

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: InstantiateMsg,
msg: InstantiateMsg,
) -> StdResult<Response> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

HRP.save(deps.storage, &msg.hrp)?;

Ok(Response::new().add_attribute("method", "instantiate"))
}

Expand All @@ -38,15 +46,18 @@ pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Respons
/// Handling contract execution
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
_deps: DepsMut,
deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: mailbox::ExpectedHandlerMsg,
) -> StdResult<Response> {
match msg {
mailbox::ExpectedHandlerMsg::Handle(msg) => Ok(Response::default().add_event(
Event::new("mailbox_msg_received").add_attributes(vec![
attr("sender", bech32_encode("osmo", &msg.sender)?),
attr(
"sender",
bech32_encode(&HRP.load(deps.storage)?, &msg.sender)?,
),
attr("origin", msg.origin.to_string()),
attr("body", std::str::from_utf8(&msg.body)?),
]),
Expand Down
2 changes: 1 addition & 1 deletion contracts/token-cw20/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ hpl-router.workspace = true
hpl-interface.workspace = true

[dev-dependencies]

rstest.workspace = true
anyhow.workspace = true
k256.workspace = true
sha3.workspace = true
5 changes: 3 additions & 2 deletions contracts/token-cw20/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use hpl_interface::{
use crate::{
error::ContractError,
msg::{InstantiateMsg, MigrateMsg, TokenOption},
state::{MAILBOX, MODE, OWNER, TOKEN},
state::{HRP, MAILBOX, MODE, OWNER, TOKEN},
CONTRACT_NAME, CONTRACT_VERSION, REPLY_ID_CREATE_DENOM,
};

Expand All @@ -29,6 +29,7 @@ pub fn instantiate(
) -> Result<Response, ContractError> {
cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

HRP.save(deps.storage, &msg.hrp)?;
MODE.save(deps.storage, &msg.mode)?;
OWNER.save(deps.storage, &deps.api.addr_validate(&msg.owner)?)?;
MAILBOX.save(deps.storage, &deps.api.addr_validate(&msg.mailbox)?)?;
Expand Down Expand Up @@ -104,7 +105,7 @@ pub fn execute(
);

let token_msg: token::Message = msg.body.into();
let recipient = bech32_encode("osmo", &token_msg.recipient)?;
let recipient = bech32_encode(&HRP.load(deps.storage)?, &token_msg.recipient)?;

let token = TOKEN.load(deps.storage)?;
let mode = MODE.load(deps.storage)?;
Expand Down
1 change: 1 addition & 0 deletions contracts/token-cw20/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct InstantiateMsg {
pub token: Option<TokenOption>,
pub mode: TokenMode,

pub hrp: String,
pub owner: String,
pub mailbox: String,
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/token-cw20/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub const TOKEN: Item<Addr> = Item::new(TOKEN_KEY);
pub const MODE_KEY: &str = "mode";
pub const MODE: Item<TokenMode> = Item::new(MODE_KEY);

pub const HRP_KEY: &str = "hrp";
pub const HRP: Item<String> = Item::new(HRP_KEY);

pub const OWNER_KEY: &str = "owner";
pub const OWNER: Item<Addr> = Item::new(OWNER_KEY);

Expand Down
26 changes: 18 additions & 8 deletions contracts/token-cw20/src/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ use hpl_interface::{
token::{self, TokenMode},
types::bech32_encode,
};
use rstest::rstest;

use crate::{error::ContractError, msg::TokenOption, state::TOKEN, tests::TokenCW20};

#[test]
fn test_router_role() -> anyhow::Result<()> {
#[rstest]
#[case("osmo")]
#[case("neutron")]
fn test_router_role(#[case] hrp: &str) -> anyhow::Result<()> {
let deployer = Addr::unchecked("deployer");
let mailbox = Addr::unchecked("mailbox");
let owner = Addr::unchecked("owner");
Expand All @@ -29,6 +32,7 @@ fn test_router_role() -> anyhow::Result<()> {
contract: token.to_string(),
}),
TokenMode::Bridged,
hrp,
)?;

// err
Expand All @@ -43,8 +47,10 @@ fn test_router_role() -> anyhow::Result<()> {
Ok(())
}

#[test]
fn test_outbound_transfer() -> anyhow::Result<()> {
#[rstest]
#[case("osmo")]
#[case("neutron")]
fn test_outbound_transfer(#[case] hrp: &str) -> anyhow::Result<()> {
let deployer = Addr::unchecked("deployer");
let mailbox = Addr::unchecked("mailbox");
let router = Addr::unchecked("router");
Expand Down Expand Up @@ -123,6 +129,7 @@ fn test_outbound_transfer() -> anyhow::Result<()> {
contract: token.to_string(),
}),
mode.clone(),
hrp,
)?;
if mode == TokenMode::Collateral {
TOKEN.save(&mut warp.deps.storage, &token)?;
Expand All @@ -149,8 +156,10 @@ fn test_outbound_transfer() -> anyhow::Result<()> {
Ok(())
}

#[test]
fn test_inbound_transfer() -> anyhow::Result<()> {
#[rstest]
#[case("osmo")]
#[case("neutron")]
fn test_inbound_transfer(#[case] hrp: &str) -> anyhow::Result<()> {
let deployer = Addr::unchecked("deployer");
let mailbox = Addr::unchecked("mailbox");
let router = Addr::unchecked("router");
Expand All @@ -169,7 +178,7 @@ fn test_inbound_transfer() -> anyhow::Result<()> {
let mint_msg: CosmosMsg = WasmMsg::Execute {
contract_addr: token.to_string(),
msg: to_binary(&cw20::Cw20ExecuteMsg::Mint {
recipient: bech32_encode("osmo", user_remote.as_bytes())?.to_string(),
recipient: bech32_encode(hrp, user_remote.as_bytes())?.to_string(),
amount: amount.into(),
})?,
funds: vec![],
Expand All @@ -179,7 +188,7 @@ fn test_inbound_transfer() -> anyhow::Result<()> {
let send_msg: CosmosMsg = WasmMsg::Execute {
contract_addr: token.to_string(),
msg: to_binary(&cw20::Cw20ExecuteMsg::Transfer {
recipient: bech32_encode("osmo", user_remote.as_bytes())?.to_string(),
recipient: bech32_encode(hrp, user_remote.as_bytes())?.to_string(),
amount: amount.into(),
})?,
funds: vec![],
Expand Down Expand Up @@ -257,6 +266,7 @@ fn test_inbound_transfer() -> anyhow::Result<()> {
contract: token.to_string(),
}),
mode.clone(),
hrp,
)?;
if mode == TokenMode::Collateral {
TOKEN.save(&mut warp.deps.storage, &token)?;
Expand Down
2 changes: 2 additions & 0 deletions contracts/token-cw20/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl TokenCW20 {
mailbox: &Addr,
token: Option<TokenOption>,
mode: TokenMode,
hrp: &str,
) -> Result<Response, ContractError> {
instantiate(
self.deps.as_mut(),
Expand All @@ -48,6 +49,7 @@ impl TokenCW20 {
InstantiateMsg {
token,
mode,
hrp: hrp.to_string(),
owner: owner.to_string(),
mailbox: mailbox.to_string(),
},
Expand Down
5 changes: 3 additions & 2 deletions contracts/token-native/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
error::ContractError,
msg::{InstantiateMsg, MigrateMsg},
proto::{self, MsgBurn, MsgCreateDenom, MsgCreateDenomResponse, MsgMint, MsgSetDenomMetadata},
state::{MAILBOX, MODE, OWNER, TOKEN},
state::{HRP, MAILBOX, MODE, OWNER, TOKEN},
CONTRACT_NAME, CONTRACT_VERSION, REPLY_ID_CREATE_DENOM,
};

Expand All @@ -30,6 +30,7 @@ pub fn instantiate(
) -> Result<Response, ContractError> {
cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

HRP.save(deps.storage, &msg.hrp)?;
MODE.save(deps.storage, &msg.mode)?;
OWNER.save(deps.storage, &deps.api.addr_validate(&msg.owner)?)?;
MAILBOX.save(deps.storage, &deps.api.addr_validate(&msg.mailbox)?)?;
Expand Down Expand Up @@ -116,7 +117,7 @@ pub fn execute(
);

let token_msg: token::Message = msg.body.into();
let recipient = bech32_encode("osmo", &token_msg.recipient)?;
let recipient = bech32_encode(&HRP.load(deps.storage)?, &token_msg.recipient)?;

let token = TOKEN.load(deps.storage)?;
let mode = MODE.load(deps.storage)?;
Expand Down
1 change: 1 addition & 0 deletions contracts/token-native/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct InstantiateMsg {
pub metadata: Option<Metadata>,
pub mode: TokenMode,

pub hrp: String,
pub owner: String,
pub mailbox: String,
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/token-native/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub const TOKEN: Item<String> = Item::new(TOKEN_KEY);
pub const MODE_KEY: &str = "mode";
pub const MODE: Item<TokenMode> = Item::new(MODE_KEY);

pub const HRP_KEY: &str = "hrp";
pub const HRP: Item<String> = Item::new(HRP_KEY);

pub const OWNER_KEY: &str = "owner";
pub const OWNER: Item<Addr> = Item::new(OWNER_KEY);

Expand Down
Loading

0 comments on commit e5064f9

Please sign in to comment.