Skip to content

Commit

Permalink
feat: add min amount check of redeem
Browse files Browse the repository at this point in the history
  • Loading branch information
hsxyl committed Sep 9, 2024
1 parent c06e805 commit 34baaf1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 57 deletions.
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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "omnity-port-cosmos"
version = "0.1.1"
version = "0.1.2"
authors = ["shenao78 <[email protected]>"]
edition = "2021"

Expand Down
97 changes: 46 additions & 51 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub fn instantiate(
counterparties: BTreeMap::default(),
chain_id: msg.chain_id,
chain_state: ChainState::Active,
target_chain_redeem_min_amount: BTreeMap::default(),
};
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
STATE.save(deps.storage, &state)?;
Expand Down Expand Up @@ -91,6 +92,8 @@ pub fn execute(
target_chain,
} => execute::redeem_token(deps, env, info, token_id, receiver, amount, target_chain),
ExecuteMsg::UpdateRoute { route } => execute::update_route(deps, info, route),
ExecuteMsg::RedeemSetting { token_id, target_chain, min_amount } =>
execute::redeem_setting(deps, info, token_id, target_chain, min_amount),
}?;
Ok(response.add_event(Event::new("execute_msg").add_attribute("contract", contract)))
}
Expand Down Expand Up @@ -353,6 +356,7 @@ pub mod execute {
})?;

check_fee(&deps, &info, target_chain.clone())?;
check_min_amount(&deps, &token_id, &target_chain, &amount)?;

let denom = token_denom(env.contract.address.to_string(), token.token_id);

Expand All @@ -373,78 +377,48 @@ pub mod execute {
))
}

pub fn mint_runes(
pub fn update_route(
deps: DepsMut,
info: MessageInfo,
token_id: String,
receiver: Addr,
target_chain: String,
route: Addr,
) -> Result<Response, ContractError> {
// let token = read_state(deps.storage, |s| match s.tokens.get(&token_id) {
// Some(token) => Ok(token.clone()),
// None => Err(ContractError::TokenNotFound),
// })?;
if !token_id.starts_with("Bitcoin-runes-") {
return Err(ContractError::TokenUnsupportMint);
if read_state(deps.storage, |s| info.sender != s.admin) {
return Err(ContractError::Unauthorized);
}

check_fee(&deps, &info, target_chain)?;

Ok(
Response::new().add_event(Event::new("RunesMintRequested").add_attributes(vec![
Attribute::new("token_id", token_id),
Attribute::new("sender", info.sender),
Attribute::new("receiver", receiver),
])),
)
}

pub fn burn_token(
deps: DepsMut,
env: Env,
info: MessageInfo,
token_id: String,
amount: String,
target_chain: String,
) -> Result<Response, ContractError> {
let token = read_state(deps.storage, |s| match s.tokens.get(&token_id) {
Some(token) => Ok(token.clone()),
None => Err(ContractError::TokenNotFound),
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
state.route = route.clone();
Ok(state)
})?;

check_fee(&deps, &info, target_chain)?;

let burn_msg = build_burn_msg(
env.contract.address,
info.sender.clone(),
token.name,
amount.clone(),
);
Ok(Response::new().add_message(burn_msg).add_event(
Event::new("TokenBurned").add_attributes(vec![
Attribute::new("token_id", token_id),
Attribute::new("sender", info.sender),
Attribute::new("amount", amount),
]),
Ok(Response::new().add_event(
Event::new("RouteUpdated").add_attributes(vec![Attribute::new("new_route", route)]),
))
}

pub fn update_route(
pub fn redeem_setting(
deps: DepsMut,
info: MessageInfo,
route: Addr,
) -> Result<Response, ContractError> {
token_id: String,
target_chain: String,
min_amount: String
) -> Result<Response, ContractError>{
if read_state(deps.storage, |s| info.sender != s.admin) {
return Err(ContractError::Unauthorized);
}

STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
state.route = route.clone();
state.target_chain_redeem_min_amount
.insert((token_id.clone(), target_chain.clone()), min_amount.clone());
Ok(state)
})?;

Ok(Response::new().add_event(
Event::new("RouteUpdated").add_attributes(vec![Attribute::new("new_route", route)]),
Event::new("RedeemSettingUpdated").add_attributes(vec![
Attribute::new("token_id", token_id),
Attribute::new("target_chain", target_chain),
Attribute::new("min_amount", min_amount),
]),
))
}

Expand All @@ -465,6 +439,27 @@ pub mod execute {
}
}

fn check_min_amount(
deps: &DepsMut,
token_id: &String,
target_chain: &String,
amount: &String,
) -> Result<(), ContractError> {
let min_amount = read_state(deps.storage, |state| {
state
.target_chain_redeem_min_amount
.get(&(token_id.clone(), target_chain.clone()))
.cloned()
.unwrap_or("0".to_string())
});

if amount.parse::<u128>().unwrap() < min_amount.parse::<u128>().unwrap() {
return Err(ContractError::RedeemAmountLessThanMinAmount(min_amount, amount.clone()));
}

Ok(())
}

fn check_fee(
deps: &DepsMut,
info: &MessageInfo,
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub enum ContractError {
#[error("Semver parsing error: {0}")]
SemVer(String),

#[error("RedeemAmountLessThanMinAmount, min: {0}, redeem: {1}")]
RedeemAmountLessThanMinAmount(String, String)

}

impl From<semver::Error> for ContractError {
Expand Down
5 changes: 5 additions & 0 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ pub enum ExecuteMsg {
},
UpdateRoute {
route: Addr,
},
RedeemSetting {
token_id: String,
target_chain: String,
min_amount: String,
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ use serde::{Deserialize, Serialize};
use cosmwasm_std::{Addr, Storage};
use cw_storage_plus::Item;

use crate::route::{Chain, ChainId, ChainState, Token};
use crate::route::{Chain, ChainId, ChainState, Token, TokenId};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct State {
pub route: Addr,
pub admin: Addr,
pub tokens: BTreeMap<String, Token>,
pub tokens: BTreeMap<TokenId, Token>,
pub handled_tickets: BTreeSet<String>,
pub handled_directives: BTreeSet<u64>,
pub target_chain_factor: BTreeMap<String, u128>,
pub target_chain_factor: BTreeMap<ChainId, u128>,
pub fee_token: Option<String>,
pub fee_token_factor: Option<u128>,
pub counterparties: BTreeMap<ChainId, Chain>,
pub chain_id: ChainId,
pub chain_state: ChainState
pub chain_state: ChainState,
#[serde(default)]
pub target_chain_redeem_min_amount: BTreeMap<(TokenId, ChainId), String>,
}

pub const STATE: Item<State> = Item::new("state");
Expand Down

0 comments on commit 34baaf1

Please sign in to comment.