Skip to content

Commit

Permalink
Merge pull request #220 from InjectiveLabs/feat/stargate-tester-contract
Browse files Browse the repository at this point in the history
Feat/stargate tester contract
  • Loading branch information
maxrobot authored Sep 16, 2024
2 parents fec3534 + 8a0f953 commit 2d58148
Show file tree
Hide file tree
Showing 42 changed files with 2,807 additions and 122 deletions.
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,
}),
};
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)),
}
}
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

0 comments on commit 2d58148

Please sign in to comment.