Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ tarpaulin-report.*
packages/*/schema
contracts/*/schema
/packages/injective-cosmwasm/Cargo.lock
/CLAUDE.md
59 changes: 36 additions & 23 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
#!/bin/bash
# Usage: ./build.sh [contract-name]
# Purpose: Builds a CosmWasm smart contract using the cosmwasm/optimizer Docker image.
# - Defaults to building the 'injective-cosmwasm-mock' contract if no argument is provided.
# - Specify a contract name (e.g., 'atomic-order-example') to build a different contract.
# - The contract must be a directory in 'contracts/' (e.g., 'contracts/[contract-name]').
# - Run from the workspace root (e.g., cw-injective) to ensure access to the workspace pCargo.toml.
# Examples:
# ./build.sh # Builds injective-cosmwasm-mock
# ./build.sh atomic-order-example # Builds atomic-order-example
# ./build.sh dummy # Builds dummy
# ./build.sh injective-cosmwasm-stargate-example # Builds injective-cosmwasm-stargate-example
# Output: Produces an optimized .wasm file in target/wasm32-unknown-unknown/release/
# Usage: ./build.sh <alias|contract-dir>
# Aliases:
# stargate -> injective-cosmwasm-stargate-example
# mock -> injective-cosmwasm-mock
# atomic -> atomic-order-example
# dummy -> dummy
# Full contract directory names also accepted.

ARCH=""

# Set architecture suffix for arm64
if [[ $(arch) = "arm64" ]]; then
if [ "$(arch)" = "arm64" ]; then
ARCH=-arm64
fi

# Default contract name
DEFAULT_CONTRACT="injective-cosmwasm-mock"
CONTRACT=${1:-$DEFAULT_CONTRACT}
resolve_alias() {
case "$1" in
stargate) echo "injective-cosmwasm-stargate-example" ;;
mock) echo "injective-cosmwasm-mock" ;;
atomic) echo "atomic-order-example" ;;
dummy) echo "dummy" ;;
*) echo "$1" ;;
esac
}

if [ -z "$1" ]; then
echo "Usage: ./build.sh <alias|contract-dir>"
echo ""
echo "Aliases:"
echo " stargate -> injective-cosmwasm-stargate-example"
echo " mock -> injective-cosmwasm-mock"
echo " atomic -> atomic-order-example"
echo " dummy -> dummy"
echo ""
echo "Available contract directories:"
ls -1 contracts/
exit 1
fi

CONTRACT=$(resolve_alias "$1")

# Validate that the contract directory exists
if [[ ! -d "contracts/$CONTRACT" ]]; then
if [ ! -d "contracts/$CONTRACT" ]; then
echo "Error: Contract directory 'contracts/$CONTRACT' does not exist."
echo "Available contracts:"
ls -1 contracts/
exit 1
fi

# Run the optimizer with the specified or default contract
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="$(basename "$(pwd)")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/optimizer${ARCH}:0.16.1 /code/contracts/$CONTRACT
cosmwasm/optimizer${ARCH}:0.17.0 /code/contracts/$CONTRACT



Expand Down
13 changes: 13 additions & 0 deletions contracts/injective-cosmwasm-stargate-example/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
ARCH=""

if [[ $(arch) = "arm64" ]]; then
ARCH=-arm64
fi
Comment on lines +4 to +6
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify architecture detection in build scripts

echo "=== Checking contracts/injective-cosmwasm-stargate-example/build.sh ==="
if [ -f "contracts/injective-cosmwasm-stargate-example/build.sh" ]; then
  echo "File exists. Content (first 30 lines):"
  head -n 30 contracts/injective-cosmwasm-stargate-example/build.sh | cat -n
else
  echo "File not found"
fi

echo ""
echo "=== Checking root build.sh ==="
if [ -f "build.sh" ]; then
  echo "File exists. Content (first 30 lines):"
  head -n 30 build.sh | cat -n
else
  echo "File not found"
fi

echo ""
echo "=== All shell scripts with 'arch' references ==="
rg -l "arch" --type sh 2>/dev/null | head -20

Repository: InjectiveLabs/cw-injective

Length of output: 1900


🏁 Script executed:

#!/bin/bash
# Check for other architecture detection patterns and verify behavior

echo "=== Searching for other architecture/platform detection patterns ==="
rg "(arch|aarch64|uname)" --type sh -A 2 -B 2 | head -80

echo ""
echo "=== Check if there are any Docker-related files that specify images ==="
rg "cosmwasm/optimizer" --type sh --type docker

Repository: InjectiveLabs/cw-injective

Length of output: 1016


🌐 Web query:

arch command output aarch64 vs arm64 Linux ARM64 systems

💡 Result:

On Linux, arch is essentially equivalent to uname -m (it prints the machine hardware name of the running kernel/userspace environment, not a distro “marketing” name). On ARM 64-bit systems you’ll commonly see either aarch64 or arm64, but in practice they refer to the same ISA/CPU execution state (ARMv8+ 64-bit). (en.wikipedia.org)

Why you often see aarch64 on Linux

  • aarch64 is the canonical GNU/ELF/GCC naming (e.g., toolchain triplets like aarch64-linux-gnu) and is what many Linux environments report via uname -m / arch. (lwn.net)
  • The Linux kernel port is commonly referred to as arm64 (a kernel-community naming choice), even though uname/arch may still say aarch64. (lwn.net)

How distros use the names

  • Debian/Ubuntu package architecture: arm64
  • Debian multiarch GNU triplet: aarch64-linux-gnu (wiki.debian.org)

Practical interpretation (Linux)

  • arch/uname -maarch64 ⇒ you’re in a 64-bit ARM environment.
  • arch/uname -marmv7l (etc.) ⇒ you’re in a 32-bit ARM environment.

Also note: in containers/VMs, arch reflects the container/guest environment’s kernel view, which can differ from what you think you’re targeting. (geekchamp.com)

Citations:


🏁 Script executed:

#!/bin/bash
# Search for any comments about Docker image variants or cosmwasm/optimizer documentation

echo "=== Search for comments about optimizer image variants ==="
rg "optimizer" -A 3 -B 3 --type sh | head -40

echo ""
echo "=== Check if there are README or doc files that mention architecture ==="
find . -maxdepth 2 -type f \( -name "README*" -o -name "*.md" \) | head -10

Repository: InjectiveLabs/cw-injective

Length of output: 1357


🏁 Script executed:

#!/bin/bash
# Check README and look for cosmwasm/optimizer image documentation

echo "=== README content (relevant sections) ==="
cat README.md | head -100

echo ""
echo "=== Search for build-related instructions in README ==="
rg -i "build|docker|arm" README.md -A 2 -B 2 | head -60

Repository: InjectiveLabs/cw-injective

Length of output: 5755


🌐 Web query:

cosmwasm/optimizer docker image arm64 aarch64 variants

💡 Result:

  • There isn’t a single multi-arch cosmwasm/optimizer image that automatically serves arm64/aarch64. CosmWasm publishes separate ARM64 images with an -arm64 suffix. aarch64 == arm64 in Docker platform terms. [1]
  • ARM64/AArch64 variants you can use:
    • cosmwasm/optimizer-arm64 (combined image; ARM, marked experimental) [1][2]
    • Deprecated but still listed: cosmwasm/rust-optimizer-arm64, cosmwasm/workspace-optimizer-arm64 [1][2]
  • Important note: the project warns the native ARM images can produce different wasm artifacts than the x86_64 images, so they’re discouraged for reproducible/production releases (primarily meant for Apple Silicon dev/testing). [1]
  • Publishing/building detail: releases are done separately (e.g., make publish-x86_64 and make publish-arm64). [3]

Sources: [1] CosmWasm/optimizer README, [2] Docker Hub cosmwasm org image list, [3] CosmWasm/optimizer “Releasing” wiki.


Architecture detection is too narrow and fails on Linux ARM64 hosts.

The arch command on Linux ARM64 systems returns aarch64, not arm64, so the current check never activates and ARM runners pull the wrong optimizer image. This issue exists in three files: contracts/injective-cosmwasm-stargate-example/build.sh (lines 4–6), build.sh (lines 10–13), and build_release.sh.

Suggested fix (apply to all three files)
-ARCH=""
-
-if [[ $(arch) = "arm64" ]]; then
-  ARCH=-arm64
-fi
+ARCH=""
+case "$(uname -m)" in
+  arm64|aarch64) ARCH="-arm64" ;;
+esac

Note: CosmWasm publishes separate ARM64 images (cosmwasm/optimizer-arm64), but these can produce different wasm artifacts than x86_64 images and are not recommended for production releases.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if [[ $(arch) = "arm64" ]]; then
ARCH=-arm64
fi
case "$(uname -m)" in
arm64|aarch64) ARCH="-arm64" ;;
esac
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@contracts/injective-cosmwasm-stargate-example/build.sh` around lines 4 - 6,
The architecture check currently only tests for "arm64" via `arch` and misses
Linux `aarch64`; update the detection in the build scripts so they test for both
"arm64" and "aarch64" (or use `uname -m` equivalently) and set ARCH=-arm64 when
either is present; apply the same change to the occurrences in the three build
scripts referenced, targeting the conditional that sets ARCH and the use of
`arch`/`uname -m`.


WORKSPACE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"

docker run --rm -v "$WORKSPACE_ROOT":/code -v "$HOME/.cargo/git":/usr/local/cargo/git \
--mount type=volume,source="$(basename "$WORKSPACE_ROOT")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/optimizer${ARCH}:0.17.0 "./"
7 changes: 3 additions & 4 deletions contracts/injective-cosmwasm-stargate-example/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::msg::QueryStargateResponse;
use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
use base64::Engine as _;
use cosmwasm_std::{to_json_binary, to_json_vec, Binary, ContractResult, Deps, QuerierWrapper, QueryRequest, StdError, StdResult, SystemResult};
use injective_cosmwasm::{InjectiveQuerier, InjectiveQueryWrapper, MarketId};
use injective_cosmwasm::{InjectiveQueryWrapper, MarketId};
use injective_std::types::{cosmos::bank::v1beta1::BankQuerier, injective::exchange::v2::ExchangeQuerier as ExchangeQuerierV2};

pub fn handle_query_stargate_raw(querier: &QuerierWrapper<InjectiveQueryWrapper>, path: String, query_request: String) -> StdResult<Binary> {
Expand All @@ -27,9 +27,8 @@ pub fn handle_query_stargate_raw(querier: &QuerierWrapper<InjectiveQueryWrapper>
}

pub fn handle_query_spot_market(deps: Deps<InjectiveQueryWrapper>, market_id: &str) -> StdResult<Binary> {
let querier = InjectiveQuerier::new(&deps.querier);
let market_id = MarketId::new(market_id)?;
to_json_binary(&querier.query_spot_market(&market_id)?)
let querier = ExchangeQuerierV2::new(&deps.querier);
to_json_binary(&querier.spot_market(market_id.to_string())?)
}

pub fn handle_query_bank_params(deps: Deps<InjectiveQueryWrapper>) -> StdResult<Binary> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use injective_std::types::injective::exchange::v2::{self, open_notional_cap::Cap
use injective_test_tube::{
injective_cosmwasm::get_default_subaccount_id_for_checked_address,
injective_std::types::injective::exchange::v1beta1::{
OrderType, QueryDerivativeMarketRequest, QueryDerivativeMidPriceAndTobRequest, QueryDerivativeOrderbookRequest,
QueryPerpetualMarketFundingRequest, QueryPerpetualMarketInfoRequest, QuerySubaccountEffectivePositionInMarketRequest,
QuerySubaccountPositionInMarketRequest, QueryTraderDerivativeOrdersRequest,
OrderType, QueryDerivativeMarketRequest, QueryDerivativeOrderbookRequest, QueryPerpetualMarketFundingRequest,
QueryPerpetualMarketInfoRequest, QuerySubaccountEffectivePositionInMarketRequest, QuerySubaccountPositionInMarketRequest,
QueryTraderDerivativeOrdersRequest,
},
Account, Exchange, Module, Wasm,
};
Expand Down Expand Up @@ -339,25 +339,24 @@ fn test_query_perpetual_market_funding() {
assert_eq!(state.cumulative_price, FPDecimal::ZERO);
}

#[ignore = "TODO fix me"]
#[test]
//#[cfg_attr(not(feature = "integration"), ignore)]
#[cfg_attr(not(feature = "integration"), ignore)]
fn test_query_derivative_market_mid_price_and_tob() {
let env = Setup::new(ExchangeType::Derivative);
let wasm = Wasm::new(&env.app);
let market_id = env.market_id.unwrap();

add_perp_initial_liquidity(&env.app, market_id.to_owned());
let query_msg = QueryMsg::QueryStargateRaw {
path: "/injective.exchange.v1beta1.Query/DerivativeMidPriceAndTOB".to_string(),
query_request: encode_proto_message(QueryDerivativeMidPriceAndTobRequest {
path: "/injective.exchange.v2.Query/DerivativeMidPriceAndTOB".to_string(),
query_request: encode_proto_message(v2::QueryDerivativeMidPriceAndTobRequest {
market_id: market_id.to_owned(),
}),
};
let res = get_stargate_query_result::<MarketMidPriceAndTOBResponse>(wasm.query(&env.contract_address, &query_msg)).unwrap();
assert_eq!(res.mid_price, Some(human_to_dec("10", QUOTE_DECIMALS)));
assert_eq!(res.best_buy_price, Some(human_to_dec("9.9", QUOTE_DECIMALS)));
assert_eq!(res.best_sell_price, Some(human_to_dec("10.1", QUOTE_DECIMALS)));
assert_eq!(res.mid_price, Some(FPDecimal::must_from_str("10")));
assert_eq!(res.best_buy_price, Some(FPDecimal::must_from_str("9.9")));
assert_eq!(res.best_sell_price, Some(FPDecimal::must_from_str("10.1")));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = "1.87.0"
channel = "1.86.0"
components = [ "rustfmt" ]
profile = "minimal"
targets = [ "wasm32-unknown-unknown" ]
Loading