Skip to content

Commit

Permalink
feat: made small changes to the refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrobot committed Aug 29, 2024
1 parent d669ef7 commit fc58500
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 121 deletions.
50 changes: 16 additions & 34 deletions Cargo.lock

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

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ cw-utils = { version = "2.0.0" }
cw2 = { version = "2.0.0" }
ethereum-types = { version = "0.5.2" }
hex = { version = "0.4.3", features = [ "serde" ] }
injective-cosmwasm = { version = "0.3.0" }
injective-math = { version = "0.3.0" }
injective-std = { version = "1.13.0" }
injective-std-derive = { version = "1.13.0" }
injective-test-tube = { version = "=1.13.0-2" }
injective-testing = { version = "0.2.1" }
injective-cosmwasm = { version = "0.3.0", path = "./packages/injective-cosmwasm" }
injective-math = { version = "0.3.0", path = "./packages/injective-math" }
injective-std = { version = "1.13.0", path = "./packages/injective-std" }
injective-std-derive = { version = "1.13.0", path = "./packages/injective-std-derive" }
injective-test-tube = { version = "1.13.2" }
injective-testing = { version = "1.1.0", path = "./packages/injective-testing" }
itertools = { version = "0.10.3" }
primitive-types = { version = "0.12.2", default-features = false }
proc-macro2 = { version = "1.0.40" }
Expand Down
2 changes: 1 addition & 1 deletion build_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ fi
docker run --rm -v "$(pwd)":/code \
--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.11
cosmwasm/optimizer${ARCH}:0.16.0
2 changes: 1 addition & 1 deletion contracts/injective-cosmwasm-mock/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use injective_test_tube::{
},
Account, Authz, Bank, Exchange, ExecuteResponse, Gov, InjectiveTestApp, Insurance, Module, Oracle, Runner, SigningAccount, Wasm,
};
use injective_testing::human_to_i64;
use injective_testing::utils::human_to_i64;
use prost::Message;
use std::{collections::HashMap, ops::Neg, str::FromStr};

Expand Down
6 changes: 6 additions & 0 deletions packages/injective-testing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.1.0] - 2024-08-23

### Changed

- Updated `lib.rs` so that the re-organisation is adhered to

## [1.0.0] - 2024-08-23

### Changed
Expand Down
2 changes: 1 addition & 1 deletion packages/injective-testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
license = "Apache-2.0"
name = "injective-testing"
repository = "https://github.com/InjectiveLabs/cw-injective/tree/dev/packages/injective-testing"
version = "1.0.0"
version = "1.1.0"

[dependencies]
anyhow = { workspace = true }
Expand Down
11 changes: 3 additions & 8 deletions packages/injective-testing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
mod mocks;
mod multi_test;
mod test_tube;
pub mod mocks;
pub mod multi_test;
pub mod test_tube;
pub mod utils;

pub use mocks::*;
pub use multi_test::*;
pub use test_tube::*;
pub use utils::*;
2 changes: 1 addition & 1 deletion packages/injective-testing/src/multi_test/chain_mock.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::InjectiveAddressGenerator;
use crate::multi_test::address_generator::InjectiveAddressGenerator;

use anyhow::{bail, Result as AnyResult};
use cosmwasm_std::{testing::MockApi, Addr, Api, Binary, BlockInfo, Coin, CustomQuery, Empty, MemoryStorage, Querier, Storage};
Expand Down
7 changes: 2 additions & 5 deletions packages/injective-testing/src/multi_test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
mod address_generator;
mod chain_mock;

pub use address_generator::{generate_inj_address, InjectiveAddressGenerator, StorageAwareInjectiveAddressGenerator};
pub use chain_mock::*;
pub mod address_generator;
pub mod chain_mock;
16 changes: 9 additions & 7 deletions packages/injective-testing/src/test_tube/authz.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use injective_std::{
shim::{Any, Timestamp},
types::cosmos::{
authz::v1beta1::{GenericAuthorization, Grant, MsgGrant, MsgRevoke, MsgRevokeResponse},
bank::v1beta1::SendAuthorization,
base::v1beta1::Coin as BaseCoin,
use injective_test_tube::{
injective_std::{
shim::{Any, Timestamp},
types::cosmos::{
authz::v1beta1::{GenericAuthorization, Grant, MsgGrant, MsgRevoke, MsgRevokeResponse},
bank::v1beta1::SendAuthorization,
base::v1beta1::Coin as BaseCoin,
},
},
Account, Authz, ExecuteResponse, InjectiveTestApp, Module, Runner, SigningAccount,
};
use injective_test_tube::{Account, Authz, ExecuteResponse, InjectiveTestApp, Module, Runner, SigningAccount};
use prost::Message;

pub fn create_generic_authorization(app: &InjectiveTestApp, granter: &SigningAccount, grantee: String, msg: String, expiration: Option<Timestamp>) {
Expand Down
10 changes: 6 additions & 4 deletions packages/injective-testing/src/test_tube/bank.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use cosmwasm_std::Uint128;
use injective_std::types::cosmos::{
bank::v1beta1::{MsgSend, QueryBalanceRequest},
base::v1beta1::Coin,
use injective_test_tube::{
injective_std::types::cosmos::{
bank::v1beta1::{MsgSend, QueryBalanceRequest},
base::v1beta1::Coin,
},
Account, Bank, InjectiveTestApp, SigningAccount,
};
use injective_test_tube::{Account, Bank, InjectiveTestApp, SigningAccount};
use std::str::FromStr;

pub fn send(bank: &Bank<InjectiveTestApp>, amount: &str, denom: &str, from: &SigningAccount, to: &SigningAccount) {
Expand Down
82 changes: 54 additions & 28 deletions packages/injective-testing/src/test_tube/exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@ use crate::{
utils::{dec_to_proto, scale_price_quantity_perp_market, scale_price_quantity_spot_market, str_coin},
};

use cosmwasm_std::Addr;
use cosmwasm_std::{Addr, Uint128};
use injective_cosmwasm::{get_default_subaccount_id_for_checked_address, SubaccountId};
use injective_math::FPDecimal;
use injective_std::{
shim::Any,
types::{
cosmos::{
base::v1beta1::Coin as BaseCoin,
gov::v1::{MsgSubmitProposal, MsgVote},
},
injective::exchange::v1beta1::{
DerivativeOrder, MsgBatchUpdateOrders, MsgBatchUpdateOrdersResponse, MsgCancelDerivativeOrder, MsgCreateDerivativeLimitOrder,
MsgCreateDerivativeLimitOrderResponse, MsgCreateSpotLimitOrder, MsgInstantPerpetualMarketLaunch, MsgInstantSpotMarketLaunch,
MsgUpdateParams, OrderInfo, OrderType, PerpetualMarketFunding, Position, QueryDerivativeMarketsRequest, QueryExchangeParamsRequest,
QueryExchangeParamsResponse, QuerySpotMarketsRequest, SpotOrder,
use injective_test_tube::{
injective_std::{
shim::Any,
types::{
cosmos::{
base::v1beta1::Coin as BaseCoin,
gov::v1::{MsgSubmitProposal, MsgVote},
},
injective::exchange::v1beta1::{
DerivativeOrder, MsgBatchUpdateOrders, MsgBatchUpdateOrdersResponse, MsgCancelDerivativeOrder, MsgCreateDerivativeLimitOrder,
MsgCreateDerivativeLimitOrderResponse, MsgCreateSpotLimitOrder, MsgInstantPerpetualMarketLaunch, MsgInstantSpotMarketLaunch,
MsgUpdateParams, OrderInfo, OrderType, PerpetualMarketFunding, Position, QueryDerivativeMarketsRequest, QueryExchangeParamsRequest,
QueryExchangeParamsResponse, QuerySpotMarketsRequest, QuerySubaccountDepositsRequest,
QuerySubaccountEffectivePositionInMarketRequest, SpotOrder,
},
},
},
Account, Exchange, Gov, InjectiveTestApp, Module, Runner, SigningAccount,
};
use injective_test_tube::{Account, Exchange, Gov, InjectiveTestApp, Module, Runner, SigningAccount};
use prost::Message;
use std::str::FromStr;

Expand Down Expand Up @@ -727,20 +730,6 @@ pub fn remove_orders(
.data
}

pub fn get_spot_market_id(exchange: &Exchange<InjectiveTestApp>, ticker: String) -> String {
let spot_markets = exchange
.query_spot_markets(&QuerySpotMarketsRequest {
status: "Active".to_string(),
market_ids: vec![],
})
.unwrap()
.markets;

let market = spot_markets.iter().find(|m| m.ticker == ticker).unwrap();

market.market_id.to_string()
}

pub fn get_perpetual_market_id(exchange: &Exchange<InjectiveTestApp>, ticker: String) -> String {
let perpetual_markets = exchange
.query_derivative_markets(&QueryDerivativeMarketsRequest {
Expand All @@ -762,3 +751,40 @@ pub fn get_perpetual_market_id(exchange: &Exchange<InjectiveTestApp>, ticker: St

market.market_id.to_string()
}

pub fn get_subaccount_total_value(exchange: &Exchange<InjectiveTestApp>, market_id: String, subaccount_id: String, denom: String) -> Uint128 {
let trade_deposits_during = exchange
.query_subaccount_deposits(&QuerySubaccountDepositsRequest {
subaccount_id: subaccount_id.clone(),
subaccount: None,
})
.unwrap();

let total_balance = Uint128::from_str(&trade_deposits_during.deposits[&denom].total_balance)
.unwrap_or(Uint128::zero()) // Use zero if the result is an Err
/ Uint128::one();

let effective_position = exchange
.query_subaccount_effective_position_in_market(&QuerySubaccountEffectivePositionInMarketRequest { market_id, subaccount_id })
.unwrap();

let effective_margin = effective_position.state.as_ref().map_or(Uint128::zero(), |state| {
Uint128::from_str(&state.effective_margin).unwrap_or(Uint128::zero())
}) / Uint128::one();

total_balance + effective_margin
}

pub fn get_spot_market_id(exchange: &Exchange<InjectiveTestApp>, ticker: String) -> String {
let spot_markets = exchange
.query_spot_markets(&QuerySpotMarketsRequest {
status: "Active".to_string(),
market_ids: vec![],
})
.unwrap()
.markets;

let market = spot_markets.iter().find(|m| m.ticker == ticker).unwrap();

market.market_id.to_string()
}
10 changes: 6 additions & 4 deletions packages/injective-testing/src/test_tube/insurance.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::{mocks::MOCK_QUOTE_DECIMALS, utils::human_to_dec};

use injective_std::types::{
cosmos::base::v1beta1::Coin as BaseCoin,
injective::{insurance::v1beta1::MsgCreateInsuranceFund, oracle::v1beta1::OracleType},
use injective_test_tube::{
injective_std::types::{
cosmos::base::v1beta1::Coin as BaseCoin,
injective::{insurance::v1beta1::MsgCreateInsuranceFund, oracle::v1beta1::OracleType},
},
Account, InjectiveTestApp, Insurance, Module, SigningAccount,
};
use injective_test_tube::{Account, InjectiveTestApp, Insurance, Module, SigningAccount};

pub fn launch_insurance_fund(
app: &InjectiveTestApp,
Expand Down
19 changes: 6 additions & 13 deletions packages/injective-testing/src/test_tube/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
mod authz;
mod bank;
mod exchange;
mod insurance;
mod oracle;
mod utils;

pub use authz::*;
pub use bank::*;
pub use exchange::*;
pub use insurance::*;
pub use oracle::*;
pub use utils::*;
pub mod authz;
pub mod bank;
pub mod exchange;
pub mod insurance;
pub mod oracle;
pub mod utils;
20 changes: 12 additions & 8 deletions packages/injective-testing/src/test_tube/oracle.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use injective_std::{
shim::Any,
types::{
cosmos::{
base::v1beta1::Coin as BaseCoin,
gov::{v1::MsgVote, v1beta1::MsgSubmitProposal as MsgSubmitProposalV1Beta1},
use injective_test_tube::{
injective_std::{
shim::Any,
types::{
cosmos::{
base::v1beta1::Coin as BaseCoin,
gov::{v1::MsgVote, v1beta1::MsgSubmitProposal as MsgSubmitProposalV1Beta1},
},
injective::oracle::v1beta1::{
GrantPriceFeederPrivilegeProposal, MsgRelayPriceFeedPrice, QueryOraclePriceRequest, QueryOraclePriceResponse,
},
},
injective::oracle::v1beta1::{GrantPriceFeederPrivilegeProposal, MsgRelayPriceFeedPrice, QueryOraclePriceRequest, QueryOraclePriceResponse},
},
Account, Gov, InjectiveTestApp, Module, Oracle, SigningAccount,
};
use injective_test_tube::{Account, Gov, InjectiveTestApp, Module, Oracle, SigningAccount};
use prost::Message;
use std::str::FromStr;

Expand Down

0 comments on commit fc58500

Please sign in to comment.