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/stargate tester contract #220

Merged
merged 26 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9d50d68
initial contract startgate example
jbernal87 Apr 24, 2024
122e45c
make query functional
jbernal87 Apr 24, 2024
f82826b
small refactor
jbernal87 Apr 24, 2024
7295a31
add subaccount deposit test
jbernal87 Apr 25, 2024
1585aa2
test: add auction tests and some clean ups
gorgos Apr 26, 2024
b657d5d
complete stargate queries
jbernal87 Apr 25, 2024
ebbb001
merge with dev
jbernal87 Jun 17, 2024
9d2c6ea
merge with dev fix
jbernal87 Jun 17, 2024
01e5f5a
chore: fix broken tests.
jbernal87 Jun 17, 2024
752c205
Merge branch 'dev' into feat/stargate-tester-contract
gorgos Jun 25, 2024
8fc14a0
chore: add to proto string helper to FPDecimal
gorgos Jun 28, 2024
a20cb20
chore: fix Cargo tomls
gorgos Jun 28, 2024
a8c5aee
feat: add more stargate examples
gorgos Jun 28, 2024
ebf0280
feat: authz tests.
jbernal87 Jul 1, 2024
825207d
chore: lints.
jbernal87 Jul 1, 2024
42ab0c6
chore: fix compilation issue
Kishan-Dhakan Jul 26, 2024
342efc6
feat: merged with dev
maxrobot Sep 16, 2024
0d5be4e
fix: linters
maxrobot Sep 16, 2024
5718e0b
chore: merge with other stargate branch
maxrobot Sep 16, 2024
41e822a
fix: incorrect message
maxrobot Sep 16, 2024
e43ad9a
chore: fix typo
maxrobot Sep 16, 2024
cd84611
Merge pull request #228 from InjectiveLabs/feat/stargate-tester-authz…
maxrobot Sep 16, 2024
2cfe4a1
fix: cargo conflict
maxrobot Sep 16, 2024
9b4741f
Merge remote-tracking branch 'origin' into feat/stargate-tester-contract
jbernal87 Sep 16, 2024
c92a4ad
chore merge dev
jbernal87 Sep 16, 2024
8a0f953
Merge remote-tracking branch 'origin/feat/stargate-tester-contract' i…
jbernal87 Sep 16, 2024
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
220 changes: 189 additions & 31 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ incremental = false

[workspace.dependencies]
anyhow = { version = "1.0.66" }
base64 = { version = "0.13.1" }
base64 = { version = "0.21.5" }
chrono = { version = "0.4.27", default-features = false }
cosmos-sdk-proto = { version = "0.20.0", default-features = false }
cosmwasm-schema = { version = "1.5.0" }
Expand Down Expand Up @@ -38,6 +38,7 @@ secp256k1 = { version = "0.6.2" }
serde = { version = "1.0.196", default-features = false, features = [ "derive" ] }
serde-cw-value = { version = "0.7.0" }
serde-json-wasm = { version = "1.0.0" }
serde_json = { version = "1.0.111" }
serde_repr = { version = "0.1.17" }
serde_test = { version = "1.0.176" }
subtle-encoding = { version = "0.5.1", features = [ "bech32-preview" ] }
Expand Down
61 changes: 0 additions & 61 deletions contracts/atomic-order-example/.circleci/config.yml

This file was deleted.

8 changes: 5 additions & 3 deletions contracts/atomic-order-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ optimize = """docker run --rm -v "$(pwd)":/code \

[dependencies]
cosmwasm-std = { workspace = true }
cosmwasm-storage = { workspace = true }
cw-storage-plus = { workspace = true }
cw-utils = { workspace = true }
cw2 = { workspace = true }
injective-cosmwasm = { workspace = true, path = "../../packages/injective-cosmwasm" }
injective-math = { workspace = true, path = "../../packages/injective-math" }
injective-std = { workspace = true, path = "../../packages/injective-std" }
injective-cosmwasm = { path = "../../packages/injective-cosmwasm" }
injective-math = { path = "../../packages/injective-math" }
injective-std = { path = "../../packages/injective-std" }
prost = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions contracts/atomic-order-example/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ fn create_spot_market_handler() -> impl HandlesMarketIdQuery {
status: MarketStatus::Active,
min_price_tick_size: FPDecimal::from_str("0.000000000000001").unwrap(),
min_quantity_tick_size: FPDecimal::from_str("1000000000000000").unwrap(),
min_notional: FPDecimal::ONE,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure Minimum Notional Value is Enforced

The creation of the spot market handler should enforce a minimum notional value. This is critical for maintaining market stability and preventing manipulation.

- min_notional: FPDecimal::ONE,
+ min_notional: FPDecimal::from_str("10").unwrap(), // Adjust according to market requirements

}),
};
SystemResult::Ok(ContractResult::from(to_json_binary(&response)))
Expand Down
2 changes: 1 addition & 1 deletion contracts/dummy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ optimize = """docker run --rm -v "$(pwd)":/code \
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
injective-cosmwasm = { workspace = true, path = "../../packages/injective-cosmwasm" }
injective-cosmwasm = { path = "../../packages/injective-cosmwasm" }
schemars = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
8 changes: 4 additions & 4 deletions contracts/injective-cosmwasm-mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
injective-cosmwasm = { workspace = true, path = "../../packages/injective-cosmwasm" }
injective-math = { workspace = true, path = "../../packages/injective-math" }
injective-std = { workspace = true, path = "../../packages/injective-std" }
injective-cosmwasm = { path = "../../packages/injective-cosmwasm" }
injective-math = { path = "../../packages/injective-math" }
injective-std = { path = "../../packages/injective-std" }
prost = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
injective-test-tube = { workspace = true }
injective-testing = { workspace = true, path = "../../packages/injective-testing" }
injective-testing = { path = "../../packages/injective-testing" }
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ fn test_query_spot_market() {
let ticker = "INJ/USDT".to_string();
let min_price_tick_size = FPDecimal::must_from_str("0.000000000000001");
let min_quantity_tick_size = FPDecimal::must_from_str("1000000000000000");
let min_notional = FPDecimal::must_from_str("1");

exchange
.instant_spot_market_launch(
Expand All @@ -164,7 +165,7 @@ fn test_query_spot_market() {
quote_denom: QUOTE_DENOM.to_string(),
min_price_tick_size: dec_to_proto(min_price_tick_size),
min_quantity_tick_size: dec_to_proto(min_quantity_tick_size),
min_notional: "1".to_string(),
min_notional: dec_to_proto(min_notional),
},
&env.signer,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn test_query_derivative_market() {
let maintenance_margin_ratio = FPDecimal::must_from_str("0.05");
let min_price_tick_size = FPDecimal::must_from_str("1000.0");
let min_quantity_tick_size = FPDecimal::must_from_str("1000000000000000");
let min_notional = FPDecimal::must_from_str("1");
let quote_denom = QUOTE_DENOM.to_string();
let maker_fee_rate = FPDecimal::ZERO;
let taker_fee_rate = FPDecimal::ZERO;
Expand All @@ -72,7 +73,7 @@ fn test_query_derivative_market() {
maintenance_margin_ratio: dec_to_proto(maintenance_margin_ratio),
min_price_tick_size: dec_to_proto(min_price_tick_size),
min_quantity_tick_size: dec_to_proto(min_quantity_tick_size),
min_notional: "1".to_string(),
min_notional: dec_to_proto(min_notional),
},
&env.signer,
)
Expand Down
42 changes: 42 additions & 0 deletions contracts/injective-cosmwasm-stargate-example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
authors = [ "Jose Luis Bernal Castillo <[email protected]>" ]
edition = "2018"
name = "injective-cosmwasm-stargate-example"
version = "0.0.1"

exclude = [
# Those files are rust-optimizer artifacts. You might want to commit them for convenience but they should not be part of the source code publication.
"contract.wasm",
"hash.txt",
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = [ "cdylib", "rlib" ]

[features]
# use library feature to disable all instantiate/execute/query exports
integration = [ ]
library = [ ]

[dependencies]
base64 = { workspace = true }
cosmos-sdk-proto = { workspace = true }
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
injective-cosmwasm = { path = "../../packages/injective-cosmwasm" }
injective-math = { path = "../../packages/injective-math" }
injective-std = { path = "../../packages/injective-std" }
prost = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
injective-std = { workspace = true }
injective-test-tube = { workspace = true }
injective-testing = { workspace = true }
70 changes: 70 additions & 0 deletions contracts/injective-cosmwasm-stargate-example/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::{
error::ContractError,
handle::{handle_test_market_spot_order, handle_test_transient_derivative_order, handle_test_transient_spot_order},
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
query::{handle_query_bank_params, handle_query_spot_market, handle_query_stargate_raw},
reply::{handle_create_derivative_order_reply_stargate, handle_create_order_reply_stargate},
};

use cosmwasm_std::{entry_point, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdResult};
use cw2::set_contract_version;
use injective_cosmwasm::{InjectiveMsgWrapper, InjectiveQueryWrapper};

const CONTRACT_NAME: &str = "crates.io:injective:dummy-stargate-contract";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const CREATE_SPOT_ORDER_REPLY_ID: u64 = 0u64;
pub const CREATE_DERIVATIVE_ORDER_REPLY_ID: u64 = 1u64;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(deps: DepsMut, _env: Env, _info: MessageInfo, _msg: InstantiateMsg) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Ok(Response::default())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut<InjectiveQueryWrapper>,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response<InjectiveMsgWrapper>, ContractError> {
match msg {
ExecuteMsg::TestTraderTransientSpotOrders {
market_id,
subaccount_id,
price,
quantity,
} => handle_test_transient_spot_order(deps, env, &info, market_id, subaccount_id, price, quantity),
ExecuteMsg::TestMarketOrderStargate {
market_id,
subaccount_id,
price,
quantity,
} => handle_test_market_spot_order(deps, env.contract.address.as_ref(), market_id, subaccount_id, price, quantity),
ExecuteMsg::TestTraderTransientDerivativeOrders {
market_id,
subaccount_id,
price,
quantity,
margin,
} => handle_test_transient_derivative_order(deps, env, &info, market_id, subaccount_id, price, quantity, margin),
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps<InjectiveQueryWrapper>, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::QueryStargateRaw { path, query_request } => handle_query_stargate_raw(&deps.querier, path, query_request),
QueryMsg::QueryBankParams {} => handle_query_bank_params(deps),
QueryMsg::QuerySpotMarket { market_id } => handle_query_spot_market(deps, &market_id),
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(deps: DepsMut<InjectiveQueryWrapper>, _env: Env, msg: Reply) -> Result<Response, ContractError> {
match msg.id {
CREATE_SPOT_ORDER_REPLY_ID => handle_create_order_reply_stargate(deps, &msg),
CREATE_DERIVATIVE_ORDER_REPLY_ID => handle_create_derivative_order_reply_stargate(deps, &msg),
_ => Err(ContractError::UnrecognizedReply(msg.id)),
}
}
Comment on lines +63 to +70
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address potential conflict with existing reply IDs and improve error handling.

The past review comments indicate a potential conflict with the CREATE_SPOT_ORDER_REPLY_ID and CREATE_DERIVATIVE_ORDER_REPLY_ID constants across different modules. Please ensure that these constants are unique to avoid any unexpected behavior.

Additionally, consider improving the error handling in the reply function by providing a more descriptive error message for unrecognized reply IDs.

Apply this diff to improve the error handling:

-    _ => Err(ContractError::UnrecognizedReply(msg.id)),
+    _ => Err(ContractError::UnrecognizedReply(format!("Unrecognized reply ID: {}", msg.id))),

Committable suggestion was skipped due to low confidence.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
use base64::Engine;
use prost::Message;

pub fn encode_proto_message<T: Message>(msg: T) -> String {
let mut buf = vec![];
T::encode(&msg, &mut buf).unwrap();
BASE64_STANDARD.encode(&buf)
}
14 changes: 14 additions & 0 deletions contracts/injective-cosmwasm-stargate-example/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use cosmwasm_std::StdError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),
#[error("Unrecognized reply id: {0}")]
UnrecognizedReply(u64),
#[error("Invalid reply from sub-message {id}, {err}")]
ReplyParseFailure { id: u64, err: String },
#[error("Failure response from submsg: {0}")]
SubMsgFailure(String),
}
Loading
Loading