Skip to content

Commit

Permalink
chore: update to latest CosmWasm
Browse files Browse the repository at this point in the history
  • Loading branch information
gorgos committed Jan 13, 2024
1 parent d56991c commit 397d2b6
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 188 deletions.
247 changes: 124 additions & 123 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions build_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ if [[ $(arch) = "arm64" ]]; then
ARCH=-arm64
fi

docker run --rm -v "$(pwd)":/code -v "$HOME/.cargo/git":/usr/local/cargo/git \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/workspace-optimizer${ARCH}:0.12.13
for contract_dir in contracts/*; do
docker run --rm -v "$(pwd)":/code -v "$HOME/.cargo/git":/usr/local/cargo/git \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/workspace-optimizer${ARCH}:0.15.0 "$contract_dir"
done
16 changes: 8 additions & 8 deletions contracts/swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ optimize = """docker run --rm -v "$(pwd)":/code \
"""

[dependencies]
cosmwasm-std = "1.0.0"
cosmwasm-storage = "1.0.0"
cosmwasm-std = { version = "1.5.0", features = [ "abort", "cosmwasm_1_2", "cosmwasm_1_3", "cosmwasm_1_4", "iterator", "stargate" ] }
cosmwasm-storage = "1.5.0"
cw-storage-plus = "0.14.0"
cw-utils = "0.14.0"
cw2 = "0.14.0"
injective-cosmwasm = { version = "0.2.2" }
injective-math = { version = "0.1.17" }
injective-protobuf = { version = "0.1", git = "https://github.com/InjectiveLabs/cw-injective", branch = "dev" }
injective-cosmwasm = { version = "0.2.18" }
injective-math = { version = "0.2.4" }
injective-protobuf = { version = "0.2.2" }
num-traits = "0.2.15"
protobuf = { version = "2", features = [ "with-bytes" ] }
schemars = "0.8.8"
Expand All @@ -45,8 +45,8 @@ serde-json-wasm = "0.5.1"
thiserror = { version = "1.0.31" }

[dev-dependencies]
cosmwasm-schema = "1.0.0"
cosmwasm-schema = "1.5.0"
cw-multi-test = "0.16.2"
injective-std = { version = "0.1.3" }
injective-test-tube = { version = "1.1.3", git = "https://github.com/InjectiveLabs/test-tube.git", tag = "v1.1.3" }
injective-std = { version = "0.1.5" }
injective-test-tube = { git = "https://github.com/InjectiveLabs/test-tube", branch = "dev" }
prost = "0.11.9"
10 changes: 5 additions & 5 deletions contracts/swap/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdResult,
to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdResult,
};
use cw2::set_contract_version;

Expand Down Expand Up @@ -104,7 +104,7 @@ pub fn query(deps: Deps<InjectiveQueryWrapper>, env: Env, msg: QueryMsg) -> StdR
QueryMsg::GetRoute {
source_denom,
target_denom,
} => Ok(to_binary(&read_swap_route(
} => Ok(to_json_binary(&read_swap_route(
deps.storage,
&source_denom,
&target_denom,
Expand All @@ -121,7 +121,7 @@ pub fn query(deps: Deps<InjectiveQueryWrapper>, env: Env, msg: QueryMsg) -> StdR
target_denom,
SwapQuantity::InputQuantity(from_quantity),
)?;
Ok(to_binary(&target_quantity)?)
Ok(to_json_binary(&target_quantity)?)
}
QueryMsg::GetInputQuantity {
to_quantity,
Expand All @@ -135,11 +135,11 @@ pub fn query(deps: Deps<InjectiveQueryWrapper>, env: Env, msg: QueryMsg) -> StdR
target_denom,
SwapQuantity::OutputQuantity(to_quantity),
)?;
Ok(to_binary(&target_quantity)?)
Ok(to_json_binary(&target_quantity)?)
}
QueryMsg::GetAllRoutes {} => {
let routes = get_all_swap_routes(deps.storage)?;
Ok(to_binary(&routes)?)
Ok(to_json_binary(&routes)?)
}
}
}
8 changes: 5 additions & 3 deletions contracts/swap/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use cosmwasm_std::{CosmosMsg, SubMsg};

use injective_cosmwasm::InjectiveMsgWrapper;
use injective_math::FPDecimal;
use num_traits::pow::Pow;

pub fn i32_to_dec(source: i32) -> FPDecimal {
FPDecimal::from(i128::from(source))
Expand Down Expand Up @@ -39,12 +38,15 @@ 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))
self.to_owned()
* FPDecimal::from(10i128)
.pow(FPDecimal::from(digits as i128))
.unwrap()
}
}

pub fn dec_scale_factor() -> FPDecimal {
FPDecimal::one().scaled(18)
FPDecimal::ONE.scaled(18)
// FPDecimal::from(1000000000000000000_i128)
}

Expand Down
21 changes: 10 additions & 11 deletions contracts/swap/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub fn estimate_single_swap_execution(

let fee_percent = market.taker_fee_rate
* fee_multiplier
* (FPDecimal::one() - get_effective_fee_discount_rate(&market, is_self_relayer));
* (FPDecimal::ONE - get_effective_fee_discount_rate(&market, is_self_relayer));

let is_estimating_from_target = matches!(
swap_estimation_amount,
Expand Down Expand Up @@ -164,7 +164,7 @@ fn estimate_execution_buy_from_source(
fee_percent: FPDecimal,
is_simulation: bool,
) -> StdResult<StepExecutionEstimate> {
let available_swap_quote_funds = input_quote_quantity / (FPDecimal::one() + fee_percent);
let available_swap_quote_funds = input_quote_quantity / (FPDecimal::ONE + fee_percent);

let orders = querier.query_spot_market_orderbook(
&market.market_id,
Expand All @@ -190,7 +190,7 @@ fn estimate_execution_buy_from_source(
let fee_estimate = input_quote_quantity - available_swap_quote_funds;

// check if user funds + contract funds are enough to create order
let required_funds = worst_price * expected_base_quantity * (FPDecimal::one() + fee_percent);
let required_funds = worst_price * expected_base_quantity * (FPDecimal::ONE + fee_percent);
let funds_in_contract = deps
.querier
.query_balance(contract_address, &market.quote_denom)
Expand Down Expand Up @@ -260,8 +260,7 @@ fn estimate_execution_buy_from_target(
let required_input_quote_quantity = expected_exchange_quote_quantity + fee_estimate;

// check if user funds + contract funds are enough to create order
let required_funds =
worst_price * target_base_output_quantity * (FPDecimal::one() + fee_percent);
let required_funds = worst_price * target_base_output_quantity * (FPDecimal::ONE + fee_percent);

let funds_in_contract = deps
.querier
Expand Down Expand Up @@ -386,7 +385,7 @@ fn estimate_execution_sell_from_target(
fee_percent: FPDecimal,
) -> StdResult<StepExecutionEstimate> {
let required_swap_quantity_in_quote =
target_quote_output_quantity / (FPDecimal::one() - fee_percent);
target_quote_output_quantity / (FPDecimal::ONE - fee_percent);
let required_fee = required_swap_quantity_in_quote - target_quote_output_quantity;

let orders = querier.query_spot_market_orderbook(
Expand Down Expand Up @@ -456,14 +455,14 @@ pub fn get_minimum_liquidity_levels(
calc: fn(&PriceLevel) -> FPDecimal,
min_quantity_tick_size: FPDecimal,
) -> StdResult<Vec<PriceLevel>> {
let mut sum = FPDecimal::zero();
let mut sum = FPDecimal::ZERO;
let mut orders: Vec<PriceLevel> = Vec::new();

for level in levels {
let value = calc(level);
assert_ne!(
value,
FPDecimal::zero(),
FPDecimal::ZERO,
"Price level with zero value, this should not happen"
);

Expand Down Expand Up @@ -506,13 +505,13 @@ fn get_average_price_from_orders(
) -> FPDecimal {
let (total_quantity, total_notional) = levels
.iter()
.fold((FPDecimal::zero(), FPDecimal::zero()), |acc, pl| {
.fold((FPDecimal::ZERO, FPDecimal::ZERO), |acc, pl| {
(acc.0 + pl.q, acc.1 + pl.p * pl.q)
});

assert_ne!(
total_quantity,
FPDecimal::zero(),
FPDecimal::ZERO,
"total_quantity was zero and would result in division by zero"
);
let average_price = total_notional / total_quantity;
Expand All @@ -530,7 +529,7 @@ fn get_worst_price_from_orders(levels: &[PriceLevel]) -> FPDecimal {

fn get_effective_fee_discount_rate(market: &SpotMarket, is_self_relayer: bool) -> FPDecimal {
if !is_self_relayer {
FPDecimal::zero()
FPDecimal::ZERO
} else {
market.relayer_fee_share_rate
}
Expand Down
5 changes: 3 additions & 2 deletions contracts/swap/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn start_swap_flow(
let is_input_quote = first_market.quote_denom == *source_denom;

let required_input = if is_input_quote {
estimation.result_quantity.int() + FPDecimal::one()
estimation.result_quantity.int() + FPDecimal::ONE
} else {
round_up_to_min_tick(
estimation.result_quantity,
Expand All @@ -104,7 +104,7 @@ pub fn start_swap_flow(

FPDecimal::from(coin_provided.amount) - estimation.result_quantity
} else {
FPDecimal::zero()
FPDecimal::ZERO
};

let swap_operation = CurrentSwapOperation {
Expand Down Expand Up @@ -157,6 +157,7 @@ pub fn execute_swap_step(
&market_id,
subaccount_id,
Some(fee_recipient.to_owned()),
None,
);

let order_message = SubMsg::reply_on_success(
Expand Down
38 changes: 19 additions & 19 deletions contracts/swap/src/testing/integration_logic_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn it_executes_a_swap_between_two_base_assets_with_multiple_price_levels() {
let to_balance = query_bank_balance(&bank, ATOM, swapper.address().as_str());
assert_eq!(
from_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"some of the original amount wasn't swapped"
);
assert_eq!(
Expand Down Expand Up @@ -271,7 +271,7 @@ fn it_executes_a_swap_between_two_base_assets_with_single_price_level() {
let to_balance = query_bank_balance(&bank, ATOM, swapper.address().as_str());
assert_eq!(
from_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"some of the original amount wasn't swapped"
);
assert_eq!(
Expand Down Expand Up @@ -413,7 +413,7 @@ fn it_executes_swap_between_markets_using_different_quote_assets() {
let to_balance = query_bank_balance(&bank, ATOM, swapper.address().as_str());
assert_eq!(
from_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"some of the original amount wasn't swapped"
);
assert_eq!(
Expand Down Expand Up @@ -548,7 +548,7 @@ fn it_reverts_swap_between_markets_using_different_quote_asset_if_one_quote_buff
);
assert_eq!(
target_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"target balance should not have changed after failed swap"
);

Expand Down Expand Up @@ -619,7 +619,7 @@ fn it_executes_a_sell_of_base_asset_to_receive_min_output_quantity() {
+ FPDecimal::from(195_000u128) * FPDecimal::from(4u128)
+ FPDecimal::from(192_000u128) * FPDecimal::from(3u128);
let expected_target_quantity = orders_nominal_total_value
* (FPDecimal::one()
* (FPDecimal::ONE
- FPDecimal::must_from_str(&format!(
"{}",
DEFAULT_TAKER_FEE * DEFAULT_ATOMIC_MULTIPLIER * DEFAULT_SELF_RELAYING_FEE_PART
Expand Down Expand Up @@ -665,7 +665,7 @@ fn it_executes_a_sell_of_base_asset_to_receive_min_output_quantity() {

assert_eq!(
from_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"some of the original amount wasn't swapped"
);
assert_eq!(
Expand Down Expand Up @@ -753,7 +753,7 @@ fn it_executes_a_buy_of_base_asset_to_receive_min_output_quantity() {

// calculate how much ETH we can buy with USDT we have
let available_usdt_after_fee = FPDecimal::from(swapper_usdt)
/ (FPDecimal::one()
/ (FPDecimal::ONE
+ FPDecimal::must_from_str(&format!(
"{}",
DEFAULT_TAKER_FEE * DEFAULT_ATOMIC_MULTIPLIER * DEFAULT_SELF_RELAYING_FEE_PART
Expand Down Expand Up @@ -826,7 +826,7 @@ fn it_executes_a_buy_of_base_asset_to_receive_min_output_quantity() {

assert_eq!(
from_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"some of the original amount wasn't swapped"
);
assert_eq!(
Expand Down Expand Up @@ -979,7 +979,7 @@ fn it_executes_a_swap_between_base_assets_with_external_fee_recipient() {

assert_eq!(
from_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"some of the original amount wasn't swapped"
);
assert_eq!(
Expand Down Expand Up @@ -1134,7 +1134,7 @@ fn it_reverts_the_swap_if_there_isnt_enough_buffer_for_buying_target_asset() {
);
assert_eq!(
to_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"target balance changes after failed swap"
);

Expand Down Expand Up @@ -1219,7 +1219,7 @@ fn it_reverts_swap_if_no_funds_were_passed() {
);
assert_eq!(
to_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"target balance changes after failed swap"
);

Expand Down Expand Up @@ -1394,7 +1394,7 @@ fn it_reverts_if_user_passes_quantities_equal_to_zero() {
&contr_addr,
&ExecuteMsg::SwapMinOutput {
target_denom: ATOM.to_string(),
min_output_quantity: FPDecimal::zero(),
min_output_quantity: FPDecimal::ZERO,
},
&[coin(12, ETH)],
&swapper,
Expand Down Expand Up @@ -1500,7 +1500,7 @@ fn it_reverts_if_user_passes_negative_quantities() {
);
assert_eq!(
to_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"target balance changed after failed swap"
);

Expand Down Expand Up @@ -1610,7 +1610,7 @@ fn it_reverts_if_there_arent_enough_orders_to_satisfy_min_quantity() {
);
assert_eq!(
to_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"target balance changed after failed swap"
);

Expand Down Expand Up @@ -1701,7 +1701,7 @@ fn it_reverts_if_min_quantity_cannot_be_reached() {
);
assert_eq!(
to_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"target balance changed after failed swap"
);

Expand Down Expand Up @@ -1806,7 +1806,7 @@ fn it_reverts_if_market_is_paused() {
);
assert_eq!(
to_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"target balance changed after failed swap"
);

Expand Down Expand Up @@ -1909,7 +1909,7 @@ fn it_reverts_if_user_doesnt_have_enough_inj_to_pay_for_gas() {
);
assert_eq!(
to_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"target balance changed after failed swap"
);

Expand Down Expand Up @@ -2105,14 +2105,14 @@ fn it_doesnt_allow_non_admin_to_withdraw_anything_from_contract() {
let random_dude_eth_balance = query_bank_balance(&bank, ETH, random_dude.address().as_str());
assert_eq!(
random_dude_eth_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"random dude has some eth balance after failed withdraw"
);

let random_dude_usdt_balance = query_bank_balance(&bank, USDT, random_dude.address().as_str());
assert_eq!(
random_dude_usdt_balance,
FPDecimal::zero(),
FPDecimal::ZERO,
"random dude has some usdt balance after failed withdraw"
);
}
Expand Down
Loading

0 comments on commit 397d2b6

Please sign in to comment.