-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
epic(nibiru-std): proto module with tests + scripts: #72 from NibiruC…
…hain/realu/proto epic(nibiru-std): proto module with tests + scripts
- Loading branch information
Showing
104 changed files
with
16,510 additions
and
52 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
[workspace] | ||
members = ["packages/*", "contracts/*"] | ||
members = ["packages/*", "contracts/*", "nibiru-std"] | ||
exclude = ["scripts"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
workspaces := "./packages ./contracts " | ||
|
||
# Displays available recipes by running `just -l`. | ||
setup: | ||
#!/usr/bin/env bash | ||
just -l | ||
install: | ||
rustup component add clippy | ||
|
||
wasm-all: | ||
bash scripts/wasm-out.sh | ||
|
||
# Move binding artifacts to teh local nibiru wasmbin | ||
wasm-export: | ||
bash scripts/wasm-export.sh | ||
|
||
# Compiles a single CW contract to wasm bytecode. | ||
# wasm-single: | ||
# bash scripts/wasm-out.sh --single | ||
|
||
# Runs rustfmt | ||
fmt: | ||
cargo fmt --all | ||
|
||
# Runs rustfmt without updating | ||
fmt-check: | ||
cargo fmt --all -- --check | ||
|
||
# Compiles Rust code | ||
build: | ||
cargo build | ||
|
||
build-update: | ||
cargo update | ||
cargo build | ||
|
||
# Clean target files and temp files | ||
clean: | ||
cargo clean | ||
|
||
# Run linter + fix | ||
clippy: | ||
cargo clippy --fix --allow-dirty --allow-staged | ||
|
||
# Run linter + check only | ||
clippy-check: | ||
cargo clippy | ||
|
||
# Test a specific package or contract | ||
test *pkg: | ||
#!/usr/bin/env bash | ||
set -e; | ||
RUST_BACKGTRACE="1" cargo test --package "{{pkg}}" | ||
# Test everything in the workspace. | ||
test-all: | ||
cargo test | ||
|
||
# Format, lint, and test | ||
tidy: fmt clippy test | ||
just | ||
|
||
# Format, lint, update dependencies, and test | ||
tidy-update: build-update | ||
just tidy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[package] | ||
name = "nibiru-std" | ||
version = "0.2.0" | ||
edition = "2021" | ||
description = "Standard library for Nibiru Chain CosmWasm smart contracts" | ||
authors = [ | ||
"Unique Divine <[email protected]>" | ||
] | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[features] | ||
backtraces = ["cosmwasm-std/backtraces"] | ||
library = [] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[dependencies] | ||
cosmwasm-std = "1.2.3" | ||
cosmwasm-schema = "1.2.3" | ||
prost = "0.12" | ||
prost-types = "0.12" | ||
|
||
# cargo run --bin script-name | ||
# [[bin]] | ||
# name = "rust script name" | ||
# path = "bin/script_name.rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod querier; | ||
pub mod query; | ||
pub mod route; | ||
pub mod state; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use cosmwasm_std::{QuerierWrapper, StdResult, Uint256}; | ||
|
||
use crate::bindings::query::{ | ||
AllMarketsResponse, BasePriceResponse, MetricsResponse, | ||
ModuleAccountsResponse, ModuleParamsResponse, OraclePricesResponse, | ||
PositionResponse, PositionsResponse, PremiumFractionResponse, QueryPerpMsg, | ||
ReservesResponse, | ||
}; | ||
use std::collections::HashMap; | ||
|
||
/// NibiruQuerier makes it easy to export the functions that correspond to each | ||
/// request without needing to know as much about the underlying types. | ||
pub struct NibiruQuerier<'a> { | ||
querier: &'a QuerierWrapper<'a, QueryPerpMsg>, | ||
} | ||
|
||
impl<'a> NibiruQuerier<'a> { | ||
pub fn new(querier: &'a QuerierWrapper<QueryPerpMsg>) -> Self { | ||
NibiruQuerier { querier } | ||
} | ||
|
||
pub fn all_markets(&self) -> StdResult<AllMarketsResponse> { | ||
let request = QueryPerpMsg::AllMarkets {}; | ||
self.querier.query(&request.into()) | ||
} | ||
|
||
pub fn base_price( | ||
&self, | ||
pair: String, | ||
is_long: bool, | ||
base_amount: Uint256, | ||
) -> StdResult<BasePriceResponse> { | ||
let request = QueryPerpMsg::BasePrice { | ||
pair, | ||
is_long, | ||
base_amount, | ||
}; | ||
self.querier.query(&request.into()) | ||
} | ||
|
||
pub fn position( | ||
&self, | ||
trader: String, | ||
pair: String, | ||
) -> StdResult<PositionResponse> { | ||
let request = QueryPerpMsg::Position { trader, pair }; | ||
self.querier.query(&request.into()) | ||
} | ||
|
||
pub fn positions(&self, trader: String) -> StdResult<PositionsResponse> { | ||
let request = QueryPerpMsg::Positions { trader }; | ||
self.querier.query(&request.into()) | ||
} | ||
|
||
pub fn reserves(&self, pair: String) -> StdResult<ReservesResponse> { | ||
let request = QueryPerpMsg::Reserves { pair }; | ||
self.querier.query(&request.into()) | ||
} | ||
|
||
pub fn oracle_prices( | ||
&self, | ||
pairs: Option<Vec<String>>, | ||
) -> StdResult<OraclePricesResponse> { | ||
let request = QueryPerpMsg::OraclePrices {}; | ||
let price_map: OraclePricesResponse = | ||
self.querier.query(&request.into())?; | ||
|
||
match pairs { | ||
Some(pair_vec) if !pair_vec.is_empty() => { | ||
let mut out_price_map: OraclePricesResponse = HashMap::new(); | ||
for p in pair_vec.iter() { | ||
if let Some(rate) = price_map.get(p) { | ||
out_price_map.insert(p.clone(), *rate); | ||
} | ||
} | ||
Ok(out_price_map) | ||
} | ||
_ => Ok(price_map), | ||
} | ||
} | ||
|
||
pub fn premium_fraction( | ||
&self, | ||
pair: String, | ||
) -> StdResult<PremiumFractionResponse> { | ||
let request = QueryPerpMsg::PremiumFraction { pair }; | ||
self.querier.query(&request.into()) | ||
} | ||
|
||
pub fn metrics(&self, pair: String) -> StdResult<MetricsResponse> { | ||
let request = QueryPerpMsg::Metrics { pair }; | ||
self.querier.query(&request.into()) | ||
} | ||
|
||
pub fn module_params(&self) -> StdResult<ModuleParamsResponse> { | ||
let request = QueryPerpMsg::ModuleParams {}; | ||
self.querier.query(&request.into()) | ||
} | ||
|
||
pub fn module_accounts(&self) -> StdResult<ModuleAccountsResponse> { | ||
let request = QueryPerpMsg::ModuleAccounts {}; | ||
self.querier.query(&request.into()) | ||
} | ||
} |
Oops, something went wrong.