Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow the contract UpdateMappingPair #33

Merged
merged 7 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion contracts/cw-ics20-latest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw-ics20-latest"
version = "1.0.8"
version = "1.0.9"
authors = ["Ethan Frey <[email protected]>, Oraichain Labs"]
edition = "2021"
description = "IBC Enabled contracts that receives CW20 tokens and sends them over ICS20 to a remote chain"
Expand Down
10 changes: 7 additions & 3 deletions contracts/cw-ics20-latest/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cw2::set_contract_version;
use cw20::{Cw20ExecuteMsg, Cw20ReceiveMsg};
use cw20_ics20_msg::converter::ConverterController;
use cw20_ics20_msg::helper::parse_ibc_wasm_port_id;
use cw_controllers::AdminError;
use cw_storage_plus::Bound;
use oraiswap::asset::AssetInfo;
use oraiswap::router::RouterController;
Expand Down Expand Up @@ -165,7 +166,7 @@ pub fn execute(
orai_receiver,
args,
} => ibc_hooks_receive(deps, env, info, func, orai_receiver, args),
ExecuteMsg::RegisterDenom(msg) => register_denom(deps, info, msg),
ExecuteMsg::RegisterDenom(msg) => register_denom(deps, env, info, msg),
ExecuteMsg::WithdrawAsset { coin, receiver } => {
execute_withdraw_asset(deps, info, coin, receiver)
}
Expand Down Expand Up @@ -201,10 +202,13 @@ fn execute_withdraw_asset(

pub fn register_denom(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: RegisterDenomMsg,
) -> Result<Response, ContractError> {
ADMIN.assert_admin(deps.as_ref(), &info.sender)?;
if !ADMIN.is_admin(deps.as_ref(), &info.sender)? && info.sender != env.contract.address {
return Err(ContractError::Admin(AdminError::NotAdmin {}));
};

let config = CONFIG.load(deps.storage)?;

Expand Down Expand Up @@ -840,7 +844,7 @@ pub fn execute_delete_mapping_pair(
}

#[entry_point]
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
// we don't need to save anything if migrating from the same version
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

Expand Down
37 changes: 31 additions & 6 deletions contracts/cw-ics20-latest/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cosmwasm_std::{
};

use cw20_ics20_msg::helper::{
denom_to_asset_info, get_prefix_decode_bech32, parse_asset_info_denom,
denom_to_asset_info, get_full_denom, get_prefix_decode_bech32, parse_asset_info_denom,
};
use cw_storage_plus::Map;
use oraiswap::asset::AssetInfo;
Expand All @@ -20,14 +20,14 @@ use skip::entry_point::ExecuteMsg as EntryPointExecuteMsg;

use crate::contract::build_mint_mapping_msg;
use crate::error::{ContractError, Never};
use crate::msg::ExecuteMsg;
use crate::msg::{ExecuteMsg, RegisterDenomMsg};
use crate::state::{
get_key_ics20_ibc_denom, ics20_denoms, undo_reduce_channel_balance, ALLOW_LIST, CHANNEL_INFO,
CONFIG, RELAYER_FEE, TOKEN_FEE,
};
use cw20_ics20_msg::amount::{convert_remote_to_local, Amount};
use cw20_ics20_msg::msg::FeeData;
use cw20_ics20_msg::state::{ChannelInfo, Ratio};
use cw20_ics20_msg::state::{ChannelInfo, MappingMetadata, Ratio};

pub const ICS20_VERSION: &str = "ics20-1";
pub const ICS20_ORDERING: IbcOrder = IbcOrder::Unordered;
Expand Down Expand Up @@ -341,9 +341,34 @@ fn handle_ibc_packet_receive_native_remote_chain(

// key in form transfer/channel-0/foo
let ibc_denom = get_key_ics20_ibc_denom(&packet.dest.port_id, &packet.dest.channel_id, denom);
let pair_mapping = ics20_denoms()
.load(storage, &ibc_denom)
.map_err(|_| ContractError::NotOnMappingList {})?;

let pair_mapping = match ics20_denoms().load(storage, &ibc_denom) {
Ok(pair_mapping) => pair_mapping,
Err(_) => {
// push a register denom msg to the contract
cosmos_msgs.push(
wasm_execute(
config.token_factory_addr.to_string(),
&ExecuteMsg::RegisterDenom(RegisterDenomMsg {
subdenom: denom.into(),
metadata: None,
}),
vec![],
)?
.into(),
);
let new_metadata = MappingMetadata {
asset_info: AssetInfo::NativeToken {
denom: get_full_denom(config.token_factory_addr.to_string(), denom.to_string()),
},
remote_decimals: 1, // Since we don't know metadata of remote_chain token, we set it to 1 in both decimals
asset_info_decimals: 1,
is_mint_burn: true, // Always mint burn if we don't know the metadata
};
ics20_denoms().save(storage, &ibc_denom, &new_metadata)?;
new_metadata
}
};
let initial_receive_asset_info = pair_mapping.asset_info;
let to_send = Amount::from_parts(
parse_asset_info_denom(&initial_receive_asset_info),
Expand Down
55 changes: 44 additions & 11 deletions contracts/cw-ics20-latest/src/testing/ibc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cosmwasm_std::{
use cosmwasm_testing_util::mock::MockContract;
use cosmwasm_vm::testing::MockInstanceOptions;
use cw20_ics20_msg::converter::ConverterController;
use cw20_ics20_msg::helper::get_full_denom;
use cw_controllers::AdminError;
use oraiswap::asset::AssetInfo;
use oraiswap::router::RouterController;
Expand All @@ -28,8 +29,8 @@ use cosmwasm_std::{

use crate::error::ContractError;
use crate::state::{
get_key_ics20_ibc_denom, increase_channel_balance, reduce_channel_balance, Config, ADMIN,
CHANNEL_REVERSE_STATE, CONFIG, RELAYER_FEE, REPLY_ARGS, TOKEN_FEE,
get_key_ics20_ibc_denom, ics20_denoms, increase_channel_balance, reduce_channel_balance,
Config, ADMIN, CHANNEL_REVERSE_STATE, CONFIG, RELAYER_FEE, REPLY_ARGS, TOKEN_FEE,
};
use cw20::{Cw20CoinVerified, Cw20ExecuteMsg, Cw20ReceiveMsg};
use cw20_ics20_msg::amount::{convert_remote_to_local, Amount};
Expand All @@ -41,7 +42,7 @@ use crate::contract::{
};
use crate::msg::{
AllowMsg, ChannelResponse, ConfigResponse, ExecuteMsg, InitMsg, ListChannelsResponse,
ListMappingResponse, PairQuery, QueryMsg,
ListMappingResponse, PairQuery, QueryMsg, RegisterDenomMsg,
};
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{coins, to_json_vec};
Expand Down Expand Up @@ -236,22 +237,54 @@ fn send_native_from_remote_mapping_not_found() {
&["channel-1", "channel-7", send_channel],
&[(cw20_addr, gas_limit)],
);

let config = CONFIG.load(deps.as_ref().storage).unwrap();
// prepare some mock packets
let recv_packet =
mock_receive_packet_remote_to_local(send_channel, 876543210, cw20_denom, custom_addr, None);

// we can receive this denom, channel balance should increase
let msg = IbcPacketReceiveMsg::new(recv_packet.clone(), relayer);
let res = ibc_packet_receive(deps.as_mut(), mock_env(), msg).unwrap();
// assert_eq!(res, StdError)
println!("res.messages: {:?}", res.messages[0].msg);
assert_eq!(
res.attributes
.into_iter()
.find(|attr| attr.key.eq("error"))
.unwrap()
.value,
"You can only send native tokens that has a map to the corresponding asset info"
res.messages[0].msg,
wasm_execute(
config.token_factory_addr.clone(),
&ExecuteMsg::RegisterDenom(RegisterDenomMsg {
subdenom: String::from("cw20:token-addr"),
metadata: None
}),
vec![]
)
.unwrap()
.into()
);
execute(
deps.as_mut(),
mock_env(),
mock_info("cosmos2contract", &[]),
ExecuteMsg::RegisterDenom(RegisterDenomMsg {
subdenom: String::from("cw20:token-addr"),
metadata: None,
}),
)
.unwrap();
let pair_mapping = ics20_denoms()
.load(
deps.as_ref().storage,
"wasm.cosmos2contract/channel-9/cw20:token-addr",
)
.unwrap();
assert_eq!(
pair_mapping,
MappingMetadata {
asset_info: AssetInfo::NativeToken {
denom: get_full_denom(config.token_factory_addr.to_string(), cw20_denom.into()),
},
remote_decimals: 1,
asset_info_decimals: 1,
is_mint_burn: true
}
);
}

Expand Down
4 changes: 4 additions & 0 deletions packages/cw20-ics20-msg/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ pub fn to_orai_bridge_address(address: &str) -> StdResult<String> {
})
}

pub fn get_full_denom(factory_contract: String, subdenom: String) -> String {
format!("factory/{}/{}", factory_contract, subdenom)
}

#[cfg(test)]
mod tests {
use crate::helper::{get_prefix_decode_bech32, to_orai_bridge_address};
Expand Down
Loading