Skip to content

Commit

Permalink
feat: general re-organisation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrobot committed Aug 23, 2024
1 parent 9fd9867 commit 64bb78c
Show file tree
Hide file tree
Showing 16 changed files with 1,212 additions and 34 deletions.
11 changes: 7 additions & 4 deletions Cargo.lock

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

12 changes: 12 additions & 0 deletions packages/injective-testing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Change Log

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.0.0] - 2024-08-23

### Changed

- Restructure of architecture to improve organisation of helper functions and also to enable re-use with multiple projects
25 changes: 14 additions & 11 deletions packages/injective-testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ edition = "2021"
license = "Apache-2.0"
name = "injective-testing"
repository = "https://github.com/InjectiveLabs/cw-injective/tree/dev/packages/injective-testing"
version = "0.2.1"
version = "1.0.0"

[dependencies]
anyhow = { workspace = true }
base64 = { workspace = true }
cosmwasm-std = { workspace = true }
cw-multi-test = { workspace = true }
injective-cosmwasm = { workspace = true, path = "../injective-cosmwasm" }
injective-math = { workspace = true, path = "../injective-math" }
rand = { workspace = true }
secp256k1 = { workspace = true }
serde = { workspace = true }
tiny-keccak = { workspace = true }
anyhow = { workspace = true }
base64 = { workspace = true }
cosmwasm-std = { workspace = true }
cw-multi-test = { workspace = true }
injective-cosmwasm = { workspace = true, path = "../injective-cosmwasm" }
injective-math = { workspace = true, path = "../injective-math" }
injective-std = { workspace = true, path = "../injective-std" }
injective-test-tube = { workspace = true }
prost = { workspace = true }
rand = { workspace = true }
secp256k1 = { workspace = true }
serde = { workspace = true }
tiny-keccak = { workspace = true }
10 changes: 6 additions & 4 deletions packages/injective-testing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod address_generator;
mod chain_mock;
mod mocks;
mod multi_test;
mod test_tube;
pub mod utils;

pub use address_generator::{generate_inj_address, InjectiveAddressGenerator, StorageAwareInjectiveAddressGenerator};
pub use chain_mock::*;
pub use mocks::*;
pub use multi_test::*;
pub use test_tube::*;
pub use utils::*;
64 changes: 64 additions & 0 deletions packages/injective-testing/src/mocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::utils::human_to_dec;

use injective_cosmwasm::{DerivativeMarket, MarketId, MarketMidPriceAndTOBResponse, MarketStatus, OracleType, SpotMarket};
use injective_math::FPDecimal;

pub const MOCKED_MARKET_ID: &str = "0x01edfab47f124748dc89998eb33144af734484ba07099014594321729a0ca16b";
pub const MOCKED_SUBACCOUNT_ID: &str = "0x427aee334987c52fa7b567b2662bdbb68614e48c000000000000000000000001";
pub const MOCKED_FEE_RECIPIENT: &str = "0x01edfab47f124748dc89998eb33144af734484ba07099014594321729a0ca16b";

pub const MOCK_EXCHANGE_DECIMALS: i32 = 18i32;
pub const MOCK_BASE_DECIMALS: i32 = 18i32;
pub const MOCK_ATOM_DECIMALS: i32 = 8i32;
pub const MOCK_QUOTE_DECIMALS: i32 = 6i32;

pub const MOCK_ATOM_DENOM: &str = "atom";
pub const MOCK_GAS_DENOM: &str = "inj";
pub const MOCK_BASE_DENOM: &str = "ubase";
pub const MOCK_QUOTE_DENOM: &str = "usdt";
pub const MOCK_USDC_DENOM: &str = "usdc";

// Mock INJ Market
pub fn mock_spot_market(market_id: &str) -> SpotMarket {
SpotMarket {
ticker: String::from("INJ:USDT"),
base_denom: String::from("inj"),
quote_denom: String::from("usdt"),
market_id: MarketId::unchecked(market_id),
maker_fee_rate: FPDecimal::ZERO,
taker_fee_rate: FPDecimal::ZERO,
status: MarketStatus::Active,
min_price_tick_size: FPDecimal::must_from_str("0.000000000000001000"),
min_quantity_tick_size: FPDecimal::must_from_str("10000000000000.0"), // 0.00001 @ 18dp
relayer_fee_share_rate: FPDecimal::must_from_str("0.4"),
}
}

// Mock INJ Market
pub fn mock_derivative_market(market_id: &str) -> DerivativeMarket {
DerivativeMarket {
ticker: String::from("INJ:USDT"),
oracle_base: String::from("inj"),
oracle_quote: String::from("usdt"),
oracle_type: OracleType::PriceFeed,
oracle_scale_factor: 0u32,
quote_denom: String::from("usdt"),
market_id: MarketId::unchecked(market_id),
initial_margin_ratio: FPDecimal::must_from_str("0.195"),
maintenance_margin_ratio: FPDecimal::must_from_str("0.05"),
maker_fee_rate: FPDecimal::ZERO,
taker_fee_rate: FPDecimal::ZERO,
isPerpetual: true,
status: MarketStatus::Active,
min_price_tick_size: FPDecimal::must_from_str("1000.0"), // 0.001
min_quantity_tick_size: FPDecimal::must_from_str("0.001"), // 0.001
}
}

pub fn mock_mid_price_tob() -> MarketMidPriceAndTOBResponse {
MarketMidPriceAndTOBResponse {
mid_price: Some(human_to_dec("10.0", MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS)),
best_buy_price: Some(human_to_dec("9.95", MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS)),
best_sell_price: Some(human_to_dec("10.05", MOCK_QUOTE_DECIMALS - MOCK_BASE_DECIMALS)),
}
}
5 changes: 5 additions & 0 deletions packages/injective-testing/src/multi_test/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod address_generator;
mod chain_mock;

pub use address_generator::{generate_inj_address, InjectiveAddressGenerator, StorageAwareInjectiveAddressGenerator};
pub use chain_mock::*;
87 changes: 87 additions & 0 deletions packages/injective-testing/src/test_tube/authz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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::{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>) {
let authz = Authz::new(app);

let mut buf = vec![];
GenericAuthorization::encode(&GenericAuthorization { msg }, &mut buf).unwrap();

authz
.grant(
MsgGrant {
granter: granter.address(),
grantee,
grant: Some(Grant {
authorization: Some(Any {
type_url: GenericAuthorization::TYPE_URL.to_string(),
value: buf.clone(),
}),
expiration,
}),
},
granter,
)
.unwrap();
}

pub fn revoke_authorization(app: &InjectiveTestApp, granter: &SigningAccount, grantee: String, msg_type_url: String) {
let _res: ExecuteResponse<MsgRevokeResponse> = app
.execute_multiple(
&[(
MsgRevoke {
granter: granter.address(),
grantee,
msg_type_url,
},
MsgRevoke::TYPE_URL,
)],
granter,
)
.unwrap();
}

pub fn create_send_authorization(app: &InjectiveTestApp, granter: &SigningAccount, grantee: String, amount: BaseCoin, expiration: Option<Timestamp>) {
let authz = Authz::new(app);

let mut buf = vec![];
SendAuthorization::encode(
&SendAuthorization {
spend_limit: vec![amount],
allow_list: vec![],
},
&mut buf,
)
.unwrap();

authz
.grant(
MsgGrant {
granter: granter.address(),
grantee,
grant: Some(Grant {
authorization: Some(Any {
type_url: SendAuthorization::TYPE_URL.to_string(),
value: buf.clone(),
}),
expiration,
}),
},
granter,
)
.unwrap();
}

pub fn execute_grid_authorizations(app: &InjectiveTestApp, granter: &SigningAccount, grantee: String, msgs: Vec<String>) {
for msg in msgs {
create_generic_authorization(app, granter, grantee.clone(), msg, None);
}
}
31 changes: 31 additions & 0 deletions packages/injective-testing/src/test_tube/bank.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use cosmwasm_std::Uint128;
use injective_std::types::cosmos::{
bank::v1beta1::{MsgSend, QueryBalanceRequest},
base::v1beta1::Coin,
};
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) {
bank.send(
MsgSend {
from_address: from.address(),
to_address: to.address(),
amount: vec![Coin {
amount: amount.to_string(),
denom: denom.to_string(),
}],
},
from,
)
.unwrap();
}

pub fn query_balance(bank: &Bank<InjectiveTestApp>, address: String, denom: String) -> Uint128 {
let response = bank.query_balance(&QueryBalanceRequest { address, denom }).unwrap();

match response.balance {
Some(balance) => Uint128::from_str(&balance.amount).unwrap(),
None => Uint128::zero(),
}
}
Loading

0 comments on commit 64bb78c

Please sign in to comment.