Skip to content

Commit

Permalink
chore: github actions tool fix.
Browse files Browse the repository at this point in the history
chore: lints
  • Loading branch information
jbernal87 committed Nov 8, 2024
1 parent 436d29d commit 851e199
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 747 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
toolchain: 1.74.0
toolchain: 1.78.0
args: --locked --tests
env:
LLVM_PROFILE_FILE: "swap-contract-%p-%m.profraw"
Expand Down
55 changes: 12 additions & 43 deletions contracts/swap/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,22 @@ use crate::state::{remove_swap_route, store_swap_route, CONFIG};
use crate::types::{Config, SwapRoute};
use crate::ContractError;
use crate::ContractError::CustomError;
use cosmwasm_std::{
ensure, ensure_eq, Addr, Attribute, BankMsg, Coin, Deps, DepsMut, Env, Event, Response,
StdResult,
};
use cosmwasm_std::{ensure, ensure_eq, Addr, Attribute, BankMsg, Coin, Deps, DepsMut, Env, Event, Response, StdResult};
use injective_cosmwasm::{InjectiveMsgWrapper, InjectiveQuerier, InjectiveQueryWrapper, MarketId};
use std::collections::HashSet;

pub fn save_config(
deps: DepsMut<InjectiveQueryWrapper>,
env: Env,
admin: Addr,
fee_recipient: FeeRecipient,
) -> StdResult<()> {
pub fn save_config(deps: DepsMut<InjectiveQueryWrapper>, env: Env, admin: Addr, fee_recipient: FeeRecipient) -> StdResult<()> {
let fee_recipient = match fee_recipient {
FeeRecipient::Address(addr) => addr,
FeeRecipient::SwapContract => env.contract.address,
};
let config = Config {
fee_recipient,
admin,
};
let config = Config { fee_recipient, admin };
config.to_owned().validate()?;

CONFIG.save(deps.storage, &config)
}

pub fn verify_sender_is_admin(
deps: Deps<InjectiveQueryWrapper>,
sender: &Addr,
) -> Result<(), ContractError> {
pub fn verify_sender_is_admin(deps: Deps<InjectiveQueryWrapper>, sender: &Addr) -> Result<(), ContractError> {
let config = CONFIG.load(deps.storage)?;
ensure_eq!(&config.admin, sender, ContractError::Unauthorized {});
Ok(())
Expand All @@ -57,10 +43,7 @@ pub fn update_config(
FeeRecipient::Address(addr) => addr,
FeeRecipient::SwapContract => env.contract.address,
};
updated_config_event_attrs.push(Attribute::new(
"fee_recipient",
config.fee_recipient.to_string(),
));
updated_config_event_attrs.push(Attribute::new("fee_recipient", config.fee_recipient.to_string()));
}
CONFIG.save(deps.storage, &config)?;

Expand Down Expand Up @@ -108,13 +91,7 @@ pub fn set_route(
});
}

if route
.clone()
.into_iter()
.collect::<HashSet<MarketId>>()
.len()
< route.len()
{
if route.clone().into_iter().collect::<HashSet<MarketId>>().len() < route.len() {
return Err(ContractError::CustomError {
val: "Route cannot have duplicate steps!".to_string(),
});
Expand All @@ -131,10 +108,7 @@ pub fn set_route(
Ok(Response::new().add_attribute("method", "set_route"))
}

fn verify_route_exists(
deps: Deps<InjectiveQueryWrapper>,
route: &SwapRoute,
) -> Result<(), ContractError> {
fn verify_route_exists(deps: Deps<InjectiveQueryWrapper>, route: &SwapRoute) -> Result<(), ContractError> {
struct MarketDenom {
quote_denom: String,
base_denom: String,
Expand All @@ -143,12 +117,9 @@ fn verify_route_exists(
let querier = InjectiveQuerier::new(&deps.querier);

for market_id in route.steps.iter() {
let market = querier
.query_spot_market(market_id)?
.market
.ok_or(CustomError {
val: format!("Market {} not found", market_id.as_str()).to_string(),
})?;
let market = querier.query_spot_market(market_id)?.market.ok_or(CustomError {
val: format!("Market {} not found", market_id.as_str()).to_string(),
})?;

denoms.push(MarketDenom {
quote_denom: market.quote_denom,
Expand All @@ -164,15 +135,13 @@ fn verify_route_exists(
}
);
ensure!(
denoms.first().unwrap().quote_denom == route.source_denom
|| denoms.first().unwrap().base_denom == route.source_denom,
denoms.first().unwrap().quote_denom == route.source_denom || denoms.first().unwrap().base_denom == route.source_denom,
CustomError {
val: "Source denom not found in first market".to_string()
}
);
ensure!(
denoms.last().unwrap().quote_denom == route.target_denom
|| denoms.last().unwrap().base_denom == route.target_denom,
denoms.last().unwrap().quote_denom == route.target_denom || denoms.last().unwrap().base_denom == route.target_denom,
CustomError {
val: "Target denom not found in last market".to_string()
}
Expand Down
14 changes: 3 additions & 11 deletions contracts/swap/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ pub fn i32_to_dec(source: i32) -> FPDecimal {
FPDecimal::from(i128::from(source))
}

pub fn get_message_data(
response: &[SubMsg<InjectiveMsgWrapper>],
position: usize,
) -> &InjectiveMsgWrapper {
pub fn get_message_data(response: &[SubMsg<InjectiveMsgWrapper>], position: usize) -> &InjectiveMsgWrapper {
let sth = match &response.get(position).unwrap().msg {
CosmosMsg::Custom(msg) => msg,
_ => panic!("No wrapped message found"),
Expand Down Expand Up @@ -41,10 +38,7 @@ pub trait Scaled {

impl Scaled for FPDecimal {
fn scaled(self, digits: i32) -> Self {
self.to_owned()
* FPDecimal::from(10i128)
.pow(FPDecimal::from(digits as i128))
.unwrap()
self.to_owned() * FPDecimal::from(10i128).pow(FPDecimal::from(digits as i128)).unwrap()
}
}

Expand All @@ -55,9 +49,7 @@ pub fn dec_scale_factor() -> FPDecimal {
type V100Config = Config;
const V100CONFIG: Item<V100Config> = Item::new("config");

pub fn handle_config_migration(
deps: DepsMut<InjectiveQueryWrapper>,
) -> Result<Response, ContractError> {
pub fn handle_config_migration(deps: DepsMut<InjectiveQueryWrapper>) -> Result<Response, ContractError> {
let v100_config = V100CONFIG.load(deps.storage)?;

let config = Config {
Expand Down
24 changes: 6 additions & 18 deletions contracts/swap/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,22 @@ pub fn store_swap_route(storage: &mut dyn Storage, route: &SwapRoute) -> StdResu
SWAP_ROUTES.save(storage, key, route)
}

pub fn read_swap_route(
storage: &dyn Storage,
source_denom: &str,
target_denom: &str,
) -> StdResult<SwapRoute> {
pub fn read_swap_route(storage: &dyn Storage, source_denom: &str, target_denom: &str) -> StdResult<SwapRoute> {
let key = route_key(source_denom, target_denom);
SWAP_ROUTES.load(storage, key).map_err(|_| {
StdError::generic_err(format!(
"No swap route not found from {source_denom} to {target_denom}",
))
})
SWAP_ROUTES
.load(storage, key)
.map_err(|_| StdError::generic_err(format!("No swap route not found from {source_denom} to {target_denom}",)))
}

pub fn get_config(storage: &dyn Storage) -> StdResult<Config> {
let config = CONFIG.load(storage)?;
Ok(config)
}

pub fn get_all_swap_routes(
storage: &dyn Storage,
start_after: Option<(String, String)>,
limit: Option<u32>,
) -> StdResult<Vec<SwapRoute>> {
pub fn get_all_swap_routes(storage: &dyn Storage, start_after: Option<(String, String)>, limit: Option<u32>) -> StdResult<Vec<SwapRoute>> {
let limit = limit.unwrap_or(DEFAULT_LIMIT) as usize;

let start_bound = start_after
.as_ref()
.map(|(s, t)| Bound::inclusive((s.clone(), t.clone())));
let start_bound = start_after.as_ref().map(|(s, t)| Bound::inclusive((s.clone(), t.clone())));

let routes = SWAP_ROUTES
.range(storage, start_bound, None, Order::Ascending)
Expand Down
Loading

0 comments on commit 851e199

Please sign in to comment.